esp32: Switch from UART driver to UART HAL.

Allows registering UART interrupt again.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
pull/11876/head
Angus Gratton 2023-05-09 15:46:01 +10:00 zatwierdzone przez Damien George
rodzic 18caf49a7f
commit 7c929d4478
2 zmienionych plików z 59 dodań i 47 usunięć

Wyświetl plik

@ -28,81 +28,93 @@
#include <stdio.h> #include <stdio.h>
#include "driver/uart.h" #include "hal/uart_hal.h"
#include "soc/uart_periph.h"
#include "py/runtime.h" #include "py/runtime.h"
#include "py/mphal.h" #include "py/mphal.h"
#include "uart.h" #include "uart.h"
// Backwards compatibility for when MICROPY_HW_UART_REPL was a ESP-IDF UART
// driver enum. Only UART_NUM_0 was supported with that version of the driver.
#define UART_NUM_0 0
STATIC void uart_irq_handler(void *arg); STATIC void uart_irq_handler(void *arg);
// Declaring the HAL structure on the stack saves a tiny amount of static RAM
#define REPL_HAL_DEFN() { .dev = UART_LL_GET_HW(MICROPY_HW_UART_REPL) }
// RXFIFO Full interrupt threshold. Set the same as the ESP-IDF UART driver
#define RXFIFO_FULL_THR (SOC_UART_FIFO_LEN - 8)
// RXFIFO RX timeout threshold. This is in bit periods, so 10==one byte. Same as ESP-IDF UART driver.
#define RXFIFO_RX_TIMEOUT (10)
void uart_stdout_init(void) { void uart_stdout_init(void) {
uart_config_t uartcfg = { uart_hal_context_t repl_hal = REPL_HAL_DEFN();
.baud_rate = MICROPY_HW_UART_REPL_BAUD, uint32_t sclk_freq;
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE, #if UART_SCLK_DEFAULT == SOC_MOD_CLK_APB
.stop_bits = UART_STOP_BITS_1, sclk_freq = APB_CLK_FREQ; // Assumes no frequency scaling
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE, #else
.rx_flow_ctrl_thresh = 0 // ESP32-H2 and ESP32-C2, I think
}; #error "This SoC uses a different default UART SCLK source, code needs updating."
#if SOC_UART_SUPPORT_XTAL_CLK
// works independently of APB frequency
uartcfg.source_clk = UART_SCLK_XTAL; // ESP32C3, ESP32S3
#endif #endif
uart_param_config(MICROPY_HW_UART_REPL, &uartcfg);
const uint32_t rxbuf = 129; // IDF requires > 128 min uart_hal_init(&repl_hal, MICROPY_HW_UART_REPL); // Sets defaults: 8n1, no flow control
const uint32_t txbuf = 0; uart_hal_set_baudrate(&repl_hal, MICROPY_HW_UART_REPL_BAUD, sclk_freq);
uart_hal_rxfifo_rst(&repl_hal);
uart_hal_txfifo_rst(&repl_hal);
uart_driver_install(MICROPY_HW_UART_REPL, rxbuf, txbuf, 0, NULL, 0); ESP_ERROR_CHECK(
esp_intr_alloc(uart_periph_signal[MICROPY_HW_UART_REPL].irq,
ESP_INTR_FLAG_LOWMED | ESP_INTR_FLAG_IRAM,
uart_irq_handler,
NULL,
NULL)
);
uart_isr_handle_t handle; // Enable RX interrupts
uart_isr_free(MICROPY_HW_UART_REPL); uart_hal_set_rxfifo_full_thr(&repl_hal, RXFIFO_FULL_THR);
uart_isr_register(MICROPY_HW_UART_REPL, uart_irq_handler, NULL, ESP_INTR_FLAG_LOWMED | ESP_INTR_FLAG_IRAM, &handle); uart_hal_set_rx_timeout(&repl_hal, RXFIFO_RX_TIMEOUT);
uart_enable_rx_intr(MICROPY_HW_UART_REPL); uart_hal_ena_intr_mask(&repl_hal, UART_INTR_RXFIFO_FULL | UART_INTR_RXFIFO_TOUT);
} }
int uart_stdout_tx_strn(const char *str, size_t len) { int uart_stdout_tx_strn(const char *str, size_t len) {
uart_hal_context_t repl_hal = REPL_HAL_DEFN();
size_t remaining = len; size_t remaining = len;
uint32_t written = 0;
// TODO add a timeout // TODO add a timeout
for (;;) { for (;;) {
int ret = uart_tx_chars(MICROPY_HW_UART_REPL, str, remaining); uart_hal_write_txfifo(&repl_hal, (const void *)str, remaining, &written);
if (ret == -1) {
return -1; if (written >= remaining) {
}
remaining -= ret;
if (remaining <= 0) {
break; break;
} }
str += ret; remaining -= written;
str += written;
ulTaskNotifyTake(pdFALSE, 1); ulTaskNotifyTake(pdFALSE, 1);
} }
return len - remaining; return len;
} }
// 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
STATIC void IRAM_ATTR uart_irq_handler(void *arg) { STATIC void IRAM_ATTR uart_irq_handler(void *arg) {
volatile uart_dev_t *uart = &UART0; uint8_t rbuf[SOC_UART_FIFO_LEN];
#if CONFIG_IDF_TARGET_ESP32S3 int len;
uart->int_clr.rxfifo_full_int_clr = 1; uart_hal_context_t repl_hal = REPL_HAL_DEFN();
uart->int_clr.rxfifo_tout_int_clr = 1;
#else uart_hal_clr_intsts_mask(&repl_hal, UART_INTR_RXFIFO_FULL | UART_INTR_RXFIFO_TOUT | UART_INTR_FRAM_ERR);
uart->int_clr.rxfifo_full = 1;
uart->int_clr.rxfifo_tout = 1; len = uart_hal_get_rxfifo_len(&repl_hal);
uart->int_clr.frm_err = 1;
#endif uart_hal_read_rxfifo(&repl_hal, rbuf, &len);
while (uart->status.rxfifo_cnt) {
#if CONFIG_IDF_TARGET_ESP32 for (int i = 0; i < len; i++) {
uint8_t c = uart->fifo.rw_byte; if (rbuf[i] == mp_interrupt_char) {
#elif CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
uint8_t c = READ_PERI_REG(UART_FIFO_AHB_REG(0)); // UART0
#endif
if (c == mp_interrupt_char) {
mp_sched_keyboard_interrupt(); mp_sched_keyboard_interrupt();
} else { } else {
// this is an inline function so will be in IRAM // this is an inline function so will be in IRAM
ringbuf_put(&stdin_ringbuf, c); ringbuf_put(&stdin_ringbuf, rbuf[i]);
} }
} }
} }

Wyświetl plik

@ -34,7 +34,7 @@
#endif #endif
#ifndef MICROPY_HW_UART_REPL #ifndef MICROPY_HW_UART_REPL
#define MICROPY_HW_UART_REPL (UART_NUM_0) #define MICROPY_HW_UART_REPL (0)
#endif #endif
#ifndef MICROPY_HW_UART_REPL_BAUD #ifndef MICROPY_HW_UART_REPL_BAUD