Browse Source

esp32/machine_uart: Add error checking for IDF API's.

Signed-off-by: Daniël van de Giessen <daniel@dvdgiessen.nl>
pull/12727/head
Daniël van de Giessen 1 year ago
committed by Damien George
parent
commit
f1d6af9819
  1. 76
      ports/esp32/machine_uart.c

76
ports/esp32/machine_uart.c

@ -84,7 +84,7 @@ STATIC const char *_parity_name[] = {"None", "1", "0"};
STATIC void mp_machine_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);
uint32_t baudrate;
uart_get_baudrate(self->uart_num, &baudrate);
check_esp_err(uart_get_baudrate(self->uart_num, &baudrate));
mp_printf(print, "UART(%u, baudrate=%u, bits=%u, parity=%s, stop=%u, tx=%d, rx=%d, rts=%d, cts=%d, txbuf=%u, rxbuf=%u, timeout=%u, timeout_char=%u",
self->uart_num, baudrate, self->bits, _parity_name[self->parity],
self->stop, self->tx, self->rx, self->rts, self->cts, self->txbuf, self->rxbuf, self->timeout, self->timeout_char);
@ -177,22 +177,22 @@ STATIC void mp_machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args,
.source_clk = UART_SOURCE_CLK,
};
uint32_t baudrate;
uart_get_baudrate(self->uart_num, &baudrate);
check_esp_err(uart_get_baudrate(self->uart_num, &baudrate));
uartcfg.baud_rate = baudrate;
uart_get_word_length(self->uart_num, &uartcfg.data_bits);
uart_get_parity(self->uart_num, &uartcfg.parity);
uart_get_stop_bits(self->uart_num, &uartcfg.stop_bits);
uart_driver_delete(self->uart_num);
uart_param_config(self->uart_num, &uartcfg);
uart_driver_install(self->uart_num, self->rxbuf, self->txbuf, 0, NULL, 0);
check_esp_err(uart_get_word_length(self->uart_num, &uartcfg.data_bits));
check_esp_err(uart_get_parity(self->uart_num, &uartcfg.parity));
check_esp_err(uart_get_stop_bits(self->uart_num, &uartcfg.stop_bits));
check_esp_err(uart_driver_delete(self->uart_num));
check_esp_err(uart_param_config(self->uart_num, &uartcfg));
check_esp_err(uart_driver_install(self->uart_num, self->rxbuf, self->txbuf, 0, NULL, 0));
}
// set baudrate
uint32_t baudrate = 115200;
if (args[ARG_baudrate].u_int > 0) {
uart_set_baudrate(self->uart_num, args[ARG_baudrate].u_int);
check_esp_err(uart_set_baudrate(self->uart_num, args[ARG_baudrate].u_int));
}
uart_get_baudrate(self->uart_num, &baudrate);
check_esp_err(uart_get_baudrate(self->uart_num, &baudrate));
if (args[ARG_tx].u_obj != MP_OBJ_NULL) {
self->tx = machine_pin_get_id(args[ARG_tx].u_obj);
@ -209,26 +209,26 @@ STATIC void mp_machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args,
if (args[ARG_cts].u_obj != MP_OBJ_NULL) {
self->cts = machine_pin_get_id(args[ARG_cts].u_obj);
}
uart_set_pin(self->uart_num, self->tx, self->rx, self->rts, self->cts);
check_esp_err(uart_set_pin(self->uart_num, self->tx, self->rx, self->rts, self->cts));
// set data bits
switch (args[ARG_bits].u_int) {
case 0:
break;
case 5:
uart_set_word_length(self->uart_num, UART_DATA_5_BITS);
check_esp_err(uart_set_word_length(self->uart_num, UART_DATA_5_BITS));
self->bits = 5;
break;
case 6:
uart_set_word_length(self->uart_num, UART_DATA_6_BITS);
check_esp_err(uart_set_word_length(self->uart_num, UART_DATA_6_BITS));
self->bits = 6;
break;
case 7:
uart_set_word_length(self->uart_num, UART_DATA_7_BITS);
check_esp_err(uart_set_word_length(self->uart_num, UART_DATA_7_BITS));
self->bits = 7;
break;
case 8:
uart_set_word_length(self->uart_num, UART_DATA_8_BITS);
check_esp_err(uart_set_word_length(self->uart_num, UART_DATA_8_BITS));
self->bits = 8;
break;
default:
@ -239,15 +239,15 @@ STATIC void mp_machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args,
// set parity
if (args[ARG_parity].u_obj != MP_OBJ_NULL) {
if (args[ARG_parity].u_obj == mp_const_none) {
uart_set_parity(self->uart_num, UART_PARITY_DISABLE);
check_esp_err(uart_set_parity(self->uart_num, UART_PARITY_DISABLE));
self->parity = 0;
} else {
mp_int_t parity = mp_obj_get_int(args[ARG_parity].u_obj);
if (parity & 1) {
uart_set_parity(self->uart_num, UART_PARITY_ODD);
check_esp_err(uart_set_parity(self->uart_num, UART_PARITY_ODD));
self->parity = 1;
} else {
uart_set_parity(self->uart_num, UART_PARITY_EVEN);
check_esp_err(uart_set_parity(self->uart_num, UART_PARITY_EVEN));
self->parity = 2;
}
}
@ -259,11 +259,11 @@ STATIC void mp_machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args,
case 0:
break;
case 1:
uart_set_stop_bits(self->uart_num, UART_STOP_BITS_1);
check_esp_err(uart_set_stop_bits(self->uart_num, UART_STOP_BITS_1));
self->stop = 1;
break;
case 2:
uart_set_stop_bits(self->uart_num, UART_STOP_BITS_2);
check_esp_err(uart_set_stop_bits(self->uart_num, UART_STOP_BITS_2));
self->stop = 2;
break;
default:
@ -283,10 +283,10 @@ STATIC void mp_machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args,
uint32_t char_time_ms = 12000 / baudrate + 1;
uint32_t rx_timeout = self->timeout_char / char_time_ms;
if (rx_timeout < 1) {
uart_set_rx_full_threshold(self->uart_num, 1);
uart_set_rx_timeout(self->uart_num, 1);
check_esp_err(uart_set_rx_full_threshold(self->uart_num, 1));
check_esp_err(uart_set_rx_timeout(self->uart_num, 1));
} else {
uart_set_rx_timeout(self->uart_num, rx_timeout);
check_esp_err(uart_set_rx_timeout(self->uart_num, rx_timeout));
}
}
@ -297,7 +297,7 @@ STATIC void mp_machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args,
}
self->invert = args[ARG_invert].u_int;
}
uart_set_line_inverse(self->uart_num, self->invert);
check_esp_err(uart_set_line_inverse(self->uart_num, self->invert));
// set hardware flow control
if (args[ARG_flow].u_int != -1) {
@ -306,7 +306,7 @@ STATIC void mp_machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args,
}
self->flowcontrol = args[ARG_flow].u_int;
}
uart_set_hw_flow_ctrl(self->uart_num, self->flowcontrol, UART_FIFO_LEN - UART_FIFO_LEN / 4);
check_esp_err(uart_set_hw_flow_ctrl(self->uart_num, self->flowcontrol, UART_FIFO_LEN - UART_FIFO_LEN / 4));
}
STATIC mp_obj_t mp_machine_uart_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
@ -367,13 +367,13 @@ STATIC mp_obj_t mp_machine_uart_make_new(const mp_obj_type_t *type, size_t n_arg
#endif
{
// Remove any existing configuration
uart_driver_delete(self->uart_num);
check_esp_err(uart_driver_delete(self->uart_num));
// init the peripheral
// Setup
uart_param_config(self->uart_num, &uartcfg);
check_esp_err(uart_param_config(self->uart_num, &uartcfg));
uart_driver_install(uart_num, self->rxbuf, self->txbuf, 0, NULL, 0);
check_esp_err(uart_driver_install(uart_num, self->rxbuf, self->txbuf, 0, NULL, 0));
}
mp_map_t kw_args;
@ -381,18 +381,18 @@ STATIC mp_obj_t mp_machine_uart_make_new(const mp_obj_type_t *type, size_t n_arg
mp_machine_uart_init_helper(self, n_args - 1, args + 1, &kw_args);
// Make sure pins are connected.
uart_set_pin(self->uart_num, self->tx, self->rx, self->rts, self->cts);
check_esp_err(uart_set_pin(self->uart_num, self->tx, self->rx, self->rts, self->cts));
return MP_OBJ_FROM_PTR(self);
}
STATIC void mp_machine_uart_deinit(machine_uart_obj_t *self) {
uart_driver_delete(self->uart_num);
check_esp_err(uart_driver_delete(self->uart_num));
}
STATIC mp_int_t mp_machine_uart_any(machine_uart_obj_t *self) {
size_t rxbufsize;
uart_get_buffered_data_len(self->uart_num, &rxbufsize);
check_esp_err(uart_get_buffered_data_len(self->uart_num, &rxbufsize));
return rxbufsize;
}
@ -403,18 +403,18 @@ STATIC bool mp_machine_uart_txdone(machine_uart_obj_t *self) {
STATIC void mp_machine_uart_sendbreak(machine_uart_obj_t *self) {
// Save settings
uint32_t baudrate;
uart_get_baudrate(self->uart_num, &baudrate);
check_esp_err(uart_get_baudrate(self->uart_num, &baudrate));
// Synthesise the break condition by reducing the baud rate,
// and cater for the worst case of 5 data bits, no parity.
uart_wait_tx_done(self->uart_num, pdMS_TO_TICKS(1000));
uart_set_baudrate(self->uart_num, baudrate * 6 / 15);
check_esp_err(uart_wait_tx_done(self->uart_num, pdMS_TO_TICKS(1000)));
check_esp_err(uart_set_baudrate(self->uart_num, baudrate * 6 / 15));
char buf[1] = {0};
uart_write_bytes(self->uart_num, buf, 1);
uart_wait_tx_done(self->uart_num, pdMS_TO_TICKS(1000));
check_esp_err(uart_wait_tx_done(self->uart_num, pdMS_TO_TICKS(1000)));
// Restore original setting
uart_set_baudrate(self->uart_num, baudrate);
check_esp_err(uart_set_baudrate(self->uart_num, baudrate));
}
STATIC mp_uint_t mp_machine_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, int *errcode) {
@ -472,7 +472,7 @@ STATIC mp_uint_t mp_machine_uart_ioctl(mp_obj_t self_in, mp_uint_t request, uint
mp_uint_t flags = arg;
ret = 0;
size_t rxbufsize;
uart_get_buffered_data_len(self->uart_num, &rxbufsize);
check_esp_err(uart_get_buffered_data_len(self->uart_num, &rxbufsize));
if ((flags & MP_STREAM_POLL_RD) && rxbufsize > 0) {
ret |= MP_STREAM_POLL_RD;
}
@ -483,7 +483,7 @@ STATIC mp_uint_t mp_machine_uart_ioctl(mp_obj_t self_in, mp_uint_t request, uint
// The timeout is estimated using the buffer size and the baudrate.
// Take the worst case assumptions at 13 bit symbol size times 2.
uint32_t baudrate;
uart_get_baudrate(self->uart_num, &baudrate);
check_esp_err(uart_get_baudrate(self->uart_num, &baudrate));
uint32_t timeout = (3 + self->txbuf) * 13000 * 2 / baudrate;
if (uart_wait_tx_done(self->uart_num, timeout) == ESP_OK) {
ret = 0;

Loading…
Cancel
Save