2121
2222// Be verbose
2323#define MIMXRT_CANIO_CAN_DEBUG (...) (void)0
24- //#define MIMXRT_CANIO_CAN_DEBUG(...) mp_printf(&mp_plat_print, __VA_ARGS__)
24+ // #define MIMXRT_CANIO_CAN_DEBUG(...) mp_printf(&mp_plat_print, __VA_ARGS__)
2525
2626#define MIMXRT_CANIO_CAN_CALLBACK_DEBUG (...) (void)0
27- //#define MIMXRT_CANIO_CAN_CALLBACK_DEBUG(...) mp_printf(&mp_plat_print, __VA_ARGS__)
27+ // #define MIMXRT_CANIO_CAN_CALLBACK_DEBUG(...) mp_printf(&mp_plat_print, __VA_ARGS__)
2828
2929
3030static CAN_Type * const flexcan_bases [] = CAN_BASE_PTRS ; // e.g.: { (CAN_Type *)0u, CAN1, CAN2, CAN3 }
@@ -67,11 +67,10 @@ static uint8_t mimxrt10xx_flexcan_get_free_tx_mbid(canio_can_obj_t *self) {
6767 bool found_free_tx_mb = false;
6868 int8_t tx_array_id = 0 ;
6969 mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION ();
70- for ( ; tx_array_id < MIMXRT10XX_FLEXCAN_TX_MB_NUM ; ++ tx_array_id )
70+ for ( ; tx_array_id < MIMXRT10XX_FLEXCAN_TX_MB_NUM ; ++ tx_array_id )
7171 {
7272 uint64_t tx_array_id_bit = (1UL << tx_array_id );
73- if (!(self -> data -> tx_state & tx_array_id_bit ))
74- {
73+ if (!(self -> data -> tx_state & tx_array_id_bit )) {
7574 // Found a free tx array id. Mark it as used.
7675 MIMXRT_CANIO_CAN_DEBUG ("canio: Found free Tx MB: %d\n" , tx_array_id );
7776 self -> data -> tx_state |= tx_array_id_bit ;
@@ -88,27 +87,23 @@ static uint8_t mimxrt10xx_flexcan_get_free_tx_mbid(canio_can_obj_t *self) {
8887 return MIMXRT10XX_FLEXCAN_TX_ARRID_TO_MBID (tx_array_id );
8988}
9089
91- static void mimxrt10xx_flexcan_set_tx_mb_free_by_mbid (canio_can_obj_t * self , uint8_t mb_idx )
92- {
90+ static void mimxrt10xx_flexcan_set_tx_mb_free_by_mbid (canio_can_obj_t * self , uint8_t mb_idx ) {
9391 // We simply set the Nth bit zero. This means that that message buffer is free to use.
9492 uint64_t tx_array_id_bit = (1UL << MIMXRT10XX_FLEXCAN_TX_MBID_TO_ARRID (mb_idx ));
9593 self -> data -> tx_state &= ~(tx_array_id_bit );
9694}
9795
98- static void mimxrt10xx_flexcan_set_tx_mb_busy_by_mbid (canio_can_obj_t * self , uint8_t mb_idx )
99- {
96+ static void mimxrt10xx_flexcan_set_tx_mb_busy_by_mbid (canio_can_obj_t * self , uint8_t mb_idx ) {
10097 // We simply set the Nth bit 1. This means that that message buffer is busy and cannot be used.
10198 uint64_t tx_array_id_bit = (1UL << MIMXRT10XX_FLEXCAN_TX_MBID_TO_ARRID (mb_idx ));
10299 self -> data -> tx_state |= tx_array_id_bit ;
103100}
104101
105- static void mimxrt10xx_flexcan_abort_tx_frames (canio_can_obj_t * self )
106- {
107- for (uint8_t tx_array_id = 0 ; tx_array_id < MIMXRT10XX_FLEXCAN_TX_MB_NUM ; ++ tx_array_id )
102+ static void mimxrt10xx_flexcan_abort_tx_frames (canio_can_obj_t * self ) {
103+ for (uint8_t tx_array_id = 0 ; tx_array_id < MIMXRT10XX_FLEXCAN_TX_MB_NUM ; ++ tx_array_id )
108104 {
109105 uint64_t tx_array_id_bit = (1UL << tx_array_id );
110- if (self -> data -> tx_state & tx_array_id_bit )
111- {
106+ if (self -> data -> tx_state & tx_array_id_bit ) {
112107 // Found a used/busy tx message buffer. Abort it.
113108 FLEXCAN_TransferAbortSend (self -> data -> base , & self -> data -> handle , MIMXRT10XX_FLEXCAN_TX_ARRID_TO_MBID (tx_array_id ));
114109
@@ -119,8 +114,7 @@ static void mimxrt10xx_flexcan_abort_tx_frames(canio_can_obj_t *self)
119114
120115static void mimxrt10xx_flexcan_handle_error (canio_can_obj_t * self ) {
121116 canio_bus_state_t state = common_hal_canio_can_state_get (self );
122- if (state == BUS_STATE_OFF )
123- {
117+ if (state == BUS_STATE_OFF ) {
124118 // Abort any pending tx and rx in case of bus-off.
125119 mimxrt10xx_flexcan_abort_tx_frames (self );
126120 FLEXCAN_TransferAbortReceiveFifo (self -> data -> base , & self -> data -> handle );
@@ -129,11 +123,11 @@ static void mimxrt10xx_flexcan_handle_error(canio_can_obj_t *self) {
129123
130124static FLEXCAN_CALLBACK (mimxrt10xx_flexcan_callback )
131125{
132- (void ) base ; // unused variable
133- (void ) handle ; // unused variable
126+ (void )base ; // unused variable
127+ (void )handle ; // unused variable
134128 // The result field can either be a message buffer index or a status flags value.
135129
136- canio_can_obj_t * self = (canio_can_obj_t * ) userData ;
130+ canio_can_obj_t * self = (canio_can_obj_t * ) userData ;
137131
138132 switch (status ) {
139133
@@ -206,7 +200,7 @@ static FLEXCAN_CALLBACK(mimxrt10xx_flexcan_callback)
206200 // Process FlexCAN module error and status event.
207201 case kStatus_FLEXCAN_ErrorStatus :
208202 // This is *very* verbose when the bus is disconnected!
209- //MIMXRT_CANIO_CAN_CALLBACK_DEBUG("canio: callback got status = UnHandled or ErrorStatus");
203+ // MIMXRT_CANIO_CAN_CALLBACK_DEBUG("canio: callback got status = UnHandled or ErrorStatus");
210204
211205 // We could do some fancy statistics update, but canio does not have.
212206 mimxrt10xx_flexcan_handle_error (self );
@@ -243,32 +237,31 @@ void common_hal_canio_can_construct(canio_can_obj_t *self, const mcu_pin_obj_t *
243237 FLEXCAN_GetDefaultConfig (& config );
244238
245239 // Change default flexcan module configuration based on canio constructor parameters.
246- config .clkSrc = CLOCK_GetMux (kCLOCK_CanMux );
247- config .baudRate = baudrate ;
248- config .enableLoopBack = loopback ;
240+ config .clkSrc = CLOCK_GetMux (kCLOCK_CanMux );
241+ config .baudRate = baudrate ;
242+ config .enableLoopBack = loopback ;
249243 config .enableListenOnlyMode = silent ;
250- config .maxMbNum = 64 ;
251- config .enableIndividMask = true; // required to enable matching using a 'Listener'
244+ config .maxMbNum = 64 ;
245+ config .enableIndividMask = true; // required to enable matching using a 'Listener'
252246 // config.disableSelfReception = true; // TODO: do we want to disable this?
253247
254248 #if (defined(MIMXRT10XX_FLEXCAN_USE_IMPROVED_TIMING_CONFIG ) && MIMXRT10XX_FLEXCAN_USE_IMPROVED_TIMING_CONFIG )
255- // If improved timing configuration is enabled then tell the SDK to calculate it.
256- flexcan_timing_config_t timing_config ;
257- memset (& timing_config , 0 , sizeof (flexcan_timing_config_t ));
258- if (FLEXCAN_CalculateImprovedTimingValues (self -> data -> base , config .baudRate , MIMXRT10XX_FLEXCAN_CLK_FREQ , & timing_config ))
259- {
260- // SDK could calculate the improved timing configuration. Yay!
261- // Let's update our flexcan module config to use it.
262- memcpy (& (config .timingConfig ), & timing_config , sizeof (flexcan_timing_config_t ));
263- }
249+ // If improved timing configuration is enabled then tell the SDK to calculate it.
250+ flexcan_timing_config_t timing_config ;
251+ memset (& timing_config , 0 , sizeof (flexcan_timing_config_t ));
252+ if (FLEXCAN_CalculateImprovedTimingValues (self -> data -> base , config .baudRate , MIMXRT10XX_FLEXCAN_CLK_FREQ , & timing_config )) {
253+ // SDK could calculate the improved timing configuration. Yay!
254+ // Let's update our flexcan module config to use it.
255+ memcpy (& (config .timingConfig ), & timing_config , sizeof (flexcan_timing_config_t ));
256+ }
264257 #endif
265258
266259 // Initialize the flexcan module with user-defined settings.
267260 FLEXCAN_Init (self -> data -> base , & config , MIMXRT10XX_FLEXCAN_CLK_FREQ );
268261
269262 // Create FlexCAN handle structure and set call back function.
270263 // As callback data we set 'self'. In callback we can cast it back to 'canio_can_obj_t'.
271- FLEXCAN_TransferCreateHandle (self -> data -> base , & self -> data -> handle , mimxrt10xx_flexcan_callback , (void * )self );
264+ FLEXCAN_TransferCreateHandle (self -> data -> base , & self -> data -> handle , mimxrt10xx_flexcan_callback , (void * )self );
272265
273266 // Set rx mask to don't care on all bits.
274267 flexcan_rx_fifo_config_t fifo_config ;
@@ -323,7 +316,9 @@ void common_hal_canio_can_restart(canio_can_obj_t *self) {
323316 // But I will leave this code here just in case.
324317
325318 canio_bus_state_t state = common_hal_canio_can_state_get (self );
326- if (state != BUS_STATE_OFF ) return ;
319+ if (state != BUS_STATE_OFF ) {
320+ return ;
321+ }
327322
328323 self -> data -> base -> CTRL1 &= ~CAN_CTRL1_BOFFREC_MASK ;
329324
@@ -357,8 +352,7 @@ void common_hal_canio_can_send(canio_can_obj_t *self, mp_obj_t message_in) {
357352 maybe_auto_restart (self );
358353
359354 canio_bus_state_t state = common_hal_canio_can_state_get (self );
360- if (state == BUS_STATE_OFF )
361- {
355+ if (state == BUS_STATE_OFF ) {
362356 // Bus is off. Transmit failed.
363357 mp_raise_OSError (MP_ENODEV );
364358 }
0 commit comments