Skip to content

Commit def19d2

Browse files
committed
ports: analog: Improve portability for BUSIO.UART
- Move Init & Flow Control into wrapper functions in peripherals/ - Unify API differences (e.g. AsyncHandler doesn't always return an err) - Create UART register macro for TX_BUSY checking in peripherals/ Signed-off-by: Brandon-Hurst <brandon.hurst97@gmail.com>
1 parent 3477ccb commit def19d2

7 files changed

Lines changed: 54 additions & 10 deletions

File tree

ports/analog/common-hal/busio/UART.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
182182
}
183183

184184
if ((rx != NULL) && (tx != NULL)) {
185-
err = MXC_UART_Init(self->uart_regs, baudrate, MXC_UART_IBRO_CLK);
185+
err = uart_init(self->uart_regs, baudrate);
186186
if (err != E_NO_ERROR) {
187187
mp_raise_RuntimeError_varg(MP_ERROR_TEXT("%q init failed"), MP_QSTR_UART);
188188
}
@@ -197,7 +197,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
197197
}
198198

199199
if ((cts) && (rts)) {
200-
MXC_UART_SetFlowCtrl(self->uart_regs, MXC_UART_FLOW_EN, 8);
200+
uart_set_flow_ctrl(self->uart_regs, true, 8);
201201
self->cts_pin = cts;
202202
self->rts_pin = rts;
203203
common_hal_mcu_pin_claim(self->cts_pin);
@@ -377,7 +377,7 @@ size_t common_hal_busio_uart_write(busio_uart_obj_t *self,
377377
uart_wr_req.rxCnt = 0;
378378
uart_wr_req.txCnt = 0;
379379
uart_wr_req.rxData = NULL;
380-
uart_wr_req.txData = data;
380+
uart_wr_req.txData = (uint8_t *)data;
381381
uart_wr_req.txLen = bytes_remaining;
382382
uart_wr_req.rxLen = 0;
383383
uart_wr_req.uart = self->uart_regs;
@@ -394,11 +394,7 @@ size_t common_hal_busio_uart_write(busio_uart_obj_t *self,
394394

395395
// Wait for transaction completion
396396
while (uart_status[self->uart_id] != UART_FREE) {
397-
// Call the handler and abort if errors
398-
uart_err = MXC_UART_AsyncHandler(self->uart_regs);
399-
if (uart_err != E_NO_ERROR) {
400-
MXC_UART_AbortAsync(self->uart_regs);
401-
}
397+
MXC_UART_AsyncHandler(self->uart_regs);
402398
}
403399
// Check for errors from the callback
404400
if (uart_err != E_NO_ERROR) {
@@ -446,7 +442,7 @@ void common_hal_busio_uart_clear_rx_buffer(busio_uart_obj_t *self) {
446442
}
447443

448444
bool common_hal_busio_uart_ready_to_tx(busio_uart_obj_t *self) {
449-
return !(MXC_UART_GetStatus(self->uart_regs) & (MXC_F_UART_STATUS_TX_BUSY));
445+
return !(MXC_UART_GetStatus(self->uart_regs) & (UART_F_STATUS_TX_BUSY));
450446
}
451447

452448
void common_hal_busio_uart_never_reset(busio_uart_obj_t *self) {

ports/analog/peripherals/max32650/max32_uart.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,12 @@ int pinsToUart(const mcu_pin_obj_t *rx, const mcu_pin_obj_t *tx) {
3232
mp_raise_ValueError_varg(MP_ERROR_TEXT("Invalid %q"), MP_QSTR_pins);
3333
return -1;
3434
}
35+
36+
int uart_init(mxc_uart_regs_t *uart, unsigned int baud) {
37+
return MXC_UART_Init(uart, baud);
38+
}
39+
40+
int uart_set_flow_ctrl(mxc_uart_regs_t *uart, bool enable, int rtsThreshold) {
41+
mxc_uart_flow_t flow = enable ? MXC_UART_FLOW_EN_LOW : MXC_UART_FLOW_DIS;
42+
return MXC_UART_SetFlowCtrl(uart, flow, rtsThreshold);
43+
}

ports/analog/peripherals/max32650/max32_uart.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,11 @@
1313

1414
#define NUM_UARTS 3
1515

16+
// UART register compatibility macro
17+
#define UART_F_STATUS_TX_BUSY MXC_F_UART_STAT_TX_BUSY
18+
1619
int pinsToUart(const mcu_pin_obj_t *rx, const mcu_pin_obj_t *tx);
20+
21+
// UART wrappers for portability
22+
int uart_init(mxc_uart_regs_t *uart, unsigned int baud);
23+
int uart_set_flow_ctrl(mxc_uart_regs_t *uart, bool enable, int rtsThreshold);

ports/analog/peripherals/max32665/max32_uart.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include "py/runtime.h"
1414
#include "py/mperrno.h"
1515

16-
// Assuming the use of MAP_A in MSDK, since all documentation
16+
// Assuming the use of MAP_A in MSDK, since all documentation
1717
// states the GPIO maps are the same
1818

1919
const mxc_gpio_cfg_t uart_maps[NUM_UARTS] = {
@@ -38,3 +38,12 @@ int pinsToUart(const mcu_pin_obj_t *rx, const mcu_pin_obj_t *tx) {
3838
mp_raise_ValueError_varg(MP_ERROR_TEXT("Invalid %q"), MP_QSTR_pins);
3939
return -1;
4040
}
41+
42+
int uart_init(mxc_uart_regs_t *uart, unsigned int baud) {
43+
return MXC_UART_Init(uart, baud, MAP_A);
44+
}
45+
46+
int uart_set_flow_ctrl(mxc_uart_regs_t *uart, bool enable, int rtsThreshold) {
47+
mxc_uart_flow_t flow = enable ? MXC_UART_FLOW_EN_LOW : MXC_UART_FLOW_DIS;
48+
return MXC_UART_SetFlowCtrl(uart, flow, rtsThreshold, MAP_A);
49+
}

ports/analog/peripherals/max32665/max32_uart.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,11 @@
1313

1414
#define NUM_UARTS 3
1515

16+
// UART register compatibility macro
17+
#define UART_F_STATUS_TX_BUSY MXC_F_UART_STATUS_TX_BUSY
18+
1619
int pinsToUart(const mcu_pin_obj_t *rx, const mcu_pin_obj_t *tx);
20+
21+
// UART wrappers for portability
22+
int uart_init(mxc_uart_regs_t *uart, unsigned int baud);
23+
int uart_set_flow_ctrl(mxc_uart_regs_t *uart, bool enable, int rtsThreshold);

ports/analog/peripherals/max32690/max32_uart.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,12 @@ int pinsToUart(const mcu_pin_obj_t *rx, const mcu_pin_obj_t *tx) {
3434
mp_raise_ValueError_varg(MP_ERROR_TEXT("Invalid %q"), MP_QSTR_pins);
3535
return -1;
3636
}
37+
38+
int uart_init(mxc_uart_regs_t *uart, unsigned int baud) {
39+
return MXC_UART_Init(uart, baud, MXC_UART_IBRO_CLK);
40+
}
41+
42+
int uart_set_flow_ctrl(mxc_uart_regs_t *uart, bool enable, int rtsThreshold) {
43+
mxc_uart_flow_t flow = enable ? MXC_UART_FLOW_EN : MXC_UART_FLOW_DIS;
44+
return MXC_UART_SetFlowCtrl(uart, flow, rtsThreshold);
45+
}

ports/analog/peripherals/max32690/max32_uart.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,11 @@
1313

1414
#define NUM_UARTS 4
1515

16+
// UART register compatibility macro
17+
#define UART_F_STATUS_TX_BUSY MXC_F_UART_STATUS_TX_BUSY
18+
1619
int pinsToUart(const mcu_pin_obj_t *rx, const mcu_pin_obj_t *tx);
20+
21+
// UART Wrappers for compatibility
22+
int uart_init(mxc_uart_regs_t *uart, unsigned int baud);
23+
int uart_set_flow_ctrl(mxc_uart_regs_t *uart, bool enable, int rtsThreshold);

0 commit comments

Comments
 (0)