Skip to content
Snippets Groups Projects
radio.rs 3.54 KiB
Newer Older
Stephen D's avatar
Stephen D committed
use ham_cats::{buffer::Buffer, packet::Packet};
Stephen D's avatar
Stephen D committed
use rf4463::{config::RADIO_CONFIG_CATS, error::TransferError, Rf4463};
Stephen D's avatar
Stephen D committed
use rtic::Mutex;
Stephen D's avatar
Stephen D committed
use stm32f4xx_hal::{
    gpio,
Stephen D's avatar
Stephen D committed
    hal::digital::v2::OutputPin,
Stephen D's avatar
Stephen D committed
    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> {
Stephen D's avatar
Stephen D committed
    radio: Radio,
Stephen D's avatar
Stephen D committed
    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,
Stephen D's avatar
Stephen D committed
    ) -> Option<Self> {
        let mut radio = Rf4463::new(spi, sdn, cs, delay, &mut RADIO_CONFIG_CATS.clone()).ok()?;
Stephen D's avatar
Stephen D committed

        // sets us up for the default CATS frequency, 430.500 MHz
        radio.set_channel(20);

Stephen D's avatar
Stephen D committed
        radio.start_rx(None, false).ok()?;
Stephen D's avatar
Stephen D committed

Stephen D's avatar
Stephen D committed
        Some(Self {
Stephen D's avatar
Stephen D committed
            radio,
            buf,
            enable_digipeating,
        })
    }

    // call me every 20-ish ms
    // technically needs to be every 100ms, tops
Stephen D's avatar
Stephen D committed
    // digipeats only if ident is Some,
    // otherwise the packet is discarded
Stephen D's avatar
Stephen D committed
    pub fn tick<P: OutputPin, M: Mutex<T = P>>(
        &mut self,
        led: &mut M,
        ident: Option<(&str, u8)>,
    ) -> Result<(), TransferError<spi::Error>> {
Stephen D's avatar
Stephen D committed
        if self.radio.is_idle() {
            self.radio
Stephen D's avatar
Stephen D committed
                .start_rx(None, false)
Stephen D's avatar
Stephen D committed
                .map_err(TransferError::SpiError)?;
        }

Stephen D's avatar
Stephen D committed
        self.radio.interrupt(Some(self.buf), None)?;
Stephen D's avatar
Stephen D committed

Stephen D's avatar
Stephen D committed
        if let Some(data) = self
            .radio
            .finish_rx(self.buf)
            .map_err(TransferError::SpiError)?
        {
Stephen D's avatar
Stephen D committed
            if self.enable_digipeating {
                if let Some((callsign, ssid)) = ident {
Stephen D's avatar
Stephen D committed
                    let mut buf = [0; MAX_PACKET_LEN];
Stephen D's avatar
Stephen D committed
                    if let Ok(packet) = Packet::fully_decode(data.data(), &mut buf) {
Stephen D's avatar
Stephen D committed
                        self.handle_packet_rx(led, packet, callsign, ssid);
Stephen D's avatar
Stephen D committed
                    }
Stephen D's avatar
Stephen D committed
                }
            }

            self.radio
Stephen D's avatar
Stephen D committed
                .start_rx(None, false)
Stephen D's avatar
Stephen D committed
                .map_err(TransferError::SpiError)?;
        }

        Ok(())
    }

Stephen D's avatar
Stephen D committed
    // 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<()> {
Stephen D's avatar
Stephen D committed
        // don't want to transmit on top of a packet
        while self.radio.is_busy_rxing().ok()? {
Stephen D's avatar
Stephen D committed
            self.tick(led, ident).ok()?;
Stephen D's avatar
Stephen D committed
        }

Stephen D's avatar
Stephen D committed
        led.lock(|l| l.set_high().ok());
Stephen D's avatar
Stephen D committed
        self.radio.start_tx(data).ok()?;

        while !self.radio.is_idle() {
            self.radio.interrupt(None, Some(data)).ok();
        }
Stephen D's avatar
Stephen D committed
        led.lock(|l| l.set_low().ok());
Stephen D's avatar
Stephen D committed

        Some(())
    }

Stephen D's avatar
Stephen D committed
    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,
    ) {
Stephen D's avatar
Stephen D committed
        if packet.should_digipeat(callsign, ssid).is_ok() {
            if packet.append_to_route(callsign, ssid).is_err() {
Stephen D's avatar
Stephen D committed
                return;
            }

Stephen D's avatar
Stephen D committed
            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);
Stephen D's avatar
Stephen D committed
            }
        }
    }
}