2023-04-21 08:20:46 +02:00
|
|
|
//! This example runs on the STM32 LoRa Discovery board, which has a builtin Semtech Sx1276 radio.
|
2023-04-24 01:32:34 +02:00
|
|
|
//! It demonstrates LORA P2P receive functionality in conjunction with the lora_p2p_send example.
|
2023-04-21 08:20:46 +02:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
#![macro_use]
|
|
|
|
#![feature(type_alias_impl_trait)]
|
|
|
|
|
|
|
|
use defmt::*;
|
|
|
|
use embassy_executor::Spawner;
|
|
|
|
use embassy_lora::iv::Stm32l0InterfaceVariant;
|
|
|
|
use embassy_stm32::exti::{Channel, ExtiInput};
|
|
|
|
use embassy_stm32::gpio::{Input, Level, Output, Pin, Pull, Speed};
|
|
|
|
use embassy_stm32::spi;
|
|
|
|
use embassy_stm32::time::khz;
|
2023-10-15 01:57:25 +02:00
|
|
|
use embassy_time::{Delay, Timer};
|
2023-04-21 08:20:46 +02:00
|
|
|
use lora_phy::mod_params::*;
|
|
|
|
use lora_phy::sx1276_7_8_9::SX1276_7_8_9;
|
|
|
|
use lora_phy::LoRa;
|
|
|
|
use {defmt_rtt as _, panic_probe as _};
|
|
|
|
|
2023-04-24 01:32:34 +02:00
|
|
|
const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region
|
|
|
|
|
2023-04-21 08:20:46 +02:00
|
|
|
#[embassy_executor::main]
|
|
|
|
async fn main(_spawner: Spawner) {
|
|
|
|
let mut config = embassy_stm32::Config::default();
|
2023-10-22 22:39:55 +02:00
|
|
|
config.rcc.mux = embassy_stm32::rcc::ClockSrc::HSI;
|
2023-04-21 08:20:46 +02:00
|
|
|
config.rcc.enable_hsi48 = true;
|
|
|
|
let p = embassy_stm32::init(config);
|
|
|
|
|
2023-07-30 19:26:24 +02:00
|
|
|
let mut spi_config = spi::Config::default();
|
|
|
|
spi_config.frequency = khz(200);
|
|
|
|
|
2023-04-21 08:20:46 +02:00
|
|
|
// SPI for sx1276
|
2023-07-30 19:31:22 +02:00
|
|
|
let spi = spi::Spi::new(p.SPI1, p.PB3, p.PA7, p.PA6, p.DMA1_CH3, p.DMA1_CH2, spi_config);
|
2023-04-21 08:20:46 +02:00
|
|
|
|
|
|
|
let nss = Output::new(p.PA15.degrade(), Level::High, Speed::Low);
|
|
|
|
let reset = Output::new(p.PC0.degrade(), Level::High, Speed::Low);
|
|
|
|
|
|
|
|
let irq_pin = Input::new(p.PB4.degrade(), Pull::Up);
|
|
|
|
let irq = ExtiInput::new(irq_pin, p.EXTI4.degrade());
|
|
|
|
|
|
|
|
let iv = Stm32l0InterfaceVariant::new(nss, reset, irq, None, None).unwrap();
|
|
|
|
|
|
|
|
let mut lora = {
|
2023-09-24 17:33:03 +02:00
|
|
|
match LoRa::new(SX1276_7_8_9::new(BoardType::Stm32l0Sx1276, spi, iv), false, Delay).await {
|
2023-04-21 08:20:46 +02:00
|
|
|
Ok(l) => l,
|
|
|
|
Err(err) => {
|
|
|
|
info!("Radio error = {}", err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut debug_indicator = Output::new(p.PB5, Level::Low, Speed::Low);
|
|
|
|
let mut start_indicator = Output::new(p.PB6, Level::Low, Speed::Low);
|
|
|
|
|
|
|
|
start_indicator.set_high();
|
2023-10-15 01:57:25 +02:00
|
|
|
Timer::after_secs(5).await;
|
2023-04-21 08:20:46 +02:00
|
|
|
start_indicator.set_low();
|
|
|
|
|
|
|
|
let mut receiving_buffer = [00u8; 100];
|
|
|
|
|
|
|
|
let mdltn_params = {
|
2023-04-24 01:32:34 +02:00
|
|
|
match lora.create_modulation_params(
|
|
|
|
SpreadingFactor::_10,
|
|
|
|
Bandwidth::_250KHz,
|
|
|
|
CodingRate::_4_8,
|
|
|
|
LORA_FREQUENCY_IN_HZ,
|
|
|
|
) {
|
2023-04-21 08:20:46 +02:00
|
|
|
Ok(mp) => mp,
|
|
|
|
Err(err) => {
|
|
|
|
info!("Radio error = {}", err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let rx_pkt_params = {
|
|
|
|
match lora.create_rx_packet_params(4, false, receiving_buffer.len() as u8, true, false, &mdltn_params) {
|
|
|
|
Ok(pp) => pp,
|
|
|
|
Err(err) => {
|
|
|
|
info!("Radio error = {}", err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
match lora
|
2023-09-24 17:33:03 +02:00
|
|
|
.prepare_for_rx(&mdltn_params, &rx_pkt_params, None, None, false)
|
2023-04-21 08:20:46 +02:00
|
|
|
.await
|
|
|
|
{
|
|
|
|
Ok(()) => {}
|
|
|
|
Err(err) => {
|
|
|
|
info!("Radio error = {}", err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
loop {
|
|
|
|
receiving_buffer = [00u8; 100];
|
|
|
|
match lora.rx(&rx_pkt_params, &mut receiving_buffer).await {
|
|
|
|
Ok((received_len, _rx_pkt_status)) => {
|
|
|
|
if (received_len == 3)
|
|
|
|
&& (receiving_buffer[0] == 0x01u8)
|
|
|
|
&& (receiving_buffer[1] == 0x02u8)
|
|
|
|
&& (receiving_buffer[2] == 0x03u8)
|
|
|
|
{
|
|
|
|
info!("rx successful");
|
|
|
|
debug_indicator.set_high();
|
2023-10-15 01:57:25 +02:00
|
|
|
Timer::after_secs(5).await;
|
2023-04-21 08:20:46 +02:00
|
|
|
debug_indicator.set_low();
|
|
|
|
} else {
|
|
|
|
info!("rx unknown packet");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(err) => info!("rx unsuccessful = {}", err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|