| 1 | use core::any::Any; |
| 2 | |
| 3 | use defmt::{trace, error}; |
| 4 | |
| 5 | use embassy_stm32::can::{Frame}; |
| 6 | |
| 7 | use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex; |
| 8 | use embassy_sync::channel::{Receiver}; |
| 9 | use embassy_time::Timer; |
| 10 | |
| 11 | pub const CAN_BUFFER: usize = 10; |
| 12 | |
| 13 | #[embassy_executor::task] |
| 14 | pub async fn can_tx_task(mut can_bus: embassy_stm32::can::CanTx<'static>, chan: Receiver<'static, ThreadModeRawMutex, u32, CAN_BUFFER>) { |
| 15 | |
| 16 | loop { |
| 17 | // wait for new messages to transmit |
| 18 | // let message = chan.receive().await; |
| 19 | let message = "Test"; |
| 20 | // TODO: check what to send in the message ID |
| 21 | // let frame = Frame::new_extended(0x123456F, &message.to_le_bytes()).unwrap(); |
| 22 | let frame = Frame::new_extended(0x123456F, message.as_bytes()).unwrap(); |
| 23 | trace!("Writing CAN frame"); |
| 24 | |
| 25 | _ = can_bus.write(&frame).await; |
| 26 | Timer::after_secs(1).await; |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | #[embassy_executor::task] |
| 31 | pub async fn can_rx_task(mut can_bus: embassy_stm32::can::CanRx<'static>) { |
| 32 | |
| 33 | loop { |
| 34 | let message = can_bus.read().await; |
| 35 | trace!("Receiving CAN..."); |
| 36 | |
| 37 | let contents: &[u8]; |
| 38 | // watch for possible bus errors |
| 39 | match message { |
| 40 | Ok(envelope) => { |
| 41 | contents = envelope.frame.data(); |
| 42 | trace!("Received CAN message: {}", contents); |
| 43 | }, |
| 44 | _ => { |
| 45 | error!("Error receiving CAN bus message!"); |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |