104 lines
3.3 KiB
Rust
104 lines
3.3 KiB
Rust
|
//! This example runs on a STM32WL board, which has a builtin Semtech Sx1262 radio.
|
||
|
//! It demonstrates LORA P2P send functionality.
|
||
|
#![no_std]
|
||
|
#![no_main]
|
||
|
#![macro_use]
|
||
|
#![feature(type_alias_impl_trait, async_fn_in_trait)]
|
||
|
#![allow(incomplete_features)]
|
||
|
|
||
|
use defmt::info;
|
||
|
use embassy_embedded_hal::adapter::BlockingAsync;
|
||
|
use embassy_executor::Spawner;
|
||
|
use embassy_lora::iv::Stm32wlInterfaceVariant;
|
||
|
use embassy_stm32::dma::NoDma;
|
||
|
use embassy_stm32::gpio::{Level, Output, Pin, Speed};
|
||
|
use embassy_stm32::peripherals::SUBGHZSPI;
|
||
|
use embassy_stm32::rcc::low_level::RccPeripheral;
|
||
|
use embassy_stm32::spi::{BitOrder, Config as SpiConfig, Spi, MODE_0};
|
||
|
use embassy_stm32::time::Hertz;
|
||
|
use embassy_stm32::{interrupt, into_ref, Peripheral};
|
||
|
use embassy_time::Delay;
|
||
|
use lora_phy::mod_params::*;
|
||
|
use lora_phy::sx1261_2::SX1261_2;
|
||
|
use lora_phy::LoRa;
|
||
|
use {defmt_rtt as _, panic_probe as _};
|
||
|
|
||
|
#[embassy_executor::main]
|
||
|
async fn main(_spawner: Spawner) {
|
||
|
let mut config = embassy_stm32::Config::default();
|
||
|
config.rcc.mux = embassy_stm32::rcc::ClockSrc::HSE32;
|
||
|
let p = embassy_stm32::init(config);
|
||
|
|
||
|
let clk = Hertz(core::cmp::min(SUBGHZSPI::frequency().0 / 2, 16_000_000));
|
||
|
let mut spi_config = SpiConfig::default();
|
||
|
spi_config.mode = MODE_0;
|
||
|
spi_config.bit_order = BitOrder::MsbFirst;
|
||
|
let spi = Spi::new_subghz(p.SUBGHZSPI, NoDma, NoDma, clk, spi_config);
|
||
|
|
||
|
let spi = BlockingAsync::new(spi);
|
||
|
|
||
|
let irq = interrupt::take!(SUBGHZ_RADIO);
|
||
|
into_ref!(irq);
|
||
|
// Set CTRL1 and CTRL3 for high-power transmission, while CTRL2 acts as an RF switch between tx and rx
|
||
|
let _ctrl1 = Output::new(p.PC4.degrade(), Level::Low, Speed::High);
|
||
|
let ctrl2 = Output::new(p.PC5.degrade(), Level::High, Speed::High);
|
||
|
let _ctrl3 = Output::new(p.PC3.degrade(), Level::High, Speed::High);
|
||
|
let iv = Stm32wlInterfaceVariant::new(irq, None, Some(ctrl2)).unwrap();
|
||
|
|
||
|
let mut delay = Delay;
|
||
|
|
||
|
let mut lora = {
|
||
|
match LoRa::new(SX1261_2::new(BoardType::Stm32wlSx1262, spi, iv), false, &mut delay).await {
|
||
|
Ok(l) => l,
|
||
|
Err(err) => {
|
||
|
info!("Radio error = {}", err);
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
|
||
|
let mdltn_params = {
|
||
|
match lora.create_modulation_params(SpreadingFactor::_10, Bandwidth::_250KHz, CodingRate::_4_8, 903900000) {
|
||
|
Ok(mp) => mp,
|
||
|
Err(err) => {
|
||
|
info!("Radio error = {}", err);
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
|
||
|
let mut tx_pkt_params = {
|
||
|
match lora.create_tx_packet_params(4, false, true, false, &mdltn_params) {
|
||
|
Ok(pp) => pp,
|
||
|
Err(err) => {
|
||
|
info!("Radio error = {}", err);
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
|
||
|
match lora.prepare_for_tx(&mdltn_params, 20, false).await {
|
||
|
Ok(()) => {}
|
||
|
Err(err) => {
|
||
|
info!("Radio error = {}", err);
|
||
|
return;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
let buffer = [0x01u8, 0x02u8, 0x03u8];
|
||
|
match lora.tx(&mdltn_params, &mut tx_pkt_params, &buffer, 0xffffff).await {
|
||
|
Ok(()) => {
|
||
|
info!("TX DONE");
|
||
|
}
|
||
|
Err(err) => {
|
||
|
info!("Radio error = {}", err);
|
||
|
return;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
match lora.sleep(&mut delay).await {
|
||
|
Ok(()) => info!("Sleep successful"),
|
||
|
Err(err) => info!("Sleep unsuccessful = {}", err),
|
||
|
}
|
||
|
}
|