11use defmt:: * ;
22use embassy_executor:: Spawner ;
3- use embassy_stm32:: can:: bxcan:: { filter , Fifo , Frame , Id , StandardId } ;
3+ use embassy_stm32:: can:: bxcan:: { Data , Fifo , Frame , Id , StandardId , filter } ;
44use embassy_stm32:: can:: { Can , CanRx , CanTx } ;
55use embassy_stm32:: peripherals:: CAN ;
66use embassy_sync:: blocking_mutex:: raw:: CriticalSectionRawMutex ;
77
88use embassy_sync:: pubsub:: { PubSubChannel , Publisher , Subscriber } ;
9- use shared_types:: { CanBusMessage , CanBusMessageId , FlightMode , TelemetryToPayloadMessage } ;
9+ use shared_types:: FlightMode ;
10+ //use shared_types::{CanBusMessage, CanBusMessageId, FlightMode, TelemetryToPayloadMessage};
11+ use heapless:: Vec ;
1012use static_cell:: StaticCell ;
1113
14+ use shared_types:: can:: {
15+ CanMessage ,
16+ structure:: { Can2aFrame , CanFrameId } ,
17+ } ;
18+
1219pub const CAN_QUEUE_SIZE : usize = 3 ; // TODO
1320pub const NUM_CAN_SUBSCRIBERS : usize = 1 ; // TODO
1421pub const NUM_CAN_PUBLISHERS : usize = 1 ; // TODO
1522
16- pub type CanFrame = ( u16 , [ u8 ; 8 ] ) ;
17- pub type CanInChannel = PubSubChannel < CriticalSectionRawMutex , CanFrame , CAN_QUEUE_SIZE , NUM_CAN_SUBSCRIBERS , 1 > ;
18- pub type CanInSubscriper =
19- Subscriber < ' static , CriticalSectionRawMutex , CanFrame , CAN_QUEUE_SIZE , NUM_CAN_SUBSCRIBERS , 1 > ;
20- pub type CanOutChannel = PubSubChannel < CriticalSectionRawMutex , CanFrame , CAN_QUEUE_SIZE , 1 , NUM_CAN_PUBLISHERS > ;
21- pub type CanOutPublisher = Publisher < ' static , CriticalSectionRawMutex , CanFrame , CAN_QUEUE_SIZE , 1 , NUM_CAN_PUBLISHERS > ;
22-
23- pub static CAN_IN : StaticCell < CanInChannel > = StaticCell :: new ( ) ;
24- pub static CAN_OUT : StaticCell < CanOutChannel > = StaticCell :: new ( ) ;
23+ pub type CanRxCh = PubSubChannel < CriticalSectionRawMutex , CanMessage , CAN_QUEUE_SIZE , NUM_CAN_SUBSCRIBERS , 1 > ;
24+ pub type CanRxSub = Subscriber < ' static , CriticalSectionRawMutex , CanMessage , CAN_QUEUE_SIZE , NUM_CAN_SUBSCRIBERS , 1 > ;
25+ pub type CanTxCh = PubSubChannel < CriticalSectionRawMutex , CanMessage , CAN_QUEUE_SIZE , 1 , NUM_CAN_PUBLISHERS > ;
26+ pub type CanTxPub = Publisher < ' static , CriticalSectionRawMutex , CanMessage , CAN_QUEUE_SIZE , 1 , NUM_CAN_PUBLISHERS > ;
2527
26- pub const NUM_FLIGHT_MODE_SUBSCRIBERS : usize = 3 ;
27- pub type FlightModeChannel = PubSubChannel < CriticalSectionRawMutex , FlightMode , 1 , NUM_FLIGHT_MODE_SUBSCRIBERS , 1 > ;
28- pub type FlightModeSubscriber =
29- Subscriber < ' static , CriticalSectionRawMutex , FlightMode , 1 , NUM_FLIGHT_MODE_SUBSCRIBERS , 1 > ;
30- pub static FLIGHT_MODE : StaticCell < FlightModeChannel > = StaticCell :: new ( ) ;
28+ pub static CAN_IN : StaticCell < CanRxCh > = StaticCell :: new ( ) ;
29+ pub static CAN_OUT : StaticCell < CanTxCh > = StaticCell :: new ( ) ;
3130
3231static CAN : StaticCell < Can < ' static , CAN > > = StaticCell :: new ( ) ;
3332static CAN_TX : StaticCell < CanTx < ' static , ' static , CAN > > = StaticCell :: new ( ) ;
@@ -36,8 +35,8 @@ static CAN_RX: StaticCell<CanRx<'static, 'static, CAN>> = StaticCell::new();
3635pub async fn spawn (
3736 mut can : Can < ' static , CAN > ,
3837 spawner : Spawner ,
39- publisher : Publisher < ' static , CriticalSectionRawMutex , CanFrame , CAN_QUEUE_SIZE , NUM_CAN_SUBSCRIBERS , 1 > ,
40- subscriber : Subscriber < ' static , CriticalSectionRawMutex , CanFrame , CAN_QUEUE_SIZE , 1 , NUM_CAN_PUBLISHERS > ,
38+ publisher : Publisher < ' static , CriticalSectionRawMutex , CanMessage , CAN_QUEUE_SIZE , NUM_CAN_SUBSCRIBERS , 1 > ,
39+ subscriber : Subscriber < ' static , CriticalSectionRawMutex , CanMessage , CAN_QUEUE_SIZE , 1 , NUM_CAN_PUBLISHERS > ,
4140) {
4241 can. modify_config ( )
4342 . set_loopback ( false )
@@ -46,8 +45,9 @@ pub async fn spawn(
4645 . leave_disabled ( ) ;
4746
4847 let telemetry_filter = filter:: Mask32 :: frames_with_std_id (
49- StandardId :: new ( CanBusMessageId :: TelemetryBroadcast ( 0 ) . into ( ) ) . unwrap ( ) ,
50- StandardId :: new ( 0x700 ) . unwrap ( ) ,
48+ //StandardId::new(CanBusMessageId::TelemetryBroadcast(0).into()).unwrap(),
49+ StandardId :: new ( 0x1af ) . unwrap ( ) ,
50+ StandardId :: new ( 0 ) . unwrap ( ) ,
5151 ) ;
5252
5353 can. modify_filters ( ) . enable_bank ( 0 , Fifo :: Fifo0 , telemetry_filter) ;
@@ -67,15 +67,15 @@ pub async fn spawn(
6767#[ embassy_executor:: task]
6868async fn run_tx (
6969 can_tx : & ' static mut CanTx < ' static , ' static , CAN > ,
70- mut subscriber : Subscriber < ' static , CriticalSectionRawMutex , CanFrame , CAN_QUEUE_SIZE , 1 , NUM_CAN_PUBLISHERS > ,
70+ mut subscriber : Subscriber < ' static , CriticalSectionRawMutex , CanMessage , CAN_QUEUE_SIZE , 1 , NUM_CAN_PUBLISHERS > ,
7171) -> ! {
7272 loop {
73- let ( address , data ) = subscriber. next_message_pure ( ) . await ;
74- let Some ( sid) = StandardId :: new ( address ) else {
73+ let msg : Can2aFrame = subscriber. next_message_pure ( ) . await . into ( ) ;
74+ let Some ( sid) = StandardId :: new ( msg . id . into ( ) ) else {
7575 continue ;
7676 } ;
7777
78- let frame = Frame :: new_data ( sid, data ) ;
78+ let frame = Frame :: new_data ( sid, Data :: new ( msg . payload . as_slice ( ) ) . unwrap ( ) ) ;
7979 can_tx. write ( & frame) . await ;
8080 can_tx. flush_all ( ) . await ;
8181 }
@@ -84,46 +84,41 @@ async fn run_tx(
8484#[ embassy_executor:: task]
8585async fn run_rx (
8686 can_rx : & ' static mut CanRx < ' static , ' static , CAN > ,
87- publisher : Publisher < ' static , CriticalSectionRawMutex , CanFrame , CAN_QUEUE_SIZE , NUM_CAN_SUBSCRIBERS , 1 > ,
87+ publisher : Publisher < ' static , CriticalSectionRawMutex , CanMessage , CAN_QUEUE_SIZE , NUM_CAN_SUBSCRIBERS , 1 > ,
8888) -> ! {
8989 loop {
9090 match can_rx. read ( ) . await {
9191 Ok ( envelope) => {
9292 let frame = envelope. frame ;
93- let Some ( data) = frame. data ( ) else {
93+ let Id :: Standard ( id) = frame. id ( ) else {
94+ warn ! ( "Unexpected extended 29bit Can Frame Id, dropping." ) ;
9495 continue ;
9596 } ;
96-
97- let Id :: Standard ( sid) = frame. id ( ) else {
98- // EID package, skipping.
97+ let Some ( frame_data) = frame. data ( ) else {
98+ warn ! ( "Unexpected non-data Can Frame, dropping." ) ;
9999 continue ;
100100 } ;
101- let id_raw = sid. as_raw ( ) ;
102101
103- let Ok ( data_array ) = data . as_ref ( ) . try_into ( ) else {
104- // frame wasn't 8 bytes long, skip.
102+ let Ok ( data ) = Vec :: from_slice ( frame_data ) else {
103+ warn ! ( "Unexpected Can Frame data of lenght > 8 bytes, dropping." ) ;
105104 continue ;
106105 } ;
107-
108- publisher. publish_immediate ( ( id_raw, data_array) ) ;
106+ let Ok ( id) = CanFrameId :: try_from ( id. as_raw ( ) ) else {
107+ warn ! (
108+ "Can Frame id could not be mapped to a type, either the sender's or this software might be outdated. id: {}" ,
109+ id. as_raw( )
110+ ) ;
111+ continue ;
112+ } ;
113+ let message = Can2aFrame { id, payload : data } ;
114+ let Ok ( message_parsed) = CanMessage :: try_from ( message) else {
115+ warn ! ( "Malformed Can Message payload, message could not be parsed into type, dropping." ) ;
116+ continue ;
117+ } ;
118+ publisher. publish_immediate ( message_parsed) ; // TODO: think about publish immediate
109119 }
110120 Err ( e) => {
111- error ! ( "Failed to read envelope: {:?}" , Debug2Format ( & e) )
112- }
113- }
114- }
115- }
116-
117- #[ embassy_executor:: task]
118- pub async fn run_flight_mode_listener (
119- mut can_subscriber : CanInSubscriper ,
120- flight_mode_publisher : Publisher < ' static , CriticalSectionRawMutex , FlightMode , 1 , NUM_FLIGHT_MODE_SUBSCRIBERS , 1 > ,
121- ) -> ! {
122- loop {
123- let ( sid, data) = can_subscriber. next_message_pure ( ) . await ;
124- if sid == CanBusMessageId :: TelemetryBroadcast ( 0 ) . into ( ) {
125- if let Ok ( Some ( msg) ) = TelemetryToPayloadMessage :: parse ( data) {
126- flight_mode_publisher. publish_immediate ( msg. mode ) ;
121+ error ! ( "Can Bus Error: Failed to read can envelope: {:?}" , Debug2Format ( & e) )
127122 }
128123 }
129124 }
0 commit comments