diff options
| author | Dario Nieuwenhuis <[email protected]> | 2021-10-10 21:23:02 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-10-10 21:23:02 +0200 |
| commit | 1c4c813255d39c9b517a0b6c5cc905df1c6aa565 (patch) | |
| tree | 80f0a6620171759f57a5d2420f73a801cc1bf953 /examples | |
| parent | 009b77c1b9874cccb9b2f81876f41e9c3d53f3a5 (diff) | |
| parent | 16a47a0ad9eb5f40aa1f202d3fbaa03c7b77b836 (diff) | |
Merge pull request #410 from lulf/embassy-lora
Add embassy-lora crate
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/stm32l0/Cargo.toml | 4 | ||||
| -rw-r--r-- | examples/stm32l0/src/bin/lorawan.rs | 104 | ||||
| -rw-r--r-- | examples/stm32wl55/Cargo.toml | 6 | ||||
| -rw-r--r-- | examples/stm32wl55/src/bin/lorawan.rs | 79 |
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 = [" | |||
| 23 | embassy-hal-common = {version = "0.1.0", path = "../../embassy-hal-common" } | 23 | embassy-hal-common = {version = "0.1.0", path = "../../embassy-hal-common" } |
| 24 | embassy-macros = { path = "../../embassy-macros" } | 24 | embassy-macros = { path = "../../embassy-macros" } |
| 25 | 25 | ||
| 26 | embassy-lora = { version = "0.1.0", path = "../../embassy-lora", features = ["sx127x", "time"] } | ||
| 27 | lorawan-device = { git = "https://github.com/lulf/rust-lorawan.git", rev = "a373d06fa8858d251bc70d5789cebcd9a638ec42", default-features = false, features = ["async"] } | ||
| 28 | lorawan-encoding = { git = "https://github.com/lulf/rust-lorawan.git", rev = "a373d06fa8858d251bc70d5789cebcd9a638ec42", default-features = false, features = ["default-crypto"] } | ||
| 29 | |||
| 26 | defmt = "0.2.3" | 30 | defmt = "0.2.3" |
| 27 | defmt-rtt = "0.2.0" | 31 | defmt-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"] | ||
| 10 | mod example_common; | ||
| 11 | |||
| 12 | use embassy_lora::{sx127x::*, LoraTimer}; | ||
| 13 | use 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 | }; | ||
| 24 | use lorawan_device::async_device::{region, Device, JoinMode}; | ||
| 25 | use lorawan_encoding::default_crypto::DefaultFactory as Crypto; | ||
| 26 | |||
| 27 | fn 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()")] | ||
| 34 | async 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 | |||
| 100 | pub struct DummySwitch; | ||
| 101 | impl 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] |
| 20 | embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-trace"] } | 20 | embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-trace"] } |
| 21 | embassy-traits = { version = "0.1.0", path = "../../embassy-traits", features = ["defmt"] } | 21 | embassy-traits = { version = "0.1.0", path = "../../embassy-traits", features = ["defmt"] } |
| 22 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "defmt-trace", "stm32wl55jc_cm4", "time-driver-tim2", "memory-x", "subghz"] } | 22 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "defmt-trace", "stm32wl55jc_cm4", "time-driver-tim2", "memory-x", "subghz", "unstable-pac"] } |
| 23 | embassy-hal-common = {version = "0.1.0", path = "../../embassy-hal-common" } | 23 | embassy-hal-common = {version = "0.1.0", path = "../../embassy-hal-common" } |
| 24 | embassy-lora = { version = "0.1.0", path = "../../embassy-lora", features = ["stm32wl", "time"] } | ||
| 25 | |||
| 26 | lorawan-device = { git = "https://github.com/lulf/rust-lorawan.git", rev = "a373d06fa8858d251bc70d5789cebcd9a638ec42", default-features = false, features = ["async"] } | ||
| 27 | lorawan-encoding = { git = "https://github.com/lulf/rust-lorawan.git", rev = "a373d06fa8858d251bc70d5789cebcd9a638ec42", default-features = false, features = ["default-crypto"] } | ||
| 24 | 28 | ||
| 25 | defmt = "0.2.3" | 29 | defmt = "0.2.3" |
| 26 | defmt-rtt = "0.2.0" | 30 | defmt-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"] | ||
| 9 | mod example_common; | ||
| 10 | |||
| 11 | use embassy_lora::{stm32wl::*, LoraTimer}; | ||
| 12 | use 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 | }; | ||
| 21 | use lorawan_device::async_device::{region, Device, JoinMode}; | ||
| 22 | use lorawan_encoding::default_crypto::DefaultFactory as Crypto; | ||
| 23 | |||
| 24 | fn 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()")] | ||
| 31 | async 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 | } | ||
