aboutsummaryrefslogtreecommitdiff
path: root/examples/nrf52840/src/bin
diff options
context:
space:
mode:
Diffstat (limited to 'examples/nrf52840/src/bin')
-rw-r--r--examples/nrf52840/src/bin/lora_cad.rs99
-rw-r--r--examples/nrf52840/src/bin/lora_lorawan.rs83
-rw-r--r--examples/nrf52840/src/bin/lora_p2p_receive.rs121
-rw-r--r--examples/nrf52840/src/bin/lora_p2p_receive_duty_cycle.rs131
-rw-r--r--examples/nrf52840/src/bin/lora_p2p_report.rs81
-rw-r--r--examples/nrf52840/src/bin/lora_p2p_send.rs104
-rw-r--r--examples/nrf52840/src/bin/lora_p2p_sense.rs128
7 files changed, 538 insertions, 209 deletions
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
9use defmt::*;
10use embassy_executor::Spawner;
11use embassy_lora::iv::GenericSx126xInterfaceVariant;
12use embassy_nrf::gpio::{Input, Level, Output, OutputDrive, Pin as _, Pull};
13use embassy_nrf::{bind_interrupts, peripherals, spim};
14use embassy_time::{Delay, Duration, Timer};
15use lora_phy::mod_params::*;
16use lora_phy::sx1261_2::SX1261_2;
17use lora_phy::LoRa;
18use {defmt_rtt as _, panic_probe as _};
19
20const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region
21
22bind_interrupts!(struct Irqs {
23 SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1 => spim::InterruptHandler<peripherals::TWISPI1>;
24});
25
26#[embassy_executor::main]
27async 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
9use defmt::*;
10use embassy_executor::Spawner;
11use embassy_lora::iv::GenericSx126xInterfaceVariant;
12use embassy_lora::LoraTimer;
13use embassy_nrf::gpio::{Input, Level, Output, OutputDrive, Pin as _, Pull};
14use embassy_nrf::rng::Rng;
15use embassy_nrf::{bind_interrupts, peripherals, rng, spim};
16use embassy_time::Delay;
17use lora_phy::mod_params::*;
18use lora_phy::sx1261_2::SX1261_2;
19use lora_phy::LoRa;
20use lorawan::default_crypto::DefaultFactory as Crypto;
21use lorawan_device::async_device::lora_radio::LoRaRadio;
22use lorawan_device::async_device::{region, Device, JoinMode};
23use {defmt_rtt as _, panic_probe as _};
24
25const LORAWAN_REGION: region::Region = region::Region::EU868; // warning: set this appropriately for the region
26
27bind_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]
33async 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
9use defmt::*;
10use embassy_executor::Spawner;
11use embassy_lora::iv::GenericSx126xInterfaceVariant;
12use embassy_nrf::gpio::{Input, Level, Output, OutputDrive, Pin as _, Pull};
13use embassy_nrf::{bind_interrupts, peripherals, spim};
14use embassy_time::{Delay, Duration, Timer};
15use lora_phy::mod_params::*;
16use lora_phy::sx1261_2::SX1261_2;
17use lora_phy::LoRa;
18use {defmt_rtt as _, panic_probe as _};
19
20const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region
21
22bind_interrupts!(struct Irqs {
23 SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1 => spim::InterruptHandler<peripherals::TWISPI1>;
24});
25
26#[embassy_executor::main]
27async 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
9use defmt::*;
10use embassy_executor::Spawner;
11use embassy_lora::iv::GenericSx126xInterfaceVariant;
12use embassy_nrf::gpio::{Input, Level, Output, OutputDrive, Pin as _, Pull};
13use embassy_nrf::{bind_interrupts, peripherals, spim};
14use embassy_time::{Delay, Duration, Timer};
15use lora_phy::mod_params::*;
16use lora_phy::sx1261_2::SX1261_2;
17use lora_phy::LoRa;
18use {defmt_rtt as _, panic_probe as _};
19
20const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region
21
22bind_interrupts!(struct Irqs {
23 SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1 => spim::InterruptHandler<peripherals::TWISPI1>;
24});
25
26#[embassy_executor::main]
27async 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
10use defmt::*;
11use embassy_executor::Spawner;
12use embassy_lora::sx126x::*;
13use embassy_nrf::gpio::{Input, Level, Output, OutputDrive, Pin as _, Pull};
14use embassy_nrf::{bind_interrupts, peripherals, spim};
15use embassy_time::{Duration, Timer};
16use lorawan_device::async_device::radio::{Bandwidth, CodingRate, PhyRxTx, RfConfig, SpreadingFactor};
17use {defmt_rtt as _, panic_probe as _};
18
19bind_interrupts!(struct Irqs {
20 SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1 => spim::InterruptHandler<peripherals::TWISPI1>;
21});
22
23#[embassy_executor::main]
24async 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
9use defmt::*;
10use embassy_executor::Spawner;
11use embassy_lora::iv::GenericSx126xInterfaceVariant;
12use embassy_nrf::gpio::{Input, Level, Output, OutputDrive, Pin as _, Pull};
13use embassy_nrf::{bind_interrupts, peripherals, spim};
14use embassy_time::Delay;
15use lora_phy::mod_params::*;
16use lora_phy::sx1261_2::SX1261_2;
17use lora_phy::LoRa;
18use {defmt_rtt as _, panic_probe as _};
19
20const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region
21
22bind_interrupts!(struct Irqs {
23 SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1 => spim::InterruptHandler<peripherals::TWISPI1>;
24});
25
26#[embassy_executor::main]
27async 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
11use defmt::*;
12use embassy_executor::Spawner;
13use embassy_lora::sx126x::*;
14use embassy_nrf::gpio::{Input, Level, Output, OutputDrive, Pin as _, Pull};
15use embassy_nrf::{bind_interrupts, peripherals, spim};
16use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
17use embassy_sync::pubsub::{PubSubChannel, Publisher};
18use embassy_time::{Duration, Timer};
19use lorawan_device::async_device::radio::{Bandwidth, CodingRate, PhyRxTx, RfConfig, SpreadingFactor, TxConfig};
20use {defmt_rtt as _, panic_probe as _, panic_probe as _};
21
22bind_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)
27static MESSAGE_BUS: PubSubChannel<CriticalSectionRawMutex, Message, 2, 1, 2> = PubSubChannel::new();
28
29#[derive(Clone, defmt::Format)]
30enum Message {
31 Temperature(i32),
32 MotionDetected,
33}
34
35#[embassy_executor::task]
36async 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]
45async 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]
54async 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}