From 99030bb351c1dcf1d95acf65085d3177cf424d35 Mon Sep 17 00:00:00 2001 From: Stephen D <webmaster@scd31.com> Date: Sun, 9 Jul 2023 11:31:38 -0300 Subject: [PATCH] reset stm32 on powerup and on any error --- src/control.rs | 70 ++++++++++++++++++++++++++++---------------------- src/radio.rs | 21 ++++++++++++--- 2 files changed, 58 insertions(+), 33 deletions(-) diff --git a/src/control.rs b/src/control.rs index 74827ab..ef709de 100644 --- a/src/control.rs +++ b/src/control.rs @@ -101,42 +101,52 @@ impl Controller { let mut text_msg_id = 0; let mut last_got_temp = Instant::now(); loop { - while let Ok(tm) = telem_rx.try_recv() { - let tm = tm.into_raw(&mut text_msg_id).into(); - - if let Err(e) = radio.send_packet(&tm) { - eprintln!("Could not send packet: {}", e); - } + if let Err(e) = Self::tx_thread_single_iter( + &callsign, + &image_rx, + &telem_rx, + &mut text_msg_id, + &mut last_got_temp, + &mut radio, + ) { + eprintln!("Radio error: {}", e); + radio.reset(); } + } + } - if let Ok(img) = image_rx.try_recv() { - if let Err(e) = radio.send_packet(&img) { - eprintln!("Could not send packet: {}", e); - } - } else { - if let Err(e) = radio.flush() { - eprintln!("Could not flush radio: {}", e); - } - thread::sleep(Duration::from_millis(50)); - } + fn tx_thread_single_iter( + callsign: &str, + image_rx: &Receiver<FecPacket>, + telem_rx: &Receiver<Packet>, + text_msg_id: &mut u16, + last_got_temp: &mut Instant, + radio: &mut McuRadio, + ) -> anyhow::Result<()> { + while let Ok(tm) = telem_rx.try_recv() { + let tm = tm.into_raw(text_msg_id).into(); + + radio.send_packet(&tm)?; + } - if Instant::now() - last_got_temp > TEMP_REFRESH_INTERVAL { - last_got_temp = Instant::now(); + if let Ok(img) = image_rx.try_recv() { + radio.send_packet(&img)?; + } else { + radio.flush()?; - let temp = match radio.get_temp() { - Ok(x) => x, - Err(e) => { - eprintln!("Could not get radio temp: {}", e); - continue; - } - }; + thread::sleep(Duration::from_millis(50)); + } - let packet = Packet::new_text_message(&callsign, &format!("Temp: {}", temp)); + if Instant::now() - *last_got_temp > TEMP_REFRESH_INTERVAL { + *last_got_temp = Instant::now(); - if let Err(e) = radio.send_packet(&packet.into_raw(&mut text_msg_id).into()) { - eprintln!("Could not send packet: {}", e); - } - } + let temp = radio.get_temp()?; + + let packet = Packet::new_text_message(callsign, &format!("Temp: {}", temp)); + + radio.send_packet(&packet.into_raw(text_msg_id).into())?; } + + Ok(()) } } diff --git a/src/radio.rs b/src/radio.rs index 4f66a44..6a35abf 100644 --- a/src/radio.rs +++ b/src/radio.rs @@ -1,5 +1,8 @@ use anyhow::anyhow; -use rppal::spi::{Bus, Mode, SlaveSelect, Spi}; +use rppal::{ + gpio::{Gpio, OutputPin}, + spi::{Bus, Mode, SlaveSelect, Spi}, +}; use std::{thread::sleep, time::Duration}; use crate::packet::FecPacket; @@ -15,14 +18,26 @@ const FREE_SPACE: u8 = 0xAA; // The microcontroller takes in packet data over SPI and sends it to the RF4463 pub struct McuRadio { spi: Spi, + reset_pin: OutputPin, } -// TODO reset mcu on initialization impl McuRadio { pub fn new() -> anyhow::Result<Self> { let spi = Spi::new(Bus::Spi0, SlaveSelect::Ss1, 1_000_000, Mode::Mode0)?; + let reset_pin = Gpio::new()?.get(8)?.into_output_high(); - Ok(Self { spi }) + let mut s = Self { reset_pin, spi }; + + s.reset(); + + Ok(s) + } + + pub fn reset(&mut self) { + self.reset_pin.set_low(); + sleep(Duration::from_millis(10)); + self.reset_pin.set_high(); + sleep(Duration::from_millis(1000)); } pub fn send_packet(&mut self, packet: &FecPacket) -> anyhow::Result<()> { -- GitLab