diff options
| author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2023-04-26 20:10:40 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-04-26 20:10:40 +0000 |
| commit | 759d911b5046b1f6201ea9728f1b42e4a0153659 (patch) | |
| tree | 2b8305193406caa6d79eed56a453a5447256dadc /examples | |
| parent | cb00fb18cbb8dcc83133fffeba42108fee932b38 (diff) | |
| parent | a277deeaa563eb1ec78856a3f6866e73ed206c6d (diff) | |
Merge #1396
1396: Add an external LoRa physical layer feature r=Dirbaio a=ceekdee
The original LoRa drivers have been deprecated and examples associated with them deleted; however, the original LoRa drivers are still available to allow a gentle transition to the external lora-phy crate.
Co-authored-by: ceekdee <[email protected]>
Co-authored-by: Chuck Davis <[email protected]>
Co-authored-by: Ulf Lilleengen <[email protected]>
Diffstat (limited to 'examples')
25 files changed, 1756 insertions, 524 deletions
diff --git a/examples/nrf52840/Cargo.toml b/examples/nrf52840/Cargo.toml index 10c269a76..59f30a9be 100644 --- a/examples/nrf52840/Cargo.toml +++ b/examples/nrf52840/Cargo.toml | |||
| @@ -6,8 +6,8 @@ license = "MIT OR Apache-2.0" | |||
| 6 | 6 | ||
| 7 | [features] | 7 | [features] |
| 8 | default = ["nightly"] | 8 | default = ["nightly"] |
| 9 | nightly = ["embassy-executor/nightly", "embassy-nrf/nightly", "embassy-net/nightly", "embassy-nrf/unstable-traits", "embassy-usb", "embedded-io/async", "embassy-net", | 9 | nightly = ["embassy-executor/nightly", "embassy-nrf/nightly", "embassy-net/nightly", "embassy-nrf/unstable-traits", "embassy-time/nightly", "embassy-time/unstable-traits", |
| 10 | "embassy-lora", "lorawan-device", "lorawan"] | 10 | "embassy-usb", "embedded-io/async", "embassy-net", "embassy-lora", "lora-phy", "lorawan-device", "lorawan"] |
| 11 | 11 | ||
| 12 | [dependencies] | 12 | [dependencies] |
| 13 | embassy-futures = { version = "0.1.0", path = "../../embassy-futures" } | 13 | embassy-futures = { version = "0.1.0", path = "../../embassy-futures" } |
| @@ -18,10 +18,10 @@ embassy-nrf = { version = "0.1.0", path = "../../embassy-nrf", features = ["defm | |||
| 18 | embassy-net = { version = "0.1.0", path = "../../embassy-net", features = ["defmt", "tcp", "dhcpv4", "medium-ethernet"], optional = true } | 18 | embassy-net = { version = "0.1.0", path = "../../embassy-net", features = ["defmt", "tcp", "dhcpv4", "medium-ethernet"], optional = true } |
| 19 | embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt", "msos-descriptor",], optional = true } | 19 | embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt", "msos-descriptor",], optional = true } |
| 20 | embedded-io = "0.4.0" | 20 | embedded-io = "0.4.0" |
| 21 | embassy-lora = { version = "0.1.0", path = "../../embassy-lora", features = ["sx126x", "time", "defmt"], optional = true } | 21 | embassy-lora = { version = "0.1.0", path = "../../embassy-lora", features = ["sx126x", "time", "defmt", "external-lora-phy"], optional = true } |
| 22 | 22 | lora-phy = { version = "1", optional = true } | |
| 23 | lorawan-device = { version = "0.9.0", default-features = false, features = ["async"], optional = true } | 23 | lorawan-device = { version = "0.10.0", default-features = false, features = ["async", "external-lora-phy"], optional = true } |
| 24 | lorawan = { version = "0.7.2", default-features = false, features = ["default-crypto"], optional = true } | 24 | lorawan = { version = "0.7.3", default-features = false, features = ["default-crypto"], optional = true } |
| 25 | 25 | ||
| 26 | defmt = "0.3" | 26 | defmt = "0.3" |
| 27 | defmt-rtt = "0.4" | 27 | defmt-rtt = "0.4" |
diff --git a/examples/nrf52840/src/bin/lora_cad.rs b/examples/nrf52840/src/bin/lora_cad.rs new file mode 100644 index 000000000..beca061ed --- /dev/null +++ b/examples/nrf52840/src/bin/lora_cad.rs | |||
| @@ -0,0 +1,99 @@ | |||
| 1 | //! This example runs on the RAK4631 WisBlock, which has an nRF52840 MCU and Semtech Sx126x radio. | ||
| 2 | //! Other nrf/sx126x combinations may work with appropriate pin modifications. | ||
| 3 | //! It demonstrates LORA CAD functionality. | ||
| 4 | #![no_std] | ||
| 5 | #![no_main] | ||
| 6 | #![macro_use] | ||
| 7 | #![feature(type_alias_impl_trait)] | ||
| 8 | |||
| 9 | use defmt::*; | ||
| 10 | use embassy_executor::Spawner; | ||
| 11 | use embassy_lora::iv::GenericSx126xInterfaceVariant; | ||
| 12 | use embassy_nrf::gpio::{Input, Level, Output, OutputDrive, Pin as _, Pull}; | ||
| 13 | use embassy_nrf::{bind_interrupts, peripherals, spim}; | ||
| 14 | use embassy_time::{Delay, Duration, Timer}; | ||
| 15 | use lora_phy::mod_params::*; | ||
| 16 | use lora_phy::sx1261_2::SX1261_2; | ||
| 17 | use lora_phy::LoRa; | ||
| 18 | use {defmt_rtt as _, panic_probe as _}; | ||
| 19 | |||
| 20 | const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region | ||
| 21 | |||
| 22 | bind_interrupts!(struct Irqs { | ||
| 23 | SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1 => spim::InterruptHandler<peripherals::TWISPI1>; | ||
| 24 | }); | ||
| 25 | |||
| 26 | #[embassy_executor::main] | ||
| 27 | async fn main(_spawner: Spawner) { | ||
| 28 | let p = embassy_nrf::init(Default::default()); | ||
| 29 | let mut spi_config = spim::Config::default(); | ||
| 30 | spi_config.frequency = spim::Frequency::M16; | ||
| 31 | |||
| 32 | let spim = spim::Spim::new(p.TWISPI1, Irqs, p.P1_11, p.P1_13, p.P1_12, spi_config); | ||
| 33 | |||
| 34 | let nss = Output::new(p.P1_10.degrade(), Level::High, OutputDrive::Standard); | ||
| 35 | let reset = Output::new(p.P1_06.degrade(), Level::High, OutputDrive::Standard); | ||
| 36 | let dio1 = Input::new(p.P1_15.degrade(), Pull::Down); | ||
| 37 | let busy = Input::new(p.P1_14.degrade(), Pull::Down); | ||
| 38 | let rf_switch_rx = Output::new(p.P1_05.degrade(), Level::Low, OutputDrive::Standard); | ||
| 39 | let rf_switch_tx = Output::new(p.P1_07.degrade(), Level::Low, OutputDrive::Standard); | ||
| 40 | |||
| 41 | let iv = | ||
| 42 | GenericSx126xInterfaceVariant::new(nss, reset, dio1, busy, Some(rf_switch_rx), Some(rf_switch_tx)).unwrap(); | ||
| 43 | |||
| 44 | let mut delay = Delay; | ||
| 45 | |||
| 46 | let mut lora = { | ||
| 47 | match LoRa::new(SX1261_2::new(BoardType::Rak4631Sx1262, spim, iv), false, &mut delay).await { | ||
| 48 | Ok(l) => l, | ||
| 49 | Err(err) => { | ||
| 50 | info!("Radio error = {}", err); | ||
| 51 | return; | ||
| 52 | } | ||
| 53 | } | ||
| 54 | }; | ||
| 55 | |||
| 56 | let mut debug_indicator = Output::new(p.P1_03, Level::Low, OutputDrive::Standard); | ||
| 57 | let mut start_indicator = Output::new(p.P1_04, Level::Low, OutputDrive::Standard); | ||
| 58 | |||
| 59 | start_indicator.set_high(); | ||
| 60 | Timer::after(Duration::from_secs(5)).await; | ||
| 61 | start_indicator.set_low(); | ||
| 62 | |||
| 63 | let mdltn_params = { | ||
| 64 | match lora.create_modulation_params( | ||
| 65 | SpreadingFactor::_10, | ||
| 66 | Bandwidth::_250KHz, | ||
| 67 | CodingRate::_4_8, | ||
| 68 | LORA_FREQUENCY_IN_HZ, | ||
| 69 | ) { | ||
| 70 | Ok(mp) => mp, | ||
| 71 | Err(err) => { | ||
| 72 | info!("Radio error = {}", err); | ||
| 73 | return; | ||
| 74 | } | ||
| 75 | } | ||
| 76 | }; | ||
| 77 | |||
| 78 | match lora.prepare_for_cad(&mdltn_params, true).await { | ||
| 79 | Ok(()) => {} | ||
| 80 | Err(err) => { | ||
| 81 | info!("Radio error = {}", err); | ||
| 82 | return; | ||
| 83 | } | ||
| 84 | }; | ||
| 85 | |||
| 86 | match lora.cad().await { | ||
| 87 | Ok(cad_activity_detected) => { | ||
| 88 | if cad_activity_detected { | ||
| 89 | info!("cad successful with activity detected") | ||
| 90 | } else { | ||
| 91 | info!("cad successful without activity detected") | ||
| 92 | } | ||
| 93 | debug_indicator.set_high(); | ||
| 94 | Timer::after(Duration::from_secs(5)).await; | ||
| 95 | debug_indicator.set_low(); | ||
| 96 | } | ||
| 97 | Err(err) => info!("cad unsuccessful = {}", err), | ||
| 98 | } | ||
| 99 | } | ||
diff --git a/examples/nrf52840/src/bin/lora_lorawan.rs b/examples/nrf52840/src/bin/lora_lorawan.rs new file mode 100644 index 000000000..c953680c6 --- /dev/null +++ b/examples/nrf52840/src/bin/lora_lorawan.rs | |||
| @@ -0,0 +1,83 @@ | |||
| 1 | //! This example runs on the RAK4631 WisBlock, which has an nRF52840 MCU and Semtech Sx126x radio. | ||
| 2 | //! Other nrf/sx126x combinations may work with appropriate pin modifications. | ||
| 3 | //! It demonstrates LoRaWAN join functionality. | ||
| 4 | #![no_std] | ||
| 5 | #![no_main] | ||
| 6 | #![macro_use] | ||
| 7 | #![feature(type_alias_impl_trait)] | ||
| 8 | |||
| 9 | use defmt::*; | ||
| 10 | use embassy_executor::Spawner; | ||
| 11 | use embassy_lora::iv::GenericSx126xInterfaceVariant; | ||
| 12 | use embassy_lora::LoraTimer; | ||
| 13 | use embassy_nrf::gpio::{Input, Level, Output, OutputDrive, Pin as _, Pull}; | ||
| 14 | use embassy_nrf::rng::Rng; | ||
| 15 | use embassy_nrf::{bind_interrupts, peripherals, rng, spim}; | ||
| 16 | use embassy_time::Delay; | ||
| 17 | use lora_phy::mod_params::*; | ||
| 18 | use lora_phy::sx1261_2::SX1261_2; | ||
| 19 | use lora_phy::LoRa; | ||
| 20 | use lorawan::default_crypto::DefaultFactory as Crypto; | ||
| 21 | use lorawan_device::async_device::lora_radio::LoRaRadio; | ||
| 22 | use lorawan_device::async_device::{region, Device, JoinMode}; | ||
| 23 | use {defmt_rtt as _, panic_probe as _}; | ||
| 24 | |||
| 25 | const LORAWAN_REGION: region::Region = region::Region::EU868; // warning: set this appropriately for the region | ||
| 26 | |||
| 27 | bind_interrupts!(struct Irqs { | ||
| 28 | SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1 => spim::InterruptHandler<peripherals::TWISPI1>; | ||
| 29 | RNG => rng::InterruptHandler<peripherals::RNG>; | ||
| 30 | }); | ||
| 31 | |||
| 32 | #[embassy_executor::main] | ||
| 33 | async fn main(_spawner: Spawner) { | ||
| 34 | let p = embassy_nrf::init(Default::default()); | ||
| 35 | let mut spi_config = spim::Config::default(); | ||
| 36 | spi_config.frequency = spim::Frequency::M16; | ||
| 37 | |||
| 38 | let spim = spim::Spim::new(p.TWISPI1, Irqs, p.P1_11, p.P1_13, p.P1_12, spi_config); | ||
| 39 | |||
| 40 | let nss = Output::new(p.P1_10.degrade(), Level::High, OutputDrive::Standard); | ||
| 41 | let reset = Output::new(p.P1_06.degrade(), Level::High, OutputDrive::Standard); | ||
| 42 | let dio1 = Input::new(p.P1_15.degrade(), Pull::Down); | ||
| 43 | let busy = Input::new(p.P1_14.degrade(), Pull::Down); | ||
| 44 | let rf_switch_rx = Output::new(p.P1_05.degrade(), Level::Low, OutputDrive::Standard); | ||
| 45 | let rf_switch_tx = Output::new(p.P1_07.degrade(), Level::Low, OutputDrive::Standard); | ||
| 46 | |||
| 47 | let iv = | ||
| 48 | GenericSx126xInterfaceVariant::new(nss, reset, dio1, busy, Some(rf_switch_rx), Some(rf_switch_tx)).unwrap(); | ||
| 49 | |||
| 50 | let mut delay = Delay; | ||
| 51 | |||
| 52 | let lora = { | ||
| 53 | match LoRa::new(SX1261_2::new(BoardType::Rak4631Sx1262, spim, iv), true, &mut delay).await { | ||
| 54 | Ok(l) => l, | ||
| 55 | Err(err) => { | ||
| 56 | info!("Radio error = {}", err); | ||
| 57 | return; | ||
| 58 | } | ||
| 59 | } | ||
| 60 | }; | ||
| 61 | |||
| 62 | let radio = LoRaRadio::new(lora); | ||
| 63 | let region: region::Configuration = region::Configuration::new(LORAWAN_REGION); | ||
| 64 | let mut device: Device<_, Crypto, _, _> = Device::new(region, radio, LoraTimer::new(), Rng::new(p.RNG, Irqs)); | ||
| 65 | |||
| 66 | defmt::info!("Joining LoRaWAN network"); | ||
| 67 | |||
| 68 | // TODO: Adjust the EUI and Keys according to your network credentials | ||
| 69 | match device | ||
| 70 | .join(&JoinMode::OTAA { | ||
| 71 | deveui: [0, 0, 0, 0, 0, 0, 0, 0], | ||
| 72 | appeui: [0, 0, 0, 0, 0, 0, 0, 0], | ||
| 73 | appkey: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
| 74 | }) | ||
| 75 | .await | ||
| 76 | { | ||
| 77 | Ok(()) => defmt::info!("LoRaWAN network joined"), | ||
| 78 | Err(err) => { | ||
| 79 | info!("Radio error = {}", err); | ||
| 80 | return; | ||
| 81 | } | ||
| 82 | }; | ||
| 83 | } | ||
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 000000000..563fe42ec --- /dev/null +++ b/examples/nrf52840/src/bin/lora_p2p_receive.rs | |||
| @@ -0,0 +1,121 @@ | |||
| 1 | //! This example runs on the RAK4631 WisBlock, which has an nRF52840 MCU and Semtech Sx126x radio. | ||
| 2 | //! Other nrf/sx126x combinations may work with appropriate pin modifications. | ||
| 3 | //! It demonstrates LORA P2P receive functionality in conjunction with the lora_p2p_send example. | ||
| 4 | #![no_std] | ||
| 5 | #![no_main] | ||
| 6 | #![macro_use] | ||
| 7 | #![feature(type_alias_impl_trait)] | ||
| 8 | |||
| 9 | use defmt::*; | ||
| 10 | use embassy_executor::Spawner; | ||
| 11 | use embassy_lora::iv::GenericSx126xInterfaceVariant; | ||
| 12 | use embassy_nrf::gpio::{Input, Level, Output, OutputDrive, Pin as _, Pull}; | ||
| 13 | use embassy_nrf::{bind_interrupts, peripherals, spim}; | ||
| 14 | use embassy_time::{Delay, Duration, Timer}; | ||
| 15 | use lora_phy::mod_params::*; | ||
| 16 | use lora_phy::sx1261_2::SX1261_2; | ||
| 17 | use lora_phy::LoRa; | ||
| 18 | use {defmt_rtt as _, panic_probe as _}; | ||
| 19 | |||
| 20 | const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region | ||
| 21 | |||
| 22 | bind_interrupts!(struct Irqs { | ||
| 23 | SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1 => spim::InterruptHandler<peripherals::TWISPI1>; | ||
| 24 | }); | ||
| 25 | |||
| 26 | #[embassy_executor::main] | ||
| 27 | async fn main(_spawner: Spawner) { | ||
| 28 | let p = embassy_nrf::init(Default::default()); | ||
| 29 | let mut spi_config = spim::Config::default(); | ||
| 30 | spi_config.frequency = spim::Frequency::M16; | ||
| 31 | |||
| 32 | let spim = spim::Spim::new(p.TWISPI1, Irqs, p.P1_11, p.P1_13, p.P1_12, spi_config); | ||
| 33 | |||
| 34 | let nss = Output::new(p.P1_10.degrade(), Level::High, OutputDrive::Standard); | ||
| 35 | let reset = Output::new(p.P1_06.degrade(), Level::High, OutputDrive::Standard); | ||
| 36 | let dio1 = Input::new(p.P1_15.degrade(), Pull::Down); | ||
| 37 | let busy = Input::new(p.P1_14.degrade(), Pull::Down); | ||
| 38 | let rf_switch_rx = Output::new(p.P1_05.degrade(), Level::Low, OutputDrive::Standard); | ||
| 39 | let rf_switch_tx = Output::new(p.P1_07.degrade(), Level::Low, OutputDrive::Standard); | ||
| 40 | |||
| 41 | let iv = | ||
| 42 | GenericSx126xInterfaceVariant::new(nss, reset, dio1, busy, Some(rf_switch_rx), Some(rf_switch_tx)).unwrap(); | ||
| 43 | |||
| 44 | let mut delay = Delay; | ||
| 45 | |||
| 46 | let mut lora = { | ||
| 47 | match LoRa::new(SX1261_2::new(BoardType::Rak4631Sx1262, spim, iv), false, &mut delay).await { | ||
| 48 | Ok(l) => l, | ||
| 49 | Err(err) => { | ||
| 50 | info!("Radio error = {}", err); | ||
| 51 | return; | ||
| 52 | } | ||
| 53 | } | ||
| 54 | }; | ||
| 55 | |||
| 56 | let mut debug_indicator = Output::new(p.P1_03, Level::Low, OutputDrive::Standard); | ||
| 57 | let mut start_indicator = Output::new(p.P1_04, Level::Low, OutputDrive::Standard); | ||
| 58 | |||
| 59 | start_indicator.set_high(); | ||
| 60 | Timer::after(Duration::from_secs(5)).await; | ||
| 61 | start_indicator.set_low(); | ||
| 62 | |||
| 63 | let mut receiving_buffer = [00u8; 100]; | ||
| 64 | |||
| 65 | let mdltn_params = { | ||
| 66 | match lora.create_modulation_params( | ||
| 67 | SpreadingFactor::_10, | ||
| 68 | Bandwidth::_250KHz, | ||
| 69 | CodingRate::_4_8, | ||
| 70 | LORA_FREQUENCY_IN_HZ, | ||
| 71 | ) { | ||
| 72 | Ok(mp) => mp, | ||
| 73 | Err(err) => { | ||
| 74 | info!("Radio error = {}", err); | ||
| 75 | return; | ||
| 76 | } | ||
| 77 | } | ||
| 78 | }; | ||
| 79 | |||
| 80 | let rx_pkt_params = { | ||
| 81 | match lora.create_rx_packet_params(4, false, receiving_buffer.len() as u8, true, false, &mdltn_params) { | ||
| 82 | Ok(pp) => pp, | ||
| 83 | Err(err) => { | ||
| 84 | info!("Radio error = {}", err); | ||
| 85 | return; | ||
| 86 | } | ||
| 87 | } | ||
| 88 | }; | ||
| 89 | |||
| 90 | match lora | ||
| 91 | .prepare_for_rx(&mdltn_params, &rx_pkt_params, None, true, false, 0, 0x00ffffffu32) | ||
| 92 | .await | ||
| 93 | { | ||
| 94 | Ok(()) => {} | ||
| 95 | Err(err) => { | ||
| 96 | info!("Radio error = {}", err); | ||
| 97 | return; | ||
| 98 | } | ||
| 99 | }; | ||
| 100 | |||
| 101 | loop { | ||
| 102 | receiving_buffer = [00u8; 100]; | ||
| 103 | match lora.rx(&rx_pkt_params, &mut receiving_buffer).await { | ||
| 104 | Ok((received_len, _rx_pkt_status)) => { | ||
| 105 | if (received_len == 3) | ||
| 106 | && (receiving_buffer[0] == 0x01u8) | ||
| 107 | && (receiving_buffer[1] == 0x02u8) | ||
| 108 | && (receiving_buffer[2] == 0x03u8) | ||
| 109 | { | ||
| 110 | info!("rx successful"); | ||
| 111 | debug_indicator.set_high(); | ||
| 112 | Timer::after(Duration::from_secs(5)).await; | ||
| 113 | debug_indicator.set_low(); | ||
| 114 | } else { | ||
| 115 | info!("rx unknown packet"); | ||
| 116 | } | ||
| 117 | } | ||
| 118 | Err(err) => info!("rx unsuccessful = {}", err), | ||
| 119 | } | ||
| 120 | } | ||
| 121 | } | ||
diff --git a/examples/nrf52840/src/bin/lora_p2p_receive_duty_cycle.rs b/examples/nrf52840/src/bin/lora_p2p_receive_duty_cycle.rs new file mode 100644 index 000000000..1fd8f61a2 --- /dev/null +++ b/examples/nrf52840/src/bin/lora_p2p_receive_duty_cycle.rs | |||
| @@ -0,0 +1,131 @@ | |||
| 1 | //! This example runs on the RAK4631 WisBlock, which has an nRF52840 MCU and Semtech Sx126x radio. | ||
| 2 | //! Other nrf/sx126x combinations may work with appropriate pin modifications. | ||
| 3 | //! It demonstrates LoRa Rx duty cycle functionality in conjunction with the lora_p2p_send example. | ||
| 4 | #![no_std] | ||
| 5 | #![no_main] | ||
| 6 | #![macro_use] | ||
| 7 | #![feature(type_alias_impl_trait)] | ||
| 8 | |||
| 9 | use defmt::*; | ||
| 10 | use embassy_executor::Spawner; | ||
| 11 | use embassy_lora::iv::GenericSx126xInterfaceVariant; | ||
| 12 | use embassy_nrf::gpio::{Input, Level, Output, OutputDrive, Pin as _, Pull}; | ||
| 13 | use embassy_nrf::{bind_interrupts, peripherals, spim}; | ||
| 14 | use embassy_time::{Delay, Duration, Timer}; | ||
| 15 | use lora_phy::mod_params::*; | ||
| 16 | use lora_phy::sx1261_2::SX1261_2; | ||
| 17 | use lora_phy::LoRa; | ||
| 18 | use {defmt_rtt as _, panic_probe as _}; | ||
| 19 | |||
| 20 | const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region | ||
| 21 | |||
| 22 | bind_interrupts!(struct Irqs { | ||
| 23 | SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1 => spim::InterruptHandler<peripherals::TWISPI1>; | ||
| 24 | }); | ||
| 25 | |||
| 26 | #[embassy_executor::main] | ||
| 27 | async fn main(_spawner: Spawner) { | ||
| 28 | let p = embassy_nrf::init(Default::default()); | ||
| 29 | let mut spi_config = spim::Config::default(); | ||
| 30 | spi_config.frequency = spim::Frequency::M16; | ||
| 31 | |||
| 32 | let spim = spim::Spim::new(p.TWISPI1, Irqs, p.P1_11, p.P1_13, p.P1_12, spi_config); | ||
| 33 | |||
| 34 | let nss = Output::new(p.P1_10.degrade(), Level::High, OutputDrive::Standard); | ||
| 35 | let reset = Output::new(p.P1_06.degrade(), Level::High, OutputDrive::Standard); | ||
| 36 | let dio1 = Input::new(p.P1_15.degrade(), Pull::Down); | ||
| 37 | let busy = Input::new(p.P1_14.degrade(), Pull::Down); | ||
| 38 | let rf_switch_rx = Output::new(p.P1_05.degrade(), Level::Low, OutputDrive::Standard); | ||
| 39 | let rf_switch_tx = Output::new(p.P1_07.degrade(), Level::Low, OutputDrive::Standard); | ||
| 40 | |||
| 41 | let iv = | ||
| 42 | GenericSx126xInterfaceVariant::new(nss, reset, dio1, busy, Some(rf_switch_rx), Some(rf_switch_tx)).unwrap(); | ||
| 43 | |||
| 44 | let mut delay = Delay; | ||
| 45 | |||
| 46 | let mut lora = { | ||
| 47 | match LoRa::new(SX1261_2::new(BoardType::Rak4631Sx1262, spim, iv), false, &mut delay).await { | ||
| 48 | Ok(l) => l, | ||
| 49 | Err(err) => { | ||
| 50 | info!("Radio error = {}", err); | ||
| 51 | return; | ||
| 52 | } | ||
| 53 | } | ||
| 54 | }; | ||
| 55 | |||
| 56 | let mut debug_indicator = Output::new(p.P1_03, Level::Low, OutputDrive::Standard); | ||
| 57 | let mut start_indicator = Output::new(p.P1_04, Level::Low, OutputDrive::Standard); | ||
| 58 | |||
| 59 | start_indicator.set_high(); | ||
| 60 | Timer::after(Duration::from_secs(5)).await; | ||
| 61 | start_indicator.set_low(); | ||
| 62 | |||
| 63 | let mut receiving_buffer = [00u8; 100]; | ||
| 64 | |||
| 65 | let mdltn_params = { | ||
| 66 | match lora.create_modulation_params( | ||
| 67 | SpreadingFactor::_10, | ||
| 68 | Bandwidth::_250KHz, | ||
| 69 | CodingRate::_4_8, | ||
| 70 | LORA_FREQUENCY_IN_HZ, | ||
| 71 | ) { | ||
| 72 | Ok(mp) => mp, | ||
| 73 | Err(err) => { | ||
| 74 | info!("Radio error = {}", err); | ||
| 75 | return; | ||
| 76 | } | ||
| 77 | } | ||
| 78 | }; | ||
| 79 | |||
| 80 | let rx_pkt_params = { | ||
| 81 | match lora.create_rx_packet_params(4, false, receiving_buffer.len() as u8, true, false, &mdltn_params) { | ||
| 82 | Ok(pp) => pp, | ||
| 83 | Err(err) => { | ||
| 84 | info!("Radio error = {}", err); | ||
| 85 | return; | ||
| 86 | } | ||
| 87 | } | ||
| 88 | }; | ||
| 89 | |||
| 90 | // See "RM0453 Reference manual STM32WL5x advanced Arm®-based 32-bit MCUs with sub-GHz radio solution" for the best explanation of Rx duty cycle processing. | ||
| 91 | match lora | ||
| 92 | .prepare_for_rx( | ||
| 93 | &mdltn_params, | ||
| 94 | &rx_pkt_params, | ||
| 95 | Some(&DutyCycleParams { | ||
| 96 | rx_time: 300_000, // 300_000 units * 15.625 us/unit = 4.69 s | ||
| 97 | sleep_time: 200_000, // 200_000 units * 15.625 us/unit = 3.13 s | ||
| 98 | }), | ||
| 99 | false, | ||
| 100 | false, | ||
| 101 | 0, | ||
| 102 | 0, | ||
| 103 | ) | ||
| 104 | .await | ||
| 105 | { | ||
| 106 | Ok(()) => {} | ||
| 107 | Err(err) => { | ||
| 108 | info!("Radio error = {}", err); | ||
| 109 | return; | ||
| 110 | } | ||
| 111 | }; | ||
| 112 | |||
| 113 | receiving_buffer = [00u8; 100]; | ||
| 114 | match lora.rx(&rx_pkt_params, &mut receiving_buffer).await { | ||
| 115 | Ok((received_len, _rx_pkt_status)) => { | ||
| 116 | if (received_len == 3) | ||
| 117 | && (receiving_buffer[0] == 0x01u8) | ||
| 118 | && (receiving_buffer[1] == 0x02u8) | ||
| 119 | && (receiving_buffer[2] == 0x03u8) | ||
| 120 | { | ||
| 121 | info!("rx successful"); | ||
| 122 | debug_indicator.set_high(); | ||
| 123 | Timer::after(Duration::from_secs(5)).await; | ||
| 124 | debug_indicator.set_low(); | ||
| 125 | } else { | ||
| 126 | info!("rx unknown packet") | ||
| 127 | } | ||
| 128 | } | ||
| 129 | Err(err) => info!("rx unsuccessful = {}", err), | ||
| 130 | } | ||
| 131 | } | ||
diff --git a/examples/nrf52840/src/bin/lora_p2p_report.rs b/examples/nrf52840/src/bin/lora_p2p_report.rs deleted file mode 100644 index e24f0db03..000000000 --- a/examples/nrf52840/src/bin/lora_p2p_report.rs +++ /dev/null | |||
| @@ -1,81 +0,0 @@ | |||
| 1 | //! This example runs on the RAK4631 WisBlock, which has an nRF52840 MCU and Semtech Sx126x radio. | ||
| 2 | //! Other nrf/sx126x combinations may work with appropriate pin modifications. | ||
| 3 | //! It demonstates LORA P2P functionality in conjunction with example lora_p2p_sense.rs. | ||
| 4 | #![no_std] | ||
| 5 | #![no_main] | ||
| 6 | #![macro_use] | ||
| 7 | #![allow(dead_code)] | ||
| 8 | #![feature(type_alias_impl_trait)] | ||
| 9 | |||
| 10 | use defmt::*; | ||
| 11 | use embassy_executor::Spawner; | ||
| 12 | use embassy_lora::sx126x::*; | ||
| 13 | use embassy_nrf::gpio::{Input, Level, Output, OutputDrive, Pin as _, Pull}; | ||
| 14 | use embassy_nrf::{bind_interrupts, peripherals, spim}; | ||
| 15 | use embassy_time::{Duration, Timer}; | ||
| 16 | use lorawan_device::async_device::radio::{Bandwidth, CodingRate, PhyRxTx, RfConfig, SpreadingFactor}; | ||
| 17 | use {defmt_rtt as _, panic_probe as _}; | ||
| 18 | |||
| 19 | bind_interrupts!(struct Irqs { | ||
| 20 | SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1 => spim::InterruptHandler<peripherals::TWISPI1>; | ||
| 21 | }); | ||
| 22 | |||
| 23 | #[embassy_executor::main] | ||
| 24 | async fn main(_spawner: Spawner) { | ||
| 25 | let p = embassy_nrf::init(Default::default()); | ||
| 26 | let mut spi_config = spim::Config::default(); | ||
| 27 | spi_config.frequency = spim::Frequency::M16; | ||
| 28 | |||
| 29 | let mut radio = { | ||
| 30 | let spim = spim::Spim::new(p.TWISPI1, Irqs, p.P1_11, p.P1_13, p.P1_12, spi_config); | ||
| 31 | |||
| 32 | let cs = Output::new(p.P1_10.degrade(), Level::High, OutputDrive::Standard); | ||
| 33 | let reset = Output::new(p.P1_06.degrade(), Level::High, OutputDrive::Standard); | ||
| 34 | let dio1 = Input::new(p.P1_15.degrade(), Pull::Down); | ||
| 35 | let busy = Input::new(p.P1_14.degrade(), Pull::Down); | ||
| 36 | let antenna_rx = Output::new(p.P1_05.degrade(), Level::Low, OutputDrive::Standard); | ||
| 37 | let antenna_tx = Output::new(p.P1_07.degrade(), Level::Low, OutputDrive::Standard); | ||
| 38 | |||
| 39 | match Sx126xRadio::new(spim, cs, reset, antenna_rx, antenna_tx, dio1, busy, false).await { | ||
| 40 | Ok(r) => r, | ||
| 41 | Err(err) => { | ||
| 42 | info!("Sx126xRadio error = {}", err); | ||
| 43 | return; | ||
| 44 | } | ||
| 45 | } | ||
| 46 | }; | ||
| 47 | |||
| 48 | let mut debug_indicator = Output::new(p.P1_03, Level::Low, OutputDrive::Standard); | ||
| 49 | let mut start_indicator = Output::new(p.P1_04, Level::Low, OutputDrive::Standard); | ||
| 50 | |||
| 51 | start_indicator.set_high(); | ||
| 52 | Timer::after(Duration::from_secs(5)).await; | ||
| 53 | start_indicator.set_low(); | ||
| 54 | |||
| 55 | loop { | ||
| 56 | let rf_config = RfConfig { | ||
| 57 | frequency: 903900000, // channel in Hz | ||
| 58 | bandwidth: Bandwidth::_250KHz, | ||
| 59 | spreading_factor: SpreadingFactor::_10, | ||
| 60 | coding_rate: CodingRate::_4_8, | ||
| 61 | }; | ||
| 62 | |||
| 63 | let mut buffer = [00u8; 100]; | ||
| 64 | |||
| 65 | // P2P receive | ||
| 66 | match radio.rx(rf_config, &mut buffer).await { | ||
| 67 | Ok((buffer_len, rx_quality)) => info!( | ||
| 68 | "RX received = {:?} with length = {} rssi = {} snr = {}", | ||
| 69 | &buffer[0..buffer_len], | ||
| 70 | buffer_len, | ||
| 71 | rx_quality.rssi(), | ||
| 72 | rx_quality.snr() | ||
| 73 | ), | ||
| 74 | Err(err) => info!("RX error = {}", err), | ||
| 75 | } | ||
| 76 | |||
| 77 | debug_indicator.set_high(); | ||
| 78 | Timer::after(Duration::from_secs(2)).await; | ||
| 79 | debug_indicator.set_low(); | ||
| 80 | } | ||
| 81 | } | ||
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 000000000..1c8bbc27a --- /dev/null +++ b/examples/nrf52840/src/bin/lora_p2p_send.rs | |||
| @@ -0,0 +1,104 @@ | |||
| 1 | //! This example runs on the RAK4631 WisBlock, which has an nRF52840 MCU and Semtech Sx126x radio. | ||
| 2 | //! Other nrf/sx126x combinations may work with appropriate pin modifications. | ||
| 3 | //! It demonstrates LORA P2P send functionality. | ||
| 4 | #![no_std] | ||
| 5 | #![no_main] | ||
| 6 | #![macro_use] | ||
| 7 | #![feature(type_alias_impl_trait)] | ||
| 8 | |||
| 9 | use defmt::*; | ||
| 10 | use embassy_executor::Spawner; | ||
| 11 | use embassy_lora::iv::GenericSx126xInterfaceVariant; | ||
| 12 | use embassy_nrf::gpio::{Input, Level, Output, OutputDrive, Pin as _, Pull}; | ||
| 13 | use embassy_nrf::{bind_interrupts, peripherals, spim}; | ||
| 14 | use embassy_time::Delay; | ||
| 15 | use lora_phy::mod_params::*; | ||
| 16 | use lora_phy::sx1261_2::SX1261_2; | ||
| 17 | use lora_phy::LoRa; | ||
| 18 | use {defmt_rtt as _, panic_probe as _}; | ||
| 19 | |||
| 20 | const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region | ||
| 21 | |||
| 22 | bind_interrupts!(struct Irqs { | ||
| 23 | SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1 => spim::InterruptHandler<peripherals::TWISPI1>; | ||
| 24 | }); | ||
| 25 | |||
| 26 | #[embassy_executor::main] | ||
| 27 | async fn main(_spawner: Spawner) { | ||
| 28 | let p = embassy_nrf::init(Default::default()); | ||
| 29 | let mut spi_config = spim::Config::default(); | ||
| 30 | spi_config.frequency = spim::Frequency::M16; | ||
| 31 | |||
| 32 | let spim = spim::Spim::new(p.TWISPI1, Irqs, p.P1_11, p.P1_13, p.P1_12, spi_config); | ||
| 33 | |||
| 34 | let nss = Output::new(p.P1_10.degrade(), Level::High, OutputDrive::Standard); | ||
| 35 | let reset = Output::new(p.P1_06.degrade(), Level::High, OutputDrive::Standard); | ||
| 36 | let dio1 = Input::new(p.P1_15.degrade(), Pull::Down); | ||
| 37 | let busy = Input::new(p.P1_14.degrade(), Pull::Down); | ||
| 38 | let rf_switch_rx = Output::new(p.P1_05.degrade(), Level::Low, OutputDrive::Standard); | ||
| 39 | let rf_switch_tx = Output::new(p.P1_07.degrade(), Level::Low, OutputDrive::Standard); | ||
| 40 | |||
| 41 | let iv = | ||
| 42 | GenericSx126xInterfaceVariant::new(nss, reset, dio1, busy, Some(rf_switch_rx), Some(rf_switch_tx)).unwrap(); | ||
| 43 | |||
| 44 | let mut delay = Delay; | ||
| 45 | |||
| 46 | let mut lora = { | ||
| 47 | match LoRa::new(SX1261_2::new(BoardType::Rak4631Sx1262, spim, iv), false, &mut delay).await { | ||
| 48 | Ok(l) => l, | ||
| 49 | Err(err) => { | ||
| 50 | info!("Radio error = {}", err); | ||
| 51 | return; | ||
| 52 | } | ||
| 53 | } | ||
| 54 | }; | ||
| 55 | |||
| 56 | let mdltn_params = { | ||
| 57 | match lora.create_modulation_params( | ||
| 58 | SpreadingFactor::_10, | ||
| 59 | Bandwidth::_250KHz, | ||
| 60 | CodingRate::_4_8, | ||
| 61 | LORA_FREQUENCY_IN_HZ, | ||
| 62 | ) { | ||
| 63 | Ok(mp) => mp, | ||
| 64 | Err(err) => { | ||
| 65 | info!("Radio error = {}", err); | ||
| 66 | return; | ||
| 67 | } | ||
| 68 | } | ||
| 69 | }; | ||
| 70 | |||
| 71 | let mut tx_pkt_params = { | ||
| 72 | match lora.create_tx_packet_params(4, false, true, false, &mdltn_params) { | ||
| 73 | Ok(pp) => pp, | ||
| 74 | Err(err) => { | ||
| 75 | info!("Radio error = {}", err); | ||
| 76 | return; | ||
| 77 | } | ||
| 78 | } | ||
| 79 | }; | ||
| 80 | |||
| 81 | match lora.prepare_for_tx(&mdltn_params, 20, false).await { | ||
| 82 | Ok(()) => {} | ||
| 83 | Err(err) => { | ||
| 84 | info!("Radio error = {}", err); | ||
| 85 | return; | ||
| 86 | } | ||
| 87 | }; | ||
| 88 | |||
| 89 | let buffer = [0x01u8, 0x02u8, 0x03u8]; | ||
| 90 | match lora.tx(&mdltn_params, &mut tx_pkt_params, &buffer, 0xffffff).await { | ||
| 91 | Ok(()) => { | ||
| 92 | info!("TX DONE"); | ||
| 93 | } | ||
| 94 | Err(err) => { | ||
| 95 | info!("Radio error = {}", err); | ||
| 96 | return; | ||
| 97 | } | ||
| 98 | }; | ||
| 99 | |||
| 100 | match lora.sleep(&mut delay).await { | ||
| 101 | Ok(()) => info!("Sleep successful"), | ||
| 102 | Err(err) => info!("Sleep unsuccessful = {}", err), | ||
| 103 | } | ||
| 104 | } | ||
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 b6f41ffcc..000000000 --- a/examples/nrf52840/src/bin/lora_p2p_sense.rs +++ /dev/null | |||
| @@ -1,128 +0,0 @@ | |||
| 1 | //! This example runs on the RAK4631 WisBlock, which has an nRF52840 MCU and Semtech Sx126x radio. | ||
| 2 | //! Other nrf/sx126x combinations may work with appropriate pin modifications. | ||
| 3 | //! It demonstates LORA P2P functionality in conjunction with example lora_p2p_report.rs. | ||
| 4 | #![no_std] | ||
| 5 | #![no_main] | ||
| 6 | #![macro_use] | ||
| 7 | #![feature(type_alias_impl_trait)] | ||
| 8 | #![feature(alloc_error_handler)] | ||
| 9 | #![allow(incomplete_features)] | ||
| 10 | |||
| 11 | use defmt::*; | ||
| 12 | use embassy_executor::Spawner; | ||
| 13 | use embassy_lora::sx126x::*; | ||
| 14 | use embassy_nrf::gpio::{Input, Level, Output, OutputDrive, Pin as _, Pull}; | ||
| 15 | use embassy_nrf::{bind_interrupts, peripherals, spim}; | ||
| 16 | use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; | ||
| 17 | use embassy_sync::pubsub::{PubSubChannel, Publisher}; | ||
| 18 | use embassy_time::{Duration, Timer}; | ||
| 19 | use lorawan_device::async_device::radio::{Bandwidth, CodingRate, PhyRxTx, RfConfig, SpreadingFactor, TxConfig}; | ||
| 20 | use {defmt_rtt as _, panic_probe as _, panic_probe as _}; | ||
| 21 | |||
| 22 | bind_interrupts!(struct Irqs { | ||
| 23 | SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1 => spim::InterruptHandler<peripherals::TWISPI1>; | ||
| 24 | }); | ||
| 25 | |||
| 26 | // Message bus: queue of 2, 1 subscriber (Lora P2P), 2 publishers (temperature, motion detection) | ||
| 27 | static MESSAGE_BUS: PubSubChannel<CriticalSectionRawMutex, Message, 2, 1, 2> = PubSubChannel::new(); | ||
| 28 | |||
| 29 | #[derive(Clone, defmt::Format)] | ||
| 30 | enum Message { | ||
| 31 | Temperature(i32), | ||
| 32 | MotionDetected, | ||
| 33 | } | ||
| 34 | |||
| 35 | #[embassy_executor::task] | ||
| 36 | async fn temperature_task(publisher: Publisher<'static, CriticalSectionRawMutex, Message, 2, 1, 2>) { | ||
| 37 | // Publish a fake temperature every 43 seconds, minimizing LORA traffic. | ||
| 38 | loop { | ||
| 39 | Timer::after(Duration::from_secs(43)).await; | ||
| 40 | publisher.publish(Message::Temperature(9)).await; | ||
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 44 | #[embassy_executor::task] | ||
| 45 | async fn motion_detection_task(publisher: Publisher<'static, CriticalSectionRawMutex, Message, 2, 1, 2>) { | ||
| 46 | // Publish a fake motion detection every 79 seconds, minimizing LORA traffic. | ||
| 47 | loop { | ||
| 48 | Timer::after(Duration::from_secs(79)).await; | ||
| 49 | publisher.publish(Message::MotionDetected).await; | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | #[embassy_executor::main] | ||
| 54 | async fn main(spawner: Spawner) { | ||
| 55 | let p = embassy_nrf::init(Default::default()); | ||
| 56 | // set up to funnel temperature and motion detection events to the Lora Tx task | ||
| 57 | let mut lora_tx_subscriber = unwrap!(MESSAGE_BUS.subscriber()); | ||
| 58 | let temperature_publisher = unwrap!(MESSAGE_BUS.publisher()); | ||
| 59 | let motion_detection_publisher = unwrap!(MESSAGE_BUS.publisher()); | ||
| 60 | |||
| 61 | let mut spi_config = spim::Config::default(); | ||
| 62 | spi_config.frequency = spim::Frequency::M16; | ||
| 63 | |||
| 64 | let mut radio = { | ||
| 65 | let spim = spim::Spim::new(p.TWISPI1, Irqs, p.P1_11, p.P1_13, p.P1_12, spi_config); | ||
| 66 | |||
| 67 | let cs = Output::new(p.P1_10.degrade(), Level::High, OutputDrive::Standard); | ||
| 68 | let reset = Output::new(p.P1_06.degrade(), Level::High, OutputDrive::Standard); | ||
| 69 | let dio1 = Input::new(p.P1_15.degrade(), Pull::Down); | ||
| 70 | let busy = Input::new(p.P1_14.degrade(), Pull::Down); | ||
| 71 | let antenna_rx = Output::new(p.P1_05.degrade(), Level::Low, OutputDrive::Standard); | ||
| 72 | let antenna_tx = Output::new(p.P1_07.degrade(), Level::Low, OutputDrive::Standard); | ||
| 73 | |||
| 74 | match Sx126xRadio::new(spim, cs, reset, antenna_rx, antenna_tx, dio1, busy, false).await { | ||
| 75 | Ok(r) => r, | ||
| 76 | Err(err) => { | ||
| 77 | info!("Sx126xRadio error = {}", err); | ||
| 78 | return; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | }; | ||
| 82 | |||
| 83 | let mut start_indicator = Output::new(p.P1_04, Level::Low, OutputDrive::Standard); | ||
| 84 | |||
| 85 | start_indicator.set_high(); | ||
| 86 | Timer::after(Duration::from_secs(5)).await; | ||
| 87 | start_indicator.set_low(); | ||
| 88 | |||
| 89 | match radio.lora.sleep().await { | ||
| 90 | Ok(()) => info!("Sleep successful"), | ||
| 91 | Err(err) => info!("Sleep unsuccessful = {}", err), | ||
| 92 | } | ||
| 93 | |||
| 94 | unwrap!(spawner.spawn(temperature_task(temperature_publisher))); | ||
| 95 | unwrap!(spawner.spawn(motion_detection_task(motion_detection_publisher))); | ||
| 96 | |||
| 97 | loop { | ||
| 98 | let message = lora_tx_subscriber.next_message_pure().await; | ||
| 99 | |||
| 100 | let tx_config = TxConfig { | ||
| 101 | // 11 byte maximum payload for Bandwidth 125 and SF 10 | ||
| 102 | pw: 10, // up to 20 | ||
| 103 | rf: RfConfig { | ||
| 104 | frequency: 903900000, // channel in Hz, not MHz | ||
| 105 | bandwidth: Bandwidth::_250KHz, | ||
| 106 | spreading_factor: SpreadingFactor::_10, | ||
| 107 | coding_rate: CodingRate::_4_8, | ||
| 108 | }, | ||
| 109 | }; | ||
| 110 | |||
| 111 | let mut buffer = [0x00u8]; | ||
| 112 | match message { | ||
| 113 | Message::Temperature(temperature) => buffer[0] = temperature as u8, | ||
| 114 | Message::MotionDetected => buffer[0] = 0x01u8, | ||
| 115 | }; | ||
| 116 | |||
| 117 | // unencrypted | ||
| 118 | match radio.tx(tx_config, &buffer).await { | ||
| 119 | Ok(ret_val) => info!("TX ret_val = {}", ret_val), | ||
| 120 | Err(err) => info!("TX error = {}", err), | ||
| 121 | } | ||
| 122 | |||
| 123 | match radio.lora.sleep().await { | ||
| 124 | Ok(()) => info!("Sleep successful"), | ||
| 125 | Err(err) => info!("Sleep unsuccessful = {}", err), | ||
| 126 | } | ||
| 127 | } | ||
| 128 | } | ||
diff --git a/examples/rp/Cargo.toml b/examples/rp/Cargo.toml index 8067f7ba5..45af8762e 100644 --- a/examples/rp/Cargo.toml +++ b/examples/rp/Cargo.toml | |||
| @@ -9,12 +9,16 @@ license = "MIT OR Apache-2.0" | |||
| 9 | embassy-embedded-hal = { version = "0.1.0", path = "../../embassy-embedded-hal", features = ["defmt"] } | 9 | embassy-embedded-hal = { version = "0.1.0", path = "../../embassy-embedded-hal", features = ["defmt"] } |
| 10 | embassy-sync = { version = "0.2.0", path = "../../embassy-sync", features = ["defmt"] } | 10 | embassy-sync = { version = "0.2.0", path = "../../embassy-sync", features = ["defmt"] } |
| 11 | embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] } | 11 | embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] } |
| 12 | embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime"] } | 12 | embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["nightly", "unstable-traits", "defmt", "defmt-timestamp-uptime"] } |
| 13 | embassy-rp = { version = "0.1.0", path = "../../embassy-rp", features = ["defmt", "unstable-traits", "nightly", "unstable-pac", "time-driver", "pio", "critical-section-impl"] } | 13 | embassy-rp = { version = "0.1.0", path = "../../embassy-rp", features = ["defmt", "unstable-traits", "nightly", "unstable-pac", "time-driver", "pio", "critical-section-impl"] } |
| 14 | embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"] } | 14 | embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"] } |
| 15 | embassy-net = { version = "0.1.0", path = "../../embassy-net", features = ["defmt", "nightly", "tcp", "dhcpv4", "medium-ethernet"] } | 15 | embassy-net = { version = "0.1.0", path = "../../embassy-net", features = ["defmt", "nightly", "tcp", "dhcpv4", "medium-ethernet"] } |
| 16 | embassy-futures = { version = "0.1.0", path = "../../embassy-futures" } | 16 | embassy-futures = { version = "0.1.0", path = "../../embassy-futures" } |
| 17 | embassy-usb-logger = { version = "0.1.0", path = "../../embassy-usb-logger" } | 17 | embassy-usb-logger = { version = "0.1.0", path = "../../embassy-usb-logger" } |
| 18 | embassy-lora = { version = "0.1.0", path = "../../embassy-lora", features = ["time", "defmt", "external-lora-phy"] } | ||
| 19 | lora-phy = { version = "1" } | ||
| 20 | lorawan-device = { version = "0.10.0", default-features = false, features = ["async", "external-lora-phy"] } | ||
| 21 | lorawan = { version = "0.7.3", default-features = false, features = ["default-crypto"] } | ||
| 18 | 22 | ||
| 19 | defmt = "0.3" | 23 | defmt = "0.3" |
| 20 | defmt-rtt = "0.4" | 24 | defmt-rtt = "0.4" |
diff --git a/examples/rp/src/bin/lora_lorawan.rs b/examples/rp/src/bin/lora_lorawan.rs new file mode 100644 index 000000000..a9c84bf95 --- /dev/null +++ b/examples/rp/src/bin/lora_lorawan.rs | |||
| @@ -0,0 +1,80 @@ | |||
| 1 | //! This example runs on the Raspberry Pi Pico with a Waveshare board containing a Semtech Sx1262 radio. | ||
| 2 | //! It demonstrates LoRaWAN join functionality. | ||
| 3 | #![no_std] | ||
| 4 | #![no_main] | ||
| 5 | #![macro_use] | ||
| 6 | #![feature(type_alias_impl_trait)] | ||
| 7 | |||
| 8 | use defmt::*; | ||
| 9 | use embassy_executor::Spawner; | ||
| 10 | use embassy_lora::iv::GenericSx126xInterfaceVariant; | ||
| 11 | use embassy_lora::LoraTimer; | ||
| 12 | use embassy_rp::gpio::{Input, Level, Output, Pin, Pull}; | ||
| 13 | use embassy_rp::spi::{Config, Spi}; | ||
| 14 | use embassy_time::Delay; | ||
| 15 | use lora_phy::mod_params::*; | ||
| 16 | use lora_phy::sx1261_2::SX1261_2; | ||
| 17 | use lora_phy::LoRa; | ||
| 18 | use lorawan::default_crypto::DefaultFactory as Crypto; | ||
| 19 | use lorawan_device::async_device::lora_radio::LoRaRadio; | ||
| 20 | use lorawan_device::async_device::{region, Device, JoinMode}; | ||
| 21 | use {defmt_rtt as _, panic_probe as _}; | ||
| 22 | |||
| 23 | const LORAWAN_REGION: region::Region = region::Region::EU868; // warning: set this appropriately for the region | ||
| 24 | |||
| 25 | #[embassy_executor::main] | ||
| 26 | async fn main(_spawner: Spawner) { | ||
| 27 | let p = embassy_rp::init(Default::default()); | ||
| 28 | |||
| 29 | let miso = p.PIN_12; | ||
| 30 | let mosi = p.PIN_11; | ||
| 31 | let clk = p.PIN_10; | ||
| 32 | let spi = Spi::new(p.SPI1, clk, mosi, miso, p.DMA_CH0, p.DMA_CH1, Config::default()); | ||
| 33 | |||
| 34 | let nss = Output::new(p.PIN_3.degrade(), Level::High); | ||
| 35 | let reset = Output::new(p.PIN_15.degrade(), Level::High); | ||
| 36 | let dio1 = Input::new(p.PIN_20.degrade(), Pull::None); | ||
| 37 | let busy = Input::new(p.PIN_2.degrade(), Pull::None); | ||
| 38 | |||
| 39 | let iv = GenericSx126xInterfaceVariant::new(nss, reset, dio1, busy, None, None).unwrap(); | ||
| 40 | |||
| 41 | let mut delay = Delay; | ||
| 42 | |||
| 43 | let lora = { | ||
| 44 | match LoRa::new( | ||
| 45 | SX1261_2::new(BoardType::RpPicoWaveshareSx1262, spi, iv), | ||
| 46 | true, | ||
| 47 | &mut delay, | ||
| 48 | ) | ||
| 49 | .await | ||
| 50 | { | ||
| 51 | Ok(l) => l, | ||
| 52 | Err(err) => { | ||
| 53 | info!("Radio error = {}", err); | ||
| 54 | return; | ||
| 55 | } | ||
| 56 | } | ||
| 57 | }; | ||
| 58 | |||
| 59 | let radio = LoRaRadio::new(lora); | ||
| 60 | let region: region::Configuration = region::Configuration::new(LORAWAN_REGION); | ||
| 61 | let mut device: Device<_, Crypto, _, _> = Device::new(region, radio, LoraTimer::new(), embassy_rp::clocks::RoscRng); | ||
| 62 | |||
| 63 | defmt::info!("Joining LoRaWAN network"); | ||
| 64 | |||
| 65 | // TODO: Adjust the EUI and Keys according to your network credentials | ||
| 66 | match device | ||
| 67 | .join(&JoinMode::OTAA { | ||
| 68 | deveui: [0, 0, 0, 0, 0, 0, 0, 0], | ||
| 69 | appeui: [0, 0, 0, 0, 0, 0, 0, 0], | ||
| 70 | appkey: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
| 71 | }) | ||
| 72 | .await | ||
| 73 | { | ||
| 74 | Ok(()) => defmt::info!("LoRaWAN network joined"), | ||
| 75 | Err(err) => { | ||
| 76 | info!("Radio error = {}", err); | ||
| 77 | return; | ||
| 78 | } | ||
| 79 | }; | ||
| 80 | } | ||
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 000000000..250419202 --- /dev/null +++ b/examples/rp/src/bin/lora_p2p_receive.rs | |||
| @@ -0,0 +1,115 @@ | |||
| 1 | //! This example runs on the Raspberry Pi Pico with a Waveshare board containing a 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)] | ||
| 7 | |||
| 8 | use defmt::*; | ||
| 9 | use embassy_executor::Spawner; | ||
| 10 | use embassy_lora::iv::GenericSx126xInterfaceVariant; | ||
| 11 | use embassy_rp::gpio::{Input, Level, Output, Pin, Pull}; | ||
| 12 | use embassy_rp::spi::{Config, Spi}; | ||
| 13 | use embassy_time::{Delay, Duration, Timer}; | ||
| 14 | use lora_phy::mod_params::*; | ||
| 15 | use lora_phy::sx1261_2::SX1261_2; | ||
| 16 | use lora_phy::LoRa; | ||
| 17 | use {defmt_rtt as _, panic_probe as _}; | ||
| 18 | |||
| 19 | const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region | ||
| 20 | |||
| 21 | #[embassy_executor::main] | ||
| 22 | async fn main(_spawner: Spawner) { | ||
| 23 | let p = embassy_rp::init(Default::default()); | ||
| 24 | |||
| 25 | let miso = p.PIN_12; | ||
| 26 | let mosi = p.PIN_11; | ||
| 27 | let clk = p.PIN_10; | ||
| 28 | let spi = Spi::new(p.SPI1, clk, mosi, miso, p.DMA_CH0, p.DMA_CH1, Config::default()); | ||
| 29 | |||
| 30 | let nss = Output::new(p.PIN_3.degrade(), Level::High); | ||
| 31 | let reset = Output::new(p.PIN_15.degrade(), Level::High); | ||
| 32 | let dio1 = Input::new(p.PIN_20.degrade(), Pull::None); | ||
| 33 | let busy = Input::new(p.PIN_2.degrade(), Pull::None); | ||
| 34 | |||
| 35 | let iv = GenericSx126xInterfaceVariant::new(nss, reset, dio1, busy, None, None).unwrap(); | ||
| 36 | |||
| 37 | let mut delay = Delay; | ||
| 38 | |||
| 39 | let mut lora = { | ||
| 40 | match LoRa::new( | ||
| 41 | SX1261_2::new(BoardType::RpPicoWaveshareSx1262, spi, iv), | ||
| 42 | false, | ||
| 43 | &mut delay, | ||
| 44 | ) | ||
| 45 | .await | ||
| 46 | { | ||
| 47 | Ok(l) => l, | ||
| 48 | Err(err) => { | ||
| 49 | info!("Radio error = {}", err); | ||
| 50 | return; | ||
| 51 | } | ||
| 52 | } | ||
| 53 | }; | ||
| 54 | |||
| 55 | let mut debug_indicator = Output::new(p.PIN_25, Level::Low); | ||
| 56 | |||
| 57 | let mut receiving_buffer = [00u8; 100]; | ||
| 58 | |||
| 59 | let mdltn_params = { | ||
| 60 | match lora.create_modulation_params( | ||
| 61 | SpreadingFactor::_10, | ||
| 62 | Bandwidth::_250KHz, | ||
| 63 | CodingRate::_4_8, | ||
| 64 | LORA_FREQUENCY_IN_HZ, | ||
| 65 | ) { | ||
| 66 | Ok(mp) => mp, | ||
| 67 | Err(err) => { | ||
| 68 | info!("Radio error = {}", err); | ||
| 69 | return; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | }; | ||
| 73 | |||
| 74 | let rx_pkt_params = { | ||
| 75 | match lora.create_rx_packet_params(4, false, receiving_buffer.len() as u8, true, false, &mdltn_params) { | ||
| 76 | Ok(pp) => pp, | ||
| 77 | Err(err) => { | ||
| 78 | info!("Radio error = {}", err); | ||
| 79 | return; | ||
| 80 | } | ||
| 81 | } | ||
| 82 | }; | ||
| 83 | |||
| 84 | match lora | ||
| 85 | .prepare_for_rx(&mdltn_params, &rx_pkt_params, None, true, false, 0, 0x00ffffffu32) | ||
| 86 | .await | ||
| 87 | { | ||
| 88 | Ok(()) => {} | ||
| 89 | Err(err) => { | ||
| 90 | info!("Radio error = {}", err); | ||
| 91 | return; | ||
| 92 | } | ||
| 93 | }; | ||
| 94 | |||
| 95 | loop { | ||
| 96 | receiving_buffer = [00u8; 100]; | ||
| 97 | match lora.rx(&rx_pkt_params, &mut receiving_buffer).await { | ||
| 98 | Ok((received_len, _rx_pkt_status)) => { | ||
| 99 | if (received_len == 3) | ||
| 100 | && (receiving_buffer[0] == 0x01u8) | ||
| 101 | && (receiving_buffer[1] == 0x02u8) | ||
| 102 | && (receiving_buffer[2] == 0x03u8) | ||
| 103 | { | ||
| 104 | info!("rx successful"); | ||
| 105 | debug_indicator.set_high(); | ||
| 106 | Timer::after(Duration::from_secs(5)).await; | ||
| 107 | debug_indicator.set_low(); | ||
| 108 | } else { | ||
| 109 | info!("rx unknown packet"); | ||
| 110 | } | ||
| 111 | } | ||
| 112 | Err(err) => info!("rx unsuccessful = {}", err), | ||
| 113 | } | ||
| 114 | } | ||
| 115 | } | ||
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 000000000..3a0544b17 --- /dev/null +++ b/examples/rp/src/bin/lora_p2p_send.rs | |||
| @@ -0,0 +1,103 @@ | |||
| 1 | //! This example runs on the Raspberry Pi Pico with a Waveshare board containing a 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)] | ||
| 7 | |||
| 8 | use defmt::*; | ||
| 9 | use embassy_executor::Spawner; | ||
| 10 | use embassy_lora::iv::GenericSx126xInterfaceVariant; | ||
| 11 | use embassy_rp::gpio::{Input, Level, Output, Pin, Pull}; | ||
| 12 | use embassy_rp::spi::{Config, Spi}; | ||
| 13 | use embassy_time::Delay; | ||
| 14 | use lora_phy::mod_params::*; | ||
| 15 | use lora_phy::sx1261_2::SX1261_2; | ||
| 16 | use lora_phy::LoRa; | ||
| 17 | use {defmt_rtt as _, panic_probe as _}; | ||
| 18 | |||
| 19 | const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region | ||
| 20 | |||
| 21 | #[embassy_executor::main] | ||
| 22 | async fn main(_spawner: Spawner) { | ||
| 23 | let p = embassy_rp::init(Default::default()); | ||
| 24 | |||
| 25 | let miso = p.PIN_12; | ||
| 26 | let mosi = p.PIN_11; | ||
| 27 | let clk = p.PIN_10; | ||
| 28 | let spi = Spi::new(p.SPI1, clk, mosi, miso, p.DMA_CH0, p.DMA_CH1, Config::default()); | ||
| 29 | |||
| 30 | let nss = Output::new(p.PIN_3.degrade(), Level::High); | ||
| 31 | let reset = Output::new(p.PIN_15.degrade(), Level::High); | ||
| 32 | let dio1 = Input::new(p.PIN_20.degrade(), Pull::None); | ||
| 33 | let busy = Input::new(p.PIN_2.degrade(), Pull::None); | ||
| 34 | |||
| 35 | let iv = GenericSx126xInterfaceVariant::new(nss, reset, dio1, busy, None, None).unwrap(); | ||
| 36 | |||
| 37 | let mut delay = Delay; | ||
| 38 | |||
| 39 | let mut lora = { | ||
| 40 | match LoRa::new( | ||
| 41 | SX1261_2::new(BoardType::RpPicoWaveshareSx1262, spi, iv), | ||
| 42 | false, | ||
| 43 | &mut delay, | ||
| 44 | ) | ||
| 45 | .await | ||
| 46 | { | ||
| 47 | Ok(l) => l, | ||
| 48 | Err(err) => { | ||
| 49 | info!("Radio error = {}", err); | ||
| 50 | return; | ||
| 51 | } | ||
| 52 | } | ||
| 53 | }; | ||
| 54 | |||
| 55 | let mdltn_params = { | ||
| 56 | match lora.create_modulation_params( | ||
| 57 | SpreadingFactor::_10, | ||
| 58 | Bandwidth::_250KHz, | ||
| 59 | CodingRate::_4_8, | ||
| 60 | LORA_FREQUENCY_IN_HZ, | ||
| 61 | ) { | ||
| 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 | } | ||
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 000000000..5585606d8 --- /dev/null +++ b/examples/rp/src/bin/lora_p2p_send_multicore.rs | |||
| @@ -0,0 +1,139 @@ | |||
| 1 | //! This example runs on the Raspberry Pi Pico with a Waveshare board containing a Semtech Sx1262 radio. | ||
| 2 | //! It demonstrates LORA P2P send functionality using the second core, with data provided by the first core. | ||
| 3 | #![no_std] | ||
| 4 | #![no_main] | ||
| 5 | #![macro_use] | ||
| 6 | #![feature(type_alias_impl_trait)] | ||
| 7 | |||
| 8 | use defmt::*; | ||
| 9 | use embassy_executor::Executor; | ||
| 10 | use embassy_executor::_export::StaticCell; | ||
| 11 | use embassy_lora::iv::GenericSx126xInterfaceVariant; | ||
| 12 | use embassy_rp::gpio::{AnyPin, Input, Level, Output, Pin, Pull}; | ||
| 13 | use embassy_rp::multicore::{spawn_core1, Stack}; | ||
| 14 | use embassy_rp::peripherals::SPI1; | ||
| 15 | use embassy_rp::spi::{Async, Config, Spi}; | ||
| 16 | use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; | ||
| 17 | use embassy_sync::channel::Channel; | ||
| 18 | use embassy_time::{Delay, Duration, Timer}; | ||
| 19 | use lora_phy::mod_params::*; | ||
| 20 | use lora_phy::sx1261_2::SX1261_2; | ||
| 21 | use lora_phy::LoRa; | ||
| 22 | use {defmt_rtt as _, panic_probe as _}; | ||
| 23 | |||
| 24 | static mut CORE1_STACK: Stack<4096> = Stack::new(); | ||
| 25 | static EXECUTOR0: StaticCell<Executor> = StaticCell::new(); | ||
| 26 | static EXECUTOR1: StaticCell<Executor> = StaticCell::new(); | ||
| 27 | static CHANNEL: Channel<CriticalSectionRawMutex, [u8; 3], 1> = Channel::new(); | ||
| 28 | |||
| 29 | const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region | ||
| 30 | |||
| 31 | #[cortex_m_rt::entry] | ||
| 32 | fn main() -> ! { | ||
| 33 | let p = embassy_rp::init(Default::default()); | ||
| 34 | |||
| 35 | let miso = p.PIN_12; | ||
| 36 | let mosi = p.PIN_11; | ||
| 37 | let clk = p.PIN_10; | ||
| 38 | let spi = Spi::new(p.SPI1, clk, mosi, miso, p.DMA_CH0, p.DMA_CH1, Config::default()); | ||
| 39 | |||
| 40 | let nss = Output::new(p.PIN_3.degrade(), Level::High); | ||
| 41 | let reset = Output::new(p.PIN_15.degrade(), Level::High); | ||
| 42 | let dio1 = Input::new(p.PIN_20.degrade(), Pull::None); | ||
| 43 | let busy = Input::new(p.PIN_2.degrade(), Pull::None); | ||
| 44 | |||
| 45 | let iv = GenericSx126xInterfaceVariant::new(nss, reset, dio1, busy, None, None).unwrap(); | ||
| 46 | |||
| 47 | spawn_core1(p.CORE1, unsafe { &mut CORE1_STACK }, move || { | ||
| 48 | let executor1 = EXECUTOR1.init(Executor::new()); | ||
| 49 | executor1.run(|spawner| unwrap!(spawner.spawn(core1_task(spi, iv)))); | ||
| 50 | }); | ||
| 51 | |||
| 52 | let executor0 = EXECUTOR0.init(Executor::new()); | ||
| 53 | executor0.run(|spawner| unwrap!(spawner.spawn(core0_task()))); | ||
| 54 | } | ||
| 55 | |||
| 56 | #[embassy_executor::task] | ||
| 57 | async fn core0_task() { | ||
| 58 | info!("Hello from core 0"); | ||
| 59 | loop { | ||
| 60 | CHANNEL.send([0x01u8, 0x02u8, 0x03u8]).await; | ||
| 61 | Timer::after(Duration::from_millis(60 * 1000)).await; | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | #[embassy_executor::task] | ||
| 66 | async fn core1_task( | ||
| 67 | spi: Spi<'static, SPI1, Async>, | ||
| 68 | iv: GenericSx126xInterfaceVariant<Output<'static, AnyPin>, Input<'static, AnyPin>>, | ||
| 69 | ) { | ||
| 70 | info!("Hello from core 1"); | ||
| 71 | let mut delay = Delay; | ||
| 72 | |||
| 73 | let mut lora = { | ||
| 74 | match LoRa::new( | ||
| 75 | SX1261_2::new(BoardType::RpPicoWaveshareSx1262, spi, iv), | ||
| 76 | false, | ||
| 77 | &mut delay, | ||
| 78 | ) | ||
| 79 | .await | ||
| 80 | { | ||
| 81 | Ok(l) => l, | ||
| 82 | Err(err) => { | ||
| 83 | info!("Radio error = {}", err); | ||
| 84 | return; | ||
| 85 | } | ||
| 86 | } | ||
| 87 | }; | ||
| 88 | |||
| 89 | let mdltn_params = { | ||
| 90 | match lora.create_modulation_params( | ||
| 91 | SpreadingFactor::_10, | ||
| 92 | Bandwidth::_250KHz, | ||
| 93 | CodingRate::_4_8, | ||
| 94 | LORA_FREQUENCY_IN_HZ, | ||
| 95 | ) { | ||
| 96 | Ok(mp) => mp, | ||
| 97 | Err(err) => { | ||
| 98 | info!("Radio error = {}", err); | ||
| 99 | return; | ||
| 100 | } | ||
| 101 | } | ||
| 102 | }; | ||
| 103 | |||
| 104 | let mut tx_pkt_params = { | ||
| 105 | match lora.create_tx_packet_params(4, false, true, false, &mdltn_params) { | ||
| 106 | Ok(pp) => pp, | ||
| 107 | Err(err) => { | ||
| 108 | info!("Radio error = {}", err); | ||
| 109 | return; | ||
| 110 | } | ||
| 111 | } | ||
| 112 | }; | ||
| 113 | |||
| 114 | loop { | ||
| 115 | let buffer: [u8; 3] = CHANNEL.recv().await; | ||
| 116 | match lora.prepare_for_tx(&mdltn_params, 20, false).await { | ||
| 117 | Ok(()) => {} | ||
| 118 | Err(err) => { | ||
| 119 | info!("Radio error = {}", err); | ||
| 120 | return; | ||
| 121 | } | ||
| 122 | }; | ||
| 123 | |||
| 124 | match lora.tx(&mdltn_params, &mut tx_pkt_params, &buffer, 0xffffff).await { | ||
| 125 | Ok(()) => { | ||
| 126 | info!("TX DONE"); | ||
| 127 | } | ||
| 128 | Err(err) => { | ||
| 129 | info!("Radio error = {}", err); | ||
| 130 | return; | ||
| 131 | } | ||
| 132 | }; | ||
| 133 | |||
| 134 | match lora.sleep(&mut delay).await { | ||
| 135 | Ok(()) => info!("Sleep successful"), | ||
| 136 | Err(err) => info!("Sleep unsuccessful = {}", err), | ||
| 137 | } | ||
| 138 | } | ||
| 139 | } | ||
diff --git a/examples/stm32l0/Cargo.toml b/examples/stm32l0/Cargo.toml index d08e2b61a..ca022e254 100644 --- a/examples/stm32l0/Cargo.toml +++ b/examples/stm32l0/Cargo.toml | |||
| @@ -6,17 +6,18 @@ license = "MIT OR Apache-2.0" | |||
| 6 | 6 | ||
| 7 | [features] | 7 | [features] |
| 8 | default = ["nightly"] | 8 | default = ["nightly"] |
| 9 | nightly = ["embassy-stm32/nightly", "embassy-lora", "lorawan-device", "lorawan", "embedded-io/async"] | 9 | nightly = ["embassy-stm32/nightly", "embassy-time/nightly", "embassy-time/unstable-traits", |
| 10 | "embassy-lora", "lora-phy", "lorawan-device", "lorawan", "embedded-io/async"] | ||
| 10 | 11 | ||
| 11 | [dependencies] | 12 | [dependencies] |
| 12 | embassy-sync = { version = "0.2.0", path = "../../embassy-sync", features = ["defmt"] } | 13 | embassy-sync = { version = "0.2.0", path = "../../embassy-sync", features = ["defmt"] } |
| 13 | embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] } | 14 | embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] } |
| 14 | embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } | 15 | embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } |
| 15 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "stm32l072cz", "time-driver-any", "exti", "unstable-traits", "memory-x"] } | 16 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "stm32l072cz", "time-driver-any", "exti", "unstable-traits", "memory-x"] } |
| 16 | embassy-lora = { version = "0.1.0", path = "../../embassy-lora", features = ["sx127x", "time", "defmt"], optional = true} | 17 | embassy-lora = { version = "0.1.0", path = "../../embassy-lora", features = ["sx127x", "time", "defmt", "external-lora-phy"], optional = true } |
| 17 | 18 | lora-phy = { version = "1", optional = true } | |
| 18 | lorawan-device = { version = "0.9.0", default-features = false, features = ["async"], optional = true } | 19 | lorawan-device = { version = "0.10.0", default-features = false, features = ["async", "external-lora-phy"], optional = true } |
| 19 | lorawan = { version = "0.7.2", default-features = false, features = ["default-crypto"], optional = true } | 20 | lorawan = { version = "0.7.3", default-features = false, features = ["default-crypto"], optional = true } |
| 20 | 21 | ||
| 21 | defmt = "0.3" | 22 | defmt = "0.3" |
| 22 | defmt-rtt = "0.4" | 23 | defmt-rtt = "0.4" |
diff --git a/examples/stm32l0/src/bin/lora_cad.rs b/examples/stm32l0/src/bin/lora_cad.rs new file mode 100644 index 000000000..588cea1e5 --- /dev/null +++ b/examples/stm32l0/src/bin/lora_cad.rs | |||
| @@ -0,0 +1,105 @@ | |||
| 1 | //! This example runs on the STM32 LoRa Discovery board, which has a builtin Semtech Sx1276 radio. | ||
| 2 | //! It demonstrates LORA P2P CAD functionality. | ||
| 3 | #![no_std] | ||
| 4 | #![no_main] | ||
| 5 | #![macro_use] | ||
| 6 | #![feature(type_alias_impl_trait)] | ||
| 7 | |||
| 8 | use defmt::*; | ||
| 9 | use embassy_executor::Spawner; | ||
| 10 | use embassy_lora::iv::Stm32l0InterfaceVariant; | ||
| 11 | use embassy_stm32::exti::{Channel, ExtiInput}; | ||
| 12 | use embassy_stm32::gpio::{Input, Level, Output, Pin, Pull, Speed}; | ||
| 13 | use embassy_stm32::spi; | ||
| 14 | use embassy_stm32::time::khz; | ||
| 15 | use embassy_time::{Delay, Duration, Timer}; | ||
| 16 | use lora_phy::mod_params::*; | ||
| 17 | use lora_phy::sx1276_7_8_9::SX1276_7_8_9; | ||
| 18 | use lora_phy::LoRa; | ||
| 19 | use {defmt_rtt as _, panic_probe as _}; | ||
| 20 | |||
| 21 | const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region | ||
| 22 | |||
| 23 | #[embassy_executor::main] | ||
| 24 | async fn main(_spawner: Spawner) { | ||
| 25 | let mut config = embassy_stm32::Config::default(); | ||
| 26 | config.rcc.mux = embassy_stm32::rcc::ClockSrc::HSI16; | ||
| 27 | config.rcc.enable_hsi48 = true; | ||
| 28 | let p = embassy_stm32::init(config); | ||
| 29 | |||
| 30 | // SPI for sx1276 | ||
| 31 | let spi = spi::Spi::new( | ||
| 32 | p.SPI1, | ||
| 33 | p.PB3, | ||
| 34 | p.PA7, | ||
| 35 | p.PA6, | ||
| 36 | p.DMA1_CH3, | ||
| 37 | p.DMA1_CH2, | ||
| 38 | khz(200), | ||
| 39 | spi::Config::default(), | ||
| 40 | ); | ||
| 41 | |||
| 42 | let nss = Output::new(p.PA15.degrade(), Level::High, Speed::Low); | ||
| 43 | let reset = Output::new(p.PC0.degrade(), Level::High, Speed::Low); | ||
| 44 | |||
| 45 | let irq_pin = Input::new(p.PB4.degrade(), Pull::Up); | ||
| 46 | let irq = ExtiInput::new(irq_pin, p.EXTI4.degrade()); | ||
| 47 | |||
| 48 | let iv = Stm32l0InterfaceVariant::new(nss, reset, irq, None, None).unwrap(); | ||
| 49 | |||
| 50 | let mut delay = Delay; | ||
| 51 | |||
| 52 | let mut lora = { | ||
| 53 | match LoRa::new(SX1276_7_8_9::new(BoardType::Stm32l0Sx1276, 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.PB5, Level::Low, Speed::Low); | ||
| 63 | let mut start_indicator = Output::new(p.PB6, 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 mdltn_params = { | ||
| 70 | match lora.create_modulation_params( | ||
| 71 | SpreadingFactor::_10, | ||
| 72 | Bandwidth::_250KHz, | ||
| 73 | CodingRate::_4_8, | ||
| 74 | LORA_FREQUENCY_IN_HZ, | ||
| 75 | ) { | ||
| 76 | Ok(mp) => mp, | ||
| 77 | Err(err) => { | ||
| 78 | info!("Radio error = {}", err); | ||
| 79 | return; | ||
| 80 | } | ||
| 81 | } | ||
| 82 | }; | ||
| 83 | |||
| 84 | match lora.prepare_for_cad(&mdltn_params, true).await { | ||
| 85 | Ok(()) => {} | ||
| 86 | Err(err) => { | ||
| 87 | info!("Radio error = {}", err); | ||
| 88 | return; | ||
| 89 | } | ||
| 90 | }; | ||
| 91 | |||
| 92 | match lora.cad().await { | ||
| 93 | Ok(cad_activity_detected) => { | ||
| 94 | if cad_activity_detected { | ||
| 95 | info!("cad successful with activity detected") | ||
| 96 | } else { | ||
| 97 | info!("cad successful without activity detected") | ||
| 98 | } | ||
| 99 | debug_indicator.set_high(); | ||
| 100 | Timer::after(Duration::from_secs(5)).await; | ||
| 101 | debug_indicator.set_low(); | ||
| 102 | } | ||
| 103 | Err(err) => info!("cad unsuccessful = {}", err), | ||
| 104 | } | ||
| 105 | } | ||
diff --git a/examples/stm32l0/src/bin/lora_lorawan.rs b/examples/stm32l0/src/bin/lora_lorawan.rs new file mode 100644 index 000000000..c397edd58 --- /dev/null +++ b/examples/stm32l0/src/bin/lora_lorawan.rs | |||
| @@ -0,0 +1,88 @@ | |||
| 1 | //! This example runs on the STM32 LoRa Discovery board, which has a builtin Semtech Sx1276 radio. | ||
| 2 | //! It demonstrates LoRaWAN join functionality. | ||
| 3 | #![no_std] | ||
| 4 | #![no_main] | ||
| 5 | #![macro_use] | ||
| 6 | #![feature(type_alias_impl_trait)] | ||
| 7 | |||
| 8 | use defmt::*; | ||
| 9 | use embassy_executor::Spawner; | ||
| 10 | use embassy_lora::iv::Stm32l0InterfaceVariant; | ||
| 11 | use embassy_lora::LoraTimer; | ||
| 12 | use embassy_stm32::exti::{Channel, ExtiInput}; | ||
| 13 | use embassy_stm32::gpio::{Input, Level, Output, Pin, Pull, Speed}; | ||
| 14 | use embassy_stm32::rng::Rng; | ||
| 15 | use embassy_stm32::spi; | ||
| 16 | use embassy_stm32::time::khz; | ||
| 17 | use embassy_time::Delay; | ||
| 18 | use lora_phy::mod_params::*; | ||
| 19 | use lora_phy::sx1276_7_8_9::SX1276_7_8_9; | ||
| 20 | use lora_phy::LoRa; | ||
| 21 | use lorawan::default_crypto::DefaultFactory as Crypto; | ||
| 22 | use lorawan_device::async_device::lora_radio::LoRaRadio; | ||
| 23 | use lorawan_device::async_device::{region, Device, JoinMode}; | ||
| 24 | use {defmt_rtt as _, panic_probe as _}; | ||
| 25 | |||
| 26 | const LORAWAN_REGION: region::Region = region::Region::EU868; // 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::HSI16; | ||
| 32 | config.rcc.enable_hsi48 = true; | ||
| 33 | let p = embassy_stm32::init(config); | ||
| 34 | |||
| 35 | // SPI for sx1276 | ||
| 36 | let spi = spi::Spi::new( | ||
| 37 | p.SPI1, | ||
| 38 | p.PB3, | ||
| 39 | p.PA7, | ||
| 40 | p.PA6, | ||
| 41 | p.DMA1_CH3, | ||
| 42 | p.DMA1_CH2, | ||
| 43 | khz(200), | ||
| 44 | spi::Config::default(), | ||
| 45 | ); | ||
| 46 | |||
| 47 | let nss = Output::new(p.PA15.degrade(), Level::High, Speed::Low); | ||
| 48 | let reset = Output::new(p.PC0.degrade(), Level::High, Speed::Low); | ||
| 49 | |||
| 50 | let irq_pin = Input::new(p.PB4.degrade(), Pull::Up); | ||
| 51 | let irq = ExtiInput::new(irq_pin, p.EXTI4.degrade()); | ||
| 52 | |||
| 53 | let iv = Stm32l0InterfaceVariant::new(nss, reset, irq, None, None).unwrap(); | ||
| 54 | |||
| 55 | let mut delay = Delay; | ||
| 56 | |||
| 57 | let lora = { | ||
| 58 | match LoRa::new(SX1276_7_8_9::new(BoardType::Stm32l0Sx1276, spi, iv), true, &mut delay).await { | ||
| 59 | Ok(l) => l, | ||
| 60 | Err(err) => { | ||
| 61 | info!("Radio error = {}", err); | ||
| 62 | return; | ||
| 63 | } | ||
| 64 | } | ||
| 65 | }; | ||
| 66 | |||
| 67 | let radio = LoRaRadio::new(lora); | ||
| 68 | 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)); | ||
| 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/stm32l0/src/bin/lora_p2p_receive.rs b/examples/stm32l0/src/bin/lora_p2p_receive.rs new file mode 100644 index 000000000..bb7509509 --- /dev/null +++ b/examples/stm32l0/src/bin/lora_p2p_receive.rs | |||
| @@ -0,0 +1,127 @@ | |||
| 1 | //! This example runs on the STM32 LoRa Discovery board, which has a builtin Semtech Sx1276 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)] | ||
| 7 | |||
| 8 | use defmt::*; | ||
| 9 | use embassy_executor::Spawner; | ||
| 10 | use embassy_lora::iv::Stm32l0InterfaceVariant; | ||
| 11 | use embassy_stm32::exti::{Channel, ExtiInput}; | ||
| 12 | use embassy_stm32::gpio::{Input, Level, Output, Pin, Pull, Speed}; | ||
| 13 | use embassy_stm32::spi; | ||
| 14 | use embassy_stm32::time::khz; | ||
| 15 | use embassy_time::{Delay, Duration, Timer}; | ||
| 16 | use lora_phy::mod_params::*; | ||
| 17 | use lora_phy::sx1276_7_8_9::SX1276_7_8_9; | ||
| 18 | use lora_phy::LoRa; | ||
| 19 | use {defmt_rtt as _, panic_probe as _}; | ||
| 20 | |||
| 21 | const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region | ||
| 22 | |||
| 23 | #[embassy_executor::main] | ||
| 24 | async fn main(_spawner: Spawner) { | ||
| 25 | let mut config = embassy_stm32::Config::default(); | ||
| 26 | config.rcc.mux = embassy_stm32::rcc::ClockSrc::HSI16; | ||
| 27 | config.rcc.enable_hsi48 = true; | ||
| 28 | let p = embassy_stm32::init(config); | ||
| 29 | |||
| 30 | // SPI for sx1276 | ||
| 31 | let spi = spi::Spi::new( | ||
| 32 | p.SPI1, | ||
| 33 | p.PB3, | ||
| 34 | p.PA7, | ||
| 35 | p.PA6, | ||
| 36 | p.DMA1_CH3, | ||
| 37 | p.DMA1_CH2, | ||
| 38 | khz(200), | ||
| 39 | spi::Config::default(), | ||
| 40 | ); | ||
| 41 | |||
| 42 | let nss = Output::new(p.PA15.degrade(), Level::High, Speed::Low); | ||
| 43 | let reset = Output::new(p.PC0.degrade(), Level::High, Speed::Low); | ||
| 44 | |||
| 45 | let irq_pin = Input::new(p.PB4.degrade(), Pull::Up); | ||
| 46 | let irq = ExtiInput::new(irq_pin, p.EXTI4.degrade()); | ||
| 47 | |||
| 48 | let iv = Stm32l0InterfaceVariant::new(nss, reset, irq, None, None).unwrap(); | ||
| 49 | |||
| 50 | let mut delay = Delay; | ||
| 51 | |||
| 52 | let mut lora = { | ||
| 53 | match LoRa::new(SX1276_7_8_9::new(BoardType::Stm32l0Sx1276, 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.PB5, Level::Low, Speed::Low); | ||
| 63 | let mut start_indicator = Output::new(p.PB6, 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/stm32l0/src/bin/lora_p2p_send.rs b/examples/stm32l0/src/bin/lora_p2p_send.rs new file mode 100644 index 000000000..e6fadc01d --- /dev/null +++ b/examples/stm32l0/src/bin/lora_p2p_send.rs | |||
| @@ -0,0 +1,110 @@ | |||
| 1 | //! This example runs on the STM32 LoRa Discovery board, which has a builtin Semtech Sx1276 radio. | ||
| 2 | //! It demonstrates LORA P2P send functionality. | ||
| 3 | #![no_std] | ||
| 4 | #![no_main] | ||
| 5 | #![macro_use] | ||
| 6 | #![feature(type_alias_impl_trait)] | ||
| 7 | |||
| 8 | use defmt::*; | ||
| 9 | use embassy_executor::Spawner; | ||
| 10 | use embassy_lora::iv::Stm32l0InterfaceVariant; | ||
| 11 | use embassy_stm32::exti::{Channel, ExtiInput}; | ||
| 12 | use embassy_stm32::gpio::{Input, Level, Output, Pin, Pull, Speed}; | ||
| 13 | use embassy_stm32::spi; | ||
| 14 | use embassy_stm32::time::khz; | ||
| 15 | use embassy_time::Delay; | ||
| 16 | use lora_phy::mod_params::*; | ||
| 17 | use lora_phy::sx1276_7_8_9::SX1276_7_8_9; | ||
| 18 | use lora_phy::LoRa; | ||
| 19 | use {defmt_rtt as _, panic_probe as _}; | ||
| 20 | |||
| 21 | const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region | ||
| 22 | |||
| 23 | #[embassy_executor::main] | ||
| 24 | async fn main(_spawner: Spawner) { | ||
| 25 | let mut config = embassy_stm32::Config::default(); | ||
| 26 | config.rcc.mux = embassy_stm32::rcc::ClockSrc::HSI16; | ||
| 27 | config.rcc.enable_hsi48 = true; | ||
| 28 | let p = embassy_stm32::init(config); | ||
| 29 | |||
| 30 | // SPI for sx1276 | ||
| 31 | let spi = spi::Spi::new( | ||
| 32 | p.SPI1, | ||
| 33 | p.PB3, | ||
| 34 | p.PA7, | ||
| 35 | p.PA6, | ||
| 36 | p.DMA1_CH3, | ||
| 37 | p.DMA1_CH2, | ||
| 38 | khz(200), | ||
| 39 | spi::Config::default(), | ||
| 40 | ); | ||
| 41 | |||
| 42 | let nss = Output::new(p.PA15.degrade(), Level::High, Speed::Low); | ||
| 43 | let reset = Output::new(p.PC0.degrade(), Level::High, Speed::Low); | ||
| 44 | |||
| 45 | let irq_pin = Input::new(p.PB4.degrade(), Pull::Up); | ||
| 46 | let irq = ExtiInput::new(irq_pin, p.EXTI4.degrade()); | ||
| 47 | |||
| 48 | let iv = Stm32l0InterfaceVariant::new(nss, reset, irq, None, None).unwrap(); | ||
| 49 | |||
| 50 | let mut delay = Delay; | ||
| 51 | |||
| 52 | let mut lora = { | ||
| 53 | match LoRa::new(SX1276_7_8_9::new(BoardType::Stm32l0Sx1276, 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 mdltn_params = { | ||
| 63 | match lora.create_modulation_params( | ||
| 64 | SpreadingFactor::_10, | ||
| 65 | Bandwidth::_250KHz, | ||
| 66 | CodingRate::_4_8, | ||
| 67 | LORA_FREQUENCY_IN_HZ, | ||
| 68 | ) { | ||
| 69 | Ok(mp) => mp, | ||
| 70 | Err(err) => { | ||
| 71 | info!("Radio error = {}", err); | ||
| 72 | return; | ||
| 73 | } | ||
| 74 | } | ||
| 75 | }; | ||
| 76 | |||
| 77 | let mut tx_pkt_params = { | ||
| 78 | match lora.create_tx_packet_params(4, false, true, false, &mdltn_params) { | ||
| 79 | Ok(pp) => pp, | ||
| 80 | Err(err) => { | ||
| 81 | info!("Radio error = {}", err); | ||
| 82 | return; | ||
| 83 | } | ||
| 84 | } | ||
| 85 | }; | ||
| 86 | |||
| 87 | match lora.prepare_for_tx(&mdltn_params, 17, true).await { | ||
| 88 | Ok(()) => {} | ||
| 89 | Err(err) => { | ||
| 90 | info!("Radio error = {}", err); | ||
| 91 | return; | ||
| 92 | } | ||
| 93 | }; | ||
| 94 | |||
| 95 | let buffer = [0x01u8, 0x02u8, 0x03u8]; | ||
| 96 | match lora.tx(&mdltn_params, &mut tx_pkt_params, &buffer, 0xffffff).await { | ||
| 97 | Ok(()) => { | ||
| 98 | info!("TX DONE"); | ||
| 99 | } | ||
| 100 | Err(err) => { | ||
| 101 | info!("Radio error = {}", err); | ||
| 102 | return; | ||
| 103 | } | ||
| 104 | }; | ||
| 105 | |||
| 106 | match lora.sleep(&mut delay).await { | ||
| 107 | Ok(()) => info!("Sleep successful"), | ||
| 108 | Err(err) => info!("Sleep unsuccessful = {}", err), | ||
| 109 | } | ||
| 110 | } | ||
diff --git a/examples/stm32l0/src/bin/lorawan.rs b/examples/stm32l0/src/bin/lorawan.rs deleted file mode 100644 index ea01f610c..000000000 --- a/examples/stm32l0/src/bin/lorawan.rs +++ /dev/null | |||
| @@ -1,74 +0,0 @@ | |||
| 1 | //! This example runs on the STM32 LoRa Discovery board which has a builtin Semtech Sx127x radio | ||
| 2 | #![no_std] | ||
| 3 | #![no_main] | ||
| 4 | #![macro_use] | ||
| 5 | #![allow(dead_code)] | ||
| 6 | #![feature(type_alias_impl_trait)] | ||
| 7 | |||
| 8 | use embassy_executor::Spawner; | ||
| 9 | use embassy_lora::sx127x::*; | ||
| 10 | use embassy_lora::LoraTimer; | ||
| 11 | use embassy_stm32::exti::ExtiInput; | ||
| 12 | use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; | ||
| 13 | use embassy_stm32::rng::Rng; | ||
| 14 | use embassy_stm32::spi; | ||
| 15 | use embassy_stm32::time::khz; | ||
| 16 | use lorawan::default_crypto::DefaultFactory as Crypto; | ||
| 17 | use lorawan_device::async_device::{region, Device, JoinMode}; | ||
| 18 | use {defmt_rtt as _, panic_probe as _}; | ||
| 19 | |||
| 20 | #[embassy_executor::main] | ||
| 21 | async fn main(_spawner: Spawner) { | ||
| 22 | let mut config = embassy_stm32::Config::default(); | ||
| 23 | config.rcc.mux = embassy_stm32::rcc::ClockSrc::HSI16; | ||
| 24 | config.rcc.enable_hsi48 = true; | ||
| 25 | let p = embassy_stm32::init(config); | ||
| 26 | |||
| 27 | // SPI for sx127x | ||
| 28 | let spi = spi::Spi::new( | ||
| 29 | p.SPI1, | ||
| 30 | p.PB3, | ||
| 31 | p.PA7, | ||
| 32 | p.PA6, | ||
| 33 | p.DMA1_CH3, | ||
| 34 | p.DMA1_CH2, | ||
| 35 | khz(200), | ||
| 36 | spi::Config::default(), | ||
| 37 | ); | ||
| 38 | |||
| 39 | let cs = Output::new(p.PA15, Level::High, Speed::Low); | ||
| 40 | let reset = Output::new(p.PC0, Level::High, Speed::Low); | ||
| 41 | let _ = Input::new(p.PB1, Pull::None); | ||
| 42 | |||
| 43 | let ready = Input::new(p.PB4, Pull::Up); | ||
| 44 | let ready_pin = ExtiInput::new(ready, p.EXTI4); | ||
| 45 | |||
| 46 | let radio = Sx127xRadio::new(spi, cs, reset, ready_pin, DummySwitch).await.unwrap(); | ||
| 47 | |||
| 48 | let region = region::Configuration::new(region::Region::EU868); | ||
| 49 | let mut device: Device<_, Crypto, _, _> = Device::new(region, radio, LoraTimer::new(), Rng::new(p.RNG)); | ||
| 50 | |||
| 51 | defmt::info!("Joining LoRaWAN network"); | ||
| 52 | |||
| 53 | // TODO: Adjust the EUI and Keys according to your network credentials | ||
| 54 | device | ||
| 55 | .join(&JoinMode::OTAA { | ||
| 56 | deveui: [0, 0, 0, 0, 0, 0, 0, 0], | ||
| 57 | appeui: [0, 0, 0, 0, 0, 0, 0, 0], | ||
| 58 | appkey: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
| 59 | }) | ||
| 60 | .await | ||
| 61 | .ok() | ||
| 62 | .unwrap(); | ||
| 63 | defmt::info!("LoRaWAN network joined"); | ||
| 64 | |||
| 65 | defmt::info!("Sending 'PING'"); | ||
| 66 | device.send(b"PING", 1, false).await.ok().unwrap(); | ||
| 67 | defmt::info!("Message sent!"); | ||
| 68 | } | ||
| 69 | |||
| 70 | pub struct DummySwitch; | ||
| 71 | impl RadioSwitch for DummySwitch { | ||
| 72 | fn set_rx(&mut self) {} | ||
| 73 | fn set_tx(&mut self) {} | ||
| 74 | } | ||
diff --git a/examples/stm32wl/Cargo.toml b/examples/stm32wl/Cargo.toml index 07f136b40..0eb24bc44 100644 --- a/examples/stm32wl/Cargo.toml +++ b/examples/stm32wl/Cargo.toml | |||
| @@ -7,12 +7,13 @@ license = "MIT OR Apache-2.0" | |||
| 7 | [dependencies] | 7 | [dependencies] |
| 8 | embassy-sync = { version = "0.2.0", path = "../../embassy-sync", features = ["defmt"] } | 8 | embassy-sync = { version = "0.2.0", path = "../../embassy-sync", features = ["defmt"] } |
| 9 | embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] } | 9 | embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] } |
| 10 | embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } | 10 | embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["nightly", "unstable-traits", "defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } |
| 11 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "stm32wl55jc-cm4", "time-driver-any", "memory-x", "unstable-pac", "exti"] } | 11 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "unstable-traits", "defmt", "stm32wl55jc-cm4", "time-driver-any", "memory-x", "unstable-pac", "exti"] } |
| 12 | embassy-lora = { version = "0.1.0", path = "../../embassy-lora", features = ["stm32wl", "time", "defmt"] } | 12 | embassy-embedded-hal = {version = "0.1.0", path = "../../embassy-embedded-hal" } |
| 13 | 13 | embassy-lora = { version = "0.1.0", path = "../../embassy-lora", features = ["stm32wl", "time", "defmt", "external-lora-phy"] } | |
| 14 | lorawan-device = { version = "0.9.0", default-features = false, features = ["async"] } | 14 | lora-phy = { version = "1" } |
| 15 | lorawan = { version = "0.7.2", default-features = false, features = ["default-crypto"] } | 15 | lorawan-device = { version = "0.10.0", default-features = false, features = ["async", "external-lora-phy"] } |
| 16 | lorawan = { version = "0.7.3", default-features = false, features = ["default-crypto"] } | ||
| 16 | 17 | ||
| 17 | defmt = "0.3" | 18 | defmt = "0.3" |
| 18 | defmt-rtt = "0.4" | 19 | defmt-rtt = "0.4" |
diff --git a/examples/stm32wl/src/bin/lora_lorawan.rs b/examples/stm32wl/src/bin/lora_lorawan.rs new file mode 100644 index 000000000..4bcc5420e --- /dev/null +++ b/examples/stm32wl/src/bin/lora_lorawan.rs | |||
| @@ -0,0 +1,90 @@ | |||
| 1 | //! This example runs on a STM32WL board, which has a builtin Semtech Sx1262 radio. | ||
| 2 | //! It demonstrates LoRaWAN join 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 | const LORAWAN_REGION: region::Region = region::Region::EU868; // warning: set this appropriately for the region | ||
| 32 | |||
| 33 | #[embassy_executor::main] | ||
| 34 | async fn main(_spawner: Spawner) { | ||
| 35 | let mut config = embassy_stm32::Config::default(); | ||
| 36 | config.rcc.mux = embassy_stm32::rcc::ClockSrc::HSI16; | ||
| 37 | config.rcc.enable_lsi = true; | ||
| 38 | let p = embassy_stm32::init(config); | ||
| 39 | |||
| 40 | unsafe { pac::RCC.ccipr().modify(|w| w.set_rngsel(0b01)) } | ||
| 41 | |||
| 42 | let clk = Hertz(core::cmp::min(SUBGHZSPI::frequency().0 / 2, 16_000_000)); | ||
| 43 | let mut spi_config = SpiConfig::default(); | ||
| 44 | spi_config.mode = MODE_0; | ||
| 45 | spi_config.bit_order = BitOrder::MsbFirst; | ||
| 46 | let spi = Spi::new_subghz(p.SUBGHZSPI, NoDma, NoDma, clk, spi_config); | ||
| 47 | |||
| 48 | let spi = BlockingAsync::new(spi); | ||
| 49 | |||
| 50 | let irq = interrupt::take!(SUBGHZ_RADIO); | ||
| 51 | into_ref!(irq); | ||
| 52 | // Set CTRL1 and CTRL3 for high-power transmission, while CTRL2 acts as an RF switch between tx and rx | ||
| 53 | let _ctrl1 = Output::new(p.PC4.degrade(), Level::Low, Speed::High); | ||
| 54 | let ctrl2 = Output::new(p.PC5.degrade(), Level::High, Speed::High); | ||
| 55 | let _ctrl3 = Output::new(p.PC3.degrade(), Level::High, Speed::High); | ||
| 56 | let iv = Stm32wlInterfaceVariant::new(irq, None, Some(ctrl2)).unwrap(); | ||
| 57 | |||
| 58 | let mut delay = Delay; | ||
| 59 | |||
| 60 | let lora = { | ||
| 61 | match LoRa::new(SX1261_2::new(BoardType::Stm32wlSx1262, spi, iv), true, &mut delay).await { | ||
| 62 | Ok(l) => l, | ||
| 63 | Err(err) => { | ||
| 64 | info!("Radio error = {}", err); | ||
| 65 | return; | ||
| 66 | } | ||
| 67 | } | ||
| 68 | }; | ||
| 69 | let radio = LoRaRadio::new(lora); | ||
| 70 | let region: region::Configuration = region::Configuration::new(LORAWAN_REGION); | ||
| 71 | let mut device: Device<_, Crypto, _, _> = Device::new(region, radio, LoraTimer::new(), Rng::new(p.RNG)); | ||
| 72 | |||
| 73 | defmt::info!("Joining LoRaWAN network"); | ||
| 74 | |||
| 75 | // TODO: Adjust the EUI and Keys according to your network credentials | ||
| 76 | match device | ||
| 77 | .join(&JoinMode::OTAA { | ||
| 78 | deveui: [0, 0, 0, 0, 0, 0, 0, 0], | ||
| 79 | appeui: [0, 0, 0, 0, 0, 0, 0, 0], | ||
| 80 | appkey: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
| 81 | }) | ||
| 82 | .await | ||
| 83 | { | ||
| 84 | Ok(()) => defmt::info!("LoRaWAN network joined"), | ||
| 85 | Err(err) => { | ||
| 86 | info!("Radio error = {}", err); | ||
| 87 | return; | ||
| 88 | } | ||
| 89 | }; | ||
| 90 | } | ||
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 new file mode 100644 index 000000000..8a38402fa --- /dev/null +++ b/examples/stm32wl/src/bin/lora_p2p_send.rs | |||
| @@ -0,0 +1,110 @@ | |||
| 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 | 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 mdltn_params = { | ||
| 63 | match lora.create_modulation_params( | ||
| 64 | SpreadingFactor::_10, | ||
| 65 | Bandwidth::_250KHz, | ||
| 66 | CodingRate::_4_8, | ||
| 67 | LORA_FREQUENCY_IN_HZ, | ||
| 68 | ) { | ||
| 69 | Ok(mp) => mp, | ||
| 70 | Err(err) => { | ||
| 71 | info!("Radio error = {}", err); | ||
| 72 | return; | ||
| 73 | } | ||
| 74 | } | ||
| 75 | }; | ||
| 76 | |||
| 77 | let mut tx_pkt_params = { | ||
| 78 | match lora.create_tx_packet_params(4, false, true, false, &mdltn_params) { | ||
| 79 | Ok(pp) => pp, | ||
| 80 | Err(err) => { | ||
| 81 | info!("Radio error = {}", err); | ||
| 82 | return; | ||
| 83 | } | ||
| 84 | } | ||
| 85 | }; | ||
| 86 | |||
| 87 | match lora.prepare_for_tx(&mdltn_params, 20, false).await { | ||
| 88 | Ok(()) => {} | ||
| 89 | Err(err) => { | ||
| 90 | info!("Radio error = {}", err); | ||
| 91 | return; | ||
| 92 | } | ||
| 93 | }; | ||
| 94 | |||
| 95 | let buffer = [0x01u8, 0x02u8, 0x03u8]; | ||
| 96 | match lora.tx(&mdltn_params, &mut tx_pkt_params, &buffer, 0xffffff).await { | ||
| 97 | Ok(()) => { | ||
| 98 | info!("TX DONE"); | ||
| 99 | } | ||
| 100 | Err(err) => { | ||
| 101 | info!("Radio error = {}", err); | ||
| 102 | return; | ||
| 103 | } | ||
| 104 | }; | ||
| 105 | |||
| 106 | match lora.sleep(&mut delay).await { | ||
| 107 | Ok(()) => info!("Sleep successful"), | ||
| 108 | Err(err) => info!("Sleep unsuccessful = {}", err), | ||
| 109 | } | ||
| 110 | } | ||
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 | } | ||
diff --git a/examples/stm32wl/src/bin/subghz.rs b/examples/stm32wl/src/bin/subghz.rs deleted file mode 100644 index 32c8b5515..000000000 --- a/examples/stm32wl/src/bin/subghz.rs +++ /dev/null | |||
| @@ -1,119 +0,0 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![macro_use] | ||
| 4 | #![allow(dead_code)] | ||
| 5 | #![feature(type_alias_impl_trait)] | ||
| 6 | |||
| 7 | use defmt::*; | ||
| 8 | use embassy_executor::Spawner; | ||
| 9 | use embassy_stm32::dma::NoDma; | ||
| 10 | use embassy_stm32::exti::ExtiInput; | ||
| 11 | use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; | ||
| 12 | use embassy_stm32::interrupt; | ||
| 13 | use embassy_stm32::interrupt::{Interrupt, InterruptExt}; | ||
| 14 | use embassy_stm32::subghz::*; | ||
| 15 | use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; | ||
| 16 | use embassy_sync::signal::Signal; | ||
| 17 | use {defmt_rtt as _, panic_probe as _}; | ||
| 18 | |||
| 19 | const PING_DATA: &str = "PING"; | ||
| 20 | const DATA_LEN: u8 = PING_DATA.len() as u8; | ||
| 21 | const PING_DATA_BYTES: &[u8] = PING_DATA.as_bytes(); | ||
| 22 | const PREAMBLE_LEN: u16 = 5 * 8; | ||
| 23 | |||
| 24 | const RF_FREQ: RfFreq = RfFreq::from_frequency(867_500_000); | ||
| 25 | |||
| 26 | const SYNC_WORD: [u8; 8] = [0x79, 0x80, 0x0C, 0xC0, 0x29, 0x95, 0xF8, 0x4A]; | ||
| 27 | const SYNC_WORD_LEN: u8 = SYNC_WORD.len() as u8; | ||
| 28 | const SYNC_WORD_LEN_BITS: u8 = SYNC_WORD_LEN * 8; | ||
| 29 | |||
| 30 | const TX_BUF_OFFSET: u8 = 128; | ||
| 31 | const RX_BUF_OFFSET: u8 = 0; | ||
| 32 | const LORA_PACKET_PARAMS: LoRaPacketParams = LoRaPacketParams::new() | ||
| 33 | .set_crc_en(true) | ||
| 34 | .set_preamble_len(PREAMBLE_LEN) | ||
| 35 | .set_payload_len(DATA_LEN) | ||
| 36 | .set_invert_iq(false) | ||
| 37 | .set_header_type(HeaderType::Fixed); | ||
| 38 | |||
| 39 | const LORA_MOD_PARAMS: LoRaModParams = LoRaModParams::new() | ||
| 40 | .set_bw(LoRaBandwidth::Bw125) | ||
| 41 | .set_cr(CodingRate::Cr45) | ||
| 42 | .set_ldro_en(true) | ||
| 43 | .set_sf(SpreadingFactor::Sf7); | ||
| 44 | |||
| 45 | // configuration for +10 dBm output power | ||
| 46 | // see table 35 "PA optimal setting and operating modes" | ||
| 47 | const PA_CONFIG: PaConfig = PaConfig::new().set_pa_duty_cycle(0x1).set_hp_max(0x0).set_pa(PaSel::Lp); | ||
| 48 | |||
| 49 | const TCXO_MODE: TcxoMode = TcxoMode::new() | ||
| 50 | .set_txco_trim(TcxoTrim::Volts1pt7) | ||
| 51 | .set_timeout(Timeout::from_duration_sat(core::time::Duration::from_millis(10))); | ||
| 52 | |||
| 53 | const TX_PARAMS: TxParams = TxParams::new().set_power(0x0D).set_ramp_time(RampTime::Micros40); | ||
| 54 | |||
| 55 | #[embassy_executor::main] | ||
| 56 | async fn main(_spawner: Spawner) { | ||
| 57 | let mut config = embassy_stm32::Config::default(); | ||
| 58 | config.rcc.mux = embassy_stm32::rcc::ClockSrc::HSE32; | ||
| 59 | let p = embassy_stm32::init(config); | ||
| 60 | |||
| 61 | let mut led1 = Output::new(p.PB15, Level::High, Speed::Low); | ||
| 62 | let mut led2 = Output::new(p.PB9, Level::Low, Speed::Low); | ||
| 63 | let mut led3 = Output::new(p.PB11, Level::Low, Speed::Low); | ||
| 64 | |||
| 65 | let button = Input::new(p.PA0, Pull::Up); | ||
| 66 | let mut pin = ExtiInput::new(button, p.EXTI0); | ||
| 67 | |||
| 68 | static IRQ_SIGNAL: Signal<CriticalSectionRawMutex, ()> = Signal::new(); | ||
| 69 | let radio_irq = interrupt::take!(SUBGHZ_RADIO); | ||
| 70 | radio_irq.set_handler(|_| { | ||
| 71 | IRQ_SIGNAL.signal(()); | ||
| 72 | unsafe { interrupt::SUBGHZ_RADIO::steal() }.disable(); | ||
| 73 | }); | ||
| 74 | |||
| 75 | let mut radio = SubGhz::new(p.SUBGHZSPI, NoDma, NoDma); | ||
| 76 | |||
| 77 | defmt::info!("Radio ready for use"); | ||
| 78 | |||
| 79 | led1.set_low(); | ||
| 80 | |||
| 81 | led2.set_high(); | ||
| 82 | |||
| 83 | unwrap!(radio.set_standby(StandbyClk::Rc)); | ||
| 84 | unwrap!(radio.set_tcxo_mode(&TCXO_MODE)); | ||
| 85 | unwrap!(radio.set_standby(StandbyClk::Hse)); | ||
| 86 | unwrap!(radio.set_regulator_mode(RegMode::Ldo)); | ||
| 87 | unwrap!(radio.set_buffer_base_address(TX_BUF_OFFSET, RX_BUF_OFFSET)); | ||
| 88 | unwrap!(radio.set_pa_config(&PA_CONFIG)); | ||
| 89 | unwrap!(radio.set_pa_ocp(Ocp::Max60m)); | ||
| 90 | unwrap!(radio.set_tx_params(&TX_PARAMS)); | ||
| 91 | unwrap!(radio.set_packet_type(PacketType::LoRa)); | ||
| 92 | unwrap!(radio.set_lora_sync_word(LoRaSyncWord::Public)); | ||
| 93 | unwrap!(radio.set_lora_mod_params(&LORA_MOD_PARAMS)); | ||
| 94 | unwrap!(radio.set_lora_packet_params(&LORA_PACKET_PARAMS)); | ||
| 95 | unwrap!(radio.calibrate_image(CalibrateImage::ISM_863_870)); | ||
| 96 | unwrap!(radio.set_rf_frequency(&RF_FREQ)); | ||
| 97 | |||
| 98 | defmt::info!("Status: {:?}", unwrap!(radio.status())); | ||
| 99 | |||
| 100 | led2.set_low(); | ||
| 101 | |||
| 102 | loop { | ||
| 103 | pin.wait_for_rising_edge().await; | ||
| 104 | led3.set_high(); | ||
| 105 | unwrap!(radio.set_irq_cfg(&CfgIrq::new().irq_enable_all(Irq::TxDone))); | ||
| 106 | unwrap!(radio.write_buffer(TX_BUF_OFFSET, PING_DATA_BYTES)); | ||
| 107 | unwrap!(radio.set_tx(Timeout::DISABLED)); | ||
| 108 | |||
| 109 | radio_irq.enable(); | ||
| 110 | IRQ_SIGNAL.wait().await; | ||
| 111 | |||
| 112 | let (_, irq_status) = unwrap!(radio.irq_status()); | ||
| 113 | if irq_status & Irq::TxDone.mask() != 0 { | ||
| 114 | defmt::info!("TX done"); | ||
| 115 | } | ||
| 116 | unwrap!(radio.clear_irq_status(irq_status)); | ||
| 117 | led3.set_low(); | ||
| 118 | } | ||
| 119 | } | ||
