diff --git a/arch/risc-v/src/esp32c3/Kconfig b/arch/risc-v/src/esp32c3/Kconfig index d44fe5f175b..824dea8878f 100644 --- a/arch/risc-v/src/esp32c3/Kconfig +++ b/arch/risc-v/src/esp32c3/Kconfig @@ -225,4 +225,33 @@ config ESP32C3_RWDT endmenu +menu "UART configuration" + depends on ESP32C3_UART + +if ESP32C3_UART0 + +config ESP32C3_UART0_TXPIN + int "UART0 TX Pin" + default 21 + +config ESP32C3_UART0_RXPIN + int "UART0 RX Pin" + default 20 + +endif # ESP32C3_UART0 + +if ESP32C3_UART1 + +config ESP32C3_UART1_TXPIN + int "UART1 TX Pin" + default 18 + +config ESP32C3_UART1_RXPIN + int "UART1 RX Pin" + default 19 + +endif # ESP32C3_UART1 + +endmenu + endif # ARCH_CHIP_ESP32C3 diff --git a/arch/risc-v/src/esp32c3/esp32c3_gpio.h b/arch/risc-v/src/esp32c3/esp32c3_gpio.h index 2665e938ec4..ba32098b131 100644 --- a/arch/risc-v/src/esp32c3/esp32c3_gpio.h +++ b/arch/risc-v/src/esp32c3/esp32c3_gpio.h @@ -34,6 +34,10 @@ * Pre-processor Definitions ****************************************************************************/ +#define MATRIX_DETACH_OUT_SIG 0x100 /* Detach an OUTPUT signal */ +#define MATRIX_DETACH_IN_LOW_PIN 0x30 /* Detach non-inverted INPUT signal */ +#define MATRIX_DETACH_IN_LOW_HIGH 0x38 /* Detach inverted INPUT signal */ + /* Bit-encoded input to esp32c3_configgpio() ********************************/ /* Encoded pin attributes used with esp32c3_configgpio() @@ -149,8 +153,8 @@ bool esp32c3_gpioread(int pin); * Description: * Set gpio input to a signal * NOTE: one gpio can input to several signals - * If gpio == 0x30, cancel input to the signal, input 0 to signal - * If gpio == 0x38, cancel input to the signal, input 1 to signal + * If signal_idx == 0x30, cancel input to the signal, input 0 to signal + * If signal_idx == 0x38, cancel input to the signal, input 1 to signal * ****************************************************************************/ diff --git a/arch/risc-v/src/esp32c3/esp32c3_lowputc.c b/arch/risc-v/src/esp32c3/esp32c3_lowputc.c index 63d91ac8452..048e142bfbe 100644 --- a/arch/risc-v/src/esp32c3/esp32c3_lowputc.c +++ b/arch/risc-v/src/esp32c3/esp32c3_lowputc.c @@ -34,13 +34,19 @@ #include #include -#include "hardware/esp32c3_uart.h" -#include "riscv_arch.h" #include "chip.h" -#include "esp32c3_lowputc.h" -#include "esp32c3_config.h" +#include "riscv_arch.h" + +#include "hardware/esp32c3_system.h" +#include "hardware/esp32c3_uart.h" #include "hardware/esp32c3_soc.h" +#include "esp32c3_clockconfig.h" +#include "esp32c3_config.h" +#include "esp32c3_gpio.h" + +#include "esp32c3_lowputc.h" + /**************************************************************************** * Private Types ****************************************************************************/ @@ -54,28 +60,38 @@ static const struct esp32c3_uart_s g_console_config = { - .base = REG_UART_BASE(0), + .periph = ESP32C3_PERIPH_UART0, + .cpuint = -ENOMEM, .id = 0, - .irq = -1, /* TODO */ + .irq = ESP32C3_IRQ_UART0, .baud = CONFIG_UART0_BAUD, .bits = CONFIG_UART0_BITS, .parity = CONFIG_UART0_PARITY, .stop_b2 = CONFIG_UART0_2STOP, - .int_pri = 1 + .int_pri = ESP32C3_INT_PRIO_DEF, + .txpin = CONFIG_ESP32C3_UART0_TXPIN, + .txsig = U0TXD_OUT_IDX, + .rxpin = CONFIG_ESP32C3_UART0_RXPIN, + .rxsig = U0RXD_IN_IDX, }; # elif defined(CONFIG_UART1_SERIAL_CONSOLE) -static const struct esp32c3_uart_s g_uart1_config = +static const struct esp32c3_uart_s g_console_config = { - .base = REG_UART_BASE(1), + .periph = ESP32C3_PERIPH_UART1, + .cpuint = -ENOMEM, .id = 1, - .irq = -1, /* TODO */ + .irq = ESP32C3_IRQ_UART1, .baud = CONFIG_UART1_BAUD, .bits = CONFIG_UART1_BITS, .parity = CONFIG_UART1_PARITY, .stop_b2 = CONFIG_UART1_2STOP, - .int_pri = 1 + .int_pri = ESP32C3_INT_PRIO_DEF, + .txpin = CONFIG_ESP32C3_UART1_TXPIN, + .txsig = U1TXD_OUT_IDX, + .rxpin = CONFIG_ESP32C3_UART1_RXPIN, + .rxsig = U1RXD_IN_IDX, }; #endif /* CONFIG_UART0_SERIAL_CONSOLE */ #endif /* HAVE_SERIAL_CONSOLE */ @@ -86,74 +102,145 @@ static const struct esp32c3_uart_s g_uart1_config = /**************************************************************************** * Name: esp32c3_lowputc_reset_core - * Reset both TX and RX core + * + * Description: + * Reset both TX and RX cores. + * + * Parameters: + * priv - Pointer to the private driver struct. + * ****************************************************************************/ -void esp32c3_lowputc_reset_core(const struct esp32c3_uart_s *conf) +void esp32c3_lowputc_reset_cores(const struct esp32c3_uart_s *priv) { uint32_t set_bit = 1 << UART_RST_CORE_S; - modifyreg32(UART_CLK_CONF_REG(conf->id), UART_RST_CORE_M, set_bit); - modifyreg32(UART_CLK_CONF_REG(conf->id), UART_RST_CORE_M, 0); + modifyreg32(UART_CLK_CONF_REG(priv->id), UART_RST_CORE_M, set_bit); + modifyreg32(UART_CLK_CONF_REG(priv->id), UART_RST_CORE_M, 0); +} + +/**************************************************************************** + * Name: esp32c3_lowputc_rst_tx + * + * Description: + * Reset TX core. + * + * Parameters: + * priv - Pointer to the private driver struct. + * + ****************************************************************************/ + +void esp32c3_lowputc_rst_tx(const struct esp32c3_uart_s *priv) +{ + uint32_t set_bit = 1 << UART_TX_RST_CORE_S; + + modifyreg32(UART_CLK_CONF_REG(priv->id), UART_TX_RST_CORE_M, set_bit); + modifyreg32(UART_CLK_CONF_REG(priv->id), UART_TX_RST_CORE_M, 0); +} + +/**************************************************************************** + * Name: esp32c3_lowputc_rst_rx + * + * Description: + * Reset RX core. + * + * Parameters: + * priv - Pointer to the private driver struct. + * + ****************************************************************************/ + +void esp32c3_lowputc_rst_rx(const struct esp32c3_uart_s *priv) +{ + uint32_t set_bit = 1 << UART_RX_RST_CORE_S; + + modifyreg32(UART_CLK_CONF_REG(priv->id), UART_RX_RST_CORE_M, set_bit); + modifyreg32(UART_CLK_CONF_REG(priv->id), UART_RX_RST_CORE_M, 0); } /**************************************************************************** * Name: esp32c3_lowputc_enable_sclk - * Enable clock for whole core + * + * Description: + * Enable clock for whole core. + * + * Parameters: + * priv - Pointer to the private driver struct. + * ****************************************************************************/ -void esp32c3_lowputc_enable_sclk(const struct esp32c3_uart_s *conf) +void esp32c3_lowputc_enable_sclk(const struct esp32c3_uart_s *priv) { - modifyreg32(UART_CLK_CONF_REG(conf->id), UART_SCLK_EN_M, + modifyreg32(UART_CLK_CONF_REG(priv->id), UART_SCLK_EN_M, 1 << UART_SCLK_EN_S); - modifyreg32(UART_CLK_CONF_REG(conf->id), UART_RX_SCLK_EN_M, + modifyreg32(UART_CLK_CONF_REG(priv->id), UART_RX_SCLK_EN_M, 1 << UART_RX_SCLK_EN_S); - modifyreg32(UART_CLK_CONF_REG(conf->id), UART_TX_SCLK_EN_M, + modifyreg32(UART_CLK_CONF_REG(priv->id), UART_TX_SCLK_EN_M, 1 << UART_TX_SCLK_EN_S); } /**************************************************************************** * Name: esp32c3_lowputc_disable_sclk - * Disable clock for whole core + * + * Description: + * Disable clock for whole core. + * + * Parameters: + * priv - Pointer to the private driver struct. + * ****************************************************************************/ -void esp32c3_lowputc_disable_sclk(const struct esp32c3_uart_s *conf) +void esp32c3_lowputc_disable_sclk(const struct esp32c3_uart_s *priv) { - modifyreg32(UART_CLK_CONF_REG(conf->id), UART_SCLK_EN_M, 0); - modifyreg32(UART_CLK_CONF_REG(conf->id), UART_RX_SCLK_EN_M, 0); - modifyreg32(UART_CLK_CONF_REG(conf->id), UART_TX_SCLK_EN_M, 0); + modifyreg32(UART_CLK_CONF_REG(priv->id), UART_SCLK_EN_M, 0); + modifyreg32(UART_CLK_CONF_REG(priv->id), UART_RX_SCLK_EN_M, 0); + modifyreg32(UART_CLK_CONF_REG(priv->id), UART_TX_SCLK_EN_M, 0); } /**************************************************************************** * Name: esp32c3_lowputc_set_sclk - * Set a source clock for UART - * APB_CLK = 1 80 MHz - * CLK_8 = 2 8 MHz - * XTAL_CLK = 3 + * + * Description: + * Set a source clock for UART. + * + * Parameters: + * priv - Pointer to the private driver struct. + * source - APB_CLK = 1 80 MHz + * CLK_8 = 2 8 MHz + * XTAL_CLK = 3 + * ****************************************************************************/ -void esp32c3_lowputc_set_sclk(const struct esp32c3_uart_s *conf, enum - uart_sclk source) +void esp32c3_lowputc_set_sclk(const struct esp32c3_uart_s *priv, + enum uart_sclk source) { uint32_t clk = (uint32_t)source << UART_SCLK_SEL_S; - modifyreg32(UART_CLK_CONF_REG(conf->id), UART_SCLK_SEL_M, clk); + modifyreg32(UART_CLK_CONF_REG(priv->id), UART_SCLK_SEL_M, clk); } /**************************************************************************** * Name: esp32c3_lowputc_get_sclk - * Get the source clock for UART + * + * Description: + * Get the source clock for UART. + * + * Parameters: + * priv - Pointer to the private driver struct. + * + * Returned Value: + * The frequency of the clock in Hz. + * ****************************************************************************/ -uint32_t esp32c3_lowputc_get_sclk(const struct esp32c3_uart_s * conf) +uint32_t esp32c3_lowputc_get_sclk(const struct esp32c3_uart_s * priv) { uint32_t clk_conf_reg; uint32_t ret = -ENODATA; - clk_conf_reg = getreg32(UART_CLK_CONF_REG(conf->id)); + clk_conf_reg = getreg32(UART_CLK_CONF_REG(priv->id)); clk_conf_reg &= UART_SCLK_SEL_M; clk_conf_reg >>= UART_SCLK_SEL_S; switch (clk_conf_reg) { case 1: - ret = APB_CLK_FREQ; + ret = esp32c3_clk_apb_freq(); break; case 2: ret = RTC_CLK_FREQ; @@ -168,14 +255,21 @@ uint32_t esp32c3_lowputc_get_sclk(const struct esp32c3_uart_s * conf) /**************************************************************************** * Name: esp32c3_lowputc_baud - * Set the baud rate + * + * Description: + * Set the baud rate according to the value in the private driver + * struct. + * + * Parameters: + * priv - Pointer to the private driver struct. + * ****************************************************************************/ -void esp32c3_lowputc_baud(const struct esp32c3_uart_s * conf) +void esp32c3_lowputc_baud(const struct esp32c3_uart_s * priv) { const int sclk_div = 1; - uint32_t sclk_freq = esp32c3_lowputc_get_sclk(conf); - uint32_t clk_div = ((sclk_freq) << 4) / conf->baud; + uint32_t sclk_freq = esp32c3_lowputc_get_sclk(priv); + uint32_t clk_div = ((sclk_freq) << 4) / priv->baud; uint32_t int_part = clk_div >> 4; uint32_t frag_part = clk_div & 0xf; @@ -183,66 +277,87 @@ void esp32c3_lowputc_baud(const struct esp32c3_uart_s * conf) * an integer part and a fractional part. */ - modifyreg32(UART_CLKDIV_REG(conf->id), UART_CLKDIV_M, int_part); - modifyreg32(UART_CLKDIV_REG(conf->id), UART_CLKDIV_FRAG_M, + modifyreg32(UART_CLKDIV_REG(priv->id), UART_CLKDIV_M, int_part); + modifyreg32(UART_CLKDIV_REG(priv->id), UART_CLKDIV_FRAG_M, frag_part << UART_CLKDIV_FRAG_S); - modifyreg32(UART_CLK_CONF_REG(conf->id), UART_SCLK_DIV_NUM_M, + modifyreg32(UART_CLK_CONF_REG(priv->id), UART_SCLK_DIV_NUM_M, (sclk_div - 1) << UART_SCLK_DIV_NUM_S); } /**************************************************************************** * Name: esp32c3_lowputc_normal_mode - * Set the UART to operate in normal mode + * + * Description: + * Set the UART to operate in normal mode, i.e., disable the RS485 mode and + * IRDA mode. + * + * Parameters: + * priv - Pointer to the private driver struct. + * ****************************************************************************/ -void esp32c3_lowputc_normal_mode(const struct esp32c3_uart_s * conf) +void esp32c3_lowputc_normal_mode(const struct esp32c3_uart_s * priv) { /* Disable RS485 mode */ - modifyreg32(UART_RS485_CONF_REG(conf->id), UART_RS485_EN_M, 0); - modifyreg32(UART_RS485_CONF_REG(conf->id), UART_RS485TX_RX_EN_M, 0); - modifyreg32(UART_RS485_CONF_REG(conf->id), UART_RS485RXBY_TX_EN_M, 0); + modifyreg32(UART_RS485_CONF_REG(priv->id), UART_RS485_EN_M, 0); + modifyreg32(UART_RS485_CONF_REG(priv->id), UART_RS485TX_RX_EN_M, 0); + modifyreg32(UART_RS485_CONF_REG(priv->id), UART_RS485RXBY_TX_EN_M, 0); /* Disable IRDA mode */ - modifyreg32(UART_CONF0_REG(conf->id), UART_IRDA_EN_M, 0); + modifyreg32(UART_CONF0_REG(priv->id), UART_IRDA_EN_M, 0); } /**************************************************************************** * Name: esp32c3_lowputc_parity - * Set the parity + * + * Description: + * Set the parity, according to the value in the private driver + * struct. + * + * Parameters: + * priv - Pointer to the private driver struct. + * ****************************************************************************/ -void esp32c3_lowputc_parity(const struct esp32c3_uart_s * conf) +void esp32c3_lowputc_parity(const struct esp32c3_uart_s * priv) { - if (conf->parity == UART_PARITY_DISABLE) + if (priv->parity == UART_PARITY_DISABLE) { - modifyreg32(UART_CONF0_REG(conf->id), UART_PARITY_EN_M, 0); + modifyreg32(UART_CONF0_REG(priv->id), UART_PARITY_EN_M, 0); } else { - modifyreg32(UART_CONF0_REG(conf->id), UART_PARITY_M, - ((conf->parity & 0x1) << UART_PARITY_S)); - modifyreg32(UART_CONF0_REG(conf->id), UART_PARITY_EN_M, + modifyreg32(UART_CONF0_REG(priv->id), UART_PARITY_M, + ((priv->parity & 0x1) << UART_PARITY_S)); + modifyreg32(UART_CONF0_REG(priv->id), UART_PARITY_EN_M, 1 << UART_PARITY_EN_S); } } /**************************************************************************** * Name: esp32c3_lowputc_data_length - * Set the data length + * + * Description: + * Set the data bits length, according to the value in the private driver + * struct. + * + * Parameters: + * priv - Pointer to the private driver struct. + * ****************************************************************************/ -int esp32c3_lowputc_data_length(const struct esp32c3_uart_s * conf) +int esp32c3_lowputc_data_length(const struct esp32c3_uart_s * priv) { int ret = OK; - uint32_t length = (conf->bits - 5); + uint32_t length = (priv->bits - 5); /* If it is the allowed range */ if (length >= UART_DATA_5_BITS && length <= UART_DATA_8_BITS) { - modifyreg32(UART_CONF0_REG(conf->id), UART_BIT_NUM_M, + modifyreg32(UART_CONF0_REG(priv->id), UART_BIT_NUM_M, length << UART_BIT_NUM_S); } else @@ -255,58 +370,87 @@ int esp32c3_lowputc_data_length(const struct esp32c3_uart_s * conf) /**************************************************************************** * Name: esp32c3_lowputc_stop_length - * Set the stop length + * + * Description: + * Set the stop bits length, according to the value in the private driver + * struct. + * + * Parameters: + * priv - Pointer to the private driver struct. + * ****************************************************************************/ -void esp32c3_lowputc_stop_length(const struct esp32c3_uart_s * conf) +void esp32c3_lowputc_stop_length(const struct esp32c3_uart_s *priv) { - if (conf->stop_b2 == 0) + if (priv->stop_b2 == 0) { - modifyreg32(UART_CONF0_REG(conf->id), UART_STOP_BIT_NUM_M, + modifyreg32(UART_CONF0_REG(priv->id), UART_STOP_BIT_NUM_M, UART_STOP_BITS_1 << UART_STOP_BIT_NUM_S); } else { - modifyreg32(UART_CONF0_REG(conf->id), UART_STOP_BIT_NUM_M, + modifyreg32(UART_CONF0_REG(priv->id), UART_STOP_BIT_NUM_M, UART_STOP_BITS_2 << UART_STOP_BIT_NUM_S); } } /**************************************************************************** * Name: esp32c3_lowputc_set_tx_idle_time - * Set the idle time between transfers + * + * Description: + * Set the idle time between transfers. + * + * Parameters: + * priv - Pointer to the private driver struct. + * time - Desired time interval between the transfers. + * ****************************************************************************/ -void esp32c3_lowputc_set_tx_idle_time(const struct esp32c3_uart_s * - conf, uint32_t time) +void esp32c3_lowputc_set_tx_idle_time(const struct esp32c3_uart_s *priv, + uint32_t time) { time = time << UART_TX_IDLE_NUM_S; time = time & UART_TX_IDLE_NUM_M; /* Just in case value overloads */ - modifyreg32(UART_IDLE_CONF_REG(conf->id), UART_TX_IDLE_NUM_M, + modifyreg32(UART_IDLE_CONF_REG(priv->id), UART_TX_IDLE_NUM_M, time); } /**************************************************************************** * Name: esp32c3_lowputc_send_byte - * Send one byte + * + * Description: + * Send one byte. + * + * Parameters: + * priv - Pointer to the private driver struct. + * byte - Byte to be sent. + * ****************************************************************************/ -void esp32c3_lowputc_send_byte(const struct esp32c3_uart_s * conf, +void esp32c3_lowputc_send_byte(const struct esp32c3_uart_s * priv, char byte) { - putreg32((uint32_t) byte, UART_FIFO_REG(conf->id)); + putreg32((uint32_t) byte, UART_FIFO_REG(priv->id)); } /**************************************************************************** * Name: esp32c3_lowputc_is_tx_fifo_full - * Verifies if TX FIFO is full + * + * Description: + * Verify if TX FIFO is full. + * + * Parameters: + * priv - Pointer to the private driver struct. + * + * Returned Value: + * True if it is full, otherwise false. + * ****************************************************************************/ -bool esp32c3_lowputc_is_tx_fifo_full(const struct esp32c3_uart_s * - conf) +bool esp32c3_lowputc_is_tx_fifo_full(const struct esp32c3_uart_s *priv) { uint32_t reg; - reg = getreg32(UART_STATUS_REG(conf->id)); + reg = getreg32(UART_STATUS_REG(priv->id)); reg = reg >> UART_TXFIFO_CNT_S; reg = reg & UART_TXFIFO_CNT_V; if (reg < (UART_TX_FIFO_SIZE -1)) @@ -319,11 +463,182 @@ bool esp32c3_lowputc_is_tx_fifo_full(const struct esp32c3_uart_s * } } +/**************************************************************************** + * Name: esp32c3_lowputc_rst_peripheral + * + * Description: + * Reset the UART peripheral by using System reg. + * + * Parameters: + * priv - Pointer to the private driver struct. + * + ****************************************************************************/ + +void esp32c3_lowputc_rst_peripheral(const struct esp32c3_uart_s *priv) +{ + if (priv->id == 0) + { + modifyreg32(SYSTEM_PERIP_RST_EN0_REG, SYSTEM_UART_RST_M, + SYSTEM_UART_RST_M); + modifyreg32(SYSTEM_PERIP_RST_EN0_REG, SYSTEM_UART_RST_M, 0); + } + else + { + modifyreg32(SYSTEM_PERIP_RST_EN0_REG, SYSTEM_UART1_RST_M, + SYSTEM_UART1_RST_M); + modifyreg32(SYSTEM_PERIP_RST_EN0_REG, SYSTEM_UART1_RST_M, 0); + } +} + +/**************************************************************************** + * Name: esp32c3_lowputc_rst_txfifo + * + * Description: + * Reset TX FIFO. + * + * Parameters: + * priv - Pointer to the private driver struct. + * + ****************************************************************************/ + +void esp32c3_lowputc_rst_txfifo(const struct esp32c3_uart_s *priv) +{ + modifyreg32(UART_CONF0_REG(priv->id), UART_TXFIFO_RST_M, + UART_TXFIFO_RST_M); + modifyreg32(UART_CONF0_REG(priv->id), UART_TXFIFO_RST_M, 0); +} + +/**************************************************************************** + * Name: esp32c3_lowputc_rst_rxfifo + * + * Description: + * Reset RX FIFO. + * + * Parameters: + * priv - Pointer to the private driver struct. + * + ****************************************************************************/ + +void esp32c3_lowputc_rst_rxfifo(const struct esp32c3_uart_s *priv) +{ + modifyreg32(UART_CONF0_REG(priv->id), UART_RXFIFO_RST_M, + UART_RXFIFO_RST_M); + modifyreg32(UART_CONF0_REG(priv->id), UART_RXFIFO_RST_M, 0); +} + +/**************************************************************************** + * Name: esp32c3_lowputc_disable_all_uart_int + * + * Description: + * Disable all UART interrupts. + * + * Parameters: + * priv - Pointer to the private driver struct. + * current_status - Pointer to a variable to store the current status of + * the interrupt enable register before disabling + * UART interrupts. + * + ****************************************************************************/ + +void esp32c3_lowputc_disable_all_uart_int(const struct esp32c3_uart_s *priv, + uint32_t *current_status) +{ + irqstate_t flags; + + flags = enter_critical_section(); + + if (current_status != NULL) + { + /* Save current status */ + + *current_status = getreg32(UART_INT_ENA_REG(priv->id)); + } + + /* Disable all UART int */ + + putreg32(0, UART_INT_ENA_REG(priv->id)); + + /* Clear all ints */ + + putreg32(0xffffffff, UART_INT_CLR_REG(priv->id)); + + leave_critical_section(flags); +} + +/**************************************************************************** + * Name: esp32c3_lowputc_restore_all_uart_int + * + * Description: + * Restore all UART interrupts. + * + * Parameters: + * priv - Pointer to the private driver struct. + * last_status - Pointer to a variable that stored the last state of the + * interrupt enable register. + * + ****************************************************************************/ + +void esp32c3_lowputc_restore_all_uart_int(const struct esp32c3_uart_s *priv, + uint32_t *last_status) +{ + /* Restore the previous behaviour */ + + putreg32(*last_status, UART_INT_ENA_REG(priv->id)); +} + +/**************************************************************************** + * Name: esp32c3_lowputc_config_pins + * + * Description: + * Configure TX and RX UART pins. + * + * Parameters: + * priv - Pointer to the private driver struct. + * + ****************************************************************************/ + +void esp32c3_lowputc_config_pins(const struct esp32c3_uart_s *priv) +{ + /* Configure the pins */ + + esp32c3_configgpio(priv->txpin, OUTPUT_FUNCTION_1); + esp32c3_gpio_matrix_out(priv->txpin, priv->txsig, 0, 0); + + esp32c3_configgpio(priv->rxpin, INPUT_FUNCTION_1); + esp32c3_gpio_matrix_in(priv->rxpin, priv->rxsig, 0); +} + +/**************************************************************************** + * Name: esp32c3_lowputc_restore_pins + * + * Description: + * Configure both pins back to INPUT mode and detach the TX pin from the + * output signal and the RX pin from the input signal. + * + * Parameters: + * priv - Pointer to the private driver struct. + * + ****************************************************************************/ + +void esp32c3_lowputc_restore_pins(const struct esp32c3_uart_s *priv) +{ + /* Configure the pins */ + + esp32c3_configgpio(priv->txpin, INPUT); + esp32c3_gpio_matrix_out(priv->txpin, MATRIX_DETACH_OUT_SIG, false, false); + + esp32c3_configgpio(priv->rxpin, INPUT); + esp32c3_gpio_matrix_in(priv->rxpin, MATRIX_DETACH_IN_LOW_PIN, false); +} + /**************************************************************************** * Name: up_lowputc * * Description: - * Output one byte on the serial console + * Output one byte on the serial console. + * + * Parameters: + * ch - Byte to be sent. * ****************************************************************************/ @@ -358,13 +673,11 @@ void esp32c3_lowsetup(void) #if defined(HAVE_SERIAL_CONSOLE) && !defined(CONFIG_SUPPRESS_UART_CONFIG) - /* Configure Clock */ - - /* esp32c3_lowputc_set_sclk(&g_console_config, APB_CLK); */ + /* Initialize UART module */ /* Configure the UART Baud Rate */ - /* esp32c3_lowputc_baud(&g_console_config); */ + esp32c3_lowputc_baud(&g_console_config); /* Set a mode */ @@ -386,6 +699,10 @@ void esp32c3_lowsetup(void) esp32c3_lowputc_set_tx_idle_time(&g_console_config, 0); + /* Set pins */ + + esp32c3_lowputc_config_pins(&g_console_config); + /* Enable cores */ esp32c3_lowputc_enable_sclk(&g_console_config); diff --git a/arch/risc-v/src/esp32c3/esp32c3_lowputc.h b/arch/risc-v/src/esp32c3/esp32c3_lowputc.h index d1e8c1b655e..44367195421 100644 --- a/arch/risc-v/src/esp32c3/esp32c3_lowputc.h +++ b/arch/risc-v/src/esp32c3/esp32c3_lowputc.h @@ -37,9 +37,13 @@ #include #include -#include "hardware/esp32c3_uart.h" #include "chip.h" +#include "hardware/esp32c3_uart.h" +#include "hardware/esp32c3_gpio_sigmap.h" + +#include "esp32c3_irq.h" + /**************************************************************************** * Public Types ****************************************************************************/ @@ -68,8 +72,8 @@ enum uart_data_length enum uart_stop_length { - UART_STOP_BITS_1 = 0x1, /* stop bit: 1 bit */ - UART_STOP_BITS_2 = 0x3, /* stop bit: 2bits */ + UART_STOP_BITS_1 = 0x1, /* Stop bit: 1 bit */ + UART_STOP_BITS_2 = 0x3, /* Stop bit: 2 bits */ }; /* Default FIFOs size */ @@ -83,7 +87,6 @@ enum uart_stop_length struct esp32c3_uart_s { - uint32_t base; /* Base address of UART registers */ uint8_t periph; /* UART peripheral ID */ int cpuint; /* CPU interrupt assigned to this UART */ uint8_t id; /* UART ID */ @@ -93,6 +96,10 @@ struct esp32c3_uart_s uint8_t parity; /* 0=no parity, 1=odd parity, 2=even parity */ uint8_t stop_b2; /* Use 2 stop bits? 0 no, others yes */ uint8_t int_pri; /* UART Interrupt Priority */ + uint8_t txpin; /* TX pin */ + uint8_t txsig; /* TX signal */ + uint8_t rxpin; /* RX pin */ + uint8_t rxsig; /* RX signal */ }; /**************************************************************************** @@ -101,101 +108,316 @@ struct esp32c3_uart_s /**************************************************************************** * Name: esp32c3_lowputc_reset_core - * Reset both TX and RX core + * + * Description: + * Reset both TX and RX cores. + * + * Parameters: + * priv - Pointer to the private driver struct. + * ****************************************************************************/ -void esp32c3_lowputc_reset_core(const struct esp32c3_uart_s *conf); +void esp32c3_lowputc_reset_cores(const struct esp32c3_uart_s *priv); + +/**************************************************************************** + * Name: esp32c3_lowputc_rst_tx + * + * Description: + * Reset TX core. + * + * Parameters: + * priv - Pointer to the private driver struct. + * + ****************************************************************************/ + +void esp32c3_lowputc_rst_tx(const struct esp32c3_uart_s *priv); + +/**************************************************************************** + * Name: esp32c3_lowputc_rst_rx + * + * Description: + * Reset RX core. + * + * Parameters: + * priv - Pointer to the private driver struct. + * + ****************************************************************************/ + +void esp32c3_lowputc_rst_rx(const struct esp32c3_uart_s *priv); /**************************************************************************** * Name: esp32c3_lowputc_enable_sclk - * Enable clock for whole core + * + * Description: + * Enable clock for whole core. + * + * Parameters: + * priv - Pointer to the private driver struct. + * ****************************************************************************/ -void esp32c3_lowputc_enable_sclk(const struct esp32c3_uart_s *conf); +void esp32c3_lowputc_enable_sclk(const struct esp32c3_uart_s *priv); /**************************************************************************** * Name: esp32c3_lowputc_disable_sclk - * Disable clock for whole core + * + * Description: + * Disable clock for whole core. + * + * Parameters: + * priv - Pointer to the private driver struct. + * ****************************************************************************/ -void esp32c3_lowputc_disable_sclk(const struct esp32c3_uart_s *conf); +void esp32c3_lowputc_disable_sclk(const struct esp32c3_uart_s *priv); /**************************************************************************** * Name: esp32c3_lowputc_set_sclk - * Set a source clock for UART - * APB_CLK = 1 80 MHz - * CLK_8 = 2 8 MHz - * XTAL_CLK = 3 + * + * Description: + * Set a source clock for UART. + * + * Parameters: + * priv - Pointer to the private driver struct. + * source - APB_CLK = 1 80 MHz + * CLK_8 = 2 8 MHz + * XTAL_CLK = 3 + * ****************************************************************************/ -void esp32c3_lowputc_set_sclk(const struct esp32c3_uart_s *conf, enum - uart_sclk source); +void esp32c3_lowputc_set_sclk(const struct esp32c3_uart_s *priv, + enum uart_sclk source); /**************************************************************************** * Name: esp32c3_lowputc_get_sclk - * Get the source clock for UART + * + * Description: + * Get the source clock for UART. + * + * Parameters: + * priv - Pointer to the private driver struct. + * + * Returned Value: + * The frequency of the clock in Hz. + * ****************************************************************************/ -uint32_t esp32c3_lowputc_get_sclk(const struct esp32c3_uart_s *conf); +uint32_t esp32c3_lowputc_get_sclk(const struct esp32c3_uart_s *priv); /**************************************************************************** * Name: esp32c3_lowputc_baud - * Set the baud rate + * + * Description: + * Set the baud rate according to the value in the private driver + * struct. + * + * Parameters: + * priv - Pointer to the private driver struct. + * ****************************************************************************/ -void esp32c3_lowputc_baud(const struct esp32c3_uart_s * conf); +void esp32c3_lowputc_baud(const struct esp32c3_uart_s *priv); /**************************************************************************** * Name: esp32c3_lowputc_normal_mode - * Set the UART to operate in normal mode + * + * Description: + * Set the UART to operate in normal mode, i.e., disable the RS485 mode and + * IRDA mode. + * + * Parameters: + * priv - Pointer to the private driver struct. + * ****************************************************************************/ -void esp32c3_lowputc_normal_mode(const struct esp32c3_uart_s * conf); +void esp32c3_lowputc_normal_mode(const struct esp32c3_uart_s *priv); /**************************************************************************** * Name: esp32c3_lowputc_parity - * Set the parity + * + * Description: + * Set the parity, according to the value in the private driver + * struct. + * + * Parameters: + * priv - Pointer to the private driver struct. + * ****************************************************************************/ -void esp32c3_lowputc_parity(const struct esp32c3_uart_s * conf); +void esp32c3_lowputc_parity(const struct esp32c3_uart_s *priv); /**************************************************************************** * Name: esp32c3_lowputc_data_length - * Set the data length + * + * Description: + * Set the data bits length, according to the value in the private driver + * struct. + * + * Parameters: + * priv - Pointer to the private driver struct. + * ****************************************************************************/ -int esp32c3_lowputc_data_length(const struct esp32c3_uart_s * conf); +int esp32c3_lowputc_data_length(const struct esp32c3_uart_s *priv); /**************************************************************************** * Name: esp32c3_lowputc_stop_length - * Set the stop length + * + * Description: + * Set the stop bits length, according to the value in the private driver + * struct. + * + * Parameters: + * priv - Pointer to the private driver struct. + * ****************************************************************************/ -void esp32c3_lowputc_stop_length(const struct esp32c3_uart_s * conf); +void esp32c3_lowputc_stop_length(const struct esp32c3_uart_s *priv); /**************************************************************************** * Name: esp32c3_lowputc_set_tx_idle_time - * Set the idle time between transfers + * + * Description: + * Set the idle time between transfers. + * + * Parameters: + * priv - Pointer to the private driver struct. + * time - Desired time interval between the transfers. + * ****************************************************************************/ -void esp32c3_lowputc_set_tx_idle_time(const struct esp32c3_uart_s * - conf, uint32_t time); +void esp32c3_lowputc_set_tx_idle_time(const struct esp32c3_uart_s *priv, + uint32_t time); /**************************************************************************** * Name: esp32c3_lowputc_send_byte - * Send one byte + * + * Description: + * Send one byte. + * + * Parameters: + * priv - Pointer to the private driver struct. + * byte - Byte to be sent. + * ****************************************************************************/ -void esp32c3_lowputc_send_byte(const struct esp32c3_uart_s * conf, +void esp32c3_lowputc_send_byte(const struct esp32c3_uart_s *priv, char byte); /**************************************************************************** * Name: esp32c3_lowputc_is_tx_fifo_full - * Send one byte + * + * Description: + * Verify if TX FIFO is full. + * + * Parameters: + * priv - Pointer to the private driver struct. + * + * Returned Value: + * True if it is full, otherwise false. + * ****************************************************************************/ -bool esp32c3_lowputc_is_tx_fifo_full(const struct esp32c3_uart_s * - conf); +bool esp32c3_lowputc_is_tx_fifo_full(const struct esp32c3_uart_s *priv); + +/**************************************************************************** + * Name: esp32c3_lowputc_rst_peripheral + * + * Description: + * Reset the UART peripheral by using System reg. + * + * Parameters: + * priv - Pointer to the private driver struct. + * + ****************************************************************************/ + +void esp32c3_lowputc_rst_peripheral(const struct esp32c3_uart_s *priv); + +/**************************************************************************** + * Name: esp32c3_lowputc_rst_txfifo + * + * Description: + * Reset TX FIFO. + * + * Parameters: + * priv - Pointer to the private driver struct. + * + ****************************************************************************/ + +void esp32c3_lowputc_rst_txfifo(const struct esp32c3_uart_s *priv); + +/**************************************************************************** + * Name: esp32c3_lowputc_rst_rxfifo + * + * Description: + * Reset RX FIFO. + * + * Parameters: + * priv - Pointer to the private driver struct. + * + ****************************************************************************/ + +void esp32c3_lowputc_rst_rxfifo(const struct esp32c3_uart_s *priv); + +/**************************************************************************** + * Name: esp32c3_lowputc_disable_all_uart_int + * + * Description: + * Disable all UART interrupts. + * + * Parameters: + * priv - Pointer to the private driver struct. + * current_status - Pointer to a variable to store the current status of + * the interrupt enable register before disabling + * UART interrupts. + * + ****************************************************************************/ + +void esp32c3_lowputc_disable_all_uart_int(const struct esp32c3_uart_s *priv, + uint32_t *current_status); + +/**************************************************************************** + * Name: esp32c3_lowputc_restore_all_uart_int + * + * Description: + * Restore all UART interrupts. + * + * Parameters: + * priv - Pointer to the private driver struct. + * last_status - Pointer to a variable that stored the last state of the + * interrupt enable register. + * + ****************************************************************************/ + +void esp32c3_lowputc_restore_all_uart_int(const struct esp32c3_uart_s *priv, + uint32_t * last_status); + +/**************************************************************************** + * Name: esp32c3_lowputc_config_pins + * + * Description: + * Configure TX and RX UART pins. + * + * Parameters: + * priv - Pointer to the private driver struct. + * + ****************************************************************************/ + +void esp32c3_lowputc_config_pins(const struct esp32c3_uart_s *priv); + +/**************************************************************************** + * Name: esp32c3_lowputc_restore_pins + * + * Description: + * Configure both pins back to INPUT mode and detach the TX pin from the + * output signal and the RX pin from the input signal. + * + * Parameters: + * priv - Pointer to the private driver struct. + * + ****************************************************************************/ + +void esp32c3_lowputc_restore_pins(const struct esp32c3_uart_s *priv); /**************************************************************************** * Name: esp32c3_lowsetup diff --git a/arch/risc-v/src/esp32c3/esp32c3_serial.c b/arch/risc-v/src/esp32c3/esp32c3_serial.c index 540f3121828..efaa5bcd053 100644 --- a/arch/risc-v/src/esp32c3/esp32c3_serial.c +++ b/arch/risc-v/src/esp32c3/esp32c3_serial.c @@ -35,13 +35,16 @@ #include #include -#include "hardware/esp32c3_uart.h" #include "riscv_internal.h" #include "riscv_arch.h" #include "chip.h" -#include "esp32c3_lowputc.h" + +#include "hardware/esp32c3_uart.h" +#include "hardware/esp32c3_system.h" + #include "esp32c3_config.h" #include "esp32c3_irq.h" +#include "esp32c3_lowputc.h" /**************************************************************************** * Pre-processor Definitions @@ -149,15 +152,19 @@ static char g_uart0_txbuffer[CONFIG_UART0_TXBUFSIZE]; static struct esp32c3_uart_s g_uart0_config = { - .base = REG_UART_BASE(0), .periph = ESP32C3_PERIPH_UART0, .id = 0, + .cpuint = -ENOMEM, .irq = ESP32C3_IRQ_UART0, .baud = CONFIG_UART0_BAUD, .bits = CONFIG_UART0_BITS, .parity = CONFIG_UART0_PARITY, .stop_b2 = CONFIG_UART0_2STOP, - .int_pri = 1 + .int_pri = ESP32C3_INT_PRIO_DEF, + .txpin = CONFIG_ESP32C3_UART0_TXPIN, + .txsig = U0TXD_OUT_IDX, + .rxpin = CONFIG_ESP32C3_UART0_RXPIN, + .rxsig = U0RXD_IN_IDX, }; /* Fill only the requested fields */ @@ -195,15 +202,19 @@ static char g_uart1_txbuffer[CONFIG_UART1_TXBUFSIZE]; static struct esp32c3_uart_s g_uart1_config = { - .base = REG_UART_BASE(1), .periph = ESP32C3_PERIPH_UART1, .id = 1, + .cpuint = -ENOMEM, .irq = ESP32C3_IRQ_UART1, .baud = CONFIG_UART1_BAUD, .bits = CONFIG_UART1_BITS, .parity = CONFIG_UART1_PARITY, .stop_b2 = CONFIG_UART1_2STOP, - .int_pri = 1 + .int_pri = ESP32C3_INT_PRIO_DEF, + .txpin = CONFIG_ESP32C3_UART1_TXPIN, + .txsig = U1TXD_OUT_IDX, + .rxpin = CONFIG_ESP32C3_UART1_RXPIN, + .rxsig = U1RXD_IN_IDX, }; /* Fill only the requested fields */ @@ -241,8 +252,8 @@ static uart_dev_t g_uart1_dev = * * Description: * This is the UART interrupt handler. It will be invoked when an - * interrupt received on the 'irq' It should call uart_transmitchars or - * uart_receivechar to perform the appropriate data transfers. The + * interrupt is received on the 'irq' It should call uart_xmitchars or + * uart_recvchars to perform the appropriate data transfers. The * interrupt handling logic must be able to map the 'irq' number into the * appropriate uart_dev_s structure in order to call these functions. * @@ -289,10 +300,52 @@ static int uart_handler(int irq, FAR void *context, FAR void *arg) * That portion of the UART setup is performed when the attach() method * is called. * + * Parameters: + * dev - Pointer to the serial driver struct. + * + * Returned Values: + * Zero (OK) is returned. + * ****************************************************************************/ static int esp32c3_setup(struct uart_dev_s *dev) { + struct esp32c3_uart_s *priv = dev->priv; + + /* Initialize UART module */ + + /* Configure the UART Baud Rate */ + + esp32c3_lowputc_baud(priv); + + /* Set a mode */ + + esp32c3_lowputc_normal_mode(priv); + + /* Parity */ + + esp32c3_lowputc_parity(priv); + + /* Data Frame size */ + + esp32c3_lowputc_data_length(priv); + + /* Stop bit */ + + esp32c3_lowputc_stop_length(priv); + + /* No Tx idle interval */ + + esp32c3_lowputc_set_tx_idle_time(priv, 0); + + /* Set pins */ + + esp32c3_lowputc_config_pins(priv); + + /* Enable cores */ + + esp32c3_lowputc_enable_sclk(priv); + return OK; } @@ -304,10 +357,27 @@ static int esp32c3_setup(struct uart_dev_s *dev) * This method reverses the operation the setup method. NOTE that the serial * console is never shutdown. * + * Parameters: + * dev - Pointer to the serial driver struct. + * ****************************************************************************/ static void esp32c3_shutdown(struct uart_dev_s *dev) { + struct esp32c3_uart_s *priv = dev->priv; + + /* Clear FIFOs */ + + esp32c3_lowputc_rst_txfifo(priv); + esp32c3_lowputc_rst_rxfifo(priv); + + /* Disable ints */ + + esp32c3_lowputc_disable_all_uart_int(priv, NULL); + + /* Back pins to normal */ + + esp32c3_lowputc_restore_pins(priv); } /**************************************************************************** @@ -324,6 +394,13 @@ static void esp32c3_shutdown(struct uart_dev_s *dev) * and TX interrupts are not enabled until the txint() and rxint() methods * are called. * + * Parameters: + * dev - Pointer to the serial driver struct. + * + * Returned Values: + * Zero (OK) is returned on success; A negated errno value is returned + * to indicate the nature of any failure. + * ****************************************************************************/ static int esp32c3_attach(struct uart_dev_s *dev) @@ -331,6 +408,8 @@ static int esp32c3_attach(struct uart_dev_s *dev) struct esp32c3_uart_s *priv = dev->priv; int ret; + DEBUGASSERT(priv->cpuint == -ENOMEM); + /* Try to attach the IRQ to a CPU int */ priv->cpuint = esp32c3_request_irq(priv->periph, priv->int_pri, @@ -347,6 +426,10 @@ static int esp32c3_attach(struct uart_dev_s *dev) { up_enable_irq(priv->cpuint); } + else + { + up_disable_irq(priv->cpuint); + } return ret; } @@ -359,39 +442,45 @@ static int esp32c3_attach(struct uart_dev_s *dev) * closed normally just before the shutdown method is called. The * exception is the serial console which is never shutdown. * + * Parameters: + * dev - Pointer to the serial driver struct. + * ****************************************************************************/ static void esp32c3_detach(struct uart_dev_s *dev) { struct esp32c3_uart_s *priv = dev->priv; + DEBUGASSERT(priv->cpuint != -ENOMEM); + up_disable_irq(priv->cpuint); irq_detach(priv->irq); esp32c3_free_cpuint(priv->periph); + priv->cpuint = -ENOMEM; } /**************************************************************************** * Name: esp32c3_txint * * Description: - * Call to enable or disable TX interrupts + * Enable or disable TX interrupts. + * + * Parameters: + * dev - Pointer to the serial driver struct. + * enable - If true enables the TX interrupt, if false disables it. * ****************************************************************************/ static void esp32c3_txint(struct uart_dev_s *dev, bool enable) { struct esp32c3_uart_s *priv = dev->priv; - irqstate_t flags; uint32_t ints_mask = UART_TXFIFO_EMPTY_INT_ENA_M | UART_TX_DONE_INT_ENA_M; - flags = enter_critical_section(); - if (enable) { /* Set to receive an interrupt when the TX holding register register * is empty */ - #ifndef CONFIG_SUPPRESS_SERIAL_INTS modifyreg32(UART_INT_ENA_REG(priv->id), ints_mask, ints_mask); #endif @@ -402,52 +491,58 @@ static void esp32c3_txint(struct uart_dev_s *dev, bool enable) modifyreg32(UART_INT_ENA_REG(priv->id), ints_mask, 0); } - - leave_critical_section(flags); } /**************************************************************************** * Name: esp32c3_rxint * * Description: - * Call to enable or disable RXRDY interrupts + * Enable or disable RX interrupts. + * + * Parameters: + * dev - Pointer to the serial driver struct. + * enable - If true enables the RX interrupt, if false disables it. * ****************************************************************************/ static void esp32c3_rxint(struct uart_dev_s *dev, bool enable) { struct esp32c3_uart_s *priv = dev->priv; - irqstate_t flags; uint32_t ints_mask = UART_RXFIFO_TOUT_INT_ENA_M | UART_RXFIFO_FULL_INT_ENA_M; - flags = enter_critical_section(); - if (enable) { - /* Receive an interrupt when their is anything in the Rx data register + /* Receive an interrupt when there is anything in the Rx data register * (or an Rx timeout occurs). */ - #ifndef CONFIG_SUPPRESS_SERIAL_INTS + modifyreg32(UART_CONF1_REG(priv->id), UART_RX_TOUT_EN_M, + UART_RX_TOUT_EN_M); modifyreg32(UART_INT_ENA_REG(priv->id), ints_mask, ints_mask); #endif } else { + modifyreg32(UART_CONF1_REG(priv->id), UART_RX_TOUT_EN_M, 0); + /* Disable the RX interrupts */ modifyreg32(UART_INT_ENA_REG(priv->id), ints_mask, 0); } - - leave_critical_section(flags); } /**************************************************************************** * Name: esp32c3_rxavailable * * Description: - * Return true if the receive holding register is not empty + * Check if there is any data available to be read. + * + * Parameters: + * dev - Pointer to the serial driver struct. + * + * Returned Values: + * Return true if the RX FIFO is not empty and false if RX FIFO is empty. * ****************************************************************************/ @@ -467,8 +562,15 @@ static bool esp32c3_rxavailable(struct uart_dev_s *dev) * Name: esp32c3_txready * * Description: - * Return true if the tranmsit hardware is ready to send another byte. This - * is used to determine if send() method can be called. + * Check if the transmit hardware is ready to send another byte. + * This is used to determine if send() method can be called. + * + * Parameters: + * dev - Pointer to the serial driver struct. + * + * Returned Values: + * Return true if the transmit hardware is ready to send another byte, + * false otherwise. * ****************************************************************************/ @@ -481,10 +583,16 @@ static bool esp32c3_txready(struct uart_dev_s *dev) * Name: esp32c3_txempty * * Description: - * Return true if all characters have been sent. If for example, the UART - * hardware implements FIFOs, then this would mean the transmit FIFO is - * empty. This method is called when the driver needs to make sure that - * all characters are "drained" from the TX hardware. + * Verify if all characters have been sent. If for example, the UART + * hardware implements FIFOs, then this would mean the transmit FIFO is + * empty. This method is called when the driver needs to make sure that + * all characters are "drained" from the TX hardware. + * + * Parameters: + * dev - Pointer to the serial driver struct. + * + * Returned Values: + * Return true if the TX FIFO is empty, false if it is not. * ****************************************************************************/ @@ -500,19 +608,19 @@ static bool esp32c3_txempty(struct uart_dev_s *dev) } /**************************************************************************** - * Name: esp32c3_shutdown + * Name: esp32c3_send * * Description: - * Disable the UART. This method is called when the serial port is closed. - * This method reverses the operation the setup method. NOTE that the serial - * console is never shutdown. + * Send a unique character + * + * Parameters: + * dev - Pointer to the serial driver struct. + * ch - Byte to be sent. * ****************************************************************************/ static void esp32c3_send(struct uart_dev_s *dev, int ch) { - /* Then send the character */ - esp32c3_lowputc_send_byte(dev->priv, ch); } @@ -524,6 +632,13 @@ static void esp32c3_send(struct uart_dev_s *dev, int ch) * character from the UART. Error bits associated with the * receipt are provided in the return 'status'. * + * Parameters: + * dev - Pointer to the serial driver struct. + * status - Pointer to a variable to store eventual error bits. + * + * Returned Values: + * Return the byte read from the RX FIFO. + * ****************************************************************************/ static int esp32c3_receive(struct uart_dev_s *dev, unsigned int *status) @@ -534,6 +649,10 @@ static int esp32c3_receive(struct uart_dev_s *dev, unsigned int *status) rx_fifo = getreg32(UART_FIFO_REG(priv->id)); rx_fifo = rx_fifo & UART_RXFIFO_RD_BYTE_M; + /* Since we don't have error bits associated with receipt, we set zero */ + + *status = 0; + return (int)rx_fifo; } @@ -582,8 +701,6 @@ void up_earlyserialinit(void) * ****************************************************************************/ -/* TODO */ - void up_serialinit(void) { #ifdef HAVE_SERIAL_CONSOLE @@ -607,13 +724,12 @@ void up_serialinit(void) * ****************************************************************************/ -/* TODO - To finish later with interrupt */ - int up_putc(int ch) { #ifdef HAVE_SERIAL_CONSOLE + uint32_t int_status; - /* TODO disable uart ints */ + esp32c3_lowputc_disable_all_uart_int(CONSOLE_DEV.priv, &int_status); /* Check for LF */ @@ -625,8 +741,7 @@ int up_putc(int ch) } up_lowputc(ch); - - /* TODO restore ints */ + esp32c3_lowputc_restore_all_uart_int(CONSOLE_DEV.priv, &int_status); #endif return ch; } @@ -674,11 +789,13 @@ int up_putc(int ch) * ****************************************************************************/ -/* TODO - Finish it disabling interrupt and restoring it later */ - int up_putc(int ch) { #ifdef HAVE_SERIAL_CONSOLE + uint32_t int_status; + + esp32c3_lowputc_disable_all_uart_int(CONSOLE_DEV.priv, &int_status); + /* Check for LF */ if (ch == '\n') @@ -689,6 +806,7 @@ int up_putc(int ch) } up_lowputc(ch); + esp32c3_lowputc_restore_all_uart_int(CONSOLE_DEV.priv, &int_status); #endif return ch; }