From 98f87dfe06e75c754031519c4c2f8cc6da2a2986 Mon Sep 17 00:00:00 2001 From: Stephen D <webmaster@scd31.com> Date: Thu, 1 Jun 2023 22:26:04 -0300 Subject: [PATCH] working --- Cargo.lock | 26 +++--------------- Cargo.toml | 4 +-- flash.rb | 2 +- src/main.rs | 75 +++++++++++++++++++++++++-------------------------- src/packet.rs | 29 ++++++++++++++++++++ 5 files changed, 72 insertions(+), 64 deletions(-) create mode 100644 src/packet.rs diff --git a/Cargo.lock b/Cargo.lock index 505eca1..61837fc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -50,12 +50,6 @@ version = "1.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" -[[package]] -name = "cfg-if" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" - [[package]] name = "core2" version = "0.3.3" @@ -141,15 +135,6 @@ version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6548a0ad5d2549e111e1f6a11a6c2e2d00ce6a3dafe22948d67c2b443f775e52" -[[package]] -name = "crossbeam-utils" -version = "0.8.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3c063cd8cc95f5c377ed0d4b49a4b21f632396ff690e8470c29b3359b346984b" -dependencies = [ - "cfg-if", -] - [[package]] name = "embedded-dma" version = "0.2.0" @@ -299,7 +284,7 @@ dependencies = [ "cortex-m-semihosting", "panic-semihosting", "rf4463", - "ringbuf", + "ringbuffer", "stm32f4xx-hal", ] @@ -359,13 +344,10 @@ dependencies = [ ] [[package]] -name = "ringbuf" -version = "0.3.3" +name = "ringbuffer" +version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "79abed428d1fd2a128201cec72c5f6938e2da607c6f3745f769fabea399d950a" -dependencies = [ - "crossbeam-utils", -] +checksum = "b6e4fbb37fd18bd949f42583141d650fcdce49dbf759264e4dd3a5df0bc15dc6" [[package]] name = "rtic-core" diff --git a/Cargo.toml b/Cargo.toml index 00acfd1..7665571 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,5 +21,5 @@ cortex-m-semihosting = "0.5" panic-semihosting = "0.6" core2 = { version = "0.3", default_features = false } rf4463 = { path = "../rf4463" } -ringbuf = { version = "0.3", default_features = false } -cortex-m-rtic = { version = "1.1.4" } \ No newline at end of file +cortex-m-rtic = { version = "1.1.4" } +ringbuffer = { version = "0.13.0", default_features = false } diff --git a/flash.rb b/flash.rb index 2e49d8c..7a8e7f3 100644 --- a/flash.rb +++ b/flash.rb @@ -1,4 +1,4 @@ -project = "ballon-transceiver-firmware" +project = "pi-transceiver-firmware" puts `cargo build --release` puts `openocd -f interface/stlink.cfg -f target/stm32f4x.cfg -f interface/stlink.cfg -c "program target/thumbv7em-none-eabihf/release/#{project} verify reset exit"` diff --git a/src/main.rs b/src/main.rs index 7d99950..74f9a67 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,8 @@ extern crate panic_semihosting; extern crate stm32f4xx_hal as hal; +mod packet; + #[rtic::app(device = stm32f4xx_hal::pac, peripherals = true)] mod app { use cortex_m::peripheral::NVIC; @@ -16,14 +18,15 @@ mod app { use hal::serial::{Config, Event, Serial}; use hal::spi::{Mode, Phase, Polarity, Spi}; use hal::timer::Delay; + use hal::{interrupt, prelude::*}; use rf4463::config::RADIO_CONFIG_500_2; use rf4463::Rf4463; - use ringbuf::{Rb, StaticRb}; + use ringbuffer::{ConstGenericRingBuffer, RingBuffer, RingBufferRead, RingBufferWrite}; - use crate::hal::{interrupt, prelude::*}; + use crate::packet::Packet; - const PACKET_LEN: usize = 256 + 2 + 65; - const BUFFER_LEN: usize = 16_384; + // in # packets + const BUFFER_LEN: usize = 50; const MODE: Mode = Mode { polarity: Polarity::IdleLow, @@ -37,6 +40,7 @@ mod app { Delay<TIM5, 1000000>, >; + #[derive(Debug)] enum SlaveCmd { BufferStatus, SendPacket, @@ -52,14 +56,15 @@ mod app { } } + #[allow(clippy::large_enum_variant)] pub enum SlaveState { Idle, - RecvPacket(usize), + RecvPacket(Packet), } #[shared] struct Shared { - tx_buf: StaticRb<u8, BUFFER_LEN>, + tx_buf: ConstGenericRingBuffer<Packet, BUFFER_LEN>, } #[local] @@ -77,7 +82,13 @@ mod app { let gpiob = ctx.device.GPIOB.split(); // setup clocks - let clocks = rcc.cfgr.use_hse(25.MHz()).sysclk(48.MHz()).freeze(); + let clocks = rcc + .cfgr + .use_hse(25.MHz()) + .sysclk(84.MHz()) + .pclk1(48.MHz()) + .pclk2(48.MHz()) + .freeze(); // setup 4463 spi let mosi = gpioa.pa7.into_alternate(); @@ -114,7 +125,7 @@ mod app { ( Shared { - tx_buf: StaticRb::default(), + tx_buf: ConstGenericRingBuffer::new(), }, Local { radio, @@ -127,46 +138,27 @@ mod app { #[idle(local = [radio], shared=[tx_buf])] fn idle(mut ctx: idle::Context) -> ! { - let mut data = [0; PACKET_LEN]; - let mut i = 0; - loop { - if i == PACKET_LEN { - ctx.local.radio.tx(&mut data).unwrap(); - i = 0; - continue; + if let Some(mut pkt) = ctx.shared.tx_buf.lock(|buf| buf.dequeue()) { + ctx.local.radio.tx(&mut pkt.data).unwrap(); } - - ctx.shared.tx_buf.lock(|buf| { - if let Some(b) = buf.pop() { - data[i] = b; - - i += 1; - - if buf.free_len() == 16000 { - //block!(USART.borrow(cs).borrow_mut().as_mut().unwrap().write(b'E')).unwrap(); - } - } - }); } } #[task(binds = USART1, local = [state, usart], shared = [tx_buf])] fn usart1(ctx: usart1::Context) { let state = ctx.local.state; - let usart = ctx.local.usart; - let mut buf = ctx.shared.tx_buf; let x = block!(usart.read()).unwrap(); - match *state { + match state { SlaveState::Idle => { if let Some(cmd) = SlaveCmd::from_u8(x) { match cmd { SlaveCmd::BufferStatus => { - if buf.lock(|b| b.free_len()) < PACKET_LEN { + if buf.lock(|b| b.is_full()) { block!(usart.write(0x01)).unwrap(); // not enough space for a new packet } else { block!(usart.write(0x02)).unwrap(); // safe to send a packet @@ -174,20 +166,25 @@ mod app { } SlaveCmd::SendPacket => { - *state = SlaveState::RecvPacket(PACKET_LEN - 1); + if buf.lock(|b| b.len() >= b.capacity() - 1) { + block!(usart.write(0x01)).unwrap(); // not enough space for a new packet + } else { + block!(usart.write(0x02)).unwrap(); // safe to send a packet + } + + *state = SlaveState::RecvPacket(Packet::default()); } } } } - SlaveState::RecvPacket(i) => { - *state = if i == 0 { - SlaveState::Idle - } else { - SlaveState::RecvPacket(i - 1) - }; + SlaveState::RecvPacket(pkt) => { + pkt.push(x); + if pkt.is_complete() { + buf.lock(|b| b.enqueue(pkt.clone())); - buf.lock(|b| b.push(x).unwrap()); + *state = SlaveState::Idle; + } } }; } diff --git a/src/packet.rs b/src/packet.rs new file mode 100644 index 0000000..0afd287 --- /dev/null +++ b/src/packet.rs @@ -0,0 +1,29 @@ +pub const PACKET_LEN: usize = 256 + 2 + 65; + +#[derive(Debug, Clone)] +pub struct Packet { + len: usize, + pub data: [u8; PACKET_LEN], +} + +impl Packet { + pub fn push(&mut self, b: u8) { + assert!(self.len < PACKET_LEN); + + self.data[self.len] = b; + self.len += 1; + } + + pub fn is_complete(&self) -> bool { + self.len == PACKET_LEN + } +} + +impl Default for Packet { + fn default() -> Self { + Self { + len: 0, + data: [0; PACKET_LEN], + } + } +} -- GitLab