21
21
22
22
// Be verbose
23
23
#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__)
25
25
26
26
#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__)
28
28
29
29
30
30
static 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) {
67
67
bool found_free_tx_mb = false;
68
68
int8_t tx_array_id = 0 ;
69
69
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 )
71
71
{
72
72
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 )) {
75
74
// Found a free tx array id. Mark it as used.
76
75
MIMXRT_CANIO_CAN_DEBUG ("canio: Found free Tx MB: %d\n" , tx_array_id );
77
76
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) {
88
87
return MIMXRT10XX_FLEXCAN_TX_ARRID_TO_MBID (tx_array_id );
89
88
}
90
89
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 ) {
93
91
// We simply set the Nth bit zero. This means that that message buffer is free to use.
94
92
uint64_t tx_array_id_bit = (1UL << MIMXRT10XX_FLEXCAN_TX_MBID_TO_ARRID (mb_idx ));
95
93
self -> data -> tx_state &= ~(tx_array_id_bit );
96
94
}
97
95
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 ) {
100
97
// We simply set the Nth bit 1. This means that that message buffer is busy and cannot be used.
101
98
uint64_t tx_array_id_bit = (1UL << MIMXRT10XX_FLEXCAN_TX_MBID_TO_ARRID (mb_idx ));
102
99
self -> data -> tx_state |= tx_array_id_bit ;
103
100
}
104
101
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 )
108
104
{
109
105
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 ) {
112
107
// Found a used/busy tx message buffer. Abort it.
113
108
FLEXCAN_TransferAbortSend (self -> data -> base , & self -> data -> handle , MIMXRT10XX_FLEXCAN_TX_ARRID_TO_MBID (tx_array_id ));
114
109
@@ -119,8 +114,7 @@ static void mimxrt10xx_flexcan_abort_tx_frames(canio_can_obj_t *self)
119
114
120
115
static void mimxrt10xx_flexcan_handle_error (canio_can_obj_t * self ) {
121
116
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 ) {
124
118
// Abort any pending tx and rx in case of bus-off.
125
119
mimxrt10xx_flexcan_abort_tx_frames (self );
126
120
FLEXCAN_TransferAbortReceiveFifo (self -> data -> base , & self -> data -> handle );
@@ -129,11 +123,11 @@ static void mimxrt10xx_flexcan_handle_error(canio_can_obj_t *self) {
129
123
130
124
static FLEXCAN_CALLBACK (mimxrt10xx_flexcan_callback )
131
125
{
132
- (void ) base ; // unused variable
133
- (void ) handle ; // unused variable
126
+ (void )base ; // unused variable
127
+ (void )handle ; // unused variable
134
128
// The result field can either be a message buffer index or a status flags value.
135
129
136
- canio_can_obj_t * self = (canio_can_obj_t * ) userData ;
130
+ canio_can_obj_t * self = (canio_can_obj_t * ) userData ;
137
131
138
132
switch (status ) {
139
133
@@ -206,7 +200,7 @@ static FLEXCAN_CALLBACK(mimxrt10xx_flexcan_callback)
206
200
// Process FlexCAN module error and status event.
207
201
case kStatus_FLEXCAN_ErrorStatus :
208
202
// 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");
210
204
211
205
// We could do some fancy statistics update, but canio does not have.
212
206
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 *
243
237
FLEXCAN_GetDefaultConfig (& config );
244
238
245
239
// 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 ;
249
243
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'
252
246
// config.disableSelfReception = true; // TODO: do we want to disable this?
253
247
254
248
#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
+ }
264
257
#endif
265
258
266
259
// Initialize the flexcan module with user-defined settings.
267
260
FLEXCAN_Init (self -> data -> base , & config , MIMXRT10XX_FLEXCAN_CLK_FREQ );
268
261
269
262
// Create FlexCAN handle structure and set call back function.
270
263
// 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 );
272
265
273
266
// Set rx mask to don't care on all bits.
274
267
flexcan_rx_fifo_config_t fifo_config ;
@@ -323,7 +316,9 @@ void common_hal_canio_can_restart(canio_can_obj_t *self) {
323
316
// But I will leave this code here just in case.
324
317
325
318
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
+ }
327
322
328
323
self -> data -> base -> CTRL1 &= ~CAN_CTRL1_BOFFREC_MASK ;
329
324
@@ -357,8 +352,7 @@ void common_hal_canio_can_send(canio_can_obj_t *self, mp_obj_t message_in) {
357
352
maybe_auto_restart (self );
358
353
359
354
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 ) {
362
356
// Bus is off. Transmit failed.
363
357
mp_raise_OSError (MP_ENODEV );
364
358
}
0 commit comments