esp32/machine_uart: Allow limited configuration of REPL UART.

Some applications may want to adjust the hard coded 115200 REPL buadrate,
and this commit allows it to be changed dynamically via machine.UART(0).
This commit is contained in:
Andrew Leech 2022-02-24 14:30:50 +11:00 committed by Damien George
parent 2cc9232781
commit 919e586e46
3 changed files with 22 additions and 20 deletions

View File

@ -35,6 +35,7 @@
#include "py/stream.h"
#include "py/mperrno.h"
#include "modmachine.h"
#include "uart.h"
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 1, 0)
#define UART_INV_TX UART_INVERSE_TXD
@ -151,6 +152,10 @@ STATIC void machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args, co
if (args[ARG_txbuf].u_int >= 0 || args[ARG_rxbuf].u_int >= 0) {
// must reinitialise driver to change the tx/rx buffer size
if (self->uart_num == MICROPY_HW_UART_REPL) {
mp_raise_ValueError(MP_ERROR_TEXT("UART buffer size is fixed"));
}
if (args[ARG_txbuf].u_int >= 0) {
self->txbuf = args[ARG_txbuf].u_int;
}
@ -291,12 +296,6 @@ STATIC mp_obj_t machine_uart_make_new(const mp_obj_type_t *type, size_t n_args,
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("UART(%d) does not exist"), uart_num);
}
// Attempts to use UART0 from Python has resulted in all sorts of fun errors.
// FIXME: UART0 is disabled for now.
if (uart_num == UART_NUM_0) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("UART(%d) is disabled (dedicated to REPL)"), uart_num);
}
// Defaults
uart_config_t uartcfg = {
.baud_rate = 115200,
@ -338,6 +337,8 @@ STATIC mp_obj_t machine_uart_make_new(const mp_obj_type_t *type, size_t n_args,
#endif
}
// Only reset the driver if it's not the REPL UART.
if (uart_num != MICROPY_HW_UART_REPL) {
// Remove any existing configuration
uart_driver_delete(self->uart_num);
@ -346,6 +347,7 @@ STATIC mp_obj_t machine_uart_make_new(const mp_obj_type_t *type, size_t n_args,
uart_param_config(self->uart_num, &uartcfg);
uart_driver_install(uart_num, self->rxbuf, self->txbuf, 0, NULL, 0);
}
mp_map_t kw_args;
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);

View File

@ -35,14 +35,6 @@
#include "py/mphal.h"
#include "uart.h"
#ifndef MICROPY_HW_UART_REPL
#define MICROPY_HW_UART_REPL (UART_NUM_0)
#endif
#ifndef MICROPY_HW_UART_REPL_BAUD
#define MICROPY_HW_UART_REPL_BAUD (115200)
#endif
STATIC void uart_irq_handler(void *arg);
void uart_stdout_init(void) {

View File

@ -28,6 +28,14 @@
#ifndef MICROPY_INCLUDED_ESP32_UART_H
#define MICROPY_INCLUDED_ESP32_UART_H
#ifndef MICROPY_HW_UART_REPL
#define MICROPY_HW_UART_REPL (UART_NUM_0)
#endif
#ifndef MICROPY_HW_UART_REPL_BAUD
#define MICROPY_HW_UART_REPL_BAUD (115200)
#endif
void uart_stdout_init(void);
int uart_stdout_tx_strn(const char *str, size_t len);