2022-09-28 04:55:41 +02:00
|
|
|
//! This example runs on the RAK4631 WisBlock, which has an nRF52840 MCU and Semtech Sx126x radio.
|
2022-10-10 19:35:42 +02:00
|
|
|
//! Other nrf/sx126x combinations may work with appropriate pin modifications.
|
|
|
|
//! It demonstates LORA P2P functionality in conjunction with example lora_p2p_sense.rs.
|
2022-09-28 04:55:41 +02:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
#![macro_use]
|
|
|
|
#![allow(dead_code)]
|
|
|
|
#![feature(type_alias_impl_trait)]
|
|
|
|
|
|
|
|
use defmt::*;
|
|
|
|
use embassy_executor::Spawner;
|
|
|
|
use embassy_lora::sx126x::*;
|
|
|
|
use embassy_nrf::gpio::{Input, Level, Output, OutputDrive, Pin as _, Pull};
|
2023-03-05 22:00:52 +01:00
|
|
|
use embassy_nrf::{bind_interrupts, peripherals, spim};
|
2022-09-28 04:55:41 +02:00
|
|
|
use embassy_time::{Duration, Timer};
|
|
|
|
use lorawan_device::async_device::radio::{Bandwidth, CodingRate, PhyRxTx, RfConfig, SpreadingFactor};
|
|
|
|
use {defmt_rtt as _, panic_probe as _};
|
|
|
|
|
2023-03-05 22:00:52 +01:00
|
|
|
bind_interrupts!(struct Irqs {
|
|
|
|
SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1 => spim::InterruptHandler<peripherals::TWISPI1>;
|
|
|
|
});
|
|
|
|
|
2022-09-28 04:55:41 +02:00
|
|
|
#[embassy_executor::main]
|
|
|
|
async fn main(_spawner: Spawner) {
|
|
|
|
let p = embassy_nrf::init(Default::default());
|
|
|
|
let mut spi_config = spim::Config::default();
|
2022-10-10 19:35:42 +02:00
|
|
|
spi_config.frequency = spim::Frequency::M16;
|
2022-09-28 04:55:41 +02:00
|
|
|
|
|
|
|
let mut radio = {
|
2023-03-05 22:00:52 +01:00
|
|
|
let spim = spim::Spim::new(p.TWISPI1, Irqs, p.P1_11, p.P1_13, p.P1_12, spi_config);
|
2022-09-28 04:55:41 +02:00
|
|
|
|
2022-10-08 21:32:22 +02:00
|
|
|
let cs = Output::new(p.P1_10.degrade(), Level::High, OutputDrive::Standard);
|
|
|
|
let reset = Output::new(p.P1_06.degrade(), Level::High, OutputDrive::Standard);
|
2022-09-28 04:55:41 +02:00
|
|
|
let dio1 = Input::new(p.P1_15.degrade(), Pull::Down);
|
|
|
|
let busy = Input::new(p.P1_14.degrade(), Pull::Down);
|
2022-10-08 21:32:22 +02:00
|
|
|
let antenna_rx = Output::new(p.P1_05.degrade(), Level::Low, OutputDrive::Standard);
|
|
|
|
let antenna_tx = Output::new(p.P1_07.degrade(), Level::Low, OutputDrive::Standard);
|
2022-09-28 04:55:41 +02:00
|
|
|
|
|
|
|
match Sx126xRadio::new(spim, cs, reset, antenna_rx, antenna_tx, dio1, busy, false).await {
|
|
|
|
Ok(r) => r,
|
|
|
|
Err(err) => {
|
|
|
|
info!("Sx126xRadio error = {}", err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut debug_indicator = Output::new(p.P1_03, Level::Low, OutputDrive::Standard);
|
|
|
|
let mut start_indicator = Output::new(p.P1_04, Level::Low, OutputDrive::Standard);
|
|
|
|
|
|
|
|
start_indicator.set_high();
|
|
|
|
Timer::after(Duration::from_secs(5)).await;
|
|
|
|
start_indicator.set_low();
|
|
|
|
|
2022-10-10 19:35:42 +02:00
|
|
|
loop {
|
|
|
|
let rf_config = RfConfig {
|
|
|
|
frequency: 903900000, // channel in Hz
|
|
|
|
bandwidth: Bandwidth::_250KHz,
|
|
|
|
spreading_factor: SpreadingFactor::_10,
|
|
|
|
coding_rate: CodingRate::_4_8,
|
|
|
|
};
|
2022-09-28 04:55:41 +02:00
|
|
|
|
2022-10-10 19:35:42 +02:00
|
|
|
let mut buffer = [00u8; 100];
|
2022-09-28 04:55:41 +02:00
|
|
|
|
2022-10-10 19:35:42 +02:00
|
|
|
// P2P receive
|
|
|
|
match radio.rx(rf_config, &mut buffer).await {
|
|
|
|
Ok((buffer_len, rx_quality)) => info!(
|
|
|
|
"RX received = {:?} with length = {} rssi = {} snr = {}",
|
|
|
|
&buffer[0..buffer_len],
|
|
|
|
buffer_len,
|
|
|
|
rx_quality.rssi(),
|
|
|
|
rx_quality.snr()
|
|
|
|
),
|
|
|
|
Err(err) => info!("RX error = {}", err),
|
|
|
|
}
|
2022-09-28 04:55:41 +02:00
|
|
|
|
2022-10-10 19:35:42 +02:00
|
|
|
debug_indicator.set_high();
|
|
|
|
Timer::after(Duration::from_secs(2)).await;
|
|
|
|
debug_indicator.set_low();
|
2022-09-28 04:55:41 +02:00
|
|
|
}
|
|
|
|
}
|