Select Git revision

Stephen D authored
radio.rs 3.54 KiB
use ham_cats::{buffer::Buffer, packet::Packet};
use rf4463::{config::RADIO_CONFIG_CATS, error::TransferError, Rf4463};
use rtic::Mutex;
use stm32f4xx_hal::{
gpio,
hal::digital::v2::OutputPin,
pac::{SPI1, TIM5},
spi::{self, Spi},
timer::Delay,
};
use crate::MAX_PACKET_LEN;
type SdnPin = gpio::Pin<'B', 12, gpio::Output>;
type CsPin = gpio::Pin<'A', 8, gpio::Output>;
type MyDelay = Delay<TIM5, 1000000>;
type Radio = Rf4463<Spi<SPI1>, SdnPin, CsPin, MyDelay>;
pub struct RadioManager<'a> {
radio: Radio,
buf: &'a mut [u8; MAX_PACKET_LEN],
enable_digipeating: bool,
}
impl<'a> RadioManager<'a> {
pub fn new(
spi: Spi<SPI1>,
sdn: SdnPin,
cs: CsPin,
delay: MyDelay,
buf: &'a mut [u8; MAX_PACKET_LEN],
enable_digipeating: bool,
) -> Option<Self> {
let mut radio = Rf4463::new(spi, sdn, cs, delay, &mut RADIO_CONFIG_CATS.clone()).ok()?;
// sets us up for the default CATS frequency, 430.500 MHz
radio.set_channel(20);
radio.start_rx(None, false).ok()?;
Some(Self {
radio,
buf,
enable_digipeating,
})
}
// call me every 20-ish ms
// technically needs to be every 100ms, tops
// digipeats only if ident is Some,
// otherwise the packet is discarded
pub fn tick<P: OutputPin, M: Mutex<T = P>>(
&mut self,
led: &mut M,
ident: Option<(&str, u8)>,
) -> Result<(), TransferError<spi::Error>> {
if self.radio.is_idle() {
self.radio
.start_rx(None, false)
.map_err(TransferError::SpiError)?;
}
self.radio.interrupt(Some(self.buf), None)?;
if let Some(data) = self
.radio
.finish_rx(self.buf)
.map_err(TransferError::SpiError)?
{
if self.enable_digipeating {
if let Some((callsign, ssid)) = ident {
let mut buf = [0; MAX_PACKET_LEN];
if let Ok(packet) = Packet::fully_decode(data.data(), &mut buf) {
self.handle_packet_rx(led, packet, callsign, ssid);
}
}
}
self.radio
.start_rx(None, false)
.map_err(TransferError::SpiError)?;
}
Ok(())
}
// digipeats only if ident is Some,
// otherwise the rx'd packet is discarded
pub fn tx<P: OutputPin, M: Mutex<T = P>>(
&mut self,
led: &mut M,
data: &[u8],
ident: Option<(&str, u8)>,
) -> Option<()> {
// don't want to transmit on top of a packet
while self.radio.is_busy_rxing().ok()? {
self.tick(led, ident).ok()?;
}
led.lock(|l| l.set_high().ok());
self.radio.start_tx(data).ok()?;
while !self.radio.is_idle() {
self.radio.interrupt(None, Some(data)).ok();
}
led.lock(|l| l.set_low().ok());
Some(())
}
fn handle_packet_rx<P: OutputPin, M: Mutex<T = P>>(
&mut self,
led: &mut M,
mut packet: Packet<MAX_PACKET_LEN>,
callsign: &str,
ssid: u8,
) {
if packet.should_digipeat(callsign, ssid).is_ok() {
if packet.append_to_route(callsign, ssid).is_err() {
return;
}
let mut buf = [0; MAX_PACKET_LEN];
let mut buf = Buffer::new_empty(&mut buf);
if packet.fully_encode(&mut buf).is_ok() {
self.tx(led, &buf, None);
}
}
}
}