esp32/uart: Correctly init low-level UART driver for REPL.

uart_driver_install() needs to be called to set up the UART correctly.
This commit is contained in:
Andrew Leech 2022-02-24 14:20:56 +11:00 committed by Damien George
parent 2d47020e15
commit 2cc9232781
4 changed files with 51 additions and 18 deletions

View File

@ -93,7 +93,7 @@ void mp_task(void *pvParameter) {
#elif CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG #elif CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
usb_serial_jtag_init(); usb_serial_jtag_init();
#else #else
uart_init(); uart_stdout_init();
#endif #endif
machine_init(); machine_init();

View File

@ -33,16 +33,6 @@
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/task.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/obj.h"
#include "py/objstr.h" #include "py/objstr.h"
#include "py/stream.h" #include "py/stream.h"
@ -54,6 +44,7 @@
#include "mphalport.h" #include "mphalport.h"
#include "usb.h" #include "usb.h"
#include "usb_serial_jtag.h" #include "usb_serial_jtag.h"
#include "uart.h"
TaskHandle_t mp_main_task_handle; 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 #elif CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
usb_serial_jtag_tx_strn(str, len); usb_serial_jtag_tx_strn(str, len);
#else #else
for (uint32_t i = 0; i < len; ++i) { uart_stdout_tx_strn(str, len);
uart_tx_one_char(str[i]);
}
#endif #endif
if (release_gil) { if (release_gil) {
MP_THREAD_GIL_ENTER(); MP_THREAD_GIL_ENTER();

View File

@ -33,13 +33,56 @@
#include "py/runtime.h" #include "py/runtime.h"
#include "py/mphal.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); 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_handle_t handle;
uart_isr_register(UART_NUM_0, uart_irq_handler, NULL, ESP_INTR_FLAG_LOWMED | ESP_INTR_FLAG_IRAM, &handle); uart_isr_free(MICROPY_HW_UART_REPL);
uart_enable_rx_intr(UART_NUM_0); 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 // all code executed in ISR must be in IRAM, and any const data must be in DRAM

View File

@ -28,6 +28,7 @@
#ifndef MICROPY_INCLUDED_ESP32_UART_H #ifndef MICROPY_INCLUDED_ESP32_UART_H
#define 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 #endif // MICROPY_INCLUDED_ESP32_UART_H