@@ -155,12 +155,13 @@ static void error(char* string) {
155155 while (1 );
156156}
157157
158- void fdcan1_handler (uint8_t opcode , uint8_t * data , uint16_t size ) {
158+ int fdcan1_handler (uint8_t opcode , uint8_t * data , uint16_t size ) {
159159 if (opcode == CONFIGURE )
160160 {
161161 uint32_t const can_bitrate = * ((uint32_t * )data );
162162 can_frequency (& fdcan_1 , can_bitrate );
163163 dbg_printf ("fdcan1_handler: configuring fdcan1 with frequency %ld\n" , can_bitrate );
164+ return 0 ;
164165 }
165166 else if (opcode == CAN_FILTER )
166167 {
@@ -175,27 +176,31 @@ void fdcan1_handler(uint8_t opcode, uint8_t *data, uint16_t size) {
175176 {
176177 dbg_printf ("fdcan1_handler: can_filter failed for idx: %ld, id: %lX, mask: %lX\n" , x8h7_msg .field .idx , x8h7_msg .field .id , x8h7_msg .field .mask );
177178 }
179+ return 0 ;
178180 }
179181 else if (opcode == CAN_TX_FRAME )
180182 {
181183 union x8h7_can_frame_message msg ;
182184 memcpy (& msg , data , size );
183185
184- dbg_printf ("fdcan1_handler: sending CAN message to %x, size %d, content[0]=0x%02X\n" , msg .id , msg .len , msg .data [0 ]);
185-
186- can_write (& fdcan_1 , & msg );
186+ dbg_printf ("fdcan1_handler: sending CAN message to %lx, size %d, content[0]=0x%02X\n" , msg .field .id , msg .field .len , msg .field .data [0 ]);
187+ return can_write (& fdcan_1 , & msg );
187188 }
188- else {
189+ else
190+ {
189191 dbg_printf ("fdcan1_handler: error invalid opcode (:%d)\n" , opcode );
192+ return 0 ;
190193 }
191194}
192195
193- void fdcan2_handler (uint8_t opcode , uint8_t * data , uint16_t size ) {
196+ int fdcan2_handler (uint8_t opcode , uint8_t * data , uint16_t size )
197+ {
194198 if (opcode == CONFIGURE )
195199 {
196200 uint32_t const can_bitrate = * ((uint32_t * )data );
197201 can_frequency (& fdcan_2 , can_bitrate );
198202 dbg_printf ("fdcan2_handler: configuring fdcan2 with frequency %ld\n" , can_bitrate );
203+ return 0 ;
199204 }
200205 else if (opcode == CAN_FILTER )
201206 {
@@ -210,18 +215,20 @@ void fdcan2_handler(uint8_t opcode, uint8_t *data, uint16_t size) {
210215 {
211216 dbg_printf ("fdcan2_handler: can_filter failed for idx: %ld, id: %lX, mask: %lX\n" , x8h7_msg .field .idx , x8h7_msg .field .id , x8h7_msg .field .mask );
212217 }
218+ return 0 ;
213219 }
214220 else if (opcode == CAN_TX_FRAME )
215221 {
216222 union x8h7_can_frame_message msg ;
217223 memcpy (& msg , data , size );
218224
219- dbg_printf ("fdcan2_handler: sending CAN message to %x, size %d, content[0]=0x%02X\n" , msg .id , msg .len , msg .data [0 ]);
220-
221- can_write (& fdcan_2 , & msg );
225+ dbg_printf ("fdcan2_handler: sending CAN message to %lx, size %d, content[0]=0x%02X\n" , msg .field .id , msg .field .len , msg .field .data [0 ]);
226+ return can_write (& fdcan_2 , & msg );
222227 }
223- else {
228+ else
229+ {
224230 dbg_printf ("fdcan2_handler: error invalid opcode (:%d)\n" , opcode );
231+ return 0 ;
225232 }
226233}
227234
@@ -251,25 +258,28 @@ void can_init()
251258 register_peripheral_callback (PERIPH_FDCAN2 , & fdcan2_handler );
252259}
253260
254- void can_handle_data ()
261+ int can_handle_data ()
255262{
263+ int bytes_enqueued = 0 ;
256264 union x8h7_can_frame_message msg ;
257265
258- if (can_read (& fdcan_1 , & msg ))
266+ /* Note: the last read package is lost in this implementation. We need to fix this by
267+ * implementing some peek method or by buffering messages in a ringbuffer.
268+ */
269+
270+ for (int rc_enq = 0 ; can_read (& fdcan_1 , & msg ); bytes_enqueued += rc_enq )
259271 {
260- enqueue_packet (PERIPH_FDCAN1 ,
261- CAN_RX_FRAME ,
262- X8H7_CAN_HEADER_SIZE + msg .field .len ,
263- msg .buf );
272+ rc_enq = enqueue_packet (PERIPH_FDCAN1 , CAN_RX_FRAME , X8H7_CAN_HEADER_SIZE + msg .field .len , msg .buf );
273+ if (!rc_enq ) return bytes_enqueued ;
264274 }
265275
266- if ( can_read (& fdcan_2 , & msg ))
276+ for ( int rc_enq = 0 ; can_read (& fdcan_2 , & msg ); bytes_enqueued += rc_enq )
267277 {
268- enqueue_packet (PERIPH_FDCAN2 ,
269- CAN_RX_FRAME ,
270- X8H7_CAN_HEADER_SIZE + msg .field .len ,
271- msg .buf );
278+ rc_enq = enqueue_packet (PERIPH_FDCAN2 , CAN_RX_FRAME , X8H7_CAN_HEADER_SIZE + msg .field .len , msg .buf );
279+ if (!rc_enq ) return bytes_enqueued ;
272280 }
281+
282+ return bytes_enqueued ;
273283}
274284
275285/** Call all the init functions
@@ -405,7 +415,7 @@ int can_filter(FDCAN_HandleTypeDef * handle, uint32_t const filter_index, uint32
405415}
406416
407417
408- void can_write (FDCAN_HandleTypeDef * handle , union x8h7_can_frame_message const * msg )
418+ int can_write (FDCAN_HandleTypeDef * handle , union x8h7_can_frame_message const * msg )
409419{
410420 FDCAN_TxHeaderTypeDef TxHeader = {0 };
411421
@@ -455,22 +465,21 @@ void can_write(FDCAN_HandleTypeDef * handle, union x8h7_can_frame_message const
455465 uint8_t msg [2 ] = {X8H7_CAN_STS_INT_ERR , 0 };
456466 if (err_code == HAL_FDCAN_ERROR_FIFO_FULL ) msg [1 ] = X8H7_CAN_STS_FLG_TX_OVR ;
457467
458- enqueue_packet (handle == & fdcan_1 ? PERIPH_FDCAN1 : PERIPH_FDCAN2 , CAN_STATUS , sizeof (msg ), msg );
468+ return enqueue_packet (handle == & fdcan_1 ? PERIPH_FDCAN1 : PERIPH_FDCAN2 , CAN_STATUS , sizeof (msg ), msg );
459469 }
460470 else
461471 {
462472 uint8_t msg [2 ] = {X8H7_CAN_STS_INT_TX , 0 };
463- enqueue_packet (handle == & fdcan_1 ? PERIPH_FDCAN1 : PERIPH_FDCAN2 , CAN_STATUS , sizeof (msg ), msg );
473+ return enqueue_packet (handle == & fdcan_1 ? PERIPH_FDCAN1 : PERIPH_FDCAN2 , CAN_STATUS , sizeof (msg ), msg );
464474 }
465475}
466476
467477int can_read (FDCAN_HandleTypeDef * handle , union x8h7_can_frame_message * msg )
468478{
469479 static const uint8_t DLCtoBytes [] = {0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 12 , 16 , 20 , 24 , 32 , 48 , 64 };
470480
471- if (HAL_FDCAN_GetRxFifoFillLevel (handle , FDCAN_RX_FIFO0 ) == 0 ) {
472- return 0 ; // No message arrived
473- }
481+ if (HAL_FDCAN_GetRxFifoFillLevel (handle , FDCAN_RX_FIFO0 ) == 0 )
482+ return 0 ; // No message arrived
474483
475484 FDCAN_RxHeaderTypeDef RxHeader = {0 };
476485 uint8_t RxData [64 ] = {0 };
0 commit comments