diff options
| author | ceekdee <[email protected]> | 2023-04-21 01:20:46 -0500 |
|---|---|---|
| committer | ceekdee <[email protected]> | 2023-04-21 01:20:46 -0500 |
| commit | 02c86bca5275328a15376176ff44487ab7655866 (patch) | |
| tree | 4f4c3c76261e95e389219b7732cfe6da5777de98 /examples/stm32wl/src | |
| parent | fb27594b2eb2cca2aea25dd92a7b730c185b6ecc (diff) | |
Add external LoRa physical layer functionality.
Diffstat (limited to 'examples/stm32wl/src')
| -rw-r--r-- | examples/stm32wl/src/bin/lora_lorawan.rs | 88 | ||||
| -rw-r--r-- | examples/stm32wl/src/bin/lora_p2p_send.rs | 103 |
2 files changed, 191 insertions, 0 deletions
diff --git a/examples/stm32wl/src/bin/lora_lorawan.rs b/examples/stm32wl/src/bin/lora_lorawan.rs new file mode 100644 index 000000000..f7cf03595 --- /dev/null +++ b/examples/stm32wl/src/bin/lora_lorawan.rs | |||
| @@ -0,0 +1,88 @@ | |||
| 1 | //! This example runs on a STM32WL board, which has a builtin Semtech Sx1262 radio. | ||
| 2 | //! It demonstrates LoRaWAN functionality. | ||
| 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_lora::LoraTimer; | ||
| 14 | use embassy_stm32::dma::NoDma; | ||
| 15 | use embassy_stm32::gpio::{Level, Output, Pin, Speed}; | ||
| 16 | use embassy_stm32::peripherals::SUBGHZSPI; | ||
| 17 | use embassy_stm32::rcc::low_level::RccPeripheral; | ||
| 18 | use embassy_stm32::rng::Rng; | ||
| 19 | use embassy_stm32::spi::{BitOrder, Config as SpiConfig, Spi, MODE_0}; | ||
| 20 | use embassy_stm32::time::Hertz; | ||
| 21 | use embassy_stm32::{interrupt, into_ref, pac, Peripheral}; | ||
| 22 | use embassy_time::Delay; | ||
| 23 | use lora_phy::mod_params::*; | ||
| 24 | use lora_phy::sx1261_2::SX1261_2; | ||
| 25 | use lora_phy::LoRa; | ||
| 26 | use lorawan::default_crypto::DefaultFactory as Crypto; | ||
| 27 | use lorawan_device::async_device::lora_radio::LoRaRadio; | ||
| 28 | use lorawan_device::async_device::{region, Device, JoinMode}; | ||
| 29 | use {defmt_rtt as _, panic_probe as _}; | ||
| 30 | |||
| 31 | #[embassy_executor::main] | ||
| 32 | async fn main(_spawner: Spawner) { | ||
| 33 | let mut config = embassy_stm32::Config::default(); | ||
| 34 | config.rcc.mux = embassy_stm32::rcc::ClockSrc::HSI16; | ||
| 35 | config.rcc.enable_lsi = true; | ||
| 36 | let p = embassy_stm32::init(config); | ||
| 37 | |||
| 38 | unsafe { pac::RCC.ccipr().modify(|w| w.set_rngsel(0b01)) } | ||
| 39 | |||
| 40 | let clk = Hertz(core::cmp::min(SUBGHZSPI::frequency().0 / 2, 16_000_000)); | ||
| 41 | let mut spi_config = SpiConfig::default(); | ||
| 42 | spi_config.mode = MODE_0; | ||
| 43 | spi_config.bit_order = BitOrder::MsbFirst; | ||
| 44 | let spi = Spi::new_subghz(p.SUBGHZSPI, NoDma, NoDma, clk, spi_config); | ||
| 45 | |||
| 46 | let spi = BlockingAsync::new(spi); | ||
| 47 | |||
| 48 | let irq = interrupt::take!(SUBGHZ_RADIO); | ||
| 49 | into_ref!(irq); | ||
| 50 | // Set CTRL1 and CTRL3 for high-power transmission, while CTRL2 acts as an RF switch between tx and rx | ||
| 51 | let _ctrl1 = Output::new(p.PC4.degrade(), Level::Low, Speed::High); | ||
| 52 | let ctrl2 = Output::new(p.PC5.degrade(), Level::High, Speed::High); | ||
| 53 | let _ctrl3 = Output::new(p.PC3.degrade(), Level::High, Speed::High); | ||
| 54 | let iv = Stm32wlInterfaceVariant::new(irq, None, Some(ctrl2)).unwrap(); | ||
| 55 | |||
| 56 | let mut delay = Delay; | ||
| 57 | |||
| 58 | let lora = { | ||
| 59 | match LoRa::new(SX1261_2::new(BoardType::Stm32wlSx1262, spi, iv), true, &mut delay).await { | ||
| 60 | Ok(l) => l, | ||
| 61 | Err(err) => { | ||
| 62 | info!("Radio error = {}", err); | ||
| 63 | return; | ||
| 64 | } | ||
| 65 | } | ||
| 66 | }; | ||
| 67 | let radio = LoRaRadio::new(lora); | ||
| 68 | let region: region::Configuration = region::Configuration::new(region::Region::EU868); | ||
| 69 | let mut device: Device<_, Crypto, _, _> = Device::new(region, radio, LoraTimer::new(), Rng::new(p.RNG)); | ||
| 70 | |||
| 71 | defmt::info!("Joining LoRaWAN network"); | ||
| 72 | |||
| 73 | // TODO: Adjust the EUI and Keys according to your network credentials | ||
| 74 | match device | ||
| 75 | .join(&JoinMode::OTAA { | ||
| 76 | deveui: [0, 0, 0, 0, 0, 0, 0, 0], | ||
| 77 | appeui: [0, 0, 0, 0, 0, 0, 0, 0], | ||
| 78 | appkey: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
| 79 | }) | ||
| 80 | .await | ||
| 81 | { | ||
| 82 | Ok(()) => defmt::info!("LoRaWAN network joined"), | ||
| 83 | Err(err) => { | ||
| 84 | info!("Radio error = {}", err); | ||
| 85 | return; | ||
| 86 | } | ||
| 87 | }; | ||
| 88 | } | ||
diff --git a/examples/stm32wl/src/bin/lora_p2p_send.rs b/examples/stm32wl/src/bin/lora_p2p_send.rs new file mode 100644 index 000000000..11c35a8fe --- /dev/null +++ b/examples/stm32wl/src/bin/lora_p2p_send.rs | |||
| @@ -0,0 +1,103 @@ | |||
| 1 | //! This example runs on a STM32WL board, which has a builtin Semtech Sx1262 radio. | ||
| 2 | //! It demonstrates LORA P2P send functionality. | ||
| 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; | ||
| 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 | #[embassy_executor::main] | ||
| 27 | async fn main(_spawner: Spawner) { | ||
| 28 | let mut config = embassy_stm32::Config::default(); | ||
| 29 | config.rcc.mux = embassy_stm32::rcc::ClockSrc::HSE32; | ||
| 30 | let p = embassy_stm32::init(config); | ||
| 31 | |||
| 32 | let clk = Hertz(core::cmp::min(SUBGHZSPI::frequency().0 / 2, 16_000_000)); | ||
| 33 | let mut spi_config = SpiConfig::default(); | ||
| 34 | spi_config.mode = MODE_0; | ||
| 35 | spi_config.bit_order = BitOrder::MsbFirst; | ||
| 36 | let spi = Spi::new_subghz(p.SUBGHZSPI, NoDma, NoDma, clk, spi_config); | ||
| 37 | |||
| 38 | let spi = BlockingAsync::new(spi); | ||
| 39 | |||
| 40 | let irq = interrupt::take!(SUBGHZ_RADIO); | ||
| 41 | into_ref!(irq); | ||
| 42 | // Set CTRL1 and CTRL3 for high-power transmission, while CTRL2 acts as an RF switch between tx and rx | ||
| 43 | let _ctrl1 = Output::new(p.PC4.degrade(), Level::Low, Speed::High); | ||
| 44 | let ctrl2 = Output::new(p.PC5.degrade(), Level::High, Speed::High); | ||
| 45 | let _ctrl3 = Output::new(p.PC3.degrade(), Level::High, Speed::High); | ||
| 46 | let iv = Stm32wlInterfaceVariant::new(irq, None, Some(ctrl2)).unwrap(); | ||
| 47 | |||
| 48 | let mut delay = Delay; | ||
| 49 | |||
| 50 | let mut lora = { | ||
| 51 | match LoRa::new(SX1261_2::new(BoardType::Stm32wlSx1262, spi, iv), false, &mut delay).await { | ||
| 52 | Ok(l) => l, | ||
| 53 | Err(err) => { | ||
| 54 | info!("Radio error = {}", err); | ||
| 55 | return; | ||
| 56 | } | ||
| 57 | } | ||
| 58 | }; | ||
| 59 | |||
| 60 | let mdltn_params = { | ||
| 61 | match lora.create_modulation_params(SpreadingFactor::_10, Bandwidth::_250KHz, CodingRate::_4_8, 903900000) { | ||
| 62 | Ok(mp) => mp, | ||
| 63 | Err(err) => { | ||
| 64 | info!("Radio error = {}", err); | ||
| 65 | return; | ||
| 66 | } | ||
| 67 | } | ||
| 68 | }; | ||
| 69 | |||
| 70 | let mut tx_pkt_params = { | ||
| 71 | match lora.create_tx_packet_params(4, false, true, false, &mdltn_params) { | ||
| 72 | Ok(pp) => pp, | ||
| 73 | Err(err) => { | ||
| 74 | info!("Radio error = {}", err); | ||
| 75 | return; | ||
| 76 | } | ||
| 77 | } | ||
| 78 | }; | ||
| 79 | |||
| 80 | match lora.prepare_for_tx(&mdltn_params, 20, false).await { | ||
| 81 | Ok(()) => {} | ||
| 82 | Err(err) => { | ||
| 83 | info!("Radio error = {}", err); | ||
| 84 | return; | ||
| 85 | } | ||
| 86 | }; | ||
| 87 | |||
| 88 | let buffer = [0x01u8, 0x02u8, 0x03u8]; | ||
| 89 | match lora.tx(&mdltn_params, &mut tx_pkt_params, &buffer, 0xffffff).await { | ||
| 90 | Ok(()) => { | ||
| 91 | info!("TX DONE"); | ||
| 92 | } | ||
| 93 | Err(err) => { | ||
| 94 | info!("Radio error = {}", err); | ||
| 95 | return; | ||
| 96 | } | ||
| 97 | }; | ||
| 98 | |||
| 99 | match lora.sleep(&mut delay).await { | ||
| 100 | Ok(()) => info!("Sleep successful"), | ||
| 101 | Err(err) => info!("Sleep unsuccessful = {}", err), | ||
| 102 | } | ||
| 103 | } | ||
