@@ -23,6 +23,34 @@ LOG_MODULE_DECLARE(can_driver, CONFIG_CAN_LOG_LEVEL);
2323#define MCAN_MAX_DLC CAN_MAX_DLC
2424#endif
2525
26+ static void memcpy32_volatile (volatile void * dst_ , const volatile void * src_ ,
27+ size_t len )
28+ {
29+ volatile uint32_t * dst = dst_ ;
30+ const volatile uint32_t * src = src_ ;
31+
32+ __ASSERT (len % 4 == 0 , "len must be a multiple of 4!" );
33+ len /= sizeof (uint32_t );
34+
35+ while (len -- ) {
36+ * dst = * src ;
37+ ++ dst ;
38+ ++ src ;
39+ }
40+ }
41+
42+ static void memset32_volatile (volatile void * dst_ , uint32_t val , size_t len )
43+ {
44+ volatile uint32_t * dst = dst_ ;
45+
46+ __ASSERT (len % 4 == 0 , "len must be a multiple of 4!" );
47+ len /= sizeof (uint32_t );
48+
49+ while (len -- ) {
50+ * dst ++ = val ;
51+ }
52+ }
53+
2654static int can_exit_sleep_mode (struct can_mcan_reg * can )
2755{
2856 uint32_t start_time ;
@@ -389,12 +417,7 @@ int can_mcan_init(const struct device *dev, const struct can_mcan_config *cfg,
389417 }
390418
391419 /* No memset because only aligned ptr are allowed */
392- for (uint32_t * ptr = (uint32_t * )msg_ram ;
393- ptr < (uint32_t * )msg_ram +
394- sizeof (struct can_mcan_msg_sram ) / sizeof (uint32_t );
395- ptr ++ ) {
396- * ptr = 0 ;
397- }
420+ memset32_volatile (msg_ram , 0 , sizeof (struct can_mcan_msg_sram ));
398421
399422 return 0 ;
400423}
@@ -486,15 +509,15 @@ static void can_mcan_get_message(struct can_mcan_data *data,
486509 uint32_t get_idx , filt_idx ;
487510 struct zcan_frame frame ;
488511 can_rx_callback_t cb ;
489- volatile uint32_t * src , * dst , * end ;
490512 int data_length ;
491513 void * cb_arg ;
492514 struct can_mcan_rx_fifo_hdr hdr ;
493515
494516 while ((* fifo_status_reg & CAN_MCAN_RXF0S_F0FL )) {
495517 get_idx = (* fifo_status_reg & CAN_MCAN_RXF0S_F0GI ) >>
496518 CAN_MCAN_RXF0S_F0GI_POS ;
497- hdr = fifo [get_idx ].hdr ;
519+ memcpy32_volatile (& hdr , & fifo [get_idx ].hdr ,
520+ sizeof (struct can_mcan_rx_fifo_hdr ));
498521
499522 if (hdr .xtd ) {
500523 frame .id = hdr .ext_id ;
@@ -525,13 +548,8 @@ static void can_mcan_get_message(struct can_mcan_data *data,
525548 data_length = can_dlc_to_bytes (frame .dlc );
526549 if (data_length <= sizeof (frame .data )) {
527550 /* data needs to be written in 32 bit blocks!*/
528- for (src = fifo [get_idx ].data_32 ,
529- dst = frame .data_32 ,
530- end = dst + CAN_DIV_CEIL (data_length , sizeof (uint32_t ));
531- dst < end ;
532- src ++ , dst ++ ) {
533- * dst = * src ;
534- }
551+ memcpy32_volatile (frame .data_32 , fifo [get_idx ].data_32 ,
552+ ROUND_UP (data_length , sizeof (uint32_t )));
535553
536554 if (frame .id_type == CAN_STANDARD_IDENTIFIER ) {
537555 LOG_DBG ("Frame on filter %d, ID: 0x%x" ,
@@ -647,8 +665,6 @@ int can_mcan_send(const struct can_mcan_config *cfg,
647665 uint32_t put_idx ;
648666 int ret ;
649667 struct can_mcan_mm mm ;
650- volatile uint32_t * dst , * end ;
651- const uint32_t * src ;
652668
653669 LOG_DBG ("Sending %d bytes. Id: 0x%x, ID type: %s %s %s %s" ,
654670 data_length , frame -> id ,
@@ -696,15 +712,9 @@ int can_mcan_send(const struct can_mcan_config *cfg,
696712 tx_hdr .ext_id = frame -> id ;
697713 }
698714
699- msg_ram -> tx_buffer [put_idx ].hdr = tx_hdr ;
700-
701- for (src = frame -> data_32 ,
702- dst = msg_ram -> tx_buffer [put_idx ].data_32 ,
703- end = dst + CAN_DIV_CEIL (data_length , sizeof (uint32_t ));
704- dst < end ;
705- src ++ , dst ++ ) {
706- * dst = * src ;
707- }
715+ memcpy32_volatile (& msg_ram -> tx_buffer [put_idx ].hdr , & tx_hdr , sizeof (tx_hdr ));
716+ memcpy32_volatile (msg_ram -> tx_buffer [put_idx ].data_32 , frame -> data_32 ,
717+ ROUND_UP (data_length , 4 ));
708718
709719 data -> tx_fin_cb [put_idx ] = callback ;
710720 data -> tx_fin_cb_arg [put_idx ] = callback_arg ;
@@ -761,7 +771,8 @@ int can_mcan_attach_std(struct can_mcan_data *data,
761771 filter_element .sfce = filter_nr & 0x01 ? CAN_MCAN_FCE_FIFO1 :
762772 CAN_MCAN_FCE_FIFO0 ;
763773
764- msg_ram -> std_filt [filter_nr ] = filter_element ;
774+ memcpy32_volatile (& msg_ram -> std_filt [filter_nr ], & filter_element ,
775+ sizeof (struct can_mcan_std_filter ));
765776
766777 k_mutex_unlock (& data -> inst_mutex );
767778
@@ -820,7 +831,8 @@ static int can_mcan_attach_ext(struct can_mcan_data *data,
820831 filter_element .efce = filter_nr & 0x01 ? CAN_MCAN_FCE_FIFO1 :
821832 CAN_MCAN_FCE_FIFO0 ;
822833
823- msg_ram -> ext_filt [filter_nr ] = filter_element ;
834+ memcpy32_volatile (& msg_ram -> ext_filt [filter_nr ], & filter_element ,
835+ sizeof (struct can_mcan_ext_filter ));
824836
825837 k_mutex_unlock (& data -> inst_mutex );
826838
@@ -874,9 +886,6 @@ int can_mcan_attach_isr(struct can_mcan_data *data,
874886void can_mcan_detach (struct can_mcan_data * data ,
875887 struct can_mcan_msg_sram * msg_ram , int filter_nr )
876888{
877- const struct can_mcan_ext_filter ext_filter = {0 };
878- const struct can_mcan_std_filter std_filter = {0 };
879-
880889 k_mutex_lock (& data -> inst_mutex , K_FOREVER );
881890 if (filter_nr >= NUM_STD_FILTER_DATA ) {
882891 filter_nr -= NUM_STD_FILTER_DATA ;
@@ -885,10 +894,12 @@ void can_mcan_detach(struct can_mcan_data *data,
885894 return ;
886895 }
887896
888- msg_ram -> ext_filt [filter_nr ] = ext_filter ;
897+ memset32_volatile (& msg_ram -> ext_filt [filter_nr ], 0 ,
898+ sizeof (struct can_mcan_ext_filter ));
889899 data -> rx_cb_ext [filter_nr ] = NULL ;
890900 } else {
891- msg_ram -> std_filt [filter_nr ] = std_filter ;
901+ memset32_volatile (& msg_ram -> std_filt [filter_nr ], 0 ,
902+ sizeof (struct can_mcan_std_filter ));
892903 data -> rx_cb_std [filter_nr ] = NULL ;
893904 }
894905
0 commit comments