From 4ed5e89bd60607e91bb8059ebc5d760e3a15d483 Mon Sep 17 00:00:00 2001
From: Stephen D <webmaster@scd31.com>
Date: Thu, 1 Jun 2023 22:29:05 -0300
Subject: [PATCH] move back to uart

---
 Cargo.lock     | 102 -------------------------------------------------
 Cargo.toml     |   4 +-
 src/control.rs |   9 ++---
 src/radio.rs   |  85 +++++------------------------------------
 4 files changed, 13 insertions(+), 187 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index 5c218dc..c20a8e5 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -40,13 +40,10 @@ dependencies = [
  "crc",
  "image",
  "kiss-tnc",
- "rf4463",
- "rppal",
  "serde",
  "serial",
  "sha3",
  "subprocess",
- "thread-priority",
  "tokio",
  "toml",
 ]
@@ -234,32 +231,6 @@ version = "1.8.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91"
 
-[[package]]
-name = "embedded-hal"
-version = "0.2.7"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "35949884794ad573cf46071e41c9b60efb0cb311e3ca01f7af807af1debc66ff"
-dependencies = [
- "nb 0.1.3",
- "void",
-]
-
-[[package]]
-name = "embedded-hal"
-version = "1.0.0-alpha.9"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "129b101ddfee640565f7c07b301a31d95aa21e5acef21a491c307139f5fa4c91"
-
-[[package]]
-name = "embedded-hal-nb"
-version = "1.0.0-alpha.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "7e0760ec0a3bf76859d5e33f39542af103f157d5b2ecfb00ace56dd461472e3a"
-dependencies = [
- "embedded-hal 1.0.0-alpha.9",
- "nb 1.1.0",
-]
-
 [[package]]
 name = "exr"
 version = "1.6.3"
@@ -544,21 +515,6 @@ dependencies = [
  "getrandom",
 ]
 
-[[package]]
-name = "nb"
-version = "0.1.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "801d31da0513b6ec5214e9bf433a77966320625a37860f910be265be6e18d06f"
-dependencies = [
- "nb 1.1.0",
-]
-
-[[package]]
-name = "nb"
-version = "1.1.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "8d5439c4ad607c3c23abf66de8c8bf57ba8adcd1f129e699851a6e43935d339d"
-
 [[package]]
 name = "num-integer"
 version = "0.1.45"
@@ -731,34 +687,6 @@ dependencies = [
  "bitflags",
 ]
 
-[[package]]
-name = "rf4463"
-version = "0.1.0"
-dependencies = [
- "embedded-hal 0.2.7",
-]
-
-[[package]]
-name = "rppal"
-version = "0.14.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "612e1a22e21f08a246657c6433fe52b773ae43d07c9ef88ccfc433cc8683caba"
-dependencies = [
- "embedded-hal 0.2.7",
- "embedded-hal 1.0.0-alpha.9",
- "embedded-hal-nb",
- "libc",
- "nb 0.1.3",
- "spin_sleep",
- "void",
-]
-
-[[package]]
-name = "rustversion"
-version = "1.0.12"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "4f3208ce4d8448b3f3e7d168a73f5e0c43a61e32930de3bceeccedb388b6bf06"
-
 [[package]]
 name = "scopeguard"
 version = "1.1.0"
@@ -886,16 +814,6 @@ dependencies = [
  "lock_api",
 ]
 
-[[package]]
-name = "spin_sleep"
-version = "1.1.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "cafa7900db085f4354dbc7025e25d7a839a14360ea13b5fc4fd717f2d3b23134"
-dependencies = [
- "once_cell",
- "winapi",
-]
-
 [[package]]
 name = "subprocess"
 version = "0.2.9"
@@ -952,20 +870,6 @@ dependencies = [
  "syn",
 ]
 
-[[package]]
-name = "thread-priority"
-version = "0.13.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "0c56ce92f1285eaaa11fc1a3201e25de97898c50e87caa4c2aee836fe05288de"
-dependencies = [
- "bitflags",
- "cfg-if",
- "libc",
- "log",
- "rustversion",
- "winapi",
-]
-
 [[package]]
 name = "tiff"
 version = "0.8.1"
@@ -1059,12 +963,6 @@ version = "0.9.4"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
 
-[[package]]
-name = "void"
-version = "1.0.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"
-
 [[package]]
 name = "wasi"
 version = "0.11.0+wasi-snapshot-preview1"
diff --git a/Cargo.toml b/Cargo.toml
index c6d27fb..18d3f05 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -23,12 +23,10 @@ bitvec = "1.0.1"
 crc = "3.0.1"
 image = "0.24.6"
 kiss-tnc = "0.2.2"
-rppal = { version = "0.14.1", features = ["hal"] }
 serde = { version = "1.0.160", features = ["derive"] }
 serial = "0.4.0"
 sha3 = "0.10.7"
 subprocess = "0.2.9"
 tokio = { version = "1.27.0", features = ["full"] }
 toml = "0.7.3"
-rf4463 = { path = "../rf4463" }
-thread-priority = "0.13.1"
+
diff --git a/src/control.rs b/src/control.rs
index 62acf44..ae899f3 100644
--- a/src/control.rs
+++ b/src/control.rs
@@ -5,14 +5,13 @@ use std::{
 };
 
 use anyhow::Context;
-use thread_priority::ThreadPriority;
 
 use crate::{
     aprs::CommandHandler,
     config::Config,
     img::ImgManager,
     packet::{FecPacket, Packet},
-    radio::{SpiRadio, UartRadio},
+    radio::UartRadio,
     ssdv::ssdv_encode,
 };
 
@@ -81,11 +80,9 @@ impl Controller {
 
     // manages our transceiver
     fn tx_thread(image_rx: Receiver<FecPacket>, telem_rx: Receiver<Packet>) {
-        ThreadPriority::Max.set_for_current().unwrap();
-
-        //let mut radio = UartRadio::new("/dev/ttyAMA0").expect("Could not initialize radio");
         let mut radio = loop {
-            let r = SpiRadio::new();
+            // TODO this should be configurable
+            let r = UartRadio::new("/dev/ttyAMA0");
 
             match r {
                 Ok(r) => break r,
diff --git a/src/radio.rs b/src/radio.rs
index 56c82db..1f33b48 100644
--- a/src/radio.rs
+++ b/src/radio.rs
@@ -1,12 +1,3 @@
-use rf4463::{
-    config::{RADIO_CONFIG_500_2, RADIO_CONFIG_5_4},
-    Rf4463, TransferError,
-};
-use rppal::{
-    gpio::{Gpio, OutputPin},
-    hal::Delay,
-    spi::{self, Bus, Mode, SlaveSelect, Spi},
-};
 use serial::{unix::TTYPort, BaudRate::BaudOther, SerialPort};
 use std::{
     io::{Read, Write},
@@ -31,81 +22,23 @@ impl UartRadio {
     }
 
     pub fn send_packet(&mut self, packet: &FecPacket) -> anyhow::Result<()> {
-        loop {
-            self.uart.write_all(&[0x00])?;
-            self.uart.flush()?;
-
-            let mut buf = [0; 1];
-            self.uart.read_exact(&mut buf)?;
-
-            if buf == [0x02] {
-                break; // enough space for packet
-            } else {
-                sleep(Duration::from_millis(10));
-            }
-        }
-
         self.uart.write_all(&[0x01])?;
         self.uart.write_all(&packet.0)?;
 
-        Ok(())
-    }
-}
-
-pub struct SpiRadio {
-    radio: Rf4463<Spi, OutputPin, OutputPin, Delay>,
-    failures: u32,
-    total: u32,
-}
-
-impl SpiRadio {
-    pub fn new() -> anyhow::Result<Self> {
-        let spi = Spi::new(Bus::Spi0, SlaveSelect::Ss0, 10_000_000, Mode::Mode0)?;
-        let gpio = Gpio::new()?;
-        let sdn = gpio.get(22)?.into_output();
-        let cs = gpio.get(24)?.into_output();
-
-        let delay = Delay::new();
+        let mut buf = [0; 1];
+        self.uart.read_exact(&mut buf)?;
 
-        // todo fix this unwrap
-        let radio = Rf4463::new(spi, sdn, cs, delay, &mut RADIO_CONFIG_500_2.clone()).unwrap();
-
-        Ok(Self {
-            radio,
-            failures: 0,
-            total: 0,
-        })
-    }
+        // wait until we have space for packets
+        while buf != [0x02] {
+            println!("full buf! sleeping...");
+            sleep(Duration::from_millis(10));
 
-    pub fn send_packet(&mut self, packet: &FecPacket) -> anyhow::Result<()> {
-        self.total += 1;
-
-        // todo fix this unwrap
-        if self.actually_send(packet, 3).is_err() {
-            self.failures += 1;
+            self.uart.write_all(&[0x00])?;
+            self.uart.flush()?;
 
-            println!(
-                "{} % fail rate",
-                (self.failures as f64) / (self.total as f64)
-            );
+            self.uart.read_exact(&mut buf)?;
         }
 
         Ok(())
     }
-
-    fn actually_send(
-        &mut self,
-        packet: &FecPacket,
-        tries: u32,
-    ) -> Result<(), TransferError<spi::Error>> {
-        if tries == 0 {
-            return Err(TransferError::FifoOverflow);
-        }
-
-        match self.radio.tx(&mut packet.0.clone()) {
-            Ok(()) => Ok(()),
-            Err(TransferError::FifoOverflow) => self.actually_send(packet, tries - 1),
-            Err(e) => Err(e),
-        }
-    }
 }
-- 
GitLab