2021-09-30 09:25:45 +02:00
|
|
|
//! This example runs on the STM32 LoRa Discovery board which has a builtin Semtech Sx127x radio
|
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
#![macro_use]
|
|
|
|
#![allow(dead_code)]
|
|
|
|
#![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::sx127x::*;
|
|
|
|
use embassy_lora::LoraTimer;
|
|
|
|
use embassy_stm32::exti::ExtiInput;
|
|
|
|
use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed};
|
|
|
|
use embassy_stm32::rng::Rng;
|
2022-08-17 18:49:55 +02:00
|
|
|
use embassy_stm32::spi;
|
2022-07-11 00:36:10 +02:00
|
|
|
use embassy_stm32::time::khz;
|
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-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_hsi48 = true;
|
2022-08-17 22:25:58 +02:00
|
|
|
let p = embassy_stm32::init(config);
|
2021-09-30 09:25:45 +02:00
|
|
|
|
|
|
|
// SPI for sx127x
|
|
|
|
let spi = spi::Spi::new(
|
|
|
|
p.SPI1,
|
|
|
|
p.PB3,
|
|
|
|
p.PA7,
|
|
|
|
p.PA6,
|
2021-12-01 21:59:53 +01:00
|
|
|
p.DMA1_CH3,
|
|
|
|
p.DMA1_CH2,
|
2022-07-11 00:36:10 +02:00
|
|
|
khz(200),
|
2021-09-30 09:25:45 +02:00
|
|
|
spi::Config::default(),
|
|
|
|
);
|
|
|
|
|
|
|
|
let cs = Output::new(p.PA15, Level::High, Speed::Low);
|
|
|
|
let reset = Output::new(p.PC0, Level::High, Speed::Low);
|
|
|
|
let _ = Input::new(p.PB1, Pull::None);
|
|
|
|
|
|
|
|
let ready = Input::new(p.PB4, Pull::Up);
|
|
|
|
let ready_pin = ExtiInput::new(ready, p.EXTI4);
|
|
|
|
|
2022-06-12 22:15:44 +02:00
|
|
|
let radio = Sx127xRadio::new(spi, cs, reset, ready_pin, DummySwitch).await.unwrap();
|
2021-09-30 09:25:45 +02:00
|
|
|
|
|
|
|
let region = region::EU868::default().into();
|
2022-08-26 15:37:20 +02:00
|
|
|
let mut device: Device<_, Crypto, _, _> = Device::new(region, radio, LoraTimer::new(), Rng::new(p.RNG));
|
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");
|
|
|
|
|
|
|
|
defmt::info!("Sending 'PING'");
|
|
|
|
device.send(b"PING", 1, false).await.ok().unwrap();
|
|
|
|
defmt::info!("Message sent!");
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct DummySwitch;
|
|
|
|
impl RadioSwitch for DummySwitch {
|
|
|
|
fn set_rx(&mut self) {}
|
|
|
|
fn set_tx(&mut self) {}
|
|
|
|
}
|