Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

arch/risc-v: Driver for the ESP32-C3's CDC console. #4809

Merged
merged 5 commits into from Nov 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -95,3 +95,17 @@ To test it, just run the following command::
nsh> watcher
nsh> watched

usbconsole
==========

This configuration tests the built-in USB-to-serial converter found in ESP32-C3 (revision 3).
`esptool` can be used to check the version of the chip and if this feature is
supported. Running `esptool.py -p <port> chip_id` should have `Chip is
ESP32-C3 (revision 3)` in its output.
When connecting the board a new device should appear, a `/dev/ttyACMX` on Linux
or a `/dev/cu.usbmodemXXX` om macOS.
This can be used to flash and monitor the device with the usual commands::

make download ESPTOOL_PORT=/dev/ttyACM0
minicom -D /dev/ttyACM0

7 changes: 4 additions & 3 deletions Documentation/platforms/risc-v/esp32c3/index.rst
Expand Up @@ -101,9 +101,9 @@ Peripheral Support

The following list indicates the state of peripherals' support in NuttX:

========== ======= =====
Peripheral Support NOTES
========== ======= =====
=========== ======= =====
Peripheral Support NOTES
=========== ======= =====
GPIO Yes
UART Yes
SPI Yes
Expand All @@ -122,6 +122,7 @@ Bluetooth Yes
LED_PWM Yes
SHA Yes
RSA Yes
CDC Console Yes Rev.3
========== ======= =====

Supported Boards
Expand Down
6 changes: 6 additions & 0 deletions arch/risc-v/src/esp32c3/Kconfig
Expand Up @@ -229,6 +229,12 @@ config ESP32C3_UART1
select UART1_SERIALDRIVER
select ARCH_HAVE_SERIAL_TERMIOS

config ESP32C3_USBSERIAL
bool "USB-Serial Driver"
default n
select OTHER_UART_SERIALDRIVER
select ARCH_HAVE_SERIAL_TERMIOS

config ESP32C3_I2C0
bool "I2C 0"
default n
Expand Down
6 changes: 3 additions & 3 deletions arch/risc-v/src/esp32c3/Make.defs
Expand Up @@ -50,7 +50,7 @@ endif
CHIP_CSRCS = esp32c3_allocateheap.c esp32c3_start.c esp32c3_wdt.c esp32c3_idle.c
CHIP_CSRCS += esp32c3_irq.c
CHIP_CSRCS += esp32c3_clockconfig.c esp32c3_gpio.c
CHIP_CSRCS += esp32c3_lowputc.c
CHIP_CSRCS += esp32c3_lowputc.c esp32c3_serial.c
CHIP_CSRCS += esp32c3_systemreset.c esp32c3_resetcause.c
CHIP_CSRCS += esp32c3_uid.c

Expand All @@ -60,8 +60,8 @@ else
CHIP_CSRCS += esp32c3_timerisr.c
endif

ifeq ($(CONFIG_ESP32C3_UART),y)
CHIP_CSRCS += esp32c3_serial.c
ifeq ($(CONFIG_ESP32C3_USBSERIAL),y)
CHIP_CSRCS += esp32c3_usbserial.c
endif

ifeq ($(CONFIG_ESP32C3_RNG),y)
Expand Down
8 changes: 8 additions & 0 deletions arch/risc-v/src/esp32c3/esp32c3_config.h
Expand Up @@ -49,15 +49,23 @@
*/

#undef HAVE_SERIAL_CONSOLE
#undef CONSOLE_UART
#if defined(CONFIG_UART0_SERIAL_CONSOLE) && defined(CONFIG_ESP32C3_UART0)
# undef CONFIG_UART1_SERIAL_CONSOLE
# define HAVE_SERIAL_CONSOLE 1
# define CONSOLE_UART 1
#elif defined(CONFIG_UART1_SERIAL_CONSOLE) && defined(CONFIG_ESP32C3_UART1)
# undef CONFIG_UART0_SERIAL_CONSOLE
# define HAVE_SERIAL_CONSOLE 1
# define CONSOLE_UART 1
#else
# undef CONFIG_UART0_SERIAL_CONSOLE
# undef CONFIG_UART1_SERIAL_CONSOLE
#endif

#ifdef CONFIG_ESP32C3_USBSERIAL
# define HAVE_SERIAL_CONSOLE 1
# define HAVE_UART_DEVICE 1
#endif

#endif /* __ARCH_RISCV_SRC_ESP32C3_ESP32C3_CONFIG_H */
7 changes: 5 additions & 2 deletions arch/risc-v/src/esp32c3/esp32c3_lowputc.c
Expand Up @@ -45,6 +45,7 @@
#include "esp32c3_clockconfig.h"
#include "esp32c3_config.h"
#include "esp32c3_gpio.h"
#include "esp32c3_usbserial.h"

#include "esp32c3_lowputc.h"

Expand Down Expand Up @@ -800,7 +801,7 @@ void esp32c3_lowputc_restore_pins(const struct esp32c3_uart_s *priv)

void riscv_lowputc(char ch)
{
#ifdef HAVE_SERIAL_CONSOLE
#ifdef CONSOLE_UART

# if defined(CONFIG_UART0_SERIAL_CONSOLE)
struct esp32c3_uart_s *priv = &g_uart0_config;
Expand All @@ -816,7 +817,9 @@ void riscv_lowputc(char ch)

esp32c3_lowputc_send_byte(priv, ch);

#endif /* HAVE_CONSOLE */
#elif defined (CONFIG_ESP32C3_USBSERIAL)
esp32c3_usbserial_write(ch);
#endif /* CONSOLE_UART */
}

/****************************************************************************
Expand Down
50 changes: 41 additions & 9 deletions arch/risc-v/src/esp32c3/esp32c3_serial.c
Expand Up @@ -52,6 +52,10 @@
#include "esp32c3_irq.h"
#include "esp32c3_lowputc.h"

#ifdef CONFIG_ESP32C3_USBSERIAL
# include "esp32c3_usbserial.h"
#endif

/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
Expand All @@ -72,7 +76,7 @@
* the console and the corresponding peripheral was also selected.
*/

#ifdef HAVE_SERIAL_CONSOLE
#ifdef CONSOLE_UART
# if defined(CONFIG_UART0_SERIAL_CONSOLE)
# define CONSOLE_DEV g_uart0_dev /* UART0 is console */
# define TTYS0_DEV g_uart0_dev /* UART0 is ttyS0 */
Expand All @@ -82,7 +86,7 @@
# define TTYS0_DEV g_uart1_dev /* UART1 is ttyS0 */
# define UART1_ASSIGNED 1
# endif /* CONFIG_UART0_SERIAL_CONSOLE */
#else /* No console */
#else /* No UART console */
# undef CONSOLE_DEV
# if defined(CONFIG_ESP32C3_UART0)
# define TTYS0_DEV g_uart0_dev /* UART0 is ttyS0 */
Expand All @@ -91,7 +95,12 @@
# define TTYS0_DEV g_uart1_dev /* UART1 is ttyS0 */
# define UART1_ASSIGNED 1
# endif
#endif /* HAVE_SERIAL_CONSOLE */
#endif /* CONSOLE_UART */

#ifdef CONFIG_ESP32C3_USBSERIAL
# define CONSOLE_DEV g_uart_usbserial
# define TTYACM0_DEV g_uart_usbserial
#endif

/* Pick ttys1 */

Expand All @@ -109,6 +118,8 @@
* Private Function Prototypes
****************************************************************************/

#ifdef CONFIG_ESP32C3_UART

/* Serial driver methods */

static int esp32c3_setup(struct uart_dev_s *dev);
Expand All @@ -127,11 +138,14 @@ static int esp32c3_ioctl(struct file *filep, int cmd, unsigned long arg);
static bool esp32c3_rxflowcontrol(struct uart_dev_s *dev,
unsigned int nbuffered, bool upper);
#endif
#endif

/****************************************************************************
* Private Data
****************************************************************************/

#ifdef CONFIG_ESP32C3_UART

/* Operations */

static struct uart_ops_s g_uart_ops =
Expand Down Expand Up @@ -219,10 +233,14 @@ static uart_dev_t g_uart1_dev =

#endif

#endif /* CONFIG_ESP32C3_UART */

/****************************************************************************
* Private Functions
****************************************************************************/

#ifdef CONFIG_ESP32C3_UART

/****************************************************************************
* Name: uart_interrupt
*
Expand Down Expand Up @@ -992,6 +1010,7 @@ static bool esp32c3_rxflowcontrol(struct uart_dev_s *dev,
return ret;
}
#endif
#endif /* CONFIG_ESP32C3_UART */

/****************************************************************************
* Public Functions
Expand Down Expand Up @@ -1019,7 +1038,10 @@ void riscv_earlyserialinit(void)

/* Disable all UARTS interrupts */

#ifdef TTYS0_DEV
esp32c3_lowputc_disable_all_uart_int(TTYS0_DEV.priv, NULL);
#endif

#ifdef TTYS1_DEV
esp32c3_lowputc_disable_all_uart_int(TTYS1_DEV.priv, NULL);
#endif
Expand All @@ -1029,7 +1051,7 @@ void riscv_earlyserialinit(void)
* open.
*/

#ifdef HAVE_SERIAL_CONSOLE
#ifdef CONSOLE_UART
esp32c3_setup(&CONSOLE_DEV);
#endif
}
Expand All @@ -1051,13 +1073,17 @@ void riscv_serialinit(void)
uart_register("/dev/console", &CONSOLE_DEV);
#endif

/* At least one UART char driver will logically be registered */

#ifdef TTYS0_DEV
uart_register("/dev/ttyS0", &TTYS0_DEV);
#endif

#ifdef TTYS1_DEV
#ifdef TTYS1_DEV
uart_register("/dev/ttyS1", &TTYS1_DEV);
#endif

#ifdef CONFIG_ESP32C3_USBSERIAL
uart_register("/dev/ttyACM0", &TTYACM0_DEV);
#endif
}

/****************************************************************************
Expand All @@ -1070,10 +1096,11 @@ void riscv_serialinit(void)

int up_putc(int ch)
{
#ifdef HAVE_SERIAL_CONSOLE
#ifdef CONSOLE_UART
uint32_t int_status;

esp32c3_lowputc_disable_all_uart_int(CONSOLE_DEV.priv, &int_status);
#endif

/* Check for LF */

Expand All @@ -1085,6 +1112,8 @@ int up_putc(int ch)
}

riscv_lowputc(ch);

#ifdef CONSOLE_UART
esp32c3_lowputc_restore_all_uart_int(CONSOLE_DEV.priv, &int_status);
#endif
return ch;
Expand All @@ -1104,10 +1133,11 @@ int up_putc(int ch)

int up_putc(int ch)
{
#ifdef HAVE_SERIAL_CONSOLE
#ifdef CONSOLE_UART
uint32_t int_status;

esp32c3_lowputc_disable_all_uart_int(CONSOLE_DEV.priv, &int_status);
#endif

/* Check for LF */

Expand All @@ -1119,6 +1149,8 @@ int up_putc(int ch)
}

riscv_lowputc(ch);

#ifdef CONSOLE_UART
esp32c3_lowputc_restore_all_uart_int(CONSOLE_DEV.priv, &int_status);
#endif
return ch;
Expand Down