Closed pdgilbert closed 3 years ago
ahh the downside of the compat layer is some nasty errors :-/
for the GPIOs think you just need to be calling .compat()
on them so they get wrapped (same as the spi
and delay
objects)?
and transactional will require a polyfill per https://github.com/ryankurte/embedded-hal-compat/issues/2 that i forgot to add
Ok, I'm getting closer. But I'm confused about the transactional polyfill. Do I define something like fn spi_exec
in my example code and call it? Or is it something I should try to get the crate maintainer to put in lib.rs? Maybe somewhere near
impl<Spi, SpiError, CsPin, BusyPin, ReadyPin, ResetPin, PinError, Delay, DelayError>
Sx127x<SpiWrapper<Spi, SpiError, CsPin, BusyPin, ReadyPin, ResetPin, PinError, Delay, DelayError>, SpiError, PinError, DelayError>
where
Spi: Transfer<u8, Error = SpiError> + Write<u8, Error = SpiError> + Transactional<u8, Error = SpiError>,
(If I were the maintainer I would not trust me to do it, because it would be more work figuring out the mess I made.)
because of the orphan rules you can only implement traits in the location that either the trait or the object is defined, for the purposes of this it'd be here because the hal
implementation you're using isn't running 1.0.0-alpha.X
yet (or this wouldn't be a problem).
added (but completely untested) in #4, see how that goes?
Ok, my send examples are building but I am having trouble with receive:
I've tried .compat()
a few places with no luck. Also the warning: unused import: embedded_spi::wrapper::Wrapper
does not happen in examples that work.
I think there are two problems, one is that radio
should be 0.8.1
and the other is that when you call .compat()
it wraps the types, so the return types from the setup functions that use static types become invalid... this patch gets the send and receive examples building for the STM32F411 (cargo build --example lora_spi_receive --target thumbv7em-none-eabihf --no-default-features --features=stm32f4xx,stm32f401
)
diff --git a/Cargo.toml b/Cargo.toml
index 02797e2..0367ab7 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -71,7 +71,7 @@ stm32l1xx-hal = {version = "^0.1.0", optional = true, default-features = fals
stm32l4xx-hal = {version = "^0.6.0", optional = true}
[dependencies.radio]
-version = "0.7.0"
+version = "0.8.1"
[dependencies.embedded-hal]
version = "1.0.0-alpha.4"
diff --git a/examples/lora_spi_receive.rs b/examples/lora_spi_receive.rs
index 2297d21..37fe0fe 100644
--- a/examples/lora_spi_receive.rs
+++ b/examples/lora_spi_receive.rs
@@ -68,7 +68,7 @@ use stm32l4xx_hal::{spi::{Mode,Phase, Polarity}};
use radio_sx127x::Error as sx127xError; // Error name conflict with hals
use radio_sx127x::{prelude::*, // prelude has Sx127x,
- device::{Modem, Channel, PaConfig, PaSelect,},
+ device::{Modem, Channel, PaConfig, PaSelect, PacketInfo},
device::lora::{LoRaConfig, LoRaChannel, Bandwidth, SpreadingFactor, CodingRate,
PayloadLength, PayloadCrc, FrequencyHopping, },
};
@@ -310,10 +310,7 @@ use stm32f4xx_hal::{prelude::*,
#[cfg(feature = "stm32f4xx")]
- fn setup() -> Sx127x<Wrapper<Spi<SPI1,
- (PA5<Alternate<AF5>>, PA6<Alternate<AF5>>, PA7<Alternate<AF5>>)>, Error,
- PA1<Output<PushPull>>, PB8<Input<Floating>>, PB9<Input<Floating>>, PA0<Output<PushPull>>,
- Infallible, Delay>, Error, Infallible, Infallible> {
+ fn setup() -> impl DelayMs<u32> + Receive<Info=PacketInfo, Error=sx127xError<Error, Infallible, Infallible>> {
let cp = cortex_m::Peripherals::take().unwrap();
let p = Peripherals::take().unwrap();
I now have my examples building for MCUs stm32f103, stm32f100, stm32f101, stm32f303xc, stm32f401, stm32f411, stm32h742, stm32l100, and stm32l151. These use crates stm32f1xx-hal
,stm32f3xx-hal
, stm32f4xx-hal
, stm32f7xx-hal
, stm32h7xx-hal
,stm32l0xx-hal
, and stm32l1xx-hal
that are based on embedded-hal
v0.2.4. The examples use crate radio-sx127x
based onembedded-hal
v1.0.0-alpha.4.
Just to summarize usage for anyone reading:
[dependencies]
embedded-hal-compat = "0.1.3"
# or embedded-hal-compat = { git = "https://github.com/ryankurte/embedded-hal-compat.git", branch = "main"}
use embedded_hal_compat::IntoCompat;
use embedded_hal_compat::eh1_0::blocking::delay::{DelayMs as _};
stm32f1xx_hal
) using an older embedded-hal
will remain unchanged but when results of those calls are used in a call to the crate using embedded-hal_1.0.0-alpha
then the result should be appended with.compat()
. For example, in my case
let lora = Sx127x::spi(
spi.compat(), //Spi
gpioa.pa1.into_push_pull_output(&mut gpioa.crl).compat(), //CsPin on PA1
gpiob.pb8.into_floating_input(&mut gpiob.crh).compat(), //BusyPin DIO0 on PB8
gpiob.pb9.into_floating_input(&mut gpiob.crh).compat(), //ReadyPin DIO1 on PB9
gpioa.pa0.into_push_pull_output(&mut gpioa.crl).compat(), //ResetPin on PA0
delay.compat(), //Delay
&CONFIG_RADIO, //&Config
).unwrap(); // should handle error
PacketInfo
above.embedded-hal
. For example, in my case I have a constant mode
defined using Mode
, Phase
, and Polarity
which is passed in a call to spi
in the MCU hal. I had to replaceuse embedded_hal::{spi::{Mode, Phase, Polarity}, };
with
There can be other needs to account for the fact that the MCU hals are not using the same embedded-hal
ahh, you could probably access this as embedded_hal_compat::eh0_2::spi::...
(or explicitly import the v0.2.x
hal) rather than having to switch for each device type
Rather than switch for each device type I added dependency
old-e-h = {version = "0.2.4", package = "embedded-hal" }
to Cargo.toml and in the code replaced all the cfg feature lines above with
use old_e_h::{spi::{Mode,Phase, Polarity}};
This also solved the problem that one hal crate was not re-exporting{Mode,Phase, Polarity}
.
I am trying to use
embedded-hal-compat
with a fork ofrust-radio-sx127x
in which I have put some examples. I am testing withstm32f1xx_hal
. In Cargo.toml I have addedIn the example code I have added
in the setup() function
but I am still getting trait bound problems with InputPin and OutputPin (as in #1) and some other problems:
Click to expand
I thought the
OutputPin
was fixed in the last PR merge so I suppose I am doing something wrong? A more complete subset of the code is:Click to expand
``` #![no_std] #![no_main] use embedded_hal_compat::IntoCompat; use embedded_hal_compat::eh1_0::blocking::delay::{DelayMs as _}; #[cfg(debug_assertions)] extern crate panic_semihosting; #[cfg(not(debug_assertions))] extern crate panic_halt; // use nb::block; use cortex_m_rt::entry; use cortex_m_semihosting::*; use embedded_hal::{blocking::delay::DelayMs, spi::{Mode, Phase, Polarity}, }; //use asm_delay::{ AsmDelay, bitrate, }; //use cortex_m::asm; //for breakpoint use radio_sx127x::Error as sx127xError; // Error name conflict with hals use radio_sx127x::{prelude::*, // prelude has Sx127x, device::{Modem, Channel, PaConfig, PaSelect,}, device::lora::{LoRaConfig, LoRaChannel, Bandwidth, SpreadingFactor, CodingRate, PayloadLength, PayloadCrc, FrequencyHopping, }, }; //use radio::{Receive, Transmit}; use radio::{Transmit}; // trait needs to be in scope to find methods start_transmit and check_transmit. // lora and radio parameters pub const MODE: Mode = Mode { // SPI mode for radio phase: Phase::CaptureOnSecondTransition, polarity: Polarity::IdleHigh, }; const FREQUENCY: u32 = 907_400_000; // frequency in hertz ch_12: 915_000_000, ch_2: 907_400_000 const CONFIG_CH: LoRaChannel = LoRaChannel { freq: FREQUENCY as u32, // frequency in hertz bw: Bandwidth::Bw125kHz, sf: SpreadingFactor::Sf7, cr: CodingRate::Cr4_8, }; const CONFIG_LORA: LoRaConfig = LoRaConfig { preamble_len: 0x8, symbol_timeout: 0x64, payload_len: PayloadLength::Variable, payload_crc: PayloadCrc::Enabled, frequency_hop: FrequencyHopping::Disabled, invert_iq: false, }; const CONFIG_PA: PaConfig = PaConfig {output: PaSelect::Boost, power: 10, }; //let CONFIG_RADIO = Config::default() ; const CONFIG_RADIO: radio_sx127x::device::Config = radio_sx127x::device::Config { modem: Modem::LoRa(CONFIG_LORA), channel: Channel::LoRa(CONFIG_CH), pa_config: CONFIG_PA, xtal_freq: 32000000, // CHECK timeout_ms: 100, }; // setup() does all hal/MCU specific setup and returns generic object for use in main code. #[cfg(feature = "stm32f1xx")] // eg blue pill stm32f103 use stm32f1xx_hal::{prelude::*, pac::Peripherals, spi::{Spi, Error,}, delay::Delay, }; #[cfg(feature = "stm32f1xx")] fn setup() -> impl DelayMsOutput from the workflow is at https://github.com/pdgilbert/rust-radio-sx127x/runs/2041700780?check_suite_focus=true