Skip to content

Commit 919e586

Browse files
pi-anldpgeorge
authored andcommitted
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).
1 parent 2cc9232 commit 919e586

File tree

3 files changed

+22
-20
lines changed

3 files changed

+22
-20
lines changed

ports/esp32/machine_uart.c

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "py/stream.h"
3636
#include "py/mperrno.h"
3737
#include "modmachine.h"
38+
#include "uart.h"
3839

3940
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 1, 0)
4041
#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
151152

152153
if (args[ARG_txbuf].u_int >= 0 || args[ARG_rxbuf].u_int >= 0) {
153154
// must reinitialise driver to change the tx/rx buffer size
155+
if (self->uart_num == MICROPY_HW_UART_REPL) {
156+
mp_raise_ValueError(MP_ERROR_TEXT("UART buffer size is fixed"));
157+
}
158+
154159
if (args[ARG_txbuf].u_int >= 0) {
155160
self->txbuf = args[ARG_txbuf].u_int;
156161
}
@@ -291,12 +296,6 @@ STATIC mp_obj_t machine_uart_make_new(const mp_obj_type_t *type, size_t n_args,
291296
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("UART(%d) does not exist"), uart_num);
292297
}
293298

294-
// Attempts to use UART0 from Python has resulted in all sorts of fun errors.
295-
// FIXME: UART0 is disabled for now.
296-
if (uart_num == UART_NUM_0) {
297-
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("UART(%d) is disabled (dedicated to REPL)"), uart_num);
298-
}
299-
300299
// Defaults
301300
uart_config_t uartcfg = {
302301
.baud_rate = 115200,
@@ -338,14 +337,17 @@ STATIC mp_obj_t machine_uart_make_new(const mp_obj_type_t *type, size_t n_args,
338337
#endif
339338
}
340339

341-
// Remove any existing configuration
342-
uart_driver_delete(self->uart_num);
340+
// Only reset the driver if it's not the REPL UART.
341+
if (uart_num != MICROPY_HW_UART_REPL) {
342+
// Remove any existing configuration
343+
uart_driver_delete(self->uart_num);
343344

344-
// init the peripheral
345-
// Setup
346-
uart_param_config(self->uart_num, &uartcfg);
345+
// init the peripheral
346+
// Setup
347+
uart_param_config(self->uart_num, &uartcfg);
347348

348-
uart_driver_install(uart_num, self->rxbuf, self->txbuf, 0, NULL, 0);
349+
uart_driver_install(uart_num, self->rxbuf, self->txbuf, 0, NULL, 0);
350+
}
349351

350352
mp_map_t kw_args;
351353
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);

ports/esp32/uart.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,6 @@
3535
#include "py/mphal.h"
3636
#include "uart.h"
3737

38-
#ifndef MICROPY_HW_UART_REPL
39-
#define MICROPY_HW_UART_REPL (UART_NUM_0)
40-
#endif
41-
42-
#ifndef MICROPY_HW_UART_REPL_BAUD
43-
#define MICROPY_HW_UART_REPL_BAUD (115200)
44-
#endif
45-
4638
STATIC void uart_irq_handler(void *arg);
4739

4840
void uart_stdout_init(void) {

ports/esp32/uart.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@
2828
#ifndef MICROPY_INCLUDED_ESP32_UART_H
2929
#define MICROPY_INCLUDED_ESP32_UART_H
3030

31+
#ifndef MICROPY_HW_UART_REPL
32+
#define MICROPY_HW_UART_REPL (UART_NUM_0)
33+
#endif
34+
35+
#ifndef MICROPY_HW_UART_REPL_BAUD
36+
#define MICROPY_HW_UART_REPL_BAUD (115200)
37+
#endif
38+
3139
void uart_stdout_init(void);
3240
int uart_stdout_tx_strn(const char *str, size_t len);
3341

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy