diff options
| author | ceekdee <[email protected]> | 2023-04-23 18:32:34 -0500 |
|---|---|---|
| committer | ceekdee <[email protected]> | 2023-04-23 18:32:34 -0500 |
| commit | 73f25093c7793ad2e8bd6fceeca46d9b5b1031ad (patch) | |
| tree | 9374f5a637e1337ceb40a51426f24f3fe68249ef /examples/stm32wl/src/bin | |
| parent | a3f727e2e1dd81fe00f19d74f7e0e590250a6970 (diff) | |
Add lora-phy examples.
Diffstat (limited to 'examples/stm32wl/src/bin')
| -rw-r--r-- | examples/stm32wl/src/bin/lora_lorawan.rs | 6 | ||||
| -rw-r--r-- | examples/stm32wl/src/bin/lora_p2p_receive.rs | 127 | ||||
| -rw-r--r-- | examples/stm32wl/src/bin/lora_p2p_send.rs | 9 | ||||
| -rw-r--r-- | examples/stm32wl/src/bin/lorawan.rs | 104 |
4 files changed, 139 insertions, 107 deletions
diff --git a/examples/stm32wl/src/bin/lora_lorawan.rs b/examples/stm32wl/src/bin/lora_lorawan.rs index f7cf03595..4bcc5420e 100644 --- a/examples/stm32wl/src/bin/lora_lorawan.rs +++ b/examples/stm32wl/src/bin/lora_lorawan.rs | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | //! This example runs on a STM32WL board, which has a builtin Semtech Sx1262 radio. | 1 | //! This example runs on a STM32WL board, which has a builtin Semtech Sx1262 radio. |
| 2 | //! It demonstrates LoRaWAN functionality. | 2 | //! It demonstrates LoRaWAN join functionality. |
| 3 | #![no_std] | 3 | #![no_std] |
| 4 | #![no_main] | 4 | #![no_main] |
| 5 | #![macro_use] | 5 | #![macro_use] |
| @@ -28,6 +28,8 @@ use lorawan_device::async_device::lora_radio::LoRaRadio; | |||
| 28 | use lorawan_device::async_device::{region, Device, JoinMode}; | 28 | use lorawan_device::async_device::{region, Device, JoinMode}; |
| 29 | use {defmt_rtt as _, panic_probe as _}; | 29 | use {defmt_rtt as _, panic_probe as _}; |
| 30 | 30 | ||
| 31 | const LORAWAN_REGION: region::Region = region::Region::EU868; // warning: set this appropriately for the region | ||
| 32 | |||
| 31 | #[embassy_executor::main] | 33 | #[embassy_executor::main] |
| 32 | async fn main(_spawner: Spawner) { | 34 | async fn main(_spawner: Spawner) { |
| 33 | let mut config = embassy_stm32::Config::default(); | 35 | let mut config = embassy_stm32::Config::default(); |
| @@ -65,7 +67,7 @@ async fn main(_spawner: Spawner) { | |||
| 65 | } | 67 | } |
| 66 | }; | 68 | }; |
| 67 | let radio = LoRaRadio::new(lora); | 69 | let radio = LoRaRadio::new(lora); |
| 68 | let region: region::Configuration = region::Configuration::new(region::Region::EU868); | 70 | let region: region::Configuration = region::Configuration::new(LORAWAN_REGION); |
| 69 | let mut device: Device<_, Crypto, _, _> = Device::new(region, radio, LoraTimer::new(), Rng::new(p.RNG)); | 71 | let mut device: Device<_, Crypto, _, _> = Device::new(region, radio, LoraTimer::new(), Rng::new(p.RNG)); |
| 70 | 72 | ||
| 71 | defmt::info!("Joining LoRaWAN network"); | 73 | defmt::info!("Joining LoRaWAN network"); |
diff --git a/examples/stm32wl/src/bin/lora_p2p_receive.rs b/examples/stm32wl/src/bin/lora_p2p_receive.rs new file mode 100644 index 000000000..bb31518c4 --- /dev/null +++ b/examples/stm32wl/src/bin/lora_p2p_receive.rs | |||
| @@ -0,0 +1,127 @@ | |||
| 1 | //! This example runs on the STM32WL board, which has a builtin Semtech Sx1262 radio. | ||
| 2 | //! It demonstrates LORA P2P receive functionality in conjunction with the lora_p2p_send example. | ||
| 3 | #![no_std] | ||
| 4 | #![no_main] | ||
| 5 | #![macro_use] | ||
| 6 | #![feature(type_alias_impl_trait, async_fn_in_trait)] | ||
| 7 | #![allow(incomplete_features)] | ||
| 8 | |||
| 9 | use defmt::info; | ||
| 10 | use embassy_embedded_hal::adapter::BlockingAsync; | ||
| 11 | use embassy_executor::Spawner; | ||
| 12 | use embassy_lora::iv::Stm32wlInterfaceVariant; | ||
| 13 | use embassy_stm32::dma::NoDma; | ||
| 14 | use embassy_stm32::gpio::{Level, Output, Pin, Speed}; | ||
| 15 | use embassy_stm32::peripherals::SUBGHZSPI; | ||
| 16 | use embassy_stm32::rcc::low_level::RccPeripheral; | ||
| 17 | use embassy_stm32::spi::{BitOrder, Config as SpiConfig, Spi, MODE_0}; | ||
| 18 | use embassy_stm32::time::Hertz; | ||
| 19 | use embassy_stm32::{interrupt, into_ref, Peripheral}; | ||
| 20 | use embassy_time::{Delay, Duration, Timer}; | ||
| 21 | use lora_phy::mod_params::*; | ||
| 22 | use lora_phy::sx1261_2::SX1261_2; | ||
| 23 | use lora_phy::LoRa; | ||
| 24 | use {defmt_rtt as _, panic_probe as _}; | ||
| 25 | |||
| 26 | const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region | ||
| 27 | |||
| 28 | #[embassy_executor::main] | ||
| 29 | async fn main(_spawner: Spawner) { | ||
| 30 | let mut config = embassy_stm32::Config::default(); | ||
| 31 | config.rcc.mux = embassy_stm32::rcc::ClockSrc::HSE32; | ||
| 32 | let p = embassy_stm32::init(config); | ||
| 33 | |||
| 34 | let clk = Hertz(core::cmp::min(SUBGHZSPI::frequency().0 / 2, 16_000_000)); | ||
| 35 | let mut spi_config = SpiConfig::default(); | ||
| 36 | spi_config.mode = MODE_0; | ||
| 37 | spi_config.bit_order = BitOrder::MsbFirst; | ||
| 38 | let spi = Spi::new_subghz(p.SUBGHZSPI, NoDma, NoDma, clk, spi_config); | ||
| 39 | |||
| 40 | let spi = BlockingAsync::new(spi); | ||
| 41 | |||
| 42 | let irq = interrupt::take!(SUBGHZ_RADIO); | ||
| 43 | into_ref!(irq); | ||
| 44 | // Set CTRL1 and CTRL3 for high-power transmission, while CTRL2 acts as an RF switch between tx and rx | ||
| 45 | let _ctrl1 = Output::new(p.PC4.degrade(), Level::Low, Speed::High); | ||
| 46 | let ctrl2 = Output::new(p.PC5.degrade(), Level::High, Speed::High); | ||
| 47 | let _ctrl3 = Output::new(p.PC3.degrade(), Level::High, Speed::High); | ||
| 48 | let iv = Stm32wlInterfaceVariant::new(irq, None, Some(ctrl2)).unwrap(); | ||
| 49 | |||
| 50 | let mut delay = Delay; | ||
| 51 | |||
| 52 | let mut lora = { | ||
| 53 | match LoRa::new(SX1261_2::new(BoardType::Stm32wlSx1262, spi, iv), false, &mut delay).await { | ||
| 54 | Ok(l) => l, | ||
| 55 | Err(err) => { | ||
| 56 | info!("Radio error = {}", err); | ||
| 57 | return; | ||
| 58 | } | ||
| 59 | } | ||
| 60 | }; | ||
| 61 | |||
| 62 | let mut debug_indicator = Output::new(p.PB9, Level::Low, Speed::Low); | ||
| 63 | let mut start_indicator = Output::new(p.PB15, Level::Low, Speed::Low); | ||
| 64 | |||
| 65 | start_indicator.set_high(); | ||
| 66 | Timer::after(Duration::from_secs(5)).await; | ||
| 67 | start_indicator.set_low(); | ||
| 68 | |||
| 69 | let mut receiving_buffer = [00u8; 100]; | ||
| 70 | |||
| 71 | let mdltn_params = { | ||
| 72 | match lora.create_modulation_params( | ||
| 73 | SpreadingFactor::_10, | ||
| 74 | Bandwidth::_250KHz, | ||
| 75 | CodingRate::_4_8, | ||
| 76 | LORA_FREQUENCY_IN_HZ, | ||
| 77 | ) { | ||
| 78 | Ok(mp) => mp, | ||
| 79 | Err(err) => { | ||
| 80 | info!("Radio error = {}", err); | ||
| 81 | return; | ||
| 82 | } | ||
| 83 | } | ||
| 84 | }; | ||
| 85 | |||
| 86 | let rx_pkt_params = { | ||
| 87 | match lora.create_rx_packet_params(4, false, receiving_buffer.len() as u8, true, false, &mdltn_params) { | ||
| 88 | Ok(pp) => pp, | ||
| 89 | Err(err) => { | ||
| 90 | info!("Radio error = {}", err); | ||
| 91 | return; | ||
| 92 | } | ||
| 93 | } | ||
| 94 | }; | ||
| 95 | |||
| 96 | match lora | ||
| 97 | .prepare_for_rx(&mdltn_params, &rx_pkt_params, None, true, false, 0, 0x00ffffffu32) | ||
| 98 | .await | ||
| 99 | { | ||
| 100 | Ok(()) => {} | ||
| 101 | Err(err) => { | ||
| 102 | info!("Radio error = {}", err); | ||
| 103 | return; | ||
| 104 | } | ||
| 105 | }; | ||
| 106 | |||
| 107 | loop { | ||
| 108 | receiving_buffer = [00u8; 100]; | ||
| 109 | match lora.rx(&rx_pkt_params, &mut receiving_buffer).await { | ||
| 110 | Ok((received_len, _rx_pkt_status)) => { | ||
| 111 | if (received_len == 3) | ||
| 112 | && (receiving_buffer[0] == 0x01u8) | ||
| 113 | && (receiving_buffer[1] == 0x02u8) | ||
| 114 | && (receiving_buffer[2] == 0x03u8) | ||
| 115 | { | ||
| 116 | info!("rx successful"); | ||
| 117 | debug_indicator.set_high(); | ||
| 118 | Timer::after(Duration::from_secs(5)).await; | ||
| 119 | debug_indicator.set_low(); | ||
| 120 | } else { | ||
| 121 | info!("rx unknown packet"); | ||
| 122 | } | ||
| 123 | } | ||
| 124 | Err(err) => info!("rx unsuccessful = {}", err), | ||
| 125 | } | ||
| 126 | } | ||
| 127 | } | ||
diff --git a/examples/stm32wl/src/bin/lora_p2p_send.rs b/examples/stm32wl/src/bin/lora_p2p_send.rs index 11c35a8fe..8a38402fa 100644 --- a/examples/stm32wl/src/bin/lora_p2p_send.rs +++ b/examples/stm32wl/src/bin/lora_p2p_send.rs | |||
| @@ -23,6 +23,8 @@ use lora_phy::sx1261_2::SX1261_2; | |||
| 23 | use lora_phy::LoRa; | 23 | use lora_phy::LoRa; |
| 24 | use {defmt_rtt as _, panic_probe as _}; | 24 | use {defmt_rtt as _, panic_probe as _}; |
| 25 | 25 | ||
| 26 | const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region | ||
| 27 | |||
| 26 | #[embassy_executor::main] | 28 | #[embassy_executor::main] |
| 27 | async fn main(_spawner: Spawner) { | 29 | async fn main(_spawner: Spawner) { |
| 28 | let mut config = embassy_stm32::Config::default(); | 30 | let mut config = embassy_stm32::Config::default(); |
| @@ -58,7 +60,12 @@ async fn main(_spawner: Spawner) { | |||
| 58 | }; | 60 | }; |
| 59 | 61 | ||
| 60 | let mdltn_params = { | 62 | let mdltn_params = { |
| 61 | match lora.create_modulation_params(SpreadingFactor::_10, Bandwidth::_250KHz, CodingRate::_4_8, 903900000) { | 63 | match lora.create_modulation_params( |
| 64 | SpreadingFactor::_10, | ||
| 65 | Bandwidth::_250KHz, | ||
| 66 | CodingRate::_4_8, | ||
| 67 | LORA_FREQUENCY_IN_HZ, | ||
| 68 | ) { | ||
| 62 | Ok(mp) => mp, | 69 | Ok(mp) => mp, |
| 63 | Err(err) => { | 70 | Err(err) => { |
| 64 | info!("Radio error = {}", err); | 71 | info!("Radio error = {}", err); |
diff --git a/examples/stm32wl/src/bin/lorawan.rs b/examples/stm32wl/src/bin/lorawan.rs deleted file mode 100644 index 32f29cc5d..000000000 --- a/examples/stm32wl/src/bin/lorawan.rs +++ /dev/null | |||
| @@ -1,104 +0,0 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![macro_use] | ||
| 4 | #![allow(dead_code)] | ||
| 5 | #![feature(type_alias_impl_trait)] | ||
| 6 | |||
| 7 | use embassy_executor::Spawner; | ||
| 8 | use embassy_lora::stm32wl::*; | ||
| 9 | use embassy_lora::LoraTimer; | ||
| 10 | use embassy_stm32::dma::NoDma; | ||
| 11 | use embassy_stm32::gpio::{AnyPin, Level, Output, Pin, Speed}; | ||
| 12 | use embassy_stm32::rng::Rng; | ||
| 13 | use embassy_stm32::subghz::*; | ||
| 14 | use embassy_stm32::{interrupt, pac}; | ||
| 15 | use lorawan::default_crypto::DefaultFactory as Crypto; | ||
| 16 | use lorawan_device::async_device::{region, Device, JoinMode}; | ||
| 17 | use {defmt_rtt as _, panic_probe as _}; | ||
| 18 | |||
| 19 | struct RadioSwitch<'a> { | ||
| 20 | ctrl1: Output<'a, AnyPin>, | ||
| 21 | ctrl2: Output<'a, AnyPin>, | ||
| 22 | ctrl3: Output<'a, AnyPin>, | ||
| 23 | } | ||
| 24 | |||
| 25 | impl<'a> RadioSwitch<'a> { | ||
| 26 | fn new(ctrl1: Output<'a, AnyPin>, ctrl2: Output<'a, AnyPin>, ctrl3: Output<'a, AnyPin>) -> Self { | ||
| 27 | Self { ctrl1, ctrl2, ctrl3 } | ||
| 28 | } | ||
| 29 | } | ||
| 30 | |||
| 31 | impl<'a> embassy_lora::stm32wl::RadioSwitch for RadioSwitch<'a> { | ||
| 32 | fn set_rx(&mut self) { | ||
| 33 | self.ctrl1.set_high(); | ||
| 34 | self.ctrl2.set_low(); | ||
| 35 | self.ctrl3.set_high(); | ||
| 36 | } | ||
| 37 | |||
| 38 | fn set_tx(&mut self) { | ||
| 39 | self.ctrl1.set_high(); | ||
| 40 | self.ctrl2.set_high(); | ||
| 41 | self.ctrl3.set_high(); | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | #[embassy_executor::main] | ||
| 46 | async fn main(_spawner: Spawner) { | ||
| 47 | let mut config = embassy_stm32::Config::default(); | ||
| 48 | config.rcc.mux = embassy_stm32::rcc::ClockSrc::HSI16; | ||
| 49 | config.rcc.enable_lsi = true; | ||
| 50 | let p = embassy_stm32::init(config); | ||
| 51 | |||
| 52 | unsafe { pac::RCC.ccipr().modify(|w| w.set_rngsel(0b01)) } | ||
| 53 | |||
| 54 | let ctrl1 = Output::new(p.PC3.degrade(), Level::High, Speed::High); | ||
| 55 | let ctrl2 = Output::new(p.PC4.degrade(), Level::High, Speed::High); | ||
| 56 | let ctrl3 = Output::new(p.PC5.degrade(), Level::High, Speed::High); | ||
| 57 | let rfs = RadioSwitch::new(ctrl1, ctrl2, ctrl3); | ||
| 58 | |||
| 59 | let radio = SubGhz::new(p.SUBGHZSPI, NoDma, NoDma); | ||
| 60 | let irq = interrupt::take!(SUBGHZ_RADIO); | ||
| 61 | |||
| 62 | let mut radio_config = SubGhzRadioConfig::default(); | ||
| 63 | radio_config.calibrate_image = CalibrateImage::ISM_863_870; | ||
| 64 | let radio = SubGhzRadio::new(radio, rfs, irq, radio_config).unwrap(); | ||
| 65 | |||
| 66 | let mut region = region::Configuration::new(region::Region::EU868); | ||
| 67 | |||
| 68 | // NOTE: This is specific for TTN, as they have a special RX1 delay | ||
| 69 | region.set_receive_delay1(5000); | ||
| 70 | |||
| 71 | let mut device: Device<_, Crypto, _, _> = Device::new(region, radio, LoraTimer::new(), Rng::new(p.RNG)); | ||
| 72 | |||
| 73 | // Depending on network, this might be part of JOIN | ||
| 74 | device.set_datarate(region::DR::_0); // SF12 | ||
| 75 | |||
| 76 | // device.set_datarate(region::DR::_1); // SF11 | ||
| 77 | // device.set_datarate(region::DR::_2); // SF10 | ||
| 78 | // device.set_datarate(region::DR::_3); // SF9 | ||
| 79 | // device.set_datarate(region::DR::_4); // SF8 | ||
| 80 | // device.set_datarate(region::DR::_5); // SF7 | ||
| 81 | |||
| 82 | defmt::info!("Joining LoRaWAN network"); | ||
| 83 | |||
| 84 | // TODO: Adjust the EUI and Keys according to your network credentials | ||
| 85 | device | ||
| 86 | .join(&JoinMode::OTAA { | ||
| 87 | deveui: [0, 0, 0, 0, 0, 0, 0, 0], | ||
| 88 | appeui: [0, 0, 0, 0, 0, 0, 0, 0], | ||
| 89 | appkey: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
| 90 | }) | ||
| 91 | .await | ||
| 92 | .ok() | ||
| 93 | .unwrap(); | ||
| 94 | defmt::info!("LoRaWAN network joined"); | ||
| 95 | |||
| 96 | let mut rx: [u8; 255] = [0; 255]; | ||
| 97 | defmt::info!("Sending 'PING'"); | ||
| 98 | let len = device.send_recv(b"PING", &mut rx[..], 1, true).await.ok().unwrap(); | ||
| 99 | if len > 0 { | ||
| 100 | defmt::info!("Message sent, received downlink: {:?}", &rx[..len]); | ||
| 101 | } else { | ||
| 102 | defmt::info!("Message sent!"); | ||
| 103 | } | ||
| 104 | } | ||
