Compare commits
2 commits
c02b7478e0
...
6e77decb9e
| Author | SHA1 | Date | |
|---|---|---|---|
| 6e77decb9e | |||
| 59ddc1d750 |
3 changed files with 22 additions and 50 deletions
|
|
@ -4,15 +4,13 @@ use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex;
|
||||||
use embassy_sync::mutex;
|
use embassy_sync::mutex;
|
||||||
use static_cell::StaticCell;
|
use static_cell::StaticCell;
|
||||||
|
|
||||||
struct Shared {
|
type CrcEngine = mutex::Mutex<ThreadModeRawMutex, crc::Crc<'static>>;
|
||||||
inner: mutex::Mutex<ThreadModeRawMutex, crc::Crc<'static>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
static STORAGE: StaticCell<Shared> = StaticCell::new();
|
static ENGINE: StaticCell<CrcEngine> = StaticCell::new();
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
pub struct Handle {
|
pub struct Handle {
|
||||||
shared: &'static Shared,
|
engine: &'static CrcEngine,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Stream {
|
pub struct Stream {
|
||||||
|
|
@ -30,16 +28,14 @@ impl Handle {
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let shared_ref = STORAGE.init(Shared {
|
let engine_ref = ENGINE.init(mutex::Mutex::new(crc::Crc::new(r.crc_engine, cfg)));
|
||||||
inner: mutex::Mutex::new(crc::Crc::new(r.crc_engine, cfg)),
|
|
||||||
});
|
|
||||||
|
|
||||||
Handle { shared: shared_ref }
|
Handle { engine: engine_ref }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(clippy::cast_possible_truncation)]
|
#[allow(clippy::cast_possible_truncation)]
|
||||||
pub async fn compute(&self, data: &[u8]) -> u16 {
|
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;
|
let engine = &mut *guard;
|
||||||
|
|
||||||
engine.reset();
|
engine.reset();
|
||||||
|
|
@ -48,7 +44,7 @@ impl Handle {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn stream(&self) -> Stream {
|
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;
|
let engine = &mut *guard;
|
||||||
|
|
||||||
trace!("CRC stream started");
|
trace!("CRC stream started");
|
||||||
|
|
|
||||||
|
|
@ -17,10 +17,7 @@ use core::mem;
|
||||||
use embassy_executor::Spawner;
|
use embassy_executor::Spawner;
|
||||||
use embassy_stm32::peripherals::*;
|
use embassy_stm32::peripherals::*;
|
||||||
use embassy_stm32::{Peri, bind_interrupts, gpio, rcc, usart, wdg};
|
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 embedded_resources::resource_group;
|
||||||
use static_cell::StaticCell;
|
|
||||||
use tasks::*;
|
use tasks::*;
|
||||||
|
|
||||||
use defmt::*;
|
use defmt::*;
|
||||||
|
|
@ -90,13 +87,6 @@ bind_interrupts!(struct Irqs {
|
||||||
USART3_4 => usart::BufferedInterruptHandler<USART3>, usart::BufferedInterruptHandler<USART4>;
|
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) {
|
pub fn spawn_tasks(spawner: Spawner) {
|
||||||
let p = embassy_stm32::init(build_rcc_config());
|
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 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);
|
||||||
|
|
||||||
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);
|
heat_pump_interface::start(heat_pump_interface_resources!(p), spawner);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,10 @@
|
||||||
use crate::io::framed::Receiver;
|
use crate::io::framed::Receiver;
|
||||||
use crate::{crc, io};
|
use crate::{crc, io, proto};
|
||||||
use defmt::*;
|
use defmt::*;
|
||||||
use embassy_executor::Spawner;
|
use embassy_executor::Spawner;
|
||||||
use embassy_stm32::{gpio, usart};
|
use embassy_stm32::{gpio, usart};
|
||||||
|
use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex;
|
||||||
|
use embassy_sync::channel;
|
||||||
use embassy_time::Duration;
|
use embassy_time::Duration;
|
||||||
use static_cell::StaticCell;
|
use static_cell::StaticCell;
|
||||||
|
|
||||||
|
|
@ -31,9 +33,11 @@ type MessageBuf = Vec<u8, MAX_HOST_MESSAGE_SIZE>;
|
||||||
|
|
||||||
static MESSAGE_BUF: StaticCell<MessageBuf> = StaticCell::new();
|
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 {
|
struct Tx {
|
||||||
uart: usart::BufferedUartTx<'static>,
|
uart: usart::BufferedUartTx<'static>,
|
||||||
channel: crate::TargetMessagePublisher,
|
|
||||||
crc: crc::Handle,
|
crc: crc::Handle,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -41,20 +45,13 @@ struct Tx {
|
||||||
struct Rx {
|
struct Rx {
|
||||||
message_buf: &'static mut MessageBuf,
|
message_buf: &'static mut MessageBuf,
|
||||||
uart: usart::BufferedUartRx<'static>,
|
uart: usart::BufferedUartRx<'static>,
|
||||||
channel: crate::TargetMessageGenerator,
|
|
||||||
crc: crc::Handle,
|
crc: crc::Handle,
|
||||||
clr_host_transmit: gpio::OutputOpenDrain<'static>,
|
clr_host_transmit: gpio::OutputOpenDrain<'static>,
|
||||||
set_host_transmit: gpio::OutputOpenDrain<'static>,
|
set_host_transmit: gpio::OutputOpenDrain<'static>,
|
||||||
host_active_led: gpio::OutputOpenDrain<'static>,
|
host_active_led: gpio::OutputOpenDrain<'static>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn start(
|
pub fn start(r: crate::HostInterfaceResources, spawner: Spawner, crc: crc::Handle) {
|
||||||
r: crate::HostInterfaceResources,
|
|
||||||
spawner: Spawner,
|
|
||||||
crc: crc::Handle,
|
|
||||||
publisher: crate::TargetMessagePublisher,
|
|
||||||
generator: crate::TargetMessageGenerator,
|
|
||||||
) {
|
|
||||||
let tx_buf = UART_TX_BUF.init(UartBuf::default());
|
let tx_buf = UART_TX_BUF.init(UartBuf::default());
|
||||||
let rx_buf = UART_RX_BUF.init(UartBuf::default());
|
let rx_buf = UART_RX_BUF.init(UartBuf::default());
|
||||||
let message_buf = MESSAGE_BUF.init(MessageBuf::new());
|
let message_buf = MESSAGE_BUF.init(MessageBuf::new());
|
||||||
|
|
@ -81,15 +78,10 @@ pub fn start(
|
||||||
let host_active_led =
|
let host_active_led =
|
||||||
gpio::OutputOpenDrain::new(r.host_active_led, gpio::Level::High, gpio::Speed::Low);
|
gpio::OutputOpenDrain::new(r.host_active_led, gpio::Level::High, gpio::Speed::Low);
|
||||||
|
|
||||||
spawner.must_spawn(host_tx_task(Tx {
|
spawner.must_spawn(host_tx_task(Tx { uart: uart_tx, crc }));
|
||||||
uart: uart_tx,
|
|
||||||
channel: publisher,
|
|
||||||
crc,
|
|
||||||
}));
|
|
||||||
spawner.must_spawn(host_rx_task(Rx {
|
spawner.must_spawn(host_rx_task(Rx {
|
||||||
message_buf,
|
message_buf,
|
||||||
uart: uart_rx,
|
uart: uart_rx,
|
||||||
channel: generator,
|
|
||||||
crc,
|
crc,
|
||||||
clr_host_transmit,
|
clr_host_transmit,
|
||||||
set_host_transmit,
|
set_host_transmit,
|
||||||
|
|
@ -100,9 +92,10 @@ pub fn start(
|
||||||
#[embassy_executor::task]
|
#[embassy_executor::task]
|
||||||
async fn host_tx_task(tx: Tx) {
|
async fn host_tx_task(tx: Tx) {
|
||||||
let mut ftx = io::framed::WriteSender::new(tx.uart, io::proto::SYNC_BYTE);
|
let mut ftx = io::framed::WriteSender::new(tx.uart, io::proto::SYNC_BYTE);
|
||||||
|
let receiver = TARGET_MESSAGE_CHANNEL.receiver();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let msg = tx.channel.receive().await;
|
let msg = receiver.receive().await;
|
||||||
|
|
||||||
debug!("sending message");
|
debug!("sending message");
|
||||||
let _ = io::proto::send_target_message(&mut ftx, msg, tx.crc).await;
|
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 {
|
match io::proto::receive_host_message(&mut frx, rx.crc).await {
|
||||||
Ok(msg) => {
|
Ok(msg) => {
|
||||||
debug!("message received");
|
debug!("message received");
|
||||||
handle_host_message(msg, rx.channel).await;
|
handle_host_message(msg).await;
|
||||||
}
|
}
|
||||||
Err(io::proto::ReceiveError::Sync) => {
|
Err(io::proto::ReceiveError::Sync) => {
|
||||||
error!("sync error");
|
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 {
|
match host.msg {
|
||||||
None => {
|
None => {
|
||||||
error!("empty message");
|
error!("empty message");
|
||||||
}
|
}
|
||||||
Some(HostMessage_::Msg::Echo(_)) => {
|
Some(HostMessage_::Msg::Echo(_)) => {
|
||||||
info!("echo request");
|
info!("echo request");
|
||||||
generator
|
TARGET_MESSAGE_CHANNEL
|
||||||
|
.sender()
|
||||||
.send(TargetMessage {
|
.send(TargetMessage {
|
||||||
id: host.id,
|
id: host.id,
|
||||||
msg: Some(TargetMessage_::Msg::Echo(EchoResponse {})),
|
msg: Some(TargetMessage_::Msg::Echo(EchoResponse {})),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue