-
Notifications
You must be signed in to change notification settings - Fork 46
/
lora_p2p_send_multicore.rs
139 lines (121 loc) · 4.19 KB
/
lora_p2p_send_multicore.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//! This example runs on the Raspberry Pi Pico with a Waveshare board containing a Semtech Sx1262 radio.
//! It demonstrates LORA P2P send functionality using the second core, with data provided by the first core.
#![no_std]
#![no_main]
use defmt::*;
use embassy_executor::Executor;
use embassy_rp::gpio::{AnyPin, Input, Level, Output, Pin, Pull};
use embassy_rp::multicore::{spawn_core1, Stack};
use embassy_rp::peripherals::SPI1;
use embassy_rp::spi::{Async, Config, Spi};
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
use embassy_sync::channel::Channel;
use embassy_time::{Delay, Timer};
use embedded_hal_bus::spi::ExclusiveDevice;
use lora_phy::iv::GenericSx126xInterfaceVariant;
use lora_phy::sx126x::{Sx1262, Sx126x, TcxoCtrlVoltage};
use lora_phy::LoRa;
use lora_phy::{mod_params::*, sx126x};
use static_cell::StaticCell;
use {defmt_rtt as _, panic_probe as _};
static mut CORE1_STACK: Stack<4096> = Stack::new();
static EXECUTOR0: StaticCell<Executor> = StaticCell::new();
static EXECUTOR1: StaticCell<Executor> = StaticCell::new();
static CHANNEL: Channel<CriticalSectionRawMutex, [u8; 3], 1> = Channel::new();
const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region
#[cortex_m_rt::entry]
fn main() -> ! {
let p = embassy_rp::init(Default::default());
let nss = Output::new(p.PIN_3.degrade(), Level::High);
let reset = Output::new(p.PIN_15.degrade(), Level::High);
let dio1 = Input::new(p.PIN_20.degrade(), Pull::None);
let busy = Input::new(p.PIN_2.degrade(), Pull::None);
let spi = Spi::new(
p.SPI1,
p.PIN_10,
p.PIN_11,
p.PIN_12,
p.DMA_CH0,
p.DMA_CH1,
Config::default(),
);
let spi = ExclusiveDevice::new(spi, nss, Delay);
let iv = GenericSx126xInterfaceVariant::new(reset, dio1, busy, None, None).unwrap();
spawn_core1(p.CORE1, unsafe { &mut CORE1_STACK }, move || {
let executor1 = EXECUTOR1.init(Executor::new());
executor1.run(|spawner| unwrap!(spawner.spawn(core1_task(spi, iv))));
});
let executor0 = EXECUTOR0.init(Executor::new());
executor0.run(|spawner| unwrap!(spawner.spawn(core0_task())));
}
#[embassy_executor::task]
async fn core0_task() {
info!("Hello from core 0");
loop {
CHANNEL.send([0x01u8, 0x02u8, 0x03u8]).await;
Timer::after_millis(60 * 1000).await;
}
}
#[embassy_executor::task]
async fn core1_task(
spi: ExclusiveDevice<Spi<'static, SPI1, Async>, Output<'static, AnyPin>, Delay>,
iv: GenericSx126xInterfaceVariant<Output<'static, AnyPin>, Input<'static, AnyPin>>,
) {
info!("Hello from core 1");
let config = sx126x::Config {
chip: Sx1262,
tcxo_ctrl: Some(TcxoCtrlVoltage::Ctrl1V7),
use_dcdc: true,
rx_boost: false,
};
let mut lora = LoRa::new(Sx126x::new(spi, iv, config), true, Delay).await.unwrap();
let mdltn_params = {
match lora.create_modulation_params(
SpreadingFactor::_10,
Bandwidth::_250KHz,
CodingRate::_4_8,
LORA_FREQUENCY_IN_HZ,
) {
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;
}
}
};
loop {
let buffer: [u8; 3] = CHANNEL.receive().await;
match lora
.prepare_for_tx(&mdltn_params, &mut tx_pkt_params, 20, &buffer)
.await
{
Ok(()) => {}
Err(err) => {
info!("Radio error = {}", err);
return;
}
};
match lora.tx().await {
Ok(()) => {
info!("TX DONE");
}
Err(err) => {
info!("Radio error = {}", err);
return;
}
};
match lora.sleep(false).await {
Ok(()) => info!("Sleep successful"),
Err(err) => info!("Sleep unsuccessful = {}", err),
}
}
}