11use defmt:: * ;
22use embassy_executor:: SendSpawner ;
3- // use embassy_stm32::can::bxcan::{Frame, Id, StandardId};
43use embassy_stm32:: can:: { Can , CanRx , CanTx , Frame } ;
5- // use embassy_stm32::peripherals::CAN;
64use embassy_sync:: blocking_mutex:: raw:: CriticalSectionRawMutex ;
7-
85use embassy_sync:: pubsub:: { PubSubChannel , Publisher , Subscriber } ;
6+
97use embedded_can:: { Id , StandardId } ;
108use heapless:: Vec ;
11- use nalgebra:: inf;
129use static_cell:: StaticCell ;
1310
1411use shared_types:: can:: {
1512 CanMessage ,
1613 structure:: { Can2aFrame , CanFrameId } ,
1714} ;
1815
19- pub const CAN_QUEUE_SIZE : usize = 20 ;
16+ pub const CAN_RX_QUEUE_SIZE : usize = 20 ;
17+ pub const CAN_TX_QUEUE_SIZE : usize = 20 ;
2018pub const NUM_CAN_SUBSCRIBERS : usize = 1 ;
2119pub const NUM_CAN_PUBLISHERS : usize = 1 ;
2220
23- pub type CanRxChannel = PubSubChannel < CriticalSectionRawMutex , CanMessage , CAN_QUEUE_SIZE , NUM_CAN_SUBSCRIBERS , 1 > ;
21+ pub type CanRxChannel = PubSubChannel < CriticalSectionRawMutex , CanMessage , CAN_RX_QUEUE_SIZE , NUM_CAN_SUBSCRIBERS , 1 > ;
2422pub type CanRxSubscriber =
25- Subscriber < ' static , CriticalSectionRawMutex , CanMessage , CAN_QUEUE_SIZE , NUM_CAN_SUBSCRIBERS , 1 > ;
26- pub type CanTxChannel = PubSubChannel < CriticalSectionRawMutex , CanMessage , CAN_QUEUE_SIZE , 1 , NUM_CAN_PUBLISHERS > ;
23+ Subscriber < ' static , CriticalSectionRawMutex , CanMessage , CAN_RX_QUEUE_SIZE , NUM_CAN_SUBSCRIBERS , 1 > ;
24+ pub type CanTxChannel = PubSubChannel < CriticalSectionRawMutex , CanMessage , CAN_TX_QUEUE_SIZE , 1 , NUM_CAN_PUBLISHERS > ;
2725pub type CanTxPublisher =
28- Publisher < ' static , CriticalSectionRawMutex , CanMessage , CAN_QUEUE_SIZE , 1 , NUM_CAN_PUBLISHERS > ;
26+ Publisher < ' static , CriticalSectionRawMutex , CanMessage , CAN_TX_QUEUE_SIZE , 1 , NUM_CAN_PUBLISHERS > ;
2927
3028// --- can1
3129pub static CAN1_RX_CH : StaticCell < CanRxChannel > = StaticCell :: new ( ) ;
@@ -41,12 +39,11 @@ pub static CAN2_TX_CH: StaticCell<CanTxChannel> = StaticCell::new();
4139static CAN2_TX : StaticCell < CanTx < ' static > > = StaticCell :: new ( ) ;
4240static CAN2_RX : StaticCell < CanRx < ' static > > = StaticCell :: new ( ) ;
4341
42+ // --- dedicated tasks for receiving and sending CAN messages for each hardware Bus
4443async fn run_can_rx ( can_rx : & ' static mut CanRx < ' static > , publisher : CanTxPublisher ) -> ! {
45- info ! ( "Run Can RX" ) ;
4644 loop {
4745 match can_rx. read ( ) . await {
4846 Ok ( envelope) => {
49- info ! ( "Read Can Message" ) ;
5047 let frame = envelope. frame ;
5148 let Id :: Standard ( id) = frame. id ( ) else {
5249 warn ! ( "Unexpected extended 29bit Can Frame Id, dropping." ) ;
@@ -58,52 +55,46 @@ async fn run_can_rx(can_rx: &'static mut CanRx<'static>, publisher: CanTxPublish
5855 } ;
5956 let Ok ( id) = CanFrameId :: try_from ( id. as_raw ( ) ) else {
6057 warn ! (
61- "Can Frame could not be converted, this is a bug in the implementation and should never happen "
58+ "Can Frame could not parsed into type, either the senders or this software might be outdated. "
6259 ) ;
6360 continue ;
6461 } ;
6562 let message = Can2aFrame { id, payload : data } ;
6663 let Ok ( message_parsed) = CanMessage :: try_from ( message) else {
67- warn ! ( "Malformed Can Message, dropping" ) ; //id: {}, payload: {}", message.id, message.payload);
64+ warn ! ( "Malformed Can Message, message could not be parsed into type, dropping" ) ; //id: {}, payload: {}", message.id, message.payload);
6865 continue ;
6966 } ;
7067 publisher. publish_immediate ( message_parsed) ; // TODO: think about publish immediate
7168 }
7269 Err ( e) => {
73- error ! ( "Failed to read can envelope: {:?}" , Debug2Format ( & e) )
70+ error ! ( "Can Bus Error: Failed to read can envelope: {:?}" , Debug2Format ( & e) )
7471 }
7572 }
7673 }
7774}
7875
79- async fn run_can_tx (
80- can_tx : & ' static mut CanTx < ' static > ,
81- // mut subscriber: Subscriber<'static, CriticalSectionRawMutex, CanMessage, CAN_QUEUE_SIZE, 1, NUM_CAN_PUBLISHERS>,
82- mut subscriber : CanRxSubscriber ,
83- ) -> ! {
84- info ! ( "Run can tx" ) ;
76+ async fn run_can_tx ( can_tx : & ' static mut CanTx < ' static > , mut subscriber : CanRxSubscriber ) -> ! {
8577 loop {
8678 let message = subscriber. next_message_pure ( ) . await ; // should we care about lag?
87- info ! ( "Can message transmit" ) ;
8879 let frame = Can2aFrame :: from ( message) ;
8980
9081 let Some ( sid) = StandardId :: new ( frame. id . into ( ) ) else {
82+ error ! ( "Can2.0A Frame could not be converted to StandardId, this is a bug in the implementation." ) ;
9183 continue ;
9284 } ;
9385
9486 // unwrap is safe, because payload slice is never larger than 8 bytes
9587 let frame = Frame :: new_data ( sid, frame. payload . as_slice ( ) ) . unwrap ( ) ;
96- info ! ( "embassy can frame write start" ) ;
9788 can_tx. write ( & frame) . await ;
98- info ! ( "embassy can frame write finished" ) ;
9989 }
10090}
10191
92+ // --- CAN1
10293pub async fn spawn_can1 (
10394 can : Can < ' static > ,
10495 spawner : SendSpawner ,
105- publisher : Publisher < ' static , CriticalSectionRawMutex , CanMessage , CAN_QUEUE_SIZE , NUM_CAN_SUBSCRIBERS , 1 > ,
106- subscriber : Subscriber < ' static , CriticalSectionRawMutex , CanMessage , CAN_QUEUE_SIZE , 1 , NUM_CAN_PUBLISHERS > ,
96+ publisher : Publisher < ' static , CriticalSectionRawMutex , CanMessage , CAN_RX_QUEUE_SIZE , NUM_CAN_SUBSCRIBERS , 1 > ,
97+ subscriber : Subscriber < ' static , CriticalSectionRawMutex , CanMessage , CAN_RX_QUEUE_SIZE , 1 , NUM_CAN_PUBLISHERS > ,
10798) {
10899 let ( can_tx, can_rx, _properties) = can. split ( ) ;
109100 let can_tx = CAN1_TX . init ( can_tx) ;
@@ -114,11 +105,7 @@ pub async fn spawn_can1(
114105}
115106
116107#[ embassy_executor:: task]
117- async fn run_can1_tx (
118- can_tx : & ' static mut CanTx < ' static > ,
119- // mut subscriber: Subscriber<'static, CriticalSectionRawMutex, CanMessage, CAN_QUEUE_SIZE, 1, NUM_CAN_PUBLISHERS>,
120- mut subscriber : CanRxSubscriber ,
121- ) -> ! {
108+ async fn run_can1_tx ( can_tx : & ' static mut CanTx < ' static > , mut subscriber : CanRxSubscriber ) -> ! {
122109 run_can_tx ( can_tx, subscriber) . await
123110}
124111
@@ -131,8 +118,8 @@ async fn run_can1_rx(can_rx: &'static mut CanRx<'static>, publisher: CanTxPublis
131118pub async fn spawn_can2 (
132119 can : Can < ' static > ,
133120 spawner : SendSpawner ,
134- publisher : Publisher < ' static , CriticalSectionRawMutex , CanMessage , CAN_QUEUE_SIZE , NUM_CAN_SUBSCRIBERS , 1 > ,
135- subscriber : Subscriber < ' static , CriticalSectionRawMutex , CanMessage , CAN_QUEUE_SIZE , 1 , NUM_CAN_PUBLISHERS > ,
121+ publisher : Publisher < ' static , CriticalSectionRawMutex , CanMessage , CAN_RX_QUEUE_SIZE , NUM_CAN_SUBSCRIBERS , 1 > ,
122+ subscriber : Subscriber < ' static , CriticalSectionRawMutex , CanMessage , CAN_RX_QUEUE_SIZE , 1 , NUM_CAN_PUBLISHERS > ,
136123) {
137124 let ( can_tx, can_rx, _properties) = can. split ( ) ;
138125 let can_tx = CAN2_TX . init ( can_tx) ;
0 commit comments