@@ -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 ;
@@ -386,12 +414,7 @@ int can_mcan_init(const struct device *dev, const struct can_mcan_config *cfg,
386414 }
387415
388416 /* No memset because only aligned ptr are allowed */
389- for (uint32_t * ptr = (uint32_t * )msg_ram ;
390- ptr < (uint32_t * )msg_ram +
391- sizeof (struct can_mcan_msg_sram ) / sizeof (uint32_t );
392- ptr ++ ) {
393- * ptr = 0 ;
394- }
417+ memset32_volatile (msg_ram , 0 , sizeof (struct can_mcan_msg_sram ));
395418
396419 return 0 ;
397420}
@@ -483,15 +506,15 @@ static void can_mcan_get_message(struct can_mcan_data *data,
483506 uint32_t get_idx , filt_idx ;
484507 struct zcan_frame frame ;
485508 can_rx_callback_t cb ;
486- volatile uint32_t * src , * dst , * end ;
487509 int data_length ;
488510 void * cb_arg ;
489511 struct can_mcan_rx_fifo_hdr hdr ;
490512
491513 while ((* fifo_status_reg & CAN_MCAN_RXF0S_F0FL )) {
492514 get_idx = (* fifo_status_reg & CAN_MCAN_RXF0S_F0GI ) >>
493515 CAN_MCAN_RXF0S_F0GI_POS ;
494- hdr = fifo [get_idx ].hdr ;
516+ memcpy32_volatile (& hdr , & fifo [get_idx ].hdr ,
517+ sizeof (struct can_mcan_rx_fifo_hdr ));
495518
496519 if (hdr .xtd ) {
497520 frame .id = hdr .ext_id ;
@@ -522,13 +545,8 @@ static void can_mcan_get_message(struct can_mcan_data *data,
522545 data_length = can_dlc_to_bytes (frame .dlc );
523546 if (data_length <= sizeof (frame .data )) {
524547 /* data needs to be written in 32 bit blocks!*/
525- for (src = fifo [get_idx ].data_32 ,
526- dst = frame .data_32 ,
527- end = dst + CAN_DIV_CEIL (data_length , sizeof (uint32_t ));
528- dst < end ;
529- src ++ , dst ++ ) {
530- * dst = * src ;
531- }
548+ memcpy32_volatile (frame .data_32 , fifo [get_idx ].data_32 ,
549+ ROUND_UP (data_length , sizeof (uint32_t )));
532550
533551 if (frame .id_type == CAN_STANDARD_IDENTIFIER ) {
534552 LOG_DBG ("Frame on filter %d, ID: 0x%x" ,
@@ -644,8 +662,6 @@ int can_mcan_send(const struct can_mcan_config *cfg,
644662 uint32_t put_idx ;
645663 int ret ;
646664 struct can_mcan_mm mm ;
647- volatile uint32_t * dst , * end ;
648- const uint32_t * src ;
649665
650666 LOG_DBG ("Sending %d bytes. Id: 0x%x, ID type: %s %s %s %s" ,
651667 data_length , frame -> id ,
@@ -693,15 +709,9 @@ int can_mcan_send(const struct can_mcan_config *cfg,
693709 tx_hdr .ext_id = frame -> id ;
694710 }
695711
696- msg_ram -> tx_buffer [put_idx ].hdr = tx_hdr ;
697-
698- for (src = frame -> data_32 ,
699- dst = msg_ram -> tx_buffer [put_idx ].data_32 ,
700- end = dst + CAN_DIV_CEIL (data_length , sizeof (uint32_t ));
701- dst < end ;
702- src ++ , dst ++ ) {
703- * dst = * src ;
704- }
712+ memcpy32_volatile (& msg_ram -> tx_buffer [put_idx ].hdr , & tx_hdr , sizeof (tx_hdr ));
713+ memcpy32_volatile (msg_ram -> tx_buffer [put_idx ].data_32 , frame -> data_32 ,
714+ ROUND_UP (data_length , 4 ));
705715
706716 data -> tx_fin_cb [put_idx ] = callback ;
707717 data -> tx_fin_cb_arg [put_idx ] = user_data ;
@@ -758,7 +768,8 @@ int can_mcan_attach_std(struct can_mcan_data *data,
758768 filter_element .sfce = filter_nr & 0x01 ? CAN_MCAN_FCE_FIFO1 :
759769 CAN_MCAN_FCE_FIFO0 ;
760770
761- msg_ram -> std_filt [filter_nr ] = filter_element ;
771+ memcpy32_volatile (& msg_ram -> std_filt [filter_nr ], & filter_element ,
772+ sizeof (struct can_mcan_std_filter ));
762773
763774 k_mutex_unlock (& data -> inst_mutex );
764775
@@ -817,7 +828,8 @@ static int can_mcan_attach_ext(struct can_mcan_data *data,
817828 filter_element .efce = filter_nr & 0x01 ? CAN_MCAN_FCE_FIFO1 :
818829 CAN_MCAN_FCE_FIFO0 ;
819830
820- msg_ram -> ext_filt [filter_nr ] = filter_element ;
831+ memcpy32_volatile (& msg_ram -> ext_filt [filter_nr ], & filter_element ,
832+ sizeof (struct can_mcan_ext_filter ));
821833
822834 k_mutex_unlock (& data -> inst_mutex );
823835
@@ -871,9 +883,6 @@ int can_mcan_attach_isr(struct can_mcan_data *data,
871883void can_mcan_detach (struct can_mcan_data * data ,
872884 struct can_mcan_msg_sram * msg_ram , int filter_nr )
873885{
874- const struct can_mcan_ext_filter ext_filter = {0 };
875- const struct can_mcan_std_filter std_filter = {0 };
876-
877886 k_mutex_lock (& data -> inst_mutex , K_FOREVER );
878887 if (filter_nr >= NUM_STD_FILTER_DATA ) {
879888 filter_nr -= NUM_STD_FILTER_DATA ;
@@ -882,10 +891,12 @@ void can_mcan_detach(struct can_mcan_data *data,
882891 return ;
883892 }
884893
885- msg_ram -> ext_filt [filter_nr ] = ext_filter ;
894+ memset32_volatile (& msg_ram -> ext_filt [filter_nr ], 0 ,
895+ sizeof (struct can_mcan_ext_filter ));
886896 data -> rx_cb_ext [filter_nr ] = NULL ;
887897 } else {
888- msg_ram -> std_filt [filter_nr ] = std_filter ;
898+ memset32_volatile (& msg_ram -> std_filt [filter_nr ], 0 ,
899+ sizeof (struct can_mcan_std_filter ));
889900 data -> rx_cb_std [filter_nr ] = NULL ;
890901 }
891902
0 commit comments