diff options
| author | Andrew Leech <andrew.leech@planetinnovation.com.au> | 2022-02-24 14:20:56 +1100 |
|---|---|---|
| committer | Damien George <damien@micropython.org> | 2022-03-01 17:33:36 +1100 |
| commit | 2cc9232781d818b8a317dc7b173ecd5b752867e7 (patch) | |
| tree | aa4e6e1889a854bd24207480c146ef0eb27f1b51 | |
| parent | 2d47020e15b82b7f021e0b52dabe75baf5cb4c4d (diff) | |
esp32/uart: Correctly init low-level UART driver for REPL.
uart_driver_install() needs to be called to set up the UART correctly.
| -rw-r--r-- | ports/esp32/main.c | 2 | ||||
| -rw-r--r-- | ports/esp32/mphalport.c | 15 | ||||
| -rw-r--r-- | ports/esp32/uart.c | 49 | ||||
| -rw-r--r-- | ports/esp32/uart.h | 3 |
4 files changed, 51 insertions, 18 deletions
diff --git a/ports/esp32/main.c b/ports/esp32/main.c index 6c5edbb35..a1c27c0a2 100644 --- a/ports/esp32/main.c +++ b/ports/esp32/main.c @@ -93,7 +93,7 @@ void mp_task(void *pvParameter) { #elif CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG usb_serial_jtag_init(); #else - uart_init(); + uart_stdout_init(); #endif machine_init(); diff --git a/ports/esp32/mphalport.c b/ports/esp32/mphalport.c index de1b173ac..15a8dce1f 100644 --- a/ports/esp32/mphalport.c +++ b/ports/esp32/mphalport.c @@ -33,16 +33,6 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#if CONFIG_IDF_TARGET_ESP32 -#include "esp32/rom/uart.h" -#elif CONFIG_IDF_TARGET_ESP32C3 -#include "esp32c3/rom/uart.h" -#elif CONFIG_IDF_TARGET_ESP32S2 -#include "esp32s2/rom/uart.h" -#elif CONFIG_IDF_TARGET_ESP32S3 -#include "esp32s3/rom/uart.h" -#endif - #include "py/obj.h" #include "py/objstr.h" #include "py/stream.h" @@ -54,6 +44,7 @@ #include "mphalport.h" #include "usb.h" #include "usb_serial_jtag.h" +#include "uart.h" TaskHandle_t mp_main_task_handle; @@ -122,9 +113,7 @@ void mp_hal_stdout_tx_strn(const char *str, size_t len) { #elif CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG usb_serial_jtag_tx_strn(str, len); #else - for (uint32_t i = 0; i < len; ++i) { - uart_tx_one_char(str[i]); - } + uart_stdout_tx_strn(str, len); #endif if (release_gil) { MP_THREAD_GIL_ENTER(); diff --git a/ports/esp32/uart.c b/ports/esp32/uart.c index 58fc0c6c6..ca4ac67cd 100644 --- a/ports/esp32/uart.c +++ b/ports/esp32/uart.c @@ -33,13 +33,56 @@ #include "py/runtime.h" #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_init(void) { +void uart_stdout_init(void) { + uart_config_t uartcfg = { + .baud_rate = MICROPY_HW_UART_REPL_BAUD, + .data_bits = UART_DATA_8_BITS, + .parity = UART_PARITY_DISABLE, + .stop_bits = UART_STOP_BITS_1, + .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, + .rx_flow_ctrl_thresh = 0 + }; + uart_param_config(MICROPY_HW_UART_REPL, &uartcfg); + + const uint32_t rxbuf = 129; // IDF requires > 128 min + const uint32_t txbuf = 0; + + uart_driver_install(MICROPY_HW_UART_REPL, rxbuf, txbuf, 0, NULL, 0); + uart_isr_handle_t handle; - uart_isr_register(UART_NUM_0, uart_irq_handler, NULL, ESP_INTR_FLAG_LOWMED | ESP_INTR_FLAG_IRAM, &handle); - uart_enable_rx_intr(UART_NUM_0); + uart_isr_free(MICROPY_HW_UART_REPL); + uart_isr_register(MICROPY_HW_UART_REPL, uart_irq_handler, NULL, ESP_INTR_FLAG_LOWMED | ESP_INTR_FLAG_IRAM, &handle); + uart_enable_rx_intr(MICROPY_HW_UART_REPL); +} + +int uart_stdout_tx_strn(const char *str, size_t len) { + size_t remaining = len; + // TODO add a timeout + for (;;) { + int ret = uart_tx_chars(MICROPY_HW_UART_REPL, str, remaining); + if (ret == -1) { + return -1; + } + remaining -= ret; + if (remaining <= 0) { + break; + } + str += ret; + ulTaskNotifyTake(pdFALSE, 1); + } + return len - remaining; } // all code executed in ISR must be in IRAM, and any const data must be in DRAM diff --git a/ports/esp32/uart.h b/ports/esp32/uart.h index 264c8b894..439be521c 100644 --- a/ports/esp32/uart.h +++ b/ports/esp32/uart.h @@ -28,6 +28,7 @@ #ifndef MICROPY_INCLUDED_ESP32_UART_H #define MICROPY_INCLUDED_ESP32_UART_H -void uart_init(void); +void uart_stdout_init(void); +int uart_stdout_tx_strn(const char *str, size_t len); #endif // MICROPY_INCLUDED_ESP32_UART_H |
