Compare commits

...

2 commits

Author SHA1 Message Date
0140e363ce WIP 2025-05-26 06:44:41 -04:00
bfa551c9e2 firmware: Improve naming of CRC module and structs. 2025-05-26 06:23:09 -04:00
3 changed files with 34 additions and 13 deletions

View file

@ -3,18 +3,22 @@ use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex;
use embassy_sync::mutex;
use static_cell::StaticCell;
struct CrcShared<'a> {
struct Shared<'a> {
inner: mutex::Mutex<ThreadModeRawMutex, crc::Crc<'a>>,
}
static STORAGE: StaticCell<Shared> = StaticCell::new();
#[derive(Clone, Copy)]
pub struct CrcHandle {
shared: &'static CrcShared<'static>,
pub struct Handle {
shared: &'static Shared<'static>,
}
static STORAGE: StaticCell<CrcShared> = StaticCell::new();
struct Stream<'a> {
guard: mutex::MutexGuard<'a, ThreadModeRawMutex, crc::Crc<'a>>,
}
impl CrcHandle {
impl Handle {
pub fn new(r: crate::CrcEngineResources) -> Self {
let cfg = crc::Config::new(
crc::InputReverseConfig::None,
@ -25,11 +29,11 @@ impl CrcHandle {
)
.unwrap();
let shared_ref = STORAGE.init(CrcShared {
let shared_ref = STORAGE.init(Shared {
inner: mutex::Mutex::new(crc::Crc::new(r.crc_engine, cfg)),
});
CrcHandle { shared: shared_ref }
Handle { shared: shared_ref }
}
#[allow(clippy::cast_possible_truncation)]
@ -40,4 +44,21 @@ impl CrcHandle {
engine.reset();
engine.feed_bytes(data) as u16
}
pub async fn stream(&self) -> Stream {
let mut guard = self.shared.inner.lock().await;
let engine = &mut *guard;
engine.reset();
Stream { guard }
}
}
impl Stream<'_> {
#[allow(clippy::cast_possible_truncation)]
pub fn compute(&mut self, data: &[u8]) -> u16 {
let engine = &mut *self.guard;
engine.feed_bytes(data) as u16
}
}

View file

@ -2,7 +2,7 @@
extern crate core;
mod crc_engine;
mod crc;
mod io;
mod tasks;
mod utils;
@ -132,7 +132,7 @@ pub fn spawn_tasks(spawner: Spawner) {
let wdg = wdg::IndependentWatchdog::new(r.wdg, 4_000_000);
spawner.must_spawn(supervisor::watchdog_task(wdg));
let crc_handle = crc_engine::CrcHandle::new(crc_engine_resources!(p));
let crc_handle = crc::Handle::new(crc_engine_resources!(p));
let tgt_msg_channel = TARGET_MESSAGE_CHANNEL.init(TargetMessageChannel::new());

View file

@ -1,4 +1,4 @@
use crate::{crc_engine, io, println, utils};
use crate::{crc, io, println, utils};
use byteorder::{ByteOrder, LittleEndian};
use embassy_executor::Spawner;
use embassy_stm32::{gpio, usart};
@ -36,7 +36,7 @@ static MESSAGE_BUF: StaticCell<MessageBuf> = StaticCell::new();
struct Tx {
uart: usart::BufferedUartTx<'static>,
channel: crate::TargetMessagePublisher,
crc: crc_engine::CrcHandle,
crc: crc::Handle,
}
#[allow(dead_code)]
@ -44,7 +44,7 @@ struct Rx {
message_buf: &'static mut MessageBuf,
uart: usart::BufferedUartRx<'static>,
channel: crate::TargetMessageGenerator,
crc: crc_engine::CrcHandle,
crc: crc::Handle,
clr_host_transmit: gpio::OutputOpenDrain<'static>,
set_host_transmit: gpio::OutputOpenDrain<'static>,
host_active_led: gpio::OutputOpenDrain<'static>,
@ -53,7 +53,7 @@ struct Rx {
pub fn start(
r: crate::HostInterfaceResources,
spawner: Spawner,
crc: crc_engine::CrcHandle,
crc: crc::Handle,
publisher: crate::TargetMessagePublisher,
generator: crate::TargetMessageGenerator,
) {