Compare commits

..

No commits in common. "6e77decb9e5561d7720e53125271ab74dd8b713f" and "c02b7478e0a35e7344e7d1292440fe62947ce875" have entirely different histories.

3 changed files with 50 additions and 22 deletions

View file

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

View file

@ -17,7 +17,10 @@ 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::*;
@ -87,6 +90,13 @@ 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());
@ -113,7 +123,15 @@ pub fn spawn_tasks(spawner: Spawner) {
let crc_handle = crc::Handle::new(crc_engine_resources!(p));
host_interface::start(host_interface_resources!(p), spawner, crc_handle);
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(),
);
heat_pump_interface::start(heat_pump_interface_resources!(p), spawner);

View file

@ -1,10 +1,8 @@
use crate::io::framed::Receiver;
use crate::{crc, io, proto};
use crate::{crc, io};
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;
@ -33,11 +31,9 @@ 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,
}
@ -45,13 +41,20 @@ 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) {
pub fn start(
r: crate::HostInterfaceResources,
spawner: Spawner,
crc: crc::Handle,
publisher: crate::TargetMessagePublisher,
generator: crate::TargetMessageGenerator,
) {
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());
@ -78,10 +81,15 @@ pub fn start(r: crate::HostInterfaceResources, spawner: Spawner, crc: crc::Handl
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, crc }));
spawner.must_spawn(host_tx_task(Tx {
uart: uart_tx,
channel: publisher,
crc,
}));
spawner.must_spawn(host_rx_task(Rx {
message_buf,
uart: uart_rx,
channel: generator,
crc,
clr_host_transmit,
set_host_transmit,
@ -92,10 +100,9 @@ pub fn start(r: crate::HostInterfaceResources, spawner: Spawner, crc: crc::Handl
#[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 = receiver.receive().await;
let msg = tx.channel.receive().await;
debug!("sending message");
let _ = io::proto::send_target_message(&mut ftx, msg, tx.crc).await;
@ -116,7 +123,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).await;
handle_host_message(msg, rx.channel).await;
}
Err(io::proto::ReceiveError::Sync) => {
error!("sync error");
@ -178,15 +185,14 @@ async fn host_rx_task(rx: Rx) {
}
}
async fn handle_host_message(host: HostMessage) {
async fn handle_host_message(host: HostMessage, generator: crate::TargetMessageGenerator) {
match host.msg {
None => {
error!("empty message");
}
Some(HostMessage_::Msg::Echo(_)) => {
info!("echo request");
TARGET_MESSAGE_CHANNEL
.sender()
generator
.send(TargetMessage {
id: host.id,
msg: Some(TargetMessage_::Msg::Echo(EchoResponse {})),