aboutsummaryrefslogtreecommitdiff
path: root/examples/nrf52840/src
diff options
context:
space:
mode:
authorceekdee <[email protected]>2023-04-21 01:20:46 -0500
committerceekdee <[email protected]>2023-04-21 01:20:46 -0500
commit02c86bca5275328a15376176ff44487ab7655866 (patch)
tree4f4c3c76261e95e389219b7732cfe6da5777de98 /examples/nrf52840/src
parentfb27594b2eb2cca2aea25dd92a7b730c185b6ecc (diff)
Add external LoRa physical layer functionality.
Diffstat (limited to 'examples/nrf52840/src')
-rw-r--r--examples/nrf52840/src/bin/lora_cad.rs92
-rw-r--r--examples/nrf52840/src/bin/lora_p2p_receive_duty_cycle.rs124
-rw-r--r--examples/nrf52840/src/bin/lora_p2p_report.rs81
3 files changed, 216 insertions, 81 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..8899c1b23
--- /dev/null
+++ b/examples/nrf52840/src/bin/lora_cad.rs
@@ -0,0 +1,92 @@
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 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
20bind_interrupts!(struct Irqs {
21 SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1 => spim::InterruptHandler<peripherals::TWISPI1>;
22});
23
24#[embassy_executor::main]
25async fn main(_spawner: Spawner) {
26 let p = embassy_nrf::init(Default::default());
27 let mut spi_config = spim::Config::default();
28 spi_config.frequency = spim::Frequency::M16;
29
30 let spim = spim::Spim::new(p.TWISPI1, Irqs, p.P1_11, p.P1_13, p.P1_12, spi_config);
31
32 let nss = 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 rf_switch_rx = Output::new(p.P1_05.degrade(), Level::Low, OutputDrive::Standard);
37 let rf_switch_tx = Output::new(p.P1_07.degrade(), Level::Low, OutputDrive::Standard);
38
39 let iv =
40 GenericSx126xInterfaceVariant::new(nss, reset, dio1, busy, Some(rf_switch_rx), Some(rf_switch_tx)).unwrap();
41
42 let mut delay = Delay;
43
44 let mut lora = {
45 match LoRa::new(SX1261_2::new(BoardType::Rak4631Sx1262, spim, iv), false, &mut delay).await {
46 Ok(l) => l,
47 Err(err) => {
48 info!("Radio error = {}", err);
49 return;
50 }
51 }
52 };
53
54 let mut debug_indicator = Output::new(p.P1_03, Level::Low, OutputDrive::Standard);
55 let mut start_indicator = Output::new(p.P1_04, Level::Low, OutputDrive::Standard);
56
57 start_indicator.set_high();
58 Timer::after(Duration::from_secs(5)).await;
59 start_indicator.set_low();
60
61 let mdltn_params = {
62 match lora.create_modulation_params(SpreadingFactor::_10, Bandwidth::_250KHz, CodingRate::_4_8, 903900000) {
63 Ok(mp) => mp,
64 Err(err) => {
65 info!("Radio error = {}", err);
66 return;
67 }
68 }
69 };
70
71 match lora.prepare_for_cad(&mdltn_params, true).await {
72 Ok(()) => {}
73 Err(err) => {
74 info!("Radio error = {}", err);
75 return;
76 }
77 };
78
79 match lora.cad().await {
80 Ok(cad_activity_detected) => {
81 if cad_activity_detected {
82 info!("cad successful with activity detected")
83 } else {
84 info!("cad successful without activity detected")
85 }
86 debug_indicator.set_high();
87 Timer::after(Duration::from_secs(15)).await;
88 debug_indicator.set_low();
89 }
90 Err(err) => info!("cad unsuccessful = {}", err),
91 }
92}
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..d84701742
--- /dev/null
+++ b/examples/nrf52840/src/bin/lora_p2p_receive_duty_cycle.rs
@@ -0,0 +1,124 @@
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 Rx duty cycle 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
20bind_interrupts!(struct Irqs {
21 SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1 => spim::InterruptHandler<peripherals::TWISPI1>;
22});
23
24#[embassy_executor::main]
25async fn main(_spawner: Spawner) {
26 let p = embassy_nrf::init(Default::default());
27 let mut spi_config = spim::Config::default();
28 spi_config.frequency = spim::Frequency::M16;
29
30 let spim = spim::Spim::new(p.TWISPI1, Irqs, p.P1_11, p.P1_13, p.P1_12, spi_config);
31
32 let nss = 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 rf_switch_rx = Output::new(p.P1_05.degrade(), Level::Low, OutputDrive::Standard);
37 let rf_switch_tx = Output::new(p.P1_07.degrade(), Level::Low, OutputDrive::Standard);
38
39 let iv =
40 GenericSx126xInterfaceVariant::new(nss, reset, dio1, busy, Some(rf_switch_rx), Some(rf_switch_tx)).unwrap();
41
42 let mut delay = Delay;
43
44 let mut lora = {
45 match LoRa::new(SX1261_2::new(BoardType::Rak4631Sx1262, spim, iv), false, &mut delay).await {
46 Ok(l) => l,
47 Err(err) => {
48 info!("Radio error = {}", err);
49 return;
50 }
51 }
52 };
53
54 let mut debug_indicator = Output::new(p.P1_03, Level::Low, OutputDrive::Standard);
55 let mut start_indicator = Output::new(p.P1_04, Level::Low, OutputDrive::Standard);
56
57 start_indicator.set_high();
58 Timer::after(Duration::from_secs(5)).await;
59 start_indicator.set_low();
60
61 let mut receiving_buffer = [00u8; 100];
62
63 let mdltn_params = {
64 match lora.create_modulation_params(SpreadingFactor::_10, Bandwidth::_250KHz, CodingRate::_4_8, 903900000) {
65 Ok(mp) => mp,
66 Err(err) => {
67 info!("Radio error = {}", err);
68 return;
69 }
70 }
71 };
72
73 let rx_pkt_params = {
74 match lora.create_rx_packet_params(4, false, receiving_buffer.len() as u8, true, false, &mdltn_params) {
75 Ok(pp) => pp,
76 Err(err) => {
77 info!("Radio error = {}", err);
78 return;
79 }
80 }
81 };
82
83 // 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.
84 match lora
85 .prepare_for_rx(
86 &mdltn_params,
87 &rx_pkt_params,
88 Some(&DutyCycleParams {
89 rx_time: 300_000, // 300_000 units * 15.625 us/unit = 4.69 s
90 sleep_time: 200_000, // 200_000 units * 15.625 us/unit = 3.13 s
91 }),
92 false,
93 false,
94 0,
95 0,
96 )
97 .await
98 {
99 Ok(()) => {}
100 Err(err) => {
101 info!("Radio error = {}", err);
102 return;
103 }
104 };
105
106 receiving_buffer = [00u8; 100];
107 match lora.rx(&rx_pkt_params, &mut receiving_buffer).await {
108 Ok((received_len, _rx_pkt_status)) => {
109 if (received_len == 3)
110 && (receiving_buffer[0] == 0x01u8)
111 && (receiving_buffer[1] == 0x02u8)
112 && (receiving_buffer[2] == 0x03u8)
113 {
114 info!("rx successful");
115 debug_indicator.set_high();
116 Timer::after(Duration::from_secs(5)).await;
117 debug_indicator.set_low();
118 } else {
119 info!("rx unknown packet")
120 }
121 }
122 Err(err) => info!("rx unsuccessful = {}", err),
123 }
124}
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}