Compare commits

...

2 commits

3 changed files with 22 additions and 50 deletions

View file

@ -4,15 +4,13 @@ use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex;
use embassy_sync::mutex;
use static_cell::StaticCell;
struct Shared {
inner: mutex::Mutex<ThreadModeRawMutex, crc::Crc<'static>>,
}
type CrcEngine = mutex::Mutex<ThreadModeRawMutex, crc::Crc<'static>>;
static STORAGE: StaticCell<Shared> = StaticCell::new();
static ENGINE: StaticCell<CrcEngine> = StaticCell::new();
#[derive(Clone, Copy)]
pub struct Handle {
shared: &'static Shared,
engine: &'static CrcEngine,
}
pub struct Stream {
@ -30,16 +28,14 @@ impl Handle {
)
.unwrap();
let shared_ref = STORAGE.init(Shared {
inner: mutex::Mutex::new(crc::Crc::new(r.crc_engine, cfg)),
});
let engine_ref = ENGINE.init(mutex::Mutex::new(crc::Crc::new(r.crc_engine, cfg)));
Handle { shared: shared_ref }
Handle { engine: engine_ref }
}
#[allow(clippy::cast_possible_truncation)]
pub async fn compute(&self, data: &[u8]) -> u16 {
let mut guard = self.shared.inner.lock().await;
let mut guard = self.engine.lock().await;
let engine = &mut *guard;
engine.reset();
@ -48,7 +44,7 @@ impl Handle {
}
pub async fn stream(&self) -> Stream {
let mut guard = self.shared.inner.lock().await;
let mut guard = self.engine.lock().await;
let engine = &mut *guard;
trace!("CRC stream started");

View file

@ -17,10 +17,7 @@ use core::mem;
use embassy_executor::Spawner;
use embassy_stm32::peripherals::*;
use embassy_stm32::{Peri, bind_interrupts, gpio, rcc, usart, wdg};
use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex;
use embassy_sync::channel;
use embedded_resources::resource_group;
use static_cell::StaticCell;
use tasks::*;
use defmt::*;
@ -90,13 +87,6 @@ bind_interrupts!(struct Irqs {
USART3_4 => usart::BufferedInterruptHandler<USART3>, usart::BufferedInterruptHandler<USART4>;
});
type TargetMessageChannel = channel::Channel<ThreadModeRawMutex, proto::api_::TargetMessage, 3>;
type TargetMessageGenerator =
channel::Sender<'static, ThreadModeRawMutex, proto::api_::TargetMessage, 3>;
type TargetMessagePublisher =
channel::Receiver<'static, ThreadModeRawMutex, proto::api_::TargetMessage, 3>;
static TARGET_MESSAGE_CHANNEL: StaticCell<TargetMessageChannel> = StaticCell::new();
pub fn spawn_tasks(spawner: Spawner) {
let p = embassy_stm32::init(build_rcc_config());
@ -123,15 +113,7 @@ pub fn spawn_tasks(spawner: Spawner) {
let crc_handle = crc::Handle::new(crc_engine_resources!(p));
let tgt_msg_channel = TARGET_MESSAGE_CHANNEL.init(TargetMessageChannel::new());
host_interface::start(
host_interface_resources!(p),
spawner,
crc_handle,
tgt_msg_channel.receiver(),
tgt_msg_channel.sender(),
);
host_interface::start(host_interface_resources!(p), spawner, crc_handle);
heat_pump_interface::start(heat_pump_interface_resources!(p), spawner);

View file

@ -1,8 +1,10 @@
use crate::io::framed::Receiver;
use crate::{crc, io};
use crate::{crc, io, proto};
use defmt::*;
use embassy_executor::Spawner;
use embassy_stm32::{gpio, usart};
use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex;
use embassy_sync::channel;
use embassy_time::Duration;
use static_cell::StaticCell;
@ -31,9 +33,11 @@ type MessageBuf = Vec<u8, MAX_HOST_MESSAGE_SIZE>;
static MESSAGE_BUF: StaticCell<MessageBuf> = StaticCell::new();
type TargetMessageChannel = channel::Channel<ThreadModeRawMutex, proto::api_::TargetMessage, 3>;
static TARGET_MESSAGE_CHANNEL: TargetMessageChannel = channel::Channel::new();
struct Tx {
uart: usart::BufferedUartTx<'static>,
channel: crate::TargetMessagePublisher,
crc: crc::Handle,
}
@ -41,20 +45,13 @@ struct Tx {
struct Rx {
message_buf: &'static mut MessageBuf,
uart: usart::BufferedUartRx<'static>,
channel: crate::TargetMessageGenerator,
crc: crc::Handle,
clr_host_transmit: gpio::OutputOpenDrain<'static>,
set_host_transmit: gpio::OutputOpenDrain<'static>,
host_active_led: gpio::OutputOpenDrain<'static>,
}
pub fn start(
r: crate::HostInterfaceResources,
spawner: Spawner,
crc: crc::Handle,
publisher: crate::TargetMessagePublisher,
generator: crate::TargetMessageGenerator,
) {
pub fn start(r: crate::HostInterfaceResources, spawner: Spawner, crc: crc::Handle) {
let tx_buf = UART_TX_BUF.init(UartBuf::default());
let rx_buf = UART_RX_BUF.init(UartBuf::default());
let message_buf = MESSAGE_BUF.init(MessageBuf::new());
@ -81,15 +78,10 @@ pub fn start(
let host_active_led =
gpio::OutputOpenDrain::new(r.host_active_led, gpio::Level::High, gpio::Speed::Low);
spawner.must_spawn(host_tx_task(Tx {
uart: uart_tx,
channel: publisher,
crc,
}));
spawner.must_spawn(host_tx_task(Tx { uart: uart_tx, crc }));
spawner.must_spawn(host_rx_task(Rx {
message_buf,
uart: uart_rx,
channel: generator,
crc,
clr_host_transmit,
set_host_transmit,
@ -100,9 +92,10 @@ pub fn start(
#[embassy_executor::task]
async fn host_tx_task(tx: Tx) {
let mut ftx = io::framed::WriteSender::new(tx.uart, io::proto::SYNC_BYTE);
let receiver = TARGET_MESSAGE_CHANNEL.receiver();
loop {
let msg = tx.channel.receive().await;
let msg = receiver.receive().await;
debug!("sending message");
let _ = io::proto::send_target_message(&mut ftx, msg, tx.crc).await;
@ -123,7 +116,7 @@ async fn host_rx_task(rx: Rx) {
match io::proto::receive_host_message(&mut frx, rx.crc).await {
Ok(msg) => {
debug!("message received");
handle_host_message(msg, rx.channel).await;
handle_host_message(msg).await;
}
Err(io::proto::ReceiveError::Sync) => {
error!("sync error");
@ -185,14 +178,15 @@ async fn host_rx_task(rx: Rx) {
}
}
async fn handle_host_message(host: HostMessage, generator: crate::TargetMessageGenerator) {
async fn handle_host_message(host: HostMessage) {
match host.msg {
None => {
error!("empty message");
}
Some(HostMessage_::Msg::Echo(_)) => {
info!("echo request");
generator
TARGET_MESSAGE_CHANNEL
.sender()
.send(TargetMessage {
id: host.id,
msg: Some(TargetMessage_::Msg::Echo(EchoResponse {})),