Skip to content
Snippets Groups Projects
Commit 99030bb3 authored by Stephen D's avatar Stephen D
Browse files

reset stm32 on powerup and on any error

parent c80078fa
No related branches found
No related tags found
1 merge request!4Spi
......@@ -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(())
}
}
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<()> {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment