@@ -89,7 +89,7 @@ typedef enum {
89
89
UART_NEVER_RESET ,
90
90
} uart_status_t ;
91
91
92
- static uint32_t timeout_ms = 0 ;
92
+ static uint32_t timeout_ms = 0 ;
93
93
94
94
// Set each bit to indicate an active UART
95
95
// will be checked by ISR Handler for which ones to call
@@ -99,7 +99,7 @@ static volatile int uart_err;
99
99
// static uint8_t uart_never_reset_mask = 0;
100
100
101
101
static int isValidBaudrate (uint32_t baudrate ) {
102
- switch (baudrate ) {
102
+ switch (baudrate ) {
103
103
case UART_9600 :
104
104
return 1 ;
105
105
break ;
@@ -133,17 +133,16 @@ static int isValidBaudrate(uint32_t baudrate) {
133
133
}
134
134
}
135
135
136
- static mxc_uart_parity_t convertParity (busio_uart_parity_t busio_parity )
137
- {
138
- switch (busio_parity ) {
139
- case BUSIO_UART_PARITY_NONE :
140
- return MXC_UART_PARITY_DISABLE ;
141
- case BUSIO_UART_PARITY_EVEN :
142
- return MXC_UART_PARITY_EVEN_0 ;
143
- case BUSIO_UART_PARITY_ODD :
144
- return MXC_UART_PARITY_ODD_0 ;
145
- default :
146
- mp_raise_ValueError (MP_ERROR_TEXT ("Parity must be ODD, EVEN, or NONE\n" ));
136
+ static mxc_uart_parity_t convertParity (busio_uart_parity_t busio_parity ) {
137
+ switch (busio_parity ) {
138
+ case BUSIO_UART_PARITY_NONE :
139
+ return MXC_UART_PARITY_DISABLE ;
140
+ case BUSIO_UART_PARITY_EVEN :
141
+ return MXC_UART_PARITY_EVEN_0 ;
142
+ case BUSIO_UART_PARITY_ODD :
143
+ return MXC_UART_PARITY_ODD_0 ;
144
+ default :
145
+ mp_raise_ValueError (MP_ERROR_TEXT ("Parity must be ODD, EVEN, or NONE\n" ));
147
146
}
148
147
}
149
148
@@ -169,33 +168,31 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
169
168
const mcu_pin_obj_t * rs485_dir , bool rs485_invert ,
170
169
uint32_t baudrate , uint8_t bits , busio_uart_parity_t parity , uint8_t stop ,
171
170
mp_float_t timeout , uint16_t receiver_buffer_size , byte * receiver_buffer ,
172
- bool sigint_enabled )
173
- {
171
+ bool sigint_enabled ) {
174
172
int err , temp ;
175
173
176
174
// Check for NULL Pointers && valid UART settings
177
- assert ( self );
175
+ assert (self );
178
176
179
177
// Assign UART ID based on pins
180
178
temp = pinsToUart (tx , rx );
181
179
if (temp == -1 ) {
182
180
// Error will be indicated by pinsToUart(tx, rx) function
183
181
return ;
184
- }
185
- else {
182
+ } else {
186
183
self -> uart_id = temp ;
187
184
self -> uart_regs = MXC_UART_GET_UART (temp );
188
185
}
189
186
190
- assert ( (self -> uart_id >= 0 ) && (self -> uart_id < NUM_UARTS ) );
187
+ assert ((self -> uart_id >= 0 ) && (self -> uart_id < NUM_UARTS ));
191
188
192
189
// Indicate RS485 not implemented
193
- if ( (rs485_dir != NULL ) || (rs485_invert ) ) {
190
+ if ((rs485_dir != NULL ) || (rs485_invert )) {
194
191
mp_raise_NotImplementedError (MP_ERROR_TEXT ("RS485" ));
195
192
}
196
193
197
194
if ((rx != NULL ) && (tx != NULL )) {
198
- err = MXC_UART_Init (self -> uart_regs , baudrate , MXC_UART_IBRO_CLK );
195
+ err = MXC_UART_Init (self -> uart_regs , baudrate , MXC_UART_IBRO_CLK );
199
196
if (err != E_NO_ERROR ) {
200
197
mp_raise_RuntimeError (MP_ERROR_TEXT ("Failed to initialize UART.\n" ));
201
198
}
@@ -205,14 +202,11 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
205
202
self -> rx_pin = rx ;
206
203
common_hal_mcu_pin_claim (self -> tx_pin );
207
204
common_hal_mcu_pin_claim (self -> rx_pin );
208
- }
209
- else if (tx != NULL ) {
205
+ } else if (tx != NULL ) {
210
206
mp_raise_NotImplementedError (MP_ERROR_TEXT ("UART needs TX & RX" ));
211
- }
212
- else if (rx != NULL ) {
207
+ } else if (rx != NULL ) {
213
208
mp_raise_NotImplementedError (MP_ERROR_TEXT ("UART needs TX & RX" ));
214
- }
215
- else {
209
+ } else {
216
210
// Should not get here, as shared-bindings API should not call this way
217
211
}
218
212
@@ -222,13 +216,12 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
222
216
self -> rts_pin = rts ;
223
217
common_hal_mcu_pin_claim (self -> cts_pin );
224
218
common_hal_mcu_pin_claim (self -> rts_pin );
225
- }
226
- else if (cts || rts ) {
219
+ } else if (cts || rts ) {
227
220
mp_raise_ValueError (MP_ERROR_TEXT ("Flow Ctrl needs both CTS & RTS" ));
228
221
}
229
222
230
223
// Set stop bits & data size
231
- assert ( (stop == 1 ) || (stop == 2 ) );
224
+ assert ((stop == 1 ) || (stop == 2 ));
232
225
mp_arg_validate_int (bits , 8 , MP_QSTR_bits );
233
226
MXC_UART_SetDataSize (self -> uart_regs , bits );
234
227
MXC_UART_SetStopBits (self -> uart_regs , stop );
@@ -270,8 +263,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
270
263
return ;
271
264
}
272
265
273
- void common_hal_busio_uart_deinit (busio_uart_obj_t * self )
274
- {
266
+ void common_hal_busio_uart_deinit (busio_uart_obj_t * self ) {
275
267
assert (self );
276
268
277
269
if (!common_hal_busio_uart_deinited (self )) {
@@ -300,22 +292,19 @@ void common_hal_busio_uart_deinit(busio_uart_obj_t *self)
300
292
}
301
293
}
302
294
303
- bool common_hal_busio_uart_deinited (busio_uart_obj_t * self )
304
- {
295
+ bool common_hal_busio_uart_deinited (busio_uart_obj_t * self ) {
305
296
if (uarts_active & (1 << self -> uart_id )) {
306
297
return false;
307
- }
308
- else {
298
+ } else {
309
299
return true;
310
300
};
311
301
}
312
302
313
303
// Read characters. len is in characters NOT bytes!
314
304
size_t common_hal_busio_uart_read (busio_uart_obj_t * self ,
315
- uint8_t * data , size_t len , int * errcode )
316
- {
305
+ uint8_t * data , size_t len , int * errcode ) {
317
306
int err ;
318
- uint32_t start_time = 0 ;
307
+ uint32_t start_time = 0 ;
319
308
static size_t bytes_remaining ;
320
309
321
310
// Setup globals & status tracking
@@ -343,12 +332,12 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self,
343
332
* errcode = err ;
344
333
MXC_UART_AbortAsync (self -> uart_regs );
345
334
NVIC_DisableIRQ (MXC_UART_GET_IRQ (self -> uart_id ));
346
- mp_raise_RuntimeError_varg (MP_ERROR_TEXT ("\nERR: Error starting trasaction : %d\n" ), err );
335
+ mp_raise_RuntimeError_varg (MP_ERROR_TEXT ("\nERR: Error starting transaction : %d\n" ), err );
347
336
}
348
337
349
338
// Wait for transaction completion or timeout
350
- while ( (uart_status [self -> uart_id ] != UART_FREE ) &&
351
- (supervisor_ticks_ms64 () - start_time < (self -> timeout * 1000 ))) {
339
+ while ((uart_status [self -> uart_id ] != UART_FREE ) &&
340
+ (supervisor_ticks_ms64 () - start_time < (self -> timeout * 1000 ))) {
352
341
}
353
342
354
343
// If the timeout gets hit, abort and error out
@@ -357,7 +346,6 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self,
357
346
NVIC_DisableIRQ (MXC_UART_GET_IRQ (self -> uart_id ));
358
347
mp_raise_RuntimeError (MP_ERROR_TEXT ("\nERR: Uart transaction timed out.\n" ));
359
348
}
360
-
361
349
// Check for errors from the callback
362
350
else if (uart_err != E_NO_ERROR ) {
363
351
MXC_UART_AbortAsync (self -> uart_regs );
@@ -371,11 +359,10 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self,
371
359
372
360
// Write characters. len is in characters NOT bytes!
373
361
// This function blocks until the timeout finishes
374
- size_t common_hal_busio_uart_write (busio_uart_obj_t * self ,
375
- const uint8_t * data , size_t len , int * errcode )
376
- {
362
+ size_t common_hal_busio_uart_write (busio_uart_obj_t * self ,
363
+ const uint8_t * data , size_t len , int * errcode ) {
377
364
int err ;
378
- uint32_t start_time = 0 ;
365
+ uint32_t start_time = 0 ;
379
366
static size_t bytes_remaining ;
380
367
381
368
// Setup globals & status tracking
@@ -407,8 +394,8 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self,
407
394
}
408
395
409
396
// Wait for transaction completion or timeout
410
- while ( (uart_status [self -> uart_id ] != UART_FREE ) &&
411
- (supervisor_ticks_ms64 () - start_time < (self -> timeout * 1000 ))) {
397
+ while ((uart_status [self -> uart_id ] != UART_FREE ) &&
398
+ (supervisor_ticks_ms64 () - start_time < (self -> timeout * 1000 ))) {
412
399
413
400
// Call the handler and abort if errors
414
401
uart_err = MXC_UART_AsyncHandler (MXC_UART_GET_UART (0 ));
@@ -425,7 +412,6 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self,
425
412
NVIC_DisableIRQ (MXC_UART_GET_IRQ (self -> uart_id ));
426
413
mp_raise_ConnectionError (MP_ERROR_TEXT ("\nERR: Uart transaction timed out.\n" ));
427
414
}
428
-
429
415
// Check for errors from the callback
430
416
else if (uart_err != E_NO_ERROR ) {
431
417
MXC_UART_AbortAsync (self -> uart_regs );
@@ -436,29 +422,24 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self,
436
422
return len ;
437
423
}
438
424
439
- uint32_t common_hal_busio_uart_get_baudrate (busio_uart_obj_t * self )
440
- {
425
+ uint32_t common_hal_busio_uart_get_baudrate (busio_uart_obj_t * self ) {
441
426
return self -> baudrate ;
442
427
}
443
428
444
429
// Validate baudrate
445
- void common_hal_busio_uart_set_baudrate (busio_uart_obj_t * self , uint32_t baudrate )
446
- {
447
- if ( isValidBaudrate (baudrate ) ) {
430
+ void common_hal_busio_uart_set_baudrate (busio_uart_obj_t * self , uint32_t baudrate ) {
431
+ if (isValidBaudrate (baudrate )) {
448
432
self -> baudrate = baudrate ;
449
- }
450
- else {
433
+ } else {
451
434
mp_raise_ValueError (MP_ERROR_TEXT ("Baudrate invalid. Must be a standard UART baudrate.\n" ));
452
435
}
453
436
}
454
437
455
- mp_float_t common_hal_busio_uart_get_timeout (busio_uart_obj_t * self )
456
- {
438
+ mp_float_t common_hal_busio_uart_get_timeout (busio_uart_obj_t * self ) {
457
439
return self -> timeout ;
458
440
}
459
441
460
- void common_hal_busio_uart_set_timeout (busio_uart_obj_t * self , mp_float_t timeout )
461
- {
442
+ void common_hal_busio_uart_set_timeout (busio_uart_obj_t * self , mp_float_t timeout ) {
462
443
if (timeout > 100.0 ) {
463
444
mp_raise_ValueError (MP_ERROR_TEXT ("Timeout must be < 100 seconds" ));
464
445
}
@@ -469,23 +450,19 @@ void common_hal_busio_uart_set_timeout(busio_uart_obj_t *self, mp_float_t timeou
469
450
return ;
470
451
}
471
452
472
- uint32_t common_hal_busio_uart_rx_characters_available (busio_uart_obj_t * self )
473
- {
453
+ uint32_t common_hal_busio_uart_rx_characters_available (busio_uart_obj_t * self ) {
474
454
return MXC_UART_GetRXFIFOAvailable (self -> uart_regs );
475
455
}
476
456
477
- void common_hal_busio_uart_clear_rx_buffer (busio_uart_obj_t * self )
478
- {
457
+ void common_hal_busio_uart_clear_rx_buffer (busio_uart_obj_t * self ) {
479
458
MXC_UART_ClearRXFIFO (self -> uart_regs );
480
459
}
481
460
482
- bool common_hal_busio_uart_ready_to_tx (busio_uart_obj_t * self )
483
- {
461
+ bool common_hal_busio_uart_ready_to_tx (busio_uart_obj_t * self ) {
484
462
return !(MXC_UART_GetStatus (self -> uart_regs ) & (MXC_F_UART_STATUS_TX_BUSY ));
485
463
}
486
464
487
- void common_hal_busio_uart_never_reset (busio_uart_obj_t * self )
488
- {
465
+ void common_hal_busio_uart_never_reset (busio_uart_obj_t * self ) {
489
466
common_hal_never_reset_pin (self -> tx_pin );
490
467
common_hal_never_reset_pin (self -> rx_pin );
491
468
common_hal_never_reset_pin (self -> cts_pin );
0 commit comments