2021-09-30 09:25:45 +02:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
#![macro_use]
|
|
|
|
#![allow(dead_code)]
|
|
|
|
#![feature(generic_associated_types)]
|
|
|
|
#![feature(type_alias_impl_trait)]
|
|
|
|
|
2022-08-17 23:40:16 +02:00
|
|
|
use embassy_executor::Spawner;
|
2022-06-12 22:15:44 +02:00
|
|
|
use embassy_lora::stm32wl::*;
|
|
|
|
use embassy_lora::LoraTimer;
|
|
|
|
use embassy_stm32::dma::NoDma;
|
2022-06-25 10:10:59 +02:00
|
|
|
use embassy_stm32::gpio::{AnyPin, Level, Output, Pin, Speed};
|
2022-06-12 22:15:44 +02:00
|
|
|
use embassy_stm32::rng::Rng;
|
|
|
|
use embassy_stm32::subghz::*;
|
2022-08-17 18:49:55 +02:00
|
|
|
use embassy_stm32::{interrupt, pac};
|
2022-04-26 18:37:46 +02:00
|
|
|
use lorawan::default_crypto::DefaultFactory as Crypto;
|
2021-09-30 09:25:45 +02:00
|
|
|
use lorawan_device::async_device::{region, Device, JoinMode};
|
2022-06-12 22:15:44 +02:00
|
|
|
use {defmt_rtt as _, panic_probe as _};
|
2021-09-30 09:25:45 +02:00
|
|
|
|
2022-06-25 10:10:59 +02:00
|
|
|
struct RadioSwitch<'a> {
|
|
|
|
ctrl1: Output<'a, AnyPin>,
|
|
|
|
ctrl2: Output<'a, AnyPin>,
|
|
|
|
ctrl3: Output<'a, AnyPin>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> RadioSwitch<'a> {
|
|
|
|
fn new(ctrl1: Output<'a, AnyPin>, ctrl2: Output<'a, AnyPin>, ctrl3: Output<'a, AnyPin>) -> Self {
|
|
|
|
Self { ctrl1, ctrl2, ctrl3 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> embassy_lora::stm32wl::RadioSwitch for RadioSwitch<'a> {
|
|
|
|
fn set_rx(&mut self) {
|
|
|
|
self.ctrl1.set_high();
|
|
|
|
self.ctrl2.set_low();
|
|
|
|
self.ctrl3.set_high();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_tx(&mut self) {
|
|
|
|
self.ctrl1.set_low();
|
|
|
|
self.ctrl2.set_high();
|
|
|
|
self.ctrl3.set_high();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-17 22:25:58 +02:00
|
|
|
#[embassy_executor::main]
|
|
|
|
async fn main(_spawner: Spawner) {
|
2021-09-30 09:25:45 +02:00
|
|
|
let mut config = embassy_stm32::Config::default();
|
2022-01-04 11:18:59 +01:00
|
|
|
config.rcc.mux = embassy_stm32::rcc::ClockSrc::HSI16;
|
2022-01-04 23:58:13 +01:00
|
|
|
config.rcc.enable_lsi = true;
|
2022-08-17 22:25:58 +02:00
|
|
|
let p = embassy_stm32::init(config);
|
2021-09-30 09:25:45 +02:00
|
|
|
|
2022-01-04 23:58:13 +01:00
|
|
|
unsafe { pac::RCC.ccipr().modify(|w| w.set_rngsel(0b01)) }
|
2021-09-30 09:25:45 +02:00
|
|
|
|
|
|
|
let ctrl1 = Output::new(p.PC3.degrade(), Level::High, Speed::High);
|
|
|
|
let ctrl2 = Output::new(p.PC4.degrade(), Level::High, Speed::High);
|
|
|
|
let ctrl3 = Output::new(p.PC5.degrade(), Level::High, Speed::High);
|
|
|
|
let rfs = RadioSwitch::new(ctrl1, ctrl2, ctrl3);
|
|
|
|
|
2022-06-24 14:31:30 +02:00
|
|
|
let radio = SubGhz::new(p.SUBGHZSPI, NoDma, NoDma);
|
2021-09-30 09:25:45 +02:00
|
|
|
let irq = interrupt::take!(SUBGHZ_RADIO);
|
2022-06-25 07:01:31 +02:00
|
|
|
|
2022-06-25 11:59:07 +02:00
|
|
|
let mut radio_config = SubGhzRadioConfig::default();
|
|
|
|
radio_config.calibrate_image = CalibrateImage::ISM_863_870;
|
|
|
|
let radio = SubGhzRadio::new(radio, rfs, irq, radio_config).unwrap();
|
2021-09-30 09:25:45 +02:00
|
|
|
|
2022-07-04 10:33:29 +02:00
|
|
|
let mut region: region::Configuration = region::EU868::default().into();
|
|
|
|
|
|
|
|
// NOTE: This is specific for TTN, as they have a special RX1 delay
|
|
|
|
region.set_receive_delay1(5000);
|
|
|
|
|
2022-06-12 22:15:44 +02:00
|
|
|
let mut device: Device<_, Crypto, _, _> = Device::new(region, radio, LoraTimer, Rng::new(p.RNG));
|
2021-09-30 09:25:45 +02:00
|
|
|
|
2022-07-04 10:33:29 +02:00
|
|
|
// Depending on network, this might be part of JOIN
|
|
|
|
device.set_datarate(region::DR::_0); // SF12
|
|
|
|
|
|
|
|
// device.set_datarate(region::DR::_1); // SF11
|
|
|
|
// device.set_datarate(region::DR::_2); // SF10
|
|
|
|
// device.set_datarate(region::DR::_3); // SF9
|
|
|
|
// device.set_datarate(region::DR::_4); // SF8
|
|
|
|
// device.set_datarate(region::DR::_5); // SF7
|
|
|
|
|
2021-09-30 09:25:45 +02:00
|
|
|
defmt::info!("Joining LoRaWAN network");
|
|
|
|
|
|
|
|
// TODO: Adjust the EUI and Keys according to your network credentials
|
|
|
|
device
|
|
|
|
.join(&JoinMode::OTAA {
|
|
|
|
deveui: [0, 0, 0, 0, 0, 0, 0, 0],
|
|
|
|
appeui: [0, 0, 0, 0, 0, 0, 0, 0],
|
|
|
|
appkey: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.ok()
|
|
|
|
.unwrap();
|
|
|
|
defmt::info!("LoRaWAN network joined");
|
|
|
|
|
2022-07-04 10:33:29 +02:00
|
|
|
let mut rx: [u8; 255] = [0; 255];
|
2021-09-30 09:25:45 +02:00
|
|
|
defmt::info!("Sending 'PING'");
|
2022-07-04 10:33:29 +02:00
|
|
|
let len = device.send_recv(b"PING", &mut rx[..], 1, true).await.ok().unwrap();
|
|
|
|
if len > 0 {
|
|
|
|
defmt::info!("Message sent, received downlink: {:?}", &rx[..len]);
|
|
|
|
} else {
|
|
|
|
defmt::info!("Message sent!");
|
|
|
|
}
|
2021-09-30 09:25:45 +02:00
|
|
|
}
|