aboutsummaryrefslogtreecommitdiff
path: root/examples/rp
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2023-04-26 20:10:40 +0000
committerGitHub <[email protected]>2023-04-26 20:10:40 +0000
commit759d911b5046b1f6201ea9728f1b42e4a0153659 (patch)
tree2b8305193406caa6d79eed56a453a5447256dadc /examples/rp
parentcb00fb18cbb8dcc83133fffeba42108fee932b38 (diff)
parenta277deeaa563eb1ec78856a3f6866e73ed206c6d (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/rp')
-rw-r--r--examples/rp/Cargo.toml6
-rw-r--r--examples/rp/src/bin/lora_lorawan.rs80
-rw-r--r--examples/rp/src/bin/lora_p2p_receive.rs115
-rw-r--r--examples/rp/src/bin/lora_p2p_send.rs103
-rw-r--r--examples/rp/src/bin/lora_p2p_send_multicore.rs139
5 files changed, 442 insertions, 1 deletions
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"
9embassy-embedded-hal = { version = "0.1.0", path = "../../embassy-embedded-hal", features = ["defmt"] } 9embassy-embedded-hal = { version = "0.1.0", path = "../../embassy-embedded-hal", features = ["defmt"] }
10embassy-sync = { version = "0.2.0", path = "../../embassy-sync", features = ["defmt"] } 10embassy-sync = { version = "0.2.0", path = "../../embassy-sync", features = ["defmt"] }
11embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] } 11embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] }
12embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime"] } 12embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["nightly", "unstable-traits", "defmt", "defmt-timestamp-uptime"] }
13embassy-rp = { version = "0.1.0", path = "../../embassy-rp", features = ["defmt", "unstable-traits", "nightly", "unstable-pac", "time-driver", "pio", "critical-section-impl"] } 13embassy-rp = { version = "0.1.0", path = "../../embassy-rp", features = ["defmt", "unstable-traits", "nightly", "unstable-pac", "time-driver", "pio", "critical-section-impl"] }
14embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"] } 14embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"] }
15embassy-net = { version = "0.1.0", path = "../../embassy-net", features = ["defmt", "nightly", "tcp", "dhcpv4", "medium-ethernet"] } 15embassy-net = { version = "0.1.0", path = "../../embassy-net", features = ["defmt", "nightly", "tcp", "dhcpv4", "medium-ethernet"] }
16embassy-futures = { version = "0.1.0", path = "../../embassy-futures" } 16embassy-futures = { version = "0.1.0", path = "../../embassy-futures" }
17embassy-usb-logger = { version = "0.1.0", path = "../../embassy-usb-logger" } 17embassy-usb-logger = { version = "0.1.0", path = "../../embassy-usb-logger" }
18embassy-lora = { version = "0.1.0", path = "../../embassy-lora", features = ["time", "defmt", "external-lora-phy"] }
19lora-phy = { version = "1" }
20lorawan-device = { version = "0.10.0", default-features = false, features = ["async", "external-lora-phy"] }
21lorawan = { version = "0.7.3", default-features = false, features = ["default-crypto"] }
18 22
19defmt = "0.3" 23defmt = "0.3"
20defmt-rtt = "0.4" 24defmt-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
8use defmt::*;
9use embassy_executor::Spawner;
10use embassy_lora::iv::GenericSx126xInterfaceVariant;
11use embassy_lora::LoraTimer;
12use embassy_rp::gpio::{Input, Level, Output, Pin, Pull};
13use embassy_rp::spi::{Config, Spi};
14use embassy_time::Delay;
15use lora_phy::mod_params::*;
16use lora_phy::sx1261_2::SX1261_2;
17use lora_phy::LoRa;
18use lorawan::default_crypto::DefaultFactory as Crypto;
19use lorawan_device::async_device::lora_radio::LoRaRadio;
20use lorawan_device::async_device::{region, Device, JoinMode};
21use {defmt_rtt as _, panic_probe as _};
22
23const LORAWAN_REGION: region::Region = region::Region::EU868; // warning: set this appropriately for the region
24
25#[embassy_executor::main]
26async 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
8use defmt::*;
9use embassy_executor::Spawner;
10use embassy_lora::iv::GenericSx126xInterfaceVariant;
11use embassy_rp::gpio::{Input, Level, Output, Pin, Pull};
12use embassy_rp::spi::{Config, Spi};
13use embassy_time::{Delay, Duration, Timer};
14use lora_phy::mod_params::*;
15use lora_phy::sx1261_2::SX1261_2;
16use lora_phy::LoRa;
17use {defmt_rtt as _, panic_probe as _};
18
19const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region
20
21#[embassy_executor::main]
22async 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
8use defmt::*;
9use embassy_executor::Spawner;
10use embassy_lora::iv::GenericSx126xInterfaceVariant;
11use embassy_rp::gpio::{Input, Level, Output, Pin, Pull};
12use embassy_rp::spi::{Config, Spi};
13use embassy_time::Delay;
14use lora_phy::mod_params::*;
15use lora_phy::sx1261_2::SX1261_2;
16use lora_phy::LoRa;
17use {defmt_rtt as _, panic_probe as _};
18
19const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region
20
21#[embassy_executor::main]
22async 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
8use defmt::*;
9use embassy_executor::Executor;
10use embassy_executor::_export::StaticCell;
11use embassy_lora::iv::GenericSx126xInterfaceVariant;
12use embassy_rp::gpio::{AnyPin, Input, Level, Output, Pin, Pull};
13use embassy_rp::multicore::{spawn_core1, Stack};
14use embassy_rp::peripherals::SPI1;
15use embassy_rp::spi::{Async, Config, Spi};
16use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
17use embassy_sync::channel::Channel;
18use embassy_time::{Delay, Duration, Timer};
19use lora_phy::mod_params::*;
20use lora_phy::sx1261_2::SX1261_2;
21use lora_phy::LoRa;
22use {defmt_rtt as _, panic_probe as _};
23
24static mut CORE1_STACK: Stack<4096> = Stack::new();
25static EXECUTOR0: StaticCell<Executor> = StaticCell::new();
26static EXECUTOR1: StaticCell<Executor> = StaticCell::new();
27static CHANNEL: Channel<CriticalSectionRawMutex, [u8; 3], 1> = Channel::new();
28
29const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region
30
31#[cortex_m_rt::entry]
32fn 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]
57async 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]
66async 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}