aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorUlf Lilleengen <[email protected]>2021-09-30 09:25:45 +0200
committerUlf Lilleengen <[email protected]>2021-09-30 10:32:24 +0200
commit16a47a0ad9eb5f40aa1f202d3fbaa03c7b77b836 (patch)
tree924181ab08c98318901ad840aa071614fb02644e /examples
parentd9e2d176256ef4959c1ba60893ea361363d480d3 (diff)
Add embassy-lora crate
This crate contains async radio drivers for various lora drivers that work with embassy timers. The code is imported from Drogue Device ( https://github.com/drogue-iot/drogue-device) The radio drivers integrate with the async LoRaWAN MAC layer in the lorawan-device crate. Also added is an example for the STM32WL55 and for STM32L0 (requires the LoRa Discovery board) for LoRaWAN. Future work is to make the underlying radio drivers using fully async SPI when communicating with the peripheral.
Diffstat (limited to 'examples')
-rw-r--r--examples/stm32l0/Cargo.toml4
-rw-r--r--examples/stm32l0/src/bin/lorawan.rs104
-rw-r--r--examples/stm32wl55/Cargo.toml6
-rw-r--r--examples/stm32wl55/src/bin/lorawan.rs79
4 files changed, 192 insertions, 1 deletions
diff --git a/examples/stm32l0/Cargo.toml b/examples/stm32l0/Cargo.toml
index 7dfcdb0fe..371ac68cc 100644
--- a/examples/stm32l0/Cargo.toml
+++ b/examples/stm32l0/Cargo.toml
@@ -23,6 +23,10 @@ embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["
23embassy-hal-common = {version = "0.1.0", path = "../../embassy-hal-common" } 23embassy-hal-common = {version = "0.1.0", path = "../../embassy-hal-common" }
24embassy-macros = { path = "../../embassy-macros" } 24embassy-macros = { path = "../../embassy-macros" }
25 25
26embassy-lora = { version = "0.1.0", path = "../../embassy-lora", features = ["sx127x", "time"] }
27lorawan-device = { git = "https://github.com/lulf/rust-lorawan.git", rev = "a373d06fa8858d251bc70d5789cebcd9a638ec42", default-features = false, features = ["async"] }
28lorawan-encoding = { git = "https://github.com/lulf/rust-lorawan.git", rev = "a373d06fa8858d251bc70d5789cebcd9a638ec42", default-features = false, features = ["default-crypto"] }
29
26defmt = "0.2.3" 30defmt = "0.2.3"
27defmt-rtt = "0.2.0" 31defmt-rtt = "0.2.0"
28 32
diff --git a/examples/stm32l0/src/bin/lorawan.rs b/examples/stm32l0/src/bin/lorawan.rs
new file mode 100644
index 000000000..5ca69f9a7
--- /dev/null
+++ b/examples/stm32l0/src/bin/lorawan.rs
@@ -0,0 +1,104 @@
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(generic_associated_types)]
7#![feature(type_alias_impl_trait)]
8
9#[path = "../example_common.rs"]
10mod example_common;
11
12use embassy_lora::{sx127x::*, LoraTimer};
13use embassy_stm32::{
14 dbgmcu::Dbgmcu,
15 dma::NoDma,
16 exti::ExtiInput,
17 gpio::{Input, Level, Output, Pull, Speed},
18 rcc,
19 rng::Rng,
20 spi,
21 time::U32Ext,
22 Peripherals,
23};
24use lorawan_device::async_device::{region, Device, JoinMode};
25use lorawan_encoding::default_crypto::DefaultFactory as Crypto;
26
27fn config() -> embassy_stm32::Config {
28 let mut config = embassy_stm32::Config::default();
29 config.rcc = config.rcc.clock_src(embassy_stm32::rcc::ClockSrc::HSI16);
30 config
31}
32
33#[embassy::main(config = "config()")]
34async fn main(_spawner: embassy::executor::Spawner, mut p: Peripherals) {
35 unsafe {
36 Dbgmcu::enable_all();
37 }
38
39 let mut rcc = rcc::Rcc::new(p.RCC);
40 let _ = rcc.enable_hsi48(&mut p.SYSCFG, p.CRS);
41
42 // SPI for sx127x
43 let spi = spi::Spi::new(
44 p.SPI1,
45 p.PB3,
46 p.PA7,
47 p.PA6,
48 NoDma,
49 NoDma,
50 200_000.hz(),
51 spi::Config::default(),
52 );
53
54 let cs = Output::new(p.PA15, Level::High, Speed::Low);
55 let reset = Output::new(p.PC0, Level::High, Speed::Low);
56 let _ = Input::new(p.PB1, Pull::None);
57
58 let ready = Input::new(p.PB4, Pull::Up);
59 let ready_pin = ExtiInput::new(ready, p.EXTI4);
60
61 let radio = Sx127xRadio::new(
62 spi,
63 cs,
64 reset,
65 ready_pin,
66 DummySwitch,
67 &mut embassy::time::Delay,
68 )
69 .unwrap();
70
71 let region = region::EU868::default().into();
72 let mut radio_buffer = [0; 256];
73 let mut device: Device<'_, _, Crypto, _, _> = Device::new(
74 region,
75 radio,
76 LoraTimer,
77 Rng::new(p.RNG),
78 &mut radio_buffer[..],
79 );
80
81 defmt::info!("Joining LoRaWAN network");
82
83 // TODO: Adjust the EUI and Keys according to your network credentials
84 device
85 .join(&JoinMode::OTAA {
86 deveui: [0, 0, 0, 0, 0, 0, 0, 0],
87 appeui: [0, 0, 0, 0, 0, 0, 0, 0],
88 appkey: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
89 })
90 .await
91 .ok()
92 .unwrap();
93 defmt::info!("LoRaWAN network joined");
94
95 defmt::info!("Sending 'PING'");
96 device.send(b"PING", 1, false).await.ok().unwrap();
97 defmt::info!("Message sent!");
98}
99
100pub struct DummySwitch;
101impl RadioSwitch for DummySwitch {
102 fn set_rx(&mut self) {}
103 fn set_tx(&mut self) {}
104}
diff --git a/examples/stm32wl55/Cargo.toml b/examples/stm32wl55/Cargo.toml
index d0c727862..4688bdad1 100644
--- a/examples/stm32wl55/Cargo.toml
+++ b/examples/stm32wl55/Cargo.toml
@@ -19,8 +19,12 @@ defmt-error = []
19[dependencies] 19[dependencies]
20embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-trace"] } 20embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-trace"] }
21embassy-traits = { version = "0.1.0", path = "../../embassy-traits", features = ["defmt"] } 21embassy-traits = { version = "0.1.0", path = "../../embassy-traits", features = ["defmt"] }
22embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "defmt-trace", "stm32wl55jc_cm4", "time-driver-tim2", "memory-x", "subghz"] } 22embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "defmt-trace", "stm32wl55jc_cm4", "time-driver-tim2", "memory-x", "subghz", "unstable-pac"] }
23embassy-hal-common = {version = "0.1.0", path = "../../embassy-hal-common" } 23embassy-hal-common = {version = "0.1.0", path = "../../embassy-hal-common" }
24embassy-lora = { version = "0.1.0", path = "../../embassy-lora", features = ["stm32wl", "time"] }
25
26lorawan-device = { git = "https://github.com/lulf/rust-lorawan.git", rev = "a373d06fa8858d251bc70d5789cebcd9a638ec42", default-features = false, features = ["async"] }
27lorawan-encoding = { git = "https://github.com/lulf/rust-lorawan.git", rev = "a373d06fa8858d251bc70d5789cebcd9a638ec42", default-features = false, features = ["default-crypto"] }
24 28
25defmt = "0.2.3" 29defmt = "0.2.3"
26defmt-rtt = "0.2.0" 30defmt-rtt = "0.2.0"
diff --git a/examples/stm32wl55/src/bin/lorawan.rs b/examples/stm32wl55/src/bin/lorawan.rs
new file mode 100644
index 000000000..155905ae7
--- /dev/null
+++ b/examples/stm32wl55/src/bin/lorawan.rs
@@ -0,0 +1,79 @@
1#![no_std]
2#![no_main]
3#![macro_use]
4#![allow(dead_code)]
5#![feature(generic_associated_types)]
6#![feature(type_alias_impl_trait)]
7
8#[path = "../example_common.rs"]
9mod example_common;
10
11use embassy_lora::{stm32wl::*, LoraTimer};
12use embassy_stm32::{
13 dbgmcu::Dbgmcu,
14 dma::NoDma,
15 gpio::{Level, Output, Pin, Speed},
16 interrupt, pac, rcc,
17 rng::Rng,
18 subghz::*,
19 Peripherals,
20};
21use lorawan_device::async_device::{region, Device, JoinMode};
22use lorawan_encoding::default_crypto::DefaultFactory as Crypto;
23
24fn config() -> embassy_stm32::Config {
25 let mut config = embassy_stm32::Config::default();
26 config.rcc = config.rcc.clock_src(embassy_stm32::rcc::ClockSrc::HSI16);
27 config
28}
29
30#[embassy::main(config = "config()")]
31async fn main(_spawner: embassy::executor::Spawner, p: Peripherals) {
32 unsafe {
33 Dbgmcu::enable_all();
34 let mut rcc = rcc::Rcc::new(p.RCC);
35 rcc.enable_lsi();
36 pac::RCC.ccipr().modify(|w| {
37 w.set_rngsel(0b01);
38 });
39 }
40
41 let ctrl1 = Output::new(p.PC3.degrade(), Level::High, Speed::High);
42 let ctrl2 = Output::new(p.PC4.degrade(), Level::High, Speed::High);
43 let ctrl3 = Output::new(p.PC5.degrade(), Level::High, Speed::High);
44 let rfs = RadioSwitch::new(ctrl1, ctrl2, ctrl3);
45
46 let radio = SubGhz::new(p.SUBGHZSPI, p.PA5, p.PA7, p.PA6, NoDma, NoDma);
47
48 let irq = interrupt::take!(SUBGHZ_RADIO);
49 static mut RADIO_STATE: SubGhzState<'static> = SubGhzState::new();
50 let radio = unsafe { SubGhzRadio::new(&mut RADIO_STATE, radio, rfs, irq) };
51
52 let region = region::EU868::default().into();
53 let mut radio_buffer = [0; 256];
54 let mut device: Device<'_, _, Crypto, _, _> = Device::new(
55 region,
56 radio,
57 LoraTimer,
58 Rng::new(p.RNG),
59 &mut radio_buffer[..],
60 );
61
62 defmt::info!("Joining LoRaWAN network");
63
64 // TODO: Adjust the EUI and Keys according to your network credentials
65 device
66 .join(&JoinMode::OTAA {
67 deveui: [0, 0, 0, 0, 0, 0, 0, 0],
68 appeui: [0, 0, 0, 0, 0, 0, 0, 0],
69 appkey: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
70 })
71 .await
72 .ok()
73 .unwrap();
74 defmt::info!("LoRaWAN network joined");
75
76 defmt::info!("Sending 'PING'");
77 device.send(b"PING", 1, false).await.ok().unwrap();
78 defmt::info!("Message sent!");
79}