From 73f25093c7793ad2e8bd6fceeca46d9b5b1031ad Mon Sep 17 00:00:00 2001 From: ceekdee Date: Sun, 23 Apr 2023 18:32:34 -0500 Subject: [PATCH] Add lora-phy examples. --- embassy-lora/src/lib.rs | 3 + examples/nrf52840/src/bin/lora_cad.rs | 13 +- examples/nrf52840/src/bin/lora_lorawan.rs | 83 +++++++++++ examples/nrf52840/src/bin/lora_p2p_receive.rs | 121 +++++++++++++++ .../src/bin/lora_p2p_receive_duty_cycle.rs | 11 +- examples/nrf52840/src/bin/lora_p2p_send.rs | 104 +++++++++++++ examples/nrf52840/src/bin/lora_p2p_sense.rs | 128 ---------------- examples/rp/src/bin/lora_lorawan.rs | 80 ++++++++++ examples/rp/src/bin/lora_p2p_receive.rs | 115 +++++++++++++++ examples/rp/src/bin/lora_p2p_send.rs | 103 +++++++++++++ .../rp/src/bin/lora_p2p_send_multicore.rs | 139 ++++++++++++++++++ examples/stm32l0/src/bin/lora_cad.rs | 105 +++++++++++++ examples/stm32l0/src/bin/lora_lorawan.rs | 88 +++++++++++ examples/stm32l0/src/bin/lora_p2p_receive.rs | 11 +- examples/stm32l0/src/bin/lora_p2p_send.rs | 110 ++++++++++++++ examples/stm32l0/src/bin/lorawan.rs | 74 ---------- examples/stm32wl/src/bin/lora_lorawan.rs | 6 +- examples/stm32wl/src/bin/lora_p2p_receive.rs | 127 ++++++++++++++++ examples/stm32wl/src/bin/lora_p2p_send.rs | 9 +- examples/stm32wl/src/bin/lorawan.rs | 104 ------------- 20 files changed, 1218 insertions(+), 316 deletions(-) create mode 100644 examples/nrf52840/src/bin/lora_lorawan.rs create mode 100644 examples/nrf52840/src/bin/lora_p2p_receive.rs create mode 100644 examples/nrf52840/src/bin/lora_p2p_send.rs delete mode 100644 examples/nrf52840/src/bin/lora_p2p_sense.rs create mode 100644 examples/rp/src/bin/lora_lorawan.rs create mode 100644 examples/rp/src/bin/lora_p2p_receive.rs create mode 100644 examples/rp/src/bin/lora_p2p_send.rs create mode 100644 examples/rp/src/bin/lora_p2p_send_multicore.rs create mode 100644 examples/stm32l0/src/bin/lora_cad.rs create mode 100644 examples/stm32l0/src/bin/lora_lorawan.rs create mode 100644 examples/stm32l0/src/bin/lora_p2p_send.rs delete mode 100644 examples/stm32l0/src/bin/lorawan.rs create mode 100644 examples/stm32wl/src/bin/lora_p2p_receive.rs delete mode 100644 examples/stm32wl/src/bin/lorawan.rs diff --git a/embassy-lora/src/lib.rs b/embassy-lora/src/lib.rs index d8bc3cd9..c01d3f71 100644 --- a/embassy-lora/src/lib.rs +++ b/embassy-lora/src/lib.rs @@ -10,10 +10,13 @@ pub(crate) mod fmt; pub mod iv; #[cfg(feature = "stm32wl")] +#[deprecated(note = "use the external LoRa physical layer crate - https://crates.io/crates/lora-phy")] pub mod stm32wl; #[cfg(feature = "sx126x")] +#[deprecated(note = "use the external LoRa physical layer crate - https://crates.io/crates/lora-phy")] pub mod sx126x; #[cfg(feature = "sx127x")] +#[deprecated(note = "use the external LoRa physical layer crate - https://crates.io/crates/lora-phy")] pub mod sx127x; #[cfg(feature = "time")] diff --git a/examples/nrf52840/src/bin/lora_cad.rs b/examples/nrf52840/src/bin/lora_cad.rs index 8899c1b2..beca061e 100644 --- a/examples/nrf52840/src/bin/lora_cad.rs +++ b/examples/nrf52840/src/bin/lora_cad.rs @@ -1,6 +1,6 @@ //! This example runs on the RAK4631 WisBlock, which has an nRF52840 MCU and Semtech Sx126x radio. //! Other nrf/sx126x combinations may work with appropriate pin modifications. -//! It demonstates LORA CAD functionality. +//! It demonstrates LORA CAD functionality. #![no_std] #![no_main] #![macro_use] @@ -17,6 +17,8 @@ use lora_phy::sx1261_2::SX1261_2; use lora_phy::LoRa; use {defmt_rtt as _, panic_probe as _}; +const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region + bind_interrupts!(struct Irqs { SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1 => spim::InterruptHandler; }); @@ -59,7 +61,12 @@ async fn main(_spawner: Spawner) { start_indicator.set_low(); let mdltn_params = { - match lora.create_modulation_params(SpreadingFactor::_10, Bandwidth::_250KHz, CodingRate::_4_8, 903900000) { + match lora.create_modulation_params( + SpreadingFactor::_10, + Bandwidth::_250KHz, + CodingRate::_4_8, + LORA_FREQUENCY_IN_HZ, + ) { Ok(mp) => mp, Err(err) => { info!("Radio error = {}", err); @@ -84,7 +91,7 @@ async fn main(_spawner: Spawner) { info!("cad successful without activity detected") } debug_indicator.set_high(); - Timer::after(Duration::from_secs(15)).await; + Timer::after(Duration::from_secs(5)).await; debug_indicator.set_low(); } Err(err) => info!("cad unsuccessful = {}", err), diff --git a/examples/nrf52840/src/bin/lora_lorawan.rs b/examples/nrf52840/src/bin/lora_lorawan.rs new file mode 100644 index 00000000..c953680c --- /dev/null +++ b/examples/nrf52840/src/bin/lora_lorawan.rs @@ -0,0 +1,83 @@ +//! This example runs on the RAK4631 WisBlock, which has an nRF52840 MCU and Semtech Sx126x radio. +//! Other nrf/sx126x combinations may work with appropriate pin modifications. +//! It demonstrates LoRaWAN join functionality. +#![no_std] +#![no_main] +#![macro_use] +#![feature(type_alias_impl_trait)] + +use defmt::*; +use embassy_executor::Spawner; +use embassy_lora::iv::GenericSx126xInterfaceVariant; +use embassy_lora::LoraTimer; +use embassy_nrf::gpio::{Input, Level, Output, OutputDrive, Pin as _, Pull}; +use embassy_nrf::rng::Rng; +use embassy_nrf::{bind_interrupts, peripherals, rng, spim}; +use embassy_time::Delay; +use lora_phy::mod_params::*; +use lora_phy::sx1261_2::SX1261_2; +use lora_phy::LoRa; +use lorawan::default_crypto::DefaultFactory as Crypto; +use lorawan_device::async_device::lora_radio::LoRaRadio; +use lorawan_device::async_device::{region, Device, JoinMode}; +use {defmt_rtt as _, panic_probe as _}; + +const LORAWAN_REGION: region::Region = region::Region::EU868; // warning: set this appropriately for the region + +bind_interrupts!(struct Irqs { + SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1 => spim::InterruptHandler; + RNG => rng::InterruptHandler; +}); + +#[embassy_executor::main] +async fn main(_spawner: Spawner) { + let p = embassy_nrf::init(Default::default()); + let mut spi_config = spim::Config::default(); + spi_config.frequency = spim::Frequency::M16; + + let spim = spim::Spim::new(p.TWISPI1, Irqs, p.P1_11, p.P1_13, p.P1_12, spi_config); + + let nss = Output::new(p.P1_10.degrade(), Level::High, OutputDrive::Standard); + let reset = Output::new(p.P1_06.degrade(), Level::High, OutputDrive::Standard); + let dio1 = Input::new(p.P1_15.degrade(), Pull::Down); + let busy = Input::new(p.P1_14.degrade(), Pull::Down); + let rf_switch_rx = Output::new(p.P1_05.degrade(), Level::Low, OutputDrive::Standard); + let rf_switch_tx = Output::new(p.P1_07.degrade(), Level::Low, OutputDrive::Standard); + + let iv = + GenericSx126xInterfaceVariant::new(nss, reset, dio1, busy, Some(rf_switch_rx), Some(rf_switch_tx)).unwrap(); + + let mut delay = Delay; + + let lora = { + match LoRa::new(SX1261_2::new(BoardType::Rak4631Sx1262, spim, iv), true, &mut delay).await { + Ok(l) => l, + Err(err) => { + info!("Radio error = {}", err); + return; + } + } + }; + + let radio = LoRaRadio::new(lora); + let region: region::Configuration = region::Configuration::new(LORAWAN_REGION); + let mut device: Device<_, Crypto, _, _> = Device::new(region, radio, LoraTimer::new(), Rng::new(p.RNG, Irqs)); + + defmt::info!("Joining LoRaWAN network"); + + // TODO: Adjust the EUI and Keys according to your network credentials + match device + .join(&JoinMode::OTAA { + deveui: [0, 0, 0, 0, 0, 0, 0, 0], + appeui: [0, 0, 0, 0, 0, 0, 0, 0], + appkey: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + }) + .await + { + Ok(()) => defmt::info!("LoRaWAN network joined"), + Err(err) => { + info!("Radio error = {}", err); + return; + } + }; +} diff --git a/examples/nrf52840/src/bin/lora_p2p_receive.rs b/examples/nrf52840/src/bin/lora_p2p_receive.rs new file mode 100644 index 00000000..563fe42e --- /dev/null +++ b/examples/nrf52840/src/bin/lora_p2p_receive.rs @@ -0,0 +1,121 @@ +//! This example runs on the RAK4631 WisBlock, which has an nRF52840 MCU and Semtech Sx126x radio. +//! Other nrf/sx126x combinations may work with appropriate pin modifications. +//! It demonstrates LORA P2P receive functionality in conjunction with the lora_p2p_send example. +#![no_std] +#![no_main] +#![macro_use] +#![feature(type_alias_impl_trait)] + +use defmt::*; +use embassy_executor::Spawner; +use embassy_lora::iv::GenericSx126xInterfaceVariant; +use embassy_nrf::gpio::{Input, Level, Output, OutputDrive, Pin as _, Pull}; +use embassy_nrf::{bind_interrupts, peripherals, spim}; +use embassy_time::{Delay, Duration, Timer}; +use lora_phy::mod_params::*; +use lora_phy::sx1261_2::SX1261_2; +use lora_phy::LoRa; +use {defmt_rtt as _, panic_probe as _}; + +const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region + +bind_interrupts!(struct Irqs { + SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1 => spim::InterruptHandler; +}); + +#[embassy_executor::main] +async fn main(_spawner: Spawner) { + let p = embassy_nrf::init(Default::default()); + let mut spi_config = spim::Config::default(); + spi_config.frequency = spim::Frequency::M16; + + let spim = spim::Spim::new(p.TWISPI1, Irqs, p.P1_11, p.P1_13, p.P1_12, spi_config); + + let nss = Output::new(p.P1_10.degrade(), Level::High, OutputDrive::Standard); + let reset = Output::new(p.P1_06.degrade(), Level::High, OutputDrive::Standard); + let dio1 = Input::new(p.P1_15.degrade(), Pull::Down); + let busy = Input::new(p.P1_14.degrade(), Pull::Down); + let rf_switch_rx = Output::new(p.P1_05.degrade(), Level::Low, OutputDrive::Standard); + let rf_switch_tx = Output::new(p.P1_07.degrade(), Level::Low, OutputDrive::Standard); + + let iv = + GenericSx126xInterfaceVariant::new(nss, reset, dio1, busy, Some(rf_switch_rx), Some(rf_switch_tx)).unwrap(); + + let mut delay = Delay; + + let mut lora = { + match LoRa::new(SX1261_2::new(BoardType::Rak4631Sx1262, spim, iv), false, &mut delay).await { + Ok(l) => l, + Err(err) => { + info!("Radio error = {}", err); + return; + } + } + }; + + let mut debug_indicator = Output::new(p.P1_03, Level::Low, OutputDrive::Standard); + let mut start_indicator = Output::new(p.P1_04, Level::Low, OutputDrive::Standard); + + start_indicator.set_high(); + Timer::after(Duration::from_secs(5)).await; + start_indicator.set_low(); + + let mut receiving_buffer = [00u8; 100]; + + let mdltn_params = { + match lora.create_modulation_params( + SpreadingFactor::_10, + Bandwidth::_250KHz, + CodingRate::_4_8, + LORA_FREQUENCY_IN_HZ, + ) { + Ok(mp) => mp, + Err(err) => { + info!("Radio error = {}", err); + return; + } + } + }; + + let rx_pkt_params = { + match lora.create_rx_packet_params(4, false, receiving_buffer.len() as u8, true, false, &mdltn_params) { + Ok(pp) => pp, + Err(err) => { + info!("Radio error = {}", err); + return; + } + } + }; + + match lora + .prepare_for_rx(&mdltn_params, &rx_pkt_params, None, true, false, 0, 0x00ffffffu32) + .await + { + Ok(()) => {} + Err(err) => { + info!("Radio error = {}", err); + return; + } + }; + + loop { + receiving_buffer = [00u8; 100]; + match lora.rx(&rx_pkt_params, &mut receiving_buffer).await { + Ok((received_len, _rx_pkt_status)) => { + if (received_len == 3) + && (receiving_buffer[0] == 0x01u8) + && (receiving_buffer[1] == 0x02u8) + && (receiving_buffer[2] == 0x03u8) + { + info!("rx successful"); + debug_indicator.set_high(); + Timer::after(Duration::from_secs(5)).await; + debug_indicator.set_low(); + } else { + info!("rx unknown packet"); + } + } + Err(err) => info!("rx unsuccessful = {}", err), + } + } +} diff --git a/examples/nrf52840/src/bin/lora_p2p_receive_duty_cycle.rs b/examples/nrf52840/src/bin/lora_p2p_receive_duty_cycle.rs index d8470174..1fd8f61a 100644 --- a/examples/nrf52840/src/bin/lora_p2p_receive_duty_cycle.rs +++ b/examples/nrf52840/src/bin/lora_p2p_receive_duty_cycle.rs @@ -1,6 +1,6 @@ //! This example runs on the RAK4631 WisBlock, which has an nRF52840 MCU and Semtech Sx126x radio. //! Other nrf/sx126x combinations may work with appropriate pin modifications. -//! It demonstates LoRa Rx duty cycle functionality. +//! It demonstrates LoRa Rx duty cycle functionality in conjunction with the lora_p2p_send example. #![no_std] #![no_main] #![macro_use] @@ -17,6 +17,8 @@ use lora_phy::sx1261_2::SX1261_2; use lora_phy::LoRa; use {defmt_rtt as _, panic_probe as _}; +const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region + bind_interrupts!(struct Irqs { SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1 => spim::InterruptHandler; }); @@ -61,7 +63,12 @@ async fn main(_spawner: Spawner) { let mut receiving_buffer = [00u8; 100]; let mdltn_params = { - match lora.create_modulation_params(SpreadingFactor::_10, Bandwidth::_250KHz, CodingRate::_4_8, 903900000) { + match lora.create_modulation_params( + SpreadingFactor::_10, + Bandwidth::_250KHz, + CodingRate::_4_8, + LORA_FREQUENCY_IN_HZ, + ) { Ok(mp) => mp, Err(err) => { info!("Radio error = {}", err); diff --git a/examples/nrf52840/src/bin/lora_p2p_send.rs b/examples/nrf52840/src/bin/lora_p2p_send.rs new file mode 100644 index 00000000..1c8bbc27 --- /dev/null +++ b/examples/nrf52840/src/bin/lora_p2p_send.rs @@ -0,0 +1,104 @@ +//! This example runs on the RAK4631 WisBlock, which has an nRF52840 MCU and Semtech Sx126x radio. +//! Other nrf/sx126x combinations may work with appropriate pin modifications. +//! It demonstrates LORA P2P send functionality. +#![no_std] +#![no_main] +#![macro_use] +#![feature(type_alias_impl_trait)] + +use defmt::*; +use embassy_executor::Spawner; +use embassy_lora::iv::GenericSx126xInterfaceVariant; +use embassy_nrf::gpio::{Input, Level, Output, OutputDrive, Pin as _, Pull}; +use embassy_nrf::{bind_interrupts, peripherals, spim}; +use embassy_time::Delay; +use lora_phy::mod_params::*; +use lora_phy::sx1261_2::SX1261_2; +use lora_phy::LoRa; +use {defmt_rtt as _, panic_probe as _}; + +const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region + +bind_interrupts!(struct Irqs { + SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1 => spim::InterruptHandler; +}); + +#[embassy_executor::main] +async fn main(_spawner: Spawner) { + let p = embassy_nrf::init(Default::default()); + let mut spi_config = spim::Config::default(); + spi_config.frequency = spim::Frequency::M16; + + let spim = spim::Spim::new(p.TWISPI1, Irqs, p.P1_11, p.P1_13, p.P1_12, spi_config); + + let nss = Output::new(p.P1_10.degrade(), Level::High, OutputDrive::Standard); + let reset = Output::new(p.P1_06.degrade(), Level::High, OutputDrive::Standard); + let dio1 = Input::new(p.P1_15.degrade(), Pull::Down); + let busy = Input::new(p.P1_14.degrade(), Pull::Down); + let rf_switch_rx = Output::new(p.P1_05.degrade(), Level::Low, OutputDrive::Standard); + let rf_switch_tx = Output::new(p.P1_07.degrade(), Level::Low, OutputDrive::Standard); + + let iv = + GenericSx126xInterfaceVariant::new(nss, reset, dio1, busy, Some(rf_switch_rx), Some(rf_switch_tx)).unwrap(); + + let mut delay = Delay; + + let mut lora = { + match LoRa::new(SX1261_2::new(BoardType::Rak4631Sx1262, spim, iv), false, &mut delay).await { + Ok(l) => l, + Err(err) => { + info!("Radio error = {}", err); + return; + } + } + }; + + let mdltn_params = { + match lora.create_modulation_params( + SpreadingFactor::_10, + Bandwidth::_250KHz, + CodingRate::_4_8, + LORA_FREQUENCY_IN_HZ, + ) { + Ok(mp) => mp, + Err(err) => { + info!("Radio error = {}", err); + return; + } + } + }; + + let mut tx_pkt_params = { + match lora.create_tx_packet_params(4, false, true, false, &mdltn_params) { + Ok(pp) => pp, + Err(err) => { + info!("Radio error = {}", err); + return; + } + } + }; + + match lora.prepare_for_tx(&mdltn_params, 20, false).await { + Ok(()) => {} + Err(err) => { + info!("Radio error = {}", err); + return; + } + }; + + let buffer = [0x01u8, 0x02u8, 0x03u8]; + match lora.tx(&mdltn_params, &mut tx_pkt_params, &buffer, 0xffffff).await { + Ok(()) => { + info!("TX DONE"); + } + Err(err) => { + info!("Radio error = {}", err); + return; + } + }; + + match lora.sleep(&mut delay).await { + Ok(()) => info!("Sleep successful"), + Err(err) => info!("Sleep unsuccessful = {}", err), + } +} diff --git a/examples/nrf52840/src/bin/lora_p2p_sense.rs b/examples/nrf52840/src/bin/lora_p2p_sense.rs deleted file mode 100644 index b6f41ffc..00000000 --- a/examples/nrf52840/src/bin/lora_p2p_sense.rs +++ /dev/null @@ -1,128 +0,0 @@ -//! This example runs on the RAK4631 WisBlock, which has an nRF52840 MCU and Semtech Sx126x radio. -//! Other nrf/sx126x combinations may work with appropriate pin modifications. -//! It demonstates LORA P2P functionality in conjunction with example lora_p2p_report.rs. -#![no_std] -#![no_main] -#![macro_use] -#![feature(type_alias_impl_trait)] -#![feature(alloc_error_handler)] -#![allow(incomplete_features)] - -use defmt::*; -use embassy_executor::Spawner; -use embassy_lora::sx126x::*; -use embassy_nrf::gpio::{Input, Level, Output, OutputDrive, Pin as _, Pull}; -use embassy_nrf::{bind_interrupts, peripherals, spim}; -use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; -use embassy_sync::pubsub::{PubSubChannel, Publisher}; -use embassy_time::{Duration, Timer}; -use lorawan_device::async_device::radio::{Bandwidth, CodingRate, PhyRxTx, RfConfig, SpreadingFactor, TxConfig}; -use {defmt_rtt as _, panic_probe as _, panic_probe as _}; - -bind_interrupts!(struct Irqs { - SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1 => spim::InterruptHandler; -}); - -// Message bus: queue of 2, 1 subscriber (Lora P2P), 2 publishers (temperature, motion detection) -static MESSAGE_BUS: PubSubChannel = PubSubChannel::new(); - -#[derive(Clone, defmt::Format)] -enum Message { - Temperature(i32), - MotionDetected, -} - -#[embassy_executor::task] -async fn temperature_task(publisher: Publisher<'static, CriticalSectionRawMutex, Message, 2, 1, 2>) { - // Publish a fake temperature every 43 seconds, minimizing LORA traffic. - loop { - Timer::after(Duration::from_secs(43)).await; - publisher.publish(Message::Temperature(9)).await; - } -} - -#[embassy_executor::task] -async fn motion_detection_task(publisher: Publisher<'static, CriticalSectionRawMutex, Message, 2, 1, 2>) { - // Publish a fake motion detection every 79 seconds, minimizing LORA traffic. - loop { - Timer::after(Duration::from_secs(79)).await; - publisher.publish(Message::MotionDetected).await; - } -} - -#[embassy_executor::main] -async fn main(spawner: Spawner) { - let p = embassy_nrf::init(Default::default()); - // set up to funnel temperature and motion detection events to the Lora Tx task - let mut lora_tx_subscriber = unwrap!(MESSAGE_BUS.subscriber()); - let temperature_publisher = unwrap!(MESSAGE_BUS.publisher()); - let motion_detection_publisher = unwrap!(MESSAGE_BUS.publisher()); - - let mut spi_config = spim::Config::default(); - spi_config.frequency = spim::Frequency::M16; - - let mut radio = { - let spim = spim::Spim::new(p.TWISPI1, Irqs, p.P1_11, p.P1_13, p.P1_12, spi_config); - - let cs = Output::new(p.P1_10.degrade(), Level::High, OutputDrive::Standard); - let reset = Output::new(p.P1_06.degrade(), Level::High, OutputDrive::Standard); - let dio1 = Input::new(p.P1_15.degrade(), Pull::Down); - let busy = Input::new(p.P1_14.degrade(), Pull::Down); - let antenna_rx = Output::new(p.P1_05.degrade(), Level::Low, OutputDrive::Standard); - let antenna_tx = Output::new(p.P1_07.degrade(), Level::Low, OutputDrive::Standard); - - match Sx126xRadio::new(spim, cs, reset, antenna_rx, antenna_tx, dio1, busy, false).await { - Ok(r) => r, - Err(err) => { - info!("Sx126xRadio error = {}", err); - return; - } - } - }; - - let mut start_indicator = Output::new(p.P1_04, Level::Low, OutputDrive::Standard); - - start_indicator.set_high(); - Timer::after(Duration::from_secs(5)).await; - start_indicator.set_low(); - - match radio.lora.sleep().await { - Ok(()) => info!("Sleep successful"), - Err(err) => info!("Sleep unsuccessful = {}", err), - } - - unwrap!(spawner.spawn(temperature_task(temperature_publisher))); - unwrap!(spawner.spawn(motion_detection_task(motion_detection_publisher))); - - loop { - let message = lora_tx_subscriber.next_message_pure().await; - - let tx_config = TxConfig { - // 11 byte maximum payload for Bandwidth 125 and SF 10 - pw: 10, // up to 20 - rf: RfConfig { - frequency: 903900000, // channel in Hz, not MHz - bandwidth: Bandwidth::_250KHz, - spreading_factor: SpreadingFactor::_10, - coding_rate: CodingRate::_4_8, - }, - }; - - let mut buffer = [0x00u8]; - match message { - Message::Temperature(temperature) => buffer[0] = temperature as u8, - Message::MotionDetected => buffer[0] = 0x01u8, - }; - - // unencrypted - match radio.tx(tx_config, &buffer).await { - Ok(ret_val) => info!("TX ret_val = {}", ret_val), - Err(err) => info!("TX error = {}", err), - } - - match radio.lora.sleep().await { - Ok(()) => info!("Sleep successful"), - Err(err) => info!("Sleep unsuccessful = {}", err), - } - } -} diff --git a/examples/rp/src/bin/lora_lorawan.rs b/examples/rp/src/bin/lora_lorawan.rs new file mode 100644 index 00000000..a9c84bf9 --- /dev/null +++ b/examples/rp/src/bin/lora_lorawan.rs @@ -0,0 +1,80 @@ +//! This example runs on the Raspberry Pi Pico with a Waveshare board containing a Semtech Sx1262 radio. +//! It demonstrates LoRaWAN join functionality. +#![no_std] +#![no_main] +#![macro_use] +#![feature(type_alias_impl_trait)] + +use defmt::*; +use embassy_executor::Spawner; +use embassy_lora::iv::GenericSx126xInterfaceVariant; +use embassy_lora::LoraTimer; +use embassy_rp::gpio::{Input, Level, Output, Pin, Pull}; +use embassy_rp::spi::{Config, Spi}; +use embassy_time::Delay; +use lora_phy::mod_params::*; +use lora_phy::sx1261_2::SX1261_2; +use lora_phy::LoRa; +use lorawan::default_crypto::DefaultFactory as Crypto; +use lorawan_device::async_device::lora_radio::LoRaRadio; +use lorawan_device::async_device::{region, Device, JoinMode}; +use {defmt_rtt as _, panic_probe as _}; + +const LORAWAN_REGION: region::Region = region::Region::EU868; // warning: set this appropriately for the region + +#[embassy_executor::main] +async fn main(_spawner: Spawner) { + let p = embassy_rp::init(Default::default()); + + let miso = p.PIN_12; + let mosi = p.PIN_11; + let clk = p.PIN_10; + let spi = Spi::new(p.SPI1, clk, mosi, miso, p.DMA_CH0, p.DMA_CH1, Config::default()); + + let nss = Output::new(p.PIN_3.degrade(), Level::High); + let reset = Output::new(p.PIN_15.degrade(), Level::High); + let dio1 = Input::new(p.PIN_20.degrade(), Pull::None); + let busy = Input::new(p.PIN_2.degrade(), Pull::None); + + let iv = GenericSx126xInterfaceVariant::new(nss, reset, dio1, busy, None, None).unwrap(); + + let mut delay = Delay; + + let lora = { + match LoRa::new( + SX1261_2::new(BoardType::RpPicoWaveshareSx1262, spi, iv), + true, + &mut delay, + ) + .await + { + Ok(l) => l, + Err(err) => { + info!("Radio error = {}", err); + return; + } + } + }; + + let radio = LoRaRadio::new(lora); + let region: region::Configuration = region::Configuration::new(LORAWAN_REGION); + let mut device: Device<_, Crypto, _, _> = Device::new(region, radio, LoraTimer::new(), embassy_rp::clocks::RoscRng); + + defmt::info!("Joining LoRaWAN network"); + + // TODO: Adjust the EUI and Keys according to your network credentials + match device + .join(&JoinMode::OTAA { + deveui: [0, 0, 0, 0, 0, 0, 0, 0], + appeui: [0, 0, 0, 0, 0, 0, 0, 0], + appkey: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + }) + .await + { + Ok(()) => defmt::info!("LoRaWAN network joined"), + Err(err) => { + info!("Radio error = {}", err); + return; + } + }; +} diff --git a/examples/rp/src/bin/lora_p2p_receive.rs b/examples/rp/src/bin/lora_p2p_receive.rs new file mode 100644 index 00000000..25041920 --- /dev/null +++ b/examples/rp/src/bin/lora_p2p_receive.rs @@ -0,0 +1,115 @@ +//! This example runs on the Raspberry Pi Pico with a Waveshare board containing a Semtech Sx1262 radio. +//! It demonstrates LORA P2P receive functionality in conjunction with the lora_p2p_send example. +#![no_std] +#![no_main] +#![macro_use] +#![feature(type_alias_impl_trait)] + +use defmt::*; +use embassy_executor::Spawner; +use embassy_lora::iv::GenericSx126xInterfaceVariant; +use embassy_rp::gpio::{Input, Level, Output, Pin, Pull}; +use embassy_rp::spi::{Config, Spi}; +use embassy_time::{Delay, Duration, Timer}; +use lora_phy::mod_params::*; +use lora_phy::sx1261_2::SX1261_2; +use lora_phy::LoRa; +use {defmt_rtt as _, panic_probe as _}; + +const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region + +#[embassy_executor::main] +async fn main(_spawner: Spawner) { + let p = embassy_rp::init(Default::default()); + + let miso = p.PIN_12; + let mosi = p.PIN_11; + let clk = p.PIN_10; + let spi = Spi::new(p.SPI1, clk, mosi, miso, p.DMA_CH0, p.DMA_CH1, Config::default()); + + let nss = Output::new(p.PIN_3.degrade(), Level::High); + let reset = Output::new(p.PIN_15.degrade(), Level::High); + let dio1 = Input::new(p.PIN_20.degrade(), Pull::None); + let busy = Input::new(p.PIN_2.degrade(), Pull::None); + + let iv = GenericSx126xInterfaceVariant::new(nss, reset, dio1, busy, None, None).unwrap(); + + let mut delay = Delay; + + let mut lora = { + match LoRa::new( + SX1261_2::new(BoardType::RpPicoWaveshareSx1262, spi, iv), + false, + &mut delay, + ) + .await + { + Ok(l) => l, + Err(err) => { + info!("Radio error = {}", err); + return; + } + } + }; + + let mut debug_indicator = Output::new(p.PIN_25, Level::Low); + + let mut receiving_buffer = [00u8; 100]; + + let mdltn_params = { + match lora.create_modulation_params( + SpreadingFactor::_10, + Bandwidth::_250KHz, + CodingRate::_4_8, + LORA_FREQUENCY_IN_HZ, + ) { + Ok(mp) => mp, + Err(err) => { + info!("Radio error = {}", err); + return; + } + } + }; + + let rx_pkt_params = { + match lora.create_rx_packet_params(4, false, receiving_buffer.len() as u8, true, false, &mdltn_params) { + Ok(pp) => pp, + Err(err) => { + info!("Radio error = {}", err); + return; + } + } + }; + + match lora + .prepare_for_rx(&mdltn_params, &rx_pkt_params, None, true, false, 0, 0x00ffffffu32) + .await + { + Ok(()) => {} + Err(err) => { + info!("Radio error = {}", err); + return; + } + }; + + loop { + receiving_buffer = [00u8; 100]; + match lora.rx(&rx_pkt_params, &mut receiving_buffer).await { + Ok((received_len, _rx_pkt_status)) => { + if (received_len == 3) + && (receiving_buffer[0] == 0x01u8) + && (receiving_buffer[1] == 0x02u8) + && (receiving_buffer[2] == 0x03u8) + { + info!("rx successful"); + debug_indicator.set_high(); + Timer::after(Duration::from_secs(5)).await; + debug_indicator.set_low(); + } else { + info!("rx unknown packet"); + } + } + Err(err) => info!("rx unsuccessful = {}", err), + } + } +} diff --git a/examples/rp/src/bin/lora_p2p_send.rs b/examples/rp/src/bin/lora_p2p_send.rs new file mode 100644 index 00000000..3a0544b1 --- /dev/null +++ b/examples/rp/src/bin/lora_p2p_send.rs @@ -0,0 +1,103 @@ +//! This example runs on the Raspberry Pi Pico with a Waveshare board containing a Semtech Sx1262 radio. +//! It demonstrates LORA P2P send functionality. +#![no_std] +#![no_main] +#![macro_use] +#![feature(type_alias_impl_trait)] + +use defmt::*; +use embassy_executor::Spawner; +use embassy_lora::iv::GenericSx126xInterfaceVariant; +use embassy_rp::gpio::{Input, Level, Output, Pin, Pull}; +use embassy_rp::spi::{Config, Spi}; +use embassy_time::Delay; +use lora_phy::mod_params::*; +use lora_phy::sx1261_2::SX1261_2; +use lora_phy::LoRa; +use {defmt_rtt as _, panic_probe as _}; + +const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region + +#[embassy_executor::main] +async fn main(_spawner: Spawner) { + let p = embassy_rp::init(Default::default()); + + let miso = p.PIN_12; + let mosi = p.PIN_11; + let clk = p.PIN_10; + let spi = Spi::new(p.SPI1, clk, mosi, miso, p.DMA_CH0, p.DMA_CH1, Config::default()); + + let nss = Output::new(p.PIN_3.degrade(), Level::High); + let reset = Output::new(p.PIN_15.degrade(), Level::High); + let dio1 = Input::new(p.PIN_20.degrade(), Pull::None); + let busy = Input::new(p.PIN_2.degrade(), Pull::None); + + let iv = GenericSx126xInterfaceVariant::new(nss, reset, dio1, busy, None, None).unwrap(); + + let mut delay = Delay; + + let mut lora = { + match LoRa::new( + SX1261_2::new(BoardType::RpPicoWaveshareSx1262, spi, iv), + false, + &mut delay, + ) + .await + { + Ok(l) => l, + Err(err) => { + info!("Radio error = {}", err); + return; + } + } + }; + + let mdltn_params = { + match lora.create_modulation_params( + SpreadingFactor::_10, + Bandwidth::_250KHz, + CodingRate::_4_8, + LORA_FREQUENCY_IN_HZ, + ) { + Ok(mp) => mp, + Err(err) => { + info!("Radio error = {}", err); + return; + } + } + }; + + let mut tx_pkt_params = { + match lora.create_tx_packet_params(4, false, true, false, &mdltn_params) { + Ok(pp) => pp, + Err(err) => { + info!("Radio error = {}", err); + return; + } + } + }; + + match lora.prepare_for_tx(&mdltn_params, 20, false).await { + Ok(()) => {} + Err(err) => { + info!("Radio error = {}", err); + return; + } + }; + + let buffer = [0x01u8, 0x02u8, 0x03u8]; + match lora.tx(&mdltn_params, &mut tx_pkt_params, &buffer, 0xffffff).await { + Ok(()) => { + info!("TX DONE"); + } + Err(err) => { + info!("Radio error = {}", err); + return; + } + }; + + match lora.sleep(&mut delay).await { + Ok(()) => info!("Sleep successful"), + Err(err) => info!("Sleep unsuccessful = {}", err), + } +} diff --git a/examples/rp/src/bin/lora_p2p_send_multicore.rs b/examples/rp/src/bin/lora_p2p_send_multicore.rs new file mode 100644 index 00000000..5585606d --- /dev/null +++ b/examples/rp/src/bin/lora_p2p_send_multicore.rs @@ -0,0 +1,139 @@ +//! This example runs on the Raspberry Pi Pico with a Waveshare board containing a Semtech Sx1262 radio. +//! It demonstrates LORA P2P send functionality using the second core, with data provided by the first core. +#![no_std] +#![no_main] +#![macro_use] +#![feature(type_alias_impl_trait)] + +use defmt::*; +use embassy_executor::Executor; +use embassy_executor::_export::StaticCell; +use embassy_lora::iv::GenericSx126xInterfaceVariant; +use embassy_rp::gpio::{AnyPin, Input, Level, Output, Pin, Pull}; +use embassy_rp::multicore::{spawn_core1, Stack}; +use embassy_rp::peripherals::SPI1; +use embassy_rp::spi::{Async, Config, Spi}; +use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; +use embassy_sync::channel::Channel; +use embassy_time::{Delay, Duration, Timer}; +use lora_phy::mod_params::*; +use lora_phy::sx1261_2::SX1261_2; +use lora_phy::LoRa; +use {defmt_rtt as _, panic_probe as _}; + +static mut CORE1_STACK: Stack<4096> = Stack::new(); +static EXECUTOR0: StaticCell = StaticCell::new(); +static EXECUTOR1: StaticCell = StaticCell::new(); +static CHANNEL: Channel = Channel::new(); + +const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region + +#[cortex_m_rt::entry] +fn main() -> ! { + let p = embassy_rp::init(Default::default()); + + let miso = p.PIN_12; + let mosi = p.PIN_11; + let clk = p.PIN_10; + let spi = Spi::new(p.SPI1, clk, mosi, miso, p.DMA_CH0, p.DMA_CH1, Config::default()); + + let nss = Output::new(p.PIN_3.degrade(), Level::High); + let reset = Output::new(p.PIN_15.degrade(), Level::High); + let dio1 = Input::new(p.PIN_20.degrade(), Pull::None); + let busy = Input::new(p.PIN_2.degrade(), Pull::None); + + let iv = GenericSx126xInterfaceVariant::new(nss, reset, dio1, busy, None, None).unwrap(); + + spawn_core1(p.CORE1, unsafe { &mut CORE1_STACK }, move || { + let executor1 = EXECUTOR1.init(Executor::new()); + executor1.run(|spawner| unwrap!(spawner.spawn(core1_task(spi, iv)))); + }); + + let executor0 = EXECUTOR0.init(Executor::new()); + executor0.run(|spawner| unwrap!(spawner.spawn(core0_task()))); +} + +#[embassy_executor::task] +async fn core0_task() { + info!("Hello from core 0"); + loop { + CHANNEL.send([0x01u8, 0x02u8, 0x03u8]).await; + Timer::after(Duration::from_millis(60 * 1000)).await; + } +} + +#[embassy_executor::task] +async fn core1_task( + spi: Spi<'static, SPI1, Async>, + iv: GenericSx126xInterfaceVariant, Input<'static, AnyPin>>, +) { + info!("Hello from core 1"); + let mut delay = Delay; + + let mut lora = { + match LoRa::new( + SX1261_2::new(BoardType::RpPicoWaveshareSx1262, spi, iv), + false, + &mut delay, + ) + .await + { + Ok(l) => l, + Err(err) => { + info!("Radio error = {}", err); + return; + } + } + }; + + let mdltn_params = { + match lora.create_modulation_params( + SpreadingFactor::_10, + Bandwidth::_250KHz, + CodingRate::_4_8, + LORA_FREQUENCY_IN_HZ, + ) { + Ok(mp) => mp, + Err(err) => { + info!("Radio error = {}", err); + return; + } + } + }; + + let mut tx_pkt_params = { + match lora.create_tx_packet_params(4, false, true, false, &mdltn_params) { + Ok(pp) => pp, + Err(err) => { + info!("Radio error = {}", err); + return; + } + } + }; + + loop { + let buffer: [u8; 3] = CHANNEL.recv().await; + match lora.prepare_for_tx(&mdltn_params, 20, false).await { + Ok(()) => {} + Err(err) => { + info!("Radio error = {}", err); + return; + } + }; + + match lora.tx(&mdltn_params, &mut tx_pkt_params, &buffer, 0xffffff).await { + Ok(()) => { + info!("TX DONE"); + } + Err(err) => { + info!("Radio error = {}", err); + return; + } + }; + + match lora.sleep(&mut delay).await { + Ok(()) => info!("Sleep successful"), + Err(err) => info!("Sleep unsuccessful = {}", err), + } + } +} diff --git a/examples/stm32l0/src/bin/lora_cad.rs b/examples/stm32l0/src/bin/lora_cad.rs new file mode 100644 index 00000000..588cea1e --- /dev/null +++ b/examples/stm32l0/src/bin/lora_cad.rs @@ -0,0 +1,105 @@ +//! This example runs on the STM32 LoRa Discovery board, which has a builtin Semtech Sx1276 radio. +//! It demonstrates LORA P2P CAD functionality. +#![no_std] +#![no_main] +#![macro_use] +#![feature(type_alias_impl_trait)] + +use defmt::*; +use embassy_executor::Spawner; +use embassy_lora::iv::Stm32l0InterfaceVariant; +use embassy_stm32::exti::{Channel, ExtiInput}; +use embassy_stm32::gpio::{Input, Level, Output, Pin, Pull, Speed}; +use embassy_stm32::spi; +use embassy_stm32::time::khz; +use embassy_time::{Delay, Duration, Timer}; +use lora_phy::mod_params::*; +use lora_phy::sx1276_7_8_9::SX1276_7_8_9; +use lora_phy::LoRa; +use {defmt_rtt as _, panic_probe as _}; + +const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region + +#[embassy_executor::main] +async fn main(_spawner: Spawner) { + let mut config = embassy_stm32::Config::default(); + config.rcc.mux = embassy_stm32::rcc::ClockSrc::HSI16; + config.rcc.enable_hsi48 = true; + let p = embassy_stm32::init(config); + + // SPI for sx1276 + let spi = spi::Spi::new( + p.SPI1, + p.PB3, + p.PA7, + p.PA6, + p.DMA1_CH3, + p.DMA1_CH2, + khz(200), + spi::Config::default(), + ); + + let nss = Output::new(p.PA15.degrade(), Level::High, Speed::Low); + let reset = Output::new(p.PC0.degrade(), Level::High, Speed::Low); + + let irq_pin = Input::new(p.PB4.degrade(), Pull::Up); + let irq = ExtiInput::new(irq_pin, p.EXTI4.degrade()); + + let iv = Stm32l0InterfaceVariant::new(nss, reset, irq, None, None).unwrap(); + + let mut delay = Delay; + + let mut lora = { + match LoRa::new(SX1276_7_8_9::new(BoardType::Stm32l0Sx1276, spi, iv), false, &mut delay).await { + Ok(l) => l, + Err(err) => { + info!("Radio error = {}", err); + return; + } + } + }; + + let mut debug_indicator = Output::new(p.PB5, Level::Low, Speed::Low); + let mut start_indicator = Output::new(p.PB6, Level::Low, Speed::Low); + + start_indicator.set_high(); + Timer::after(Duration::from_secs(5)).await; + start_indicator.set_low(); + + let mdltn_params = { + match lora.create_modulation_params( + SpreadingFactor::_10, + Bandwidth::_250KHz, + CodingRate::_4_8, + LORA_FREQUENCY_IN_HZ, + ) { + Ok(mp) => mp, + Err(err) => { + info!("Radio error = {}", err); + return; + } + } + }; + + match lora.prepare_for_cad(&mdltn_params, true).await { + Ok(()) => {} + Err(err) => { + info!("Radio error = {}", err); + return; + } + }; + + match lora.cad().await { + Ok(cad_activity_detected) => { + if cad_activity_detected { + info!("cad successful with activity detected") + } else { + info!("cad successful without activity detected") + } + debug_indicator.set_high(); + Timer::after(Duration::from_secs(5)).await; + debug_indicator.set_low(); + } + Err(err) => info!("cad unsuccessful = {}", err), + } +} diff --git a/examples/stm32l0/src/bin/lora_lorawan.rs b/examples/stm32l0/src/bin/lora_lorawan.rs new file mode 100644 index 00000000..c397edd5 --- /dev/null +++ b/examples/stm32l0/src/bin/lora_lorawan.rs @@ -0,0 +1,88 @@ +//! This example runs on the STM32 LoRa Discovery board, which has a builtin Semtech Sx1276 radio. +//! It demonstrates LoRaWAN join functionality. +#![no_std] +#![no_main] +#![macro_use] +#![feature(type_alias_impl_trait)] + +use defmt::*; +use embassy_executor::Spawner; +use embassy_lora::iv::Stm32l0InterfaceVariant; +use embassy_lora::LoraTimer; +use embassy_stm32::exti::{Channel, ExtiInput}; +use embassy_stm32::gpio::{Input, Level, Output, Pin, Pull, Speed}; +use embassy_stm32::rng::Rng; +use embassy_stm32::spi; +use embassy_stm32::time::khz; +use embassy_time::Delay; +use lora_phy::mod_params::*; +use lora_phy::sx1276_7_8_9::SX1276_7_8_9; +use lora_phy::LoRa; +use lorawan::default_crypto::DefaultFactory as Crypto; +use lorawan_device::async_device::lora_radio::LoRaRadio; +use lorawan_device::async_device::{region, Device, JoinMode}; +use {defmt_rtt as _, panic_probe as _}; + +const LORAWAN_REGION: region::Region = region::Region::EU868; // warning: set this appropriately for the region + +#[embassy_executor::main] +async fn main(_spawner: Spawner) { + let mut config = embassy_stm32::Config::default(); + config.rcc.mux = embassy_stm32::rcc::ClockSrc::HSI16; + config.rcc.enable_hsi48 = true; + let p = embassy_stm32::init(config); + + // SPI for sx1276 + let spi = spi::Spi::new( + p.SPI1, + p.PB3, + p.PA7, + p.PA6, + p.DMA1_CH3, + p.DMA1_CH2, + khz(200), + spi::Config::default(), + ); + + let nss = Output::new(p.PA15.degrade(), Level::High, Speed::Low); + let reset = Output::new(p.PC0.degrade(), Level::High, Speed::Low); + + let irq_pin = Input::new(p.PB4.degrade(), Pull::Up); + let irq = ExtiInput::new(irq_pin, p.EXTI4.degrade()); + + let iv = Stm32l0InterfaceVariant::new(nss, reset, irq, None, None).unwrap(); + + let mut delay = Delay; + + let lora = { + match LoRa::new(SX1276_7_8_9::new(BoardType::Stm32l0Sx1276, spi, iv), true, &mut delay).await { + Ok(l) => l, + Err(err) => { + info!("Radio error = {}", err); + return; + } + } + }; + + let radio = LoRaRadio::new(lora); + let region: region::Configuration = region::Configuration::new(LORAWAN_REGION); + let mut device: Device<_, Crypto, _, _> = Device::new(region, radio, LoraTimer::new(), Rng::new(p.RNG)); + + defmt::info!("Joining LoRaWAN network"); + + // TODO: Adjust the EUI and Keys according to your network credentials + match device + .join(&JoinMode::OTAA { + deveui: [0, 0, 0, 0, 0, 0, 0, 0], + appeui: [0, 0, 0, 0, 0, 0, 0, 0], + appkey: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + }) + .await + { + Ok(()) => defmt::info!("LoRaWAN network joined"), + Err(err) => { + info!("Radio error = {}", err); + return; + } + }; +} diff --git a/examples/stm32l0/src/bin/lora_p2p_receive.rs b/examples/stm32l0/src/bin/lora_p2p_receive.rs index a5c5f75e..bb750950 100644 --- a/examples/stm32l0/src/bin/lora_p2p_receive.rs +++ b/examples/stm32l0/src/bin/lora_p2p_receive.rs @@ -1,5 +1,5 @@ //! This example runs on the STM32 LoRa Discovery board, which has a builtin Semtech Sx1276 radio. -//! It demonstrates LORA P2P receive functionality. +//! It demonstrates LORA P2P receive functionality in conjunction with the lora_p2p_send example. #![no_std] #![no_main] #![macro_use] @@ -18,6 +18,8 @@ use lora_phy::sx1276_7_8_9::SX1276_7_8_9; use lora_phy::LoRa; use {defmt_rtt as _, panic_probe as _}; +const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region + #[embassy_executor::main] async fn main(_spawner: Spawner) { let mut config = embassy_stm32::Config::default(); @@ -67,7 +69,12 @@ async fn main(_spawner: Spawner) { let mut receiving_buffer = [00u8; 100]; let mdltn_params = { - match lora.create_modulation_params(SpreadingFactor::_10, Bandwidth::_250KHz, CodingRate::_4_8, 903900000) { + match lora.create_modulation_params( + SpreadingFactor::_10, + Bandwidth::_250KHz, + CodingRate::_4_8, + LORA_FREQUENCY_IN_HZ, + ) { Ok(mp) => mp, Err(err) => { info!("Radio error = {}", err); diff --git a/examples/stm32l0/src/bin/lora_p2p_send.rs b/examples/stm32l0/src/bin/lora_p2p_send.rs new file mode 100644 index 00000000..e6fadc01 --- /dev/null +++ b/examples/stm32l0/src/bin/lora_p2p_send.rs @@ -0,0 +1,110 @@ +//! This example runs on the STM32 LoRa Discovery board, which has a builtin Semtech Sx1276 radio. +//! It demonstrates LORA P2P send functionality. +#![no_std] +#![no_main] +#![macro_use] +#![feature(type_alias_impl_trait)] + +use defmt::*; +use embassy_executor::Spawner; +use embassy_lora::iv::Stm32l0InterfaceVariant; +use embassy_stm32::exti::{Channel, ExtiInput}; +use embassy_stm32::gpio::{Input, Level, Output, Pin, Pull, Speed}; +use embassy_stm32::spi; +use embassy_stm32::time::khz; +use embassy_time::Delay; +use lora_phy::mod_params::*; +use lora_phy::sx1276_7_8_9::SX1276_7_8_9; +use lora_phy::LoRa; +use {defmt_rtt as _, panic_probe as _}; + +const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region + +#[embassy_executor::main] +async fn main(_spawner: Spawner) { + let mut config = embassy_stm32::Config::default(); + config.rcc.mux = embassy_stm32::rcc::ClockSrc::HSI16; + config.rcc.enable_hsi48 = true; + let p = embassy_stm32::init(config); + + // SPI for sx1276 + let spi = spi::Spi::new( + p.SPI1, + p.PB3, + p.PA7, + p.PA6, + p.DMA1_CH3, + p.DMA1_CH2, + khz(200), + spi::Config::default(), + ); + + let nss = Output::new(p.PA15.degrade(), Level::High, Speed::Low); + let reset = Output::new(p.PC0.degrade(), Level::High, Speed::Low); + + let irq_pin = Input::new(p.PB4.degrade(), Pull::Up); + let irq = ExtiInput::new(irq_pin, p.EXTI4.degrade()); + + let iv = Stm32l0InterfaceVariant::new(nss, reset, irq, None, None).unwrap(); + + let mut delay = Delay; + + let mut lora = { + match LoRa::new(SX1276_7_8_9::new(BoardType::Stm32l0Sx1276, spi, iv), false, &mut delay).await { + Ok(l) => l, + Err(err) => { + info!("Radio error = {}", err); + return; + } + } + }; + + let mdltn_params = { + match lora.create_modulation_params( + SpreadingFactor::_10, + Bandwidth::_250KHz, + CodingRate::_4_8, + LORA_FREQUENCY_IN_HZ, + ) { + Ok(mp) => mp, + Err(err) => { + info!("Radio error = {}", err); + return; + } + } + }; + + let mut tx_pkt_params = { + match lora.create_tx_packet_params(4, false, true, false, &mdltn_params) { + Ok(pp) => pp, + Err(err) => { + info!("Radio error = {}", err); + return; + } + } + }; + + match lora.prepare_for_tx(&mdltn_params, 17, true).await { + Ok(()) => {} + Err(err) => { + info!("Radio error = {}", err); + return; + } + }; + + let buffer = [0x01u8, 0x02u8, 0x03u8]; + match lora.tx(&mdltn_params, &mut tx_pkt_params, &buffer, 0xffffff).await { + Ok(()) => { + info!("TX DONE"); + } + Err(err) => { + info!("Radio error = {}", err); + return; + } + }; + + match lora.sleep(&mut delay).await { + Ok(()) => info!("Sleep successful"), + Err(err) => info!("Sleep unsuccessful = {}", err), + } +} diff --git a/examples/stm32l0/src/bin/lorawan.rs b/examples/stm32l0/src/bin/lorawan.rs deleted file mode 100644 index ea01f610..00000000 --- a/examples/stm32l0/src/bin/lorawan.rs +++ /dev/null @@ -1,74 +0,0 @@ -//! This example runs on the STM32 LoRa Discovery board which has a builtin Semtech Sx127x radio -#![no_std] -#![no_main] -#![macro_use] -#![allow(dead_code)] -#![feature(type_alias_impl_trait)] - -use embassy_executor::Spawner; -use embassy_lora::sx127x::*; -use embassy_lora::LoraTimer; -use embassy_stm32::exti::ExtiInput; -use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; -use embassy_stm32::rng::Rng; -use embassy_stm32::spi; -use embassy_stm32::time::khz; -use lorawan::default_crypto::DefaultFactory as Crypto; -use lorawan_device::async_device::{region, Device, JoinMode}; -use {defmt_rtt as _, panic_probe as _}; - -#[embassy_executor::main] -async fn main(_spawner: Spawner) { - let mut config = embassy_stm32::Config::default(); - config.rcc.mux = embassy_stm32::rcc::ClockSrc::HSI16; - config.rcc.enable_hsi48 = true; - let p = embassy_stm32::init(config); - - // SPI for sx127x - let spi = spi::Spi::new( - p.SPI1, - p.PB3, - p.PA7, - p.PA6, - p.DMA1_CH3, - p.DMA1_CH2, - khz(200), - spi::Config::default(), - ); - - let cs = Output::new(p.PA15, Level::High, Speed::Low); - let reset = Output::new(p.PC0, Level::High, Speed::Low); - let _ = Input::new(p.PB1, Pull::None); - - let ready = Input::new(p.PB4, Pull::Up); - let ready_pin = ExtiInput::new(ready, p.EXTI4); - - let radio = Sx127xRadio::new(spi, cs, reset, ready_pin, DummySwitch).await.unwrap(); - - let region = region::Configuration::new(region::Region::EU868); - let mut device: Device<_, Crypto, _, _> = Device::new(region, radio, LoraTimer::new(), Rng::new(p.RNG)); - - defmt::info!("Joining LoRaWAN network"); - - // TODO: Adjust the EUI and Keys according to your network credentials - device - .join(&JoinMode::OTAA { - deveui: [0, 0, 0, 0, 0, 0, 0, 0], - appeui: [0, 0, 0, 0, 0, 0, 0, 0], - appkey: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], - }) - .await - .ok() - .unwrap(); - defmt::info!("LoRaWAN network joined"); - - defmt::info!("Sending 'PING'"); - device.send(b"PING", 1, false).await.ok().unwrap(); - defmt::info!("Message sent!"); -} - -pub struct DummySwitch; -impl RadioSwitch for DummySwitch { - fn set_rx(&mut self) {} - fn set_tx(&mut self) {} -} diff --git a/examples/stm32wl/src/bin/lora_lorawan.rs b/examples/stm32wl/src/bin/lora_lorawan.rs index f7cf0359..4bcc5420 100644 --- a/examples/stm32wl/src/bin/lora_lorawan.rs +++ b/examples/stm32wl/src/bin/lora_lorawan.rs @@ -1,5 +1,5 @@ //! This example runs on a STM32WL board, which has a builtin Semtech Sx1262 radio. -//! It demonstrates LoRaWAN functionality. +//! It demonstrates LoRaWAN join functionality. #![no_std] #![no_main] #![macro_use] @@ -28,6 +28,8 @@ use lorawan_device::async_device::lora_radio::LoRaRadio; use lorawan_device::async_device::{region, Device, JoinMode}; use {defmt_rtt as _, panic_probe as _}; +const LORAWAN_REGION: region::Region = region::Region::EU868; // warning: set this appropriately for the region + #[embassy_executor::main] async fn main(_spawner: Spawner) { let mut config = embassy_stm32::Config::default(); @@ -65,7 +67,7 @@ async fn main(_spawner: Spawner) { } }; let radio = LoRaRadio::new(lora); - let region: region::Configuration = region::Configuration::new(region::Region::EU868); + let region: region::Configuration = region::Configuration::new(LORAWAN_REGION); let mut device: Device<_, Crypto, _, _> = Device::new(region, radio, LoraTimer::new(), Rng::new(p.RNG)); 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 00000000..bb31518c --- /dev/null +++ b/examples/stm32wl/src/bin/lora_p2p_receive.rs @@ -0,0 +1,127 @@ +//! This example runs on the STM32WL board, which has a builtin Semtech Sx1262 radio. +//! It demonstrates LORA P2P receive functionality in conjunction with the lora_p2p_send example. +#![no_std] +#![no_main] +#![macro_use] +#![feature(type_alias_impl_trait, async_fn_in_trait)] +#![allow(incomplete_features)] + +use defmt::info; +use embassy_embedded_hal::adapter::BlockingAsync; +use embassy_executor::Spawner; +use embassy_lora::iv::Stm32wlInterfaceVariant; +use embassy_stm32::dma::NoDma; +use embassy_stm32::gpio::{Level, Output, Pin, Speed}; +use embassy_stm32::peripherals::SUBGHZSPI; +use embassy_stm32::rcc::low_level::RccPeripheral; +use embassy_stm32::spi::{BitOrder, Config as SpiConfig, Spi, MODE_0}; +use embassy_stm32::time::Hertz; +use embassy_stm32::{interrupt, into_ref, Peripheral}; +use embassy_time::{Delay, Duration, Timer}; +use lora_phy::mod_params::*; +use lora_phy::sx1261_2::SX1261_2; +use lora_phy::LoRa; +use {defmt_rtt as _, panic_probe as _}; + +const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region + +#[embassy_executor::main] +async fn main(_spawner: Spawner) { + let mut config = embassy_stm32::Config::default(); + config.rcc.mux = embassy_stm32::rcc::ClockSrc::HSE32; + let p = embassy_stm32::init(config); + + let clk = Hertz(core::cmp::min(SUBGHZSPI::frequency().0 / 2, 16_000_000)); + let mut spi_config = SpiConfig::default(); + spi_config.mode = MODE_0; + spi_config.bit_order = BitOrder::MsbFirst; + let spi = Spi::new_subghz(p.SUBGHZSPI, NoDma, NoDma, clk, spi_config); + + let spi = BlockingAsync::new(spi); + + let irq = interrupt::take!(SUBGHZ_RADIO); + into_ref!(irq); + // Set CTRL1 and CTRL3 for high-power transmission, while CTRL2 acts as an RF switch between tx and rx + let _ctrl1 = Output::new(p.PC4.degrade(), Level::Low, Speed::High); + let ctrl2 = Output::new(p.PC5.degrade(), Level::High, Speed::High); + let _ctrl3 = Output::new(p.PC3.degrade(), Level::High, Speed::High); + let iv = Stm32wlInterfaceVariant::new(irq, None, Some(ctrl2)).unwrap(); + + let mut delay = Delay; + + let mut lora = { + match LoRa::new(SX1261_2::new(BoardType::Stm32wlSx1262, spi, iv), false, &mut delay).await { + Ok(l) => l, + Err(err) => { + info!("Radio error = {}", err); + return; + } + } + }; + + let mut debug_indicator = Output::new(p.PB9, Level::Low, Speed::Low); + let mut start_indicator = Output::new(p.PB15, Level::Low, Speed::Low); + + start_indicator.set_high(); + Timer::after(Duration::from_secs(5)).await; + start_indicator.set_low(); + + let mut receiving_buffer = [00u8; 100]; + + let mdltn_params = { + match lora.create_modulation_params( + SpreadingFactor::_10, + Bandwidth::_250KHz, + CodingRate::_4_8, + LORA_FREQUENCY_IN_HZ, + ) { + Ok(mp) => mp, + Err(err) => { + info!("Radio error = {}", err); + return; + } + } + }; + + let rx_pkt_params = { + match lora.create_rx_packet_params(4, false, receiving_buffer.len() as u8, true, false, &mdltn_params) { + Ok(pp) => pp, + Err(err) => { + info!("Radio error = {}", err); + return; + } + } + }; + + match lora + .prepare_for_rx(&mdltn_params, &rx_pkt_params, None, true, false, 0, 0x00ffffffu32) + .await + { + Ok(()) => {} + Err(err) => { + info!("Radio error = {}", err); + return; + } + }; + + loop { + receiving_buffer = [00u8; 100]; + match lora.rx(&rx_pkt_params, &mut receiving_buffer).await { + Ok((received_len, _rx_pkt_status)) => { + if (received_len == 3) + && (receiving_buffer[0] == 0x01u8) + && (receiving_buffer[1] == 0x02u8) + && (receiving_buffer[2] == 0x03u8) + { + info!("rx successful"); + debug_indicator.set_high(); + Timer::after(Duration::from_secs(5)).await; + debug_indicator.set_low(); + } else { + info!("rx unknown packet"); + } + } + Err(err) => info!("rx unsuccessful = {}", err), + } + } +} diff --git a/examples/stm32wl/src/bin/lora_p2p_send.rs b/examples/stm32wl/src/bin/lora_p2p_send.rs index 11c35a8f..8a38402f 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; use lora_phy::LoRa; use {defmt_rtt as _, panic_probe as _}; +const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region + #[embassy_executor::main] async fn main(_spawner: Spawner) { let mut config = embassy_stm32::Config::default(); @@ -58,7 +60,12 @@ async fn main(_spawner: Spawner) { }; let mdltn_params = { - match lora.create_modulation_params(SpreadingFactor::_10, Bandwidth::_250KHz, CodingRate::_4_8, 903900000) { + match lora.create_modulation_params( + SpreadingFactor::_10, + Bandwidth::_250KHz, + CodingRate::_4_8, + LORA_FREQUENCY_IN_HZ, + ) { Ok(mp) => mp, Err(err) => { 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 32f29cc5..00000000 --- a/examples/stm32wl/src/bin/lorawan.rs +++ /dev/null @@ -1,104 +0,0 @@ -#![no_std] -#![no_main] -#![macro_use] -#![allow(dead_code)] -#![feature(type_alias_impl_trait)] - -use embassy_executor::Spawner; -use embassy_lora::stm32wl::*; -use embassy_lora::LoraTimer; -use embassy_stm32::dma::NoDma; -use embassy_stm32::gpio::{AnyPin, Level, Output, Pin, Speed}; -use embassy_stm32::rng::Rng; -use embassy_stm32::subghz::*; -use embassy_stm32::{interrupt, pac}; -use lorawan::default_crypto::DefaultFactory as Crypto; -use lorawan_device::async_device::{region, Device, JoinMode}; -use {defmt_rtt as _, panic_probe as _}; - -struct RadioSwitch<'a> { - ctrl1: Output<'a, AnyPin>, - ctrl2: Output<'a, AnyPin>, - ctrl3: Output<'a, AnyPin>, -} - -impl<'a> RadioSwitch<'a> { - fn new(ctrl1: Output<'a, AnyPin>, ctrl2: Output<'a, AnyPin>, ctrl3: Output<'a, AnyPin>) -> Self { - Self { ctrl1, ctrl2, ctrl3 } - } -} - -impl<'a> embassy_lora::stm32wl::RadioSwitch for RadioSwitch<'a> { - fn set_rx(&mut self) { - self.ctrl1.set_high(); - self.ctrl2.set_low(); - self.ctrl3.set_high(); - } - - fn set_tx(&mut self) { - self.ctrl1.set_high(); - self.ctrl2.set_high(); - self.ctrl3.set_high(); - } -} - -#[embassy_executor::main] -async fn main(_spawner: Spawner) { - let mut config = embassy_stm32::Config::default(); - config.rcc.mux = embassy_stm32::rcc::ClockSrc::HSI16; - config.rcc.enable_lsi = true; - let p = embassy_stm32::init(config); - - unsafe { pac::RCC.ccipr().modify(|w| w.set_rngsel(0b01)) } - - let ctrl1 = Output::new(p.PC3.degrade(), Level::High, Speed::High); - let ctrl2 = Output::new(p.PC4.degrade(), Level::High, Speed::High); - let ctrl3 = Output::new(p.PC5.degrade(), Level::High, Speed::High); - let rfs = RadioSwitch::new(ctrl1, ctrl2, ctrl3); - - let radio = SubGhz::new(p.SUBGHZSPI, NoDma, NoDma); - let irq = interrupt::take!(SUBGHZ_RADIO); - - let mut radio_config = SubGhzRadioConfig::default(); - radio_config.calibrate_image = CalibrateImage::ISM_863_870; - let radio = SubGhzRadio::new(radio, rfs, irq, radio_config).unwrap(); - - let mut region = region::Configuration::new(region::Region::EU868); - - // NOTE: This is specific for TTN, as they have a special RX1 delay - region.set_receive_delay1(5000); - - let mut device: Device<_, Crypto, _, _> = Device::new(region, radio, LoraTimer::new(), Rng::new(p.RNG)); - - // Depending on network, this might be part of JOIN - device.set_datarate(region::DR::_0); // SF12 - - // device.set_datarate(region::DR::_1); // SF11 - // device.set_datarate(region::DR::_2); // SF10 - // device.set_datarate(region::DR::_3); // SF9 - // device.set_datarate(region::DR::_4); // SF8 - // device.set_datarate(region::DR::_5); // SF7 - - defmt::info!("Joining LoRaWAN network"); - - // TODO: Adjust the EUI and Keys according to your network credentials - device - .join(&JoinMode::OTAA { - deveui: [0, 0, 0, 0, 0, 0, 0, 0], - appeui: [0, 0, 0, 0, 0, 0, 0, 0], - appkey: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], - }) - .await - .ok() - .unwrap(); - defmt::info!("LoRaWAN network joined"); - - let mut rx: [u8; 255] = [0; 255]; - defmt::info!("Sending 'PING'"); - let len = device.send_recv(b"PING", &mut rx[..], 1, true).await.ok().unwrap(); - if len > 0 { - defmt::info!("Message sent, received downlink: {:?}", &rx[..len]); - } else { - defmt::info!("Message sent!"); - } -}