diff --git a/components/console/esp_console_repl.c b/components/console/esp_console_repl.c index 123572d62f..cad69c3805 100644 --- a/components/console/esp_console_repl.c +++ b/components/console/esp_console_repl.c @@ -18,6 +18,7 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "driver/uart.h" +#include "driver/uart_vfs.h" #include "driver/usb_serial_jtag.h" #include "linenoise/linenoise.h" @@ -224,9 +225,9 @@ esp_err_t esp_console_new_repl_uart(const esp_console_dev_uart_config_t *dev_con fsync(fileno(stdout)); /* Minicom, screen, idf_monitor send CR when ENTER key is pressed */ - esp_vfs_dev_uart_port_set_rx_line_endings(dev_config->channel, ESP_LINE_ENDINGS_CR); + uart_vfs_dev_port_set_rx_line_endings(dev_config->channel, ESP_LINE_ENDINGS_CR); /* Move the caret to the beginning of the next line on '\n' */ - esp_vfs_dev_uart_port_set_tx_line_endings(dev_config->channel, ESP_LINE_ENDINGS_CRLF); + uart_vfs_dev_port_set_tx_line_endings(dev_config->channel, ESP_LINE_ENDINGS_CRLF); /* Configure UART. Note that REF_TICK/XTAL is used so that the baud rate remains * correct while APB frequency is changing in light sleep mode. @@ -261,7 +262,7 @@ esp_err_t esp_console_new_repl_uart(const esp_console_dev_uart_config_t *dev_con } /* Tell VFS to use UART driver */ - esp_vfs_dev_uart_use_driver(dev_config->channel); + uart_vfs_dev_use_driver(dev_config->channel); // initialize console, common part ret = esp_console_common_init(repl_config->max_cmdline_length, &uart_repl->repl_com); @@ -423,7 +424,7 @@ static esp_err_t esp_console_repl_uart_delete(esp_console_repl_t *repl) } repl_com->state = CONSOLE_REPL_STATE_DEINIT; esp_console_deinit(); - esp_vfs_dev_uart_use_nonblocking(uart_repl->uart_channel); + uart_vfs_dev_use_nonblocking(uart_repl->uart_channel); uart_driver_delete(uart_repl->uart_channel); free(uart_repl); _exit: diff --git a/components/esp_driver_uart/CMakeLists.txt b/components/esp_driver_uart/CMakeLists.txt index 8d3fbddd56..e6046edd67 100644 --- a/components/esp_driver_uart/CMakeLists.txt +++ b/components/esp_driver_uart/CMakeLists.txt @@ -9,3 +9,11 @@ idf_component_register(SRCS ${srcs} PRIV_REQUIRES esp_pm esp_driver_gpio esp_ringbuf LDFRAGMENTS "linker.lf" ) + +if(CONFIG_VFS_SUPPORT_IO) + target_link_libraries(${COMPONENT_LIB} PUBLIC idf::vfs) + target_sources(${COMPONENT_LIB} PRIVATE "src/uart_vfs.c") + if(CONFIG_ESP_CONSOLE_UART) + target_link_libraries(${COMPONENT_LIB} INTERFACE "-u uart_vfs_include_dev_init") + endif() +endif() diff --git a/components/esp_driver_uart/include/driver/uart_vfs.h b/components/esp_driver_uart/include/driver/uart_vfs.h new file mode 100644 index 0000000000..0521603ded --- /dev/null +++ b/components/esp_driver_uart/include/driver/uart_vfs.h @@ -0,0 +1,84 @@ +/* + * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include "esp_vfs_common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Add /dev/uart virtual filesystem driver + * + * This function is called from startup code to enable serial output + */ +void uart_vfs_dev_register(void); + +/** + * @brief Set the line endings expected to be received on specified UART + * + * This specifies the conversion between line endings received on UART and + * newlines ('\n', LF) passed into stdin: + * + * - ESP_LINE_ENDINGS_CRLF: convert CRLF to LF + * - ESP_LINE_ENDINGS_CR: convert CR to LF + * - ESP_LINE_ENDINGS_LF: no modification + * + * @note this function is not thread safe w.r.t. reading from UART + * + * @param uart_num the UART number + * @param mode line endings to send to UART + * + * @return 0 if successed, or -1 + * when an error (specified by errno) have occurred. + */ +int uart_vfs_dev_port_set_rx_line_endings(int uart_num, esp_line_endings_t mode); + +/** + * @brief Set the line endings to sent to specified UART + * + * This specifies the conversion between newlines ('\n', LF) on stdout and line + * endings sent over UART: + * + * - ESP_LINE_ENDINGS_CRLF: convert LF to CRLF + * - ESP_LINE_ENDINGS_CR: convert LF to CR + * - ESP_LINE_ENDINGS_LF: no modification + * + * @note this function is not thread safe w.r.t. writing to UART + * + * @param uart_num the UART number + * @param mode line endings to send to UART + * + * @return 0 if successed, or -1 + * when an error (specified by errno) have occurred. + */ +int uart_vfs_dev_port_set_tx_line_endings(int uart_num, esp_line_endings_t mode); + +/** + * @brief set VFS to use simple functions for reading and writing UART + * + * Read is non-blocking, write is busy waiting until TX FIFO has enough space. + * These functions are used by default. + * + * @param uart_num UART peripheral number + */ +void uart_vfs_dev_use_nonblocking(int uart_num); + +/** + * @brief set VFS to use UART driver for reading and writing + * + * @note Application must configure UART driver before calling these functions + * With these functions, read and write are blocking and interrupt-driven. + * + * @param uart_num UART peripheral number + */ +void uart_vfs_dev_use_driver(int uart_num); + +#ifdef __cplusplus +} +#endif diff --git a/components/esp_driver_uart/linker.lf b/components/esp_driver_uart/linker.lf index eb5d2d6f09..ff50da4503 100644 --- a/components/esp_driver_uart/linker.lf +++ b/components/esp_driver_uart/linker.lf @@ -1,3 +1,9 @@ +[mapping:uart_vfs] +archive: libesp_driver_uart.a +entries: + if VFS_SELECT_IN_RAM = y: + uart_vfs: select_notif_callback_isr (noflash) + [mapping:uart_hal] archive: libhal.a entries: diff --git a/components/vfs/vfs_uart.c b/components/esp_driver_uart/src/uart_vfs.c similarity index 67% rename from components/vfs/vfs_uart.c rename to components/esp_driver_uart/src/uart_vfs.c index 3aac392ce1..c3e1bca87b 100644 --- a/components/vfs/vfs_uart.c +++ b/components/esp_driver_uart/src/uart_vfs.c @@ -13,15 +13,15 @@ #include #include "sdkconfig.h" #include "esp_attr.h" -#include "esp_vfs.h" -#include "esp_vfs_dev.h" -#include "esp_vfs_private.h" -#include "esp_rom_uart.h" +#include "driver/uart_vfs.h" #include "driver/uart.h" #include "driver/uart_select.h" +#include "esp_rom_uart.h" #include "hal/uart_ll.h" #include "soc/soc_caps.h" -#include "soc/uart_periph.h" +#include "esp_private/esp_vfs_console.h" +#include "esp_vfs_dev.h" // Old headers for the aliasing functions +#include "esp_private/startup_internal.h" #define UART_NUM SOC_UART_HP_NUM @@ -44,6 +44,12 @@ # define DEFAULT_RX_MODE ESP_LINE_ENDINGS_LF #endif +#if CONFIG_VFS_SELECT_IN_RAM +#define UART_VFS_MALLOC_FLAGS (MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT) +#else +#define UART_VFS_MALLOC_FLAGS MALLOC_CAP_DEFAULT +#endif + // UART write bytes function type typedef void (*tx_func_t)(int, int); // UART read bytes function type @@ -77,9 +83,9 @@ typedef struct { tx_func_t tx_func; // Functions used to read bytes from UART. Default to "basic" functions. rx_func_t rx_func; -} vfs_uart_context_t; +} uart_vfs_context_t; -#define VFS_CTX_DEFAULT_VAL(uart_dev) (vfs_uart_context_t) {\ +#define VFS_CTX_DEFAULT_VAL(uart_dev) (uart_vfs_context_t) {\ .uart = (uart_dev),\ .peek_char = NONE,\ .tx_mode = DEFAULT_TX_MODE,\ @@ -90,7 +96,7 @@ typedef struct { //If the context should be dynamically initialized, remove this structure //and point s_ctx to allocated data. -static vfs_uart_context_t s_context[UART_NUM] = { +static uart_vfs_context_t s_context[UART_NUM] = { VFS_CTX_DEFAULT_VAL(&UART0), VFS_CTX_DEFAULT_VAL(&UART1), #if UART_NUM > 2 @@ -98,7 +104,7 @@ static vfs_uart_context_t s_context[UART_NUM] = { #endif }; -static vfs_uart_context_t* s_ctx[UART_NUM] = { +static uart_vfs_context_t* s_ctx[UART_NUM] = { &s_context[0], &s_context[1], #if UART_NUM > 2 @@ -200,7 +206,7 @@ static int uart_rx_char_via_driver(int fd) static ssize_t uart_write(int fd, const void * data, size_t size) { - assert(fd >=0 && fd < 3); + assert(fd >= 0 && fd < 3); const char *data_c = (const char *)data; /* Even though newlib does stream locking on each individual stream, we need * a dedicated UART lock if two streams (stdout and stderr) point to the @@ -245,7 +251,7 @@ static void uart_return_char(int fd, int c) static ssize_t uart_read(int fd, void* data, size_t size) { - assert(fd >=0 && fd < 3); + assert(fd >= 0 && fd < 3); char *data_c = (char *) data; size_t received = 0; _lock_acquire_recursive(&s_ctx[fd]->read_lock); @@ -291,7 +297,7 @@ static ssize_t uart_read(int fd, void* data, size_t size) static int uart_fstat(int fd, struct stat * st) { - assert(fd >=0 && fd < 3); + assert(fd >= 0 && fd < 3); memset(st, 0, sizeof(*st)); st->st_mode = S_IFCHR; return 0; @@ -299,13 +305,13 @@ static int uart_fstat(int fd, struct stat * st) static int uart_close(int fd) { - assert(fd >=0 && fd < 3); + assert(fd >= 0 && fd < 3); return 0; } static int uart_fcntl(int fd, int cmd, int arg) { - assert(fd >=0 && fd < 3); + assert(fd >= 0 && fd < 3); int result = 0; if (cmd == F_GETFL) { result |= O_RDWR; @@ -366,7 +372,7 @@ static esp_err_t register_select(uart_select_args_t *args) portENTER_CRITICAL(&s_registered_select_lock); const int new_size = s_registered_select_num + 1; uart_select_args_t **new_selects; - if ((new_selects = heap_caps_realloc(s_registered_selects, new_size * sizeof(uart_select_args_t *), VFS_MALLOC_FLAGS)) == NULL) { + if ((new_selects = heap_caps_realloc(s_registered_selects, new_size * sizeof(uart_select_args_t *), UART_VFS_MALLOC_FLAGS)) == NULL) { ret = ESP_ERR_NO_MEM; } else { s_registered_selects = new_selects; @@ -392,7 +398,7 @@ static esp_err_t unregister_select(uart_select_args_t *args) // The item is removed by overwriting it with the last item. The subsequent rellocation will drop the // last item. s_registered_selects[i] = s_registered_selects[new_size]; - s_registered_selects = heap_caps_realloc(s_registered_selects, new_size * sizeof(uart_select_args_t *), VFS_MALLOC_FLAGS); + s_registered_selects = heap_caps_realloc(s_registered_selects, new_size * sizeof(uart_select_args_t *), UART_VFS_MALLOC_FLAGS); // Shrinking a buffer with realloc is guaranteed to succeed. s_registered_select_num = new_size; ret = ESP_OK; @@ -411,24 +417,24 @@ static void select_notif_callback_isr(uart_port_t uart_num, uart_select_notif_t uart_select_args_t *args = s_registered_selects[i]; if (args) { switch (uart_select_notif) { - case UART_SELECT_READ_NOTIF: - if (FD_ISSET(uart_num, &args->readfds_orig)) { - FD_SET(uart_num, args->readfds); - esp_vfs_select_triggered_isr(args->select_sem, task_woken); - } - break; - case UART_SELECT_WRITE_NOTIF: - if (FD_ISSET(uart_num, &args->writefds_orig)) { - FD_SET(uart_num, args->writefds); - esp_vfs_select_triggered_isr(args->select_sem, task_woken); - } - break; - case UART_SELECT_ERROR_NOTIF: - if (FD_ISSET(uart_num, &args->errorfds_orig)) { - FD_SET(uart_num, args->errorfds); - esp_vfs_select_triggered_isr(args->select_sem, task_woken); - } - break; + case UART_SELECT_READ_NOTIF: + if (FD_ISSET(uart_num, &args->readfds_orig)) { + FD_SET(uart_num, args->readfds); + esp_vfs_select_triggered_isr(args->select_sem, task_woken); + } + break; + case UART_SELECT_WRITE_NOTIF: + if (FD_ISSET(uart_num, &args->writefds_orig)) { + FD_SET(uart_num, args->writefds); + esp_vfs_select_triggered_isr(args->select_sem, task_woken); + } + break; + case UART_SELECT_ERROR_NOTIF: + if (FD_ISSET(uart_num, &args->errorfds_orig)) { + FD_SET(uart_num, args->errorfds); + esp_vfs_select_triggered_isr(args->select_sem, task_woken); + } + break; } } } @@ -436,7 +442,7 @@ static void select_notif_callback_isr(uart_port_t uart_num, uart_select_notif_t } static esp_err_t uart_start_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, - esp_vfs_select_sem_t select_sem, void **end_select_args) + esp_vfs_select_sem_t select_sem, void **end_select_args) { const int max_fds = MIN(nfds, UART_NUM); *end_select_args = NULL; @@ -449,7 +455,7 @@ static esp_err_t uart_start_select(int nfds, fd_set *readfds, fd_set *writefds, } } - uart_select_args_t *args = heap_caps_malloc(sizeof(uart_select_args_t), VFS_MALLOC_FLAGS); + uart_select_args_t *args = heap_caps_malloc(sizeof(uart_select_args_t), UART_VFS_MALLOC_FLAGS); if (args == NULL) { return ESP_ERR_NO_MEM; @@ -542,26 +548,26 @@ static int uart_tcsetattr(int fd, int optional_actions, const struct termios *p) } switch (optional_actions) { - case TCSANOW: - // nothing to do - break; - case TCSADRAIN: - if (uart_wait_tx_done(fd, portMAX_DELAY) != ESP_OK) { - errno = EINVAL; - return -1; - } - - /* FALLTHRU */ - - case TCSAFLUSH: - if (uart_flush_input(fd) != ESP_OK) { - errno = EINVAL; - return -1; - } - break; - default: + case TCSANOW: + // nothing to do + break; + case TCSADRAIN: + if (uart_wait_tx_done(fd, portMAX_DELAY) != ESP_OK) { errno = EINVAL; return -1; + } + + /* FALLTHRU */ + + case TCSAFLUSH: + if (uart_flush_input(fd) != ESP_OK) { + errno = EINVAL; + return -1; + } + break; + default: + errno = EINVAL; + return -1; } if (p->c_iflag & IGNCR) { @@ -579,21 +585,21 @@ static int uart_tcsetattr(int fd, int optional_actions, const struct termios *p) const tcflag_t csize_bits = p->c_cflag & CSIZE; switch (csize_bits) { - case CS5: - data_bits = UART_DATA_5_BITS; - break; - case CS6: - data_bits = UART_DATA_6_BITS; - break; - case CS7: - data_bits = UART_DATA_7_BITS; - break; - case CS8: - data_bits = UART_DATA_8_BITS; - break; - default: - errno = EINVAL; - return -1; + case CS5: + data_bits = UART_DATA_5_BITS; + break; + case CS6: + data_bits = UART_DATA_6_BITS; + break; + case CS7: + data_bits = UART_DATA_7_BITS; + break; + case CS8: + data_bits = UART_DATA_8_BITS; + break; + default: + errno = EINVAL; + return -1; } if (uart_set_word_length(fd, data_bits) != ESP_OK) { @@ -608,9 +614,9 @@ static int uart_tcsetattr(int fd, int optional_actions, const struct termios *p) } if (uart_set_parity(fd, (p->c_cflag & PARENB) ? - ((p->c_cflag & PARODD) ? UART_PARITY_ODD : UART_PARITY_EVEN) - : - UART_PARITY_DISABLE) != ESP_OK) { + ((p->c_cflag & PARODD) ? UART_PARITY_ODD : UART_PARITY_EVEN) + : + UART_PARITY_DISABLE) != ESP_OK) { errno = EINVAL; return -1; } @@ -625,102 +631,102 @@ static int uart_tcsetattr(int fd, int optional_actions, const struct termios *p) b = p->c_ispeed; } else { switch (p->c_ispeed) { - case B0: - b = 0; - break; - case B50: - b = 50; - break; - case B75: - b = 75; - break; - case B110: - b = 110; - break; - case B134: - b = 134; - break; - case B150: - b = 150; - break; - case B200: - b = 200; - break; - case B300: - b = 300; - break; - case B600: - b = 600; - break; - case B1200: - b = 1200; - break; - case B1800: - b = 1800; - break; - case B2400: - b = 2400; - break; - case B4800: - b = 4800; - break; - case B9600: - b = 9600; - break; - case B19200: - b = 19200; - break; - case B38400: - b = 38400; - break; - case B57600: - b = 57600; - break; - case B115200: - b = 115200; - break; - case B230400: - b = 230400; - break; - case B460800: - b = 460800; - break; - case B500000: - b = 500000; - break; - case B576000: - b = 576000; - break; - case B921600: - b = 921600; - break; - case B1000000: - b = 1000000; - break; - case B1152000: - b = 1152000; - break; - case B1500000: - b = 1500000; - break; - case B2000000: - b = 2000000; - break; - case B2500000: - b = 2500000; - break; - case B3000000: - b = 3000000; - break; - case B3500000: - b = 3500000; - break; - case B4000000: - b = 4000000; - break; - default: - errno = EINVAL; - return -1; + case B0: + b = 0; + break; + case B50: + b = 50; + break; + case B75: + b = 75; + break; + case B110: + b = 110; + break; + case B134: + b = 134; + break; + case B150: + b = 150; + break; + case B200: + b = 200; + break; + case B300: + b = 300; + break; + case B600: + b = 600; + break; + case B1200: + b = 1200; + break; + case B1800: + b = 1800; + break; + case B2400: + b = 2400; + break; + case B4800: + b = 4800; + break; + case B9600: + b = 9600; + break; + case B19200: + b = 19200; + break; + case B38400: + b = 38400; + break; + case B57600: + b = 57600; + break; + case B115200: + b = 115200; + break; + case B230400: + b = 230400; + break; + case B460800: + b = 460800; + break; + case B500000: + b = 500000; + break; + case B576000: + b = 576000; + break; + case B921600: + b = 921600; + break; + case B1000000: + b = 1000000; + break; + case B1152000: + b = 1152000; + break; + case B1500000: + b = 1500000; + break; + case B2000000: + b = 2000000; + break; + case B2500000: + b = 2500000; + break; + case B3000000: + b = 3000000; + break; + case B3500000: + b = 3500000; + break; + case B4000000: + b = 4000000; + break; + default: + errno = EINVAL; + return -1; } } @@ -765,21 +771,21 @@ static int uart_tcgetattr(int fd, struct termios *p) p->c_cflag &= (~CSIZE); switch (data_bits) { - case UART_DATA_5_BITS: - p->c_cflag |= CS5; - break; - case UART_DATA_6_BITS: - p->c_cflag |= CS6; - break; - case UART_DATA_7_BITS: - p->c_cflag |= CS7; - break; - case UART_DATA_8_BITS: - p->c_cflag |= CS8; - break; - default: - errno = ENOSYS; - return -1; + case UART_DATA_5_BITS: + p->c_cflag |= CS5; + break; + case UART_DATA_6_BITS: + p->c_cflag |= CS6; + break; + case UART_DATA_7_BITS: + p->c_cflag |= CS7; + break; + case UART_DATA_8_BITS: + p->c_cflag |= CS8; + break; + default: + errno = ENOSYS; + return -1; } } @@ -791,16 +797,16 @@ static int uart_tcgetattr(int fd, struct termios *p) } switch (stop_bits) { - case UART_STOP_BITS_1: - // nothing to do - break; - case UART_STOP_BITS_2: - p->c_cflag |= CSTOPB; - break; - default: - // UART_STOP_BITS_1_5 is unsupported by termios - errno = ENOSYS; - return -1; + case UART_STOP_BITS_1: + // nothing to do + break; + case UART_STOP_BITS_2: + p->c_cflag |= CSTOPB; + break; + default: + // UART_STOP_BITS_1_5 is unsupported by termios + errno = ENOSYS; + return -1; } } @@ -812,18 +818,18 @@ static int uart_tcgetattr(int fd, struct termios *p) } switch (parity_mode) { - case UART_PARITY_EVEN: - p->c_cflag |= PARENB; - break; - case UART_PARITY_ODD: - p->c_cflag |= (PARENB | PARODD); - break; - case UART_PARITY_DISABLE: - // nothing to do - break; - default: - errno = ENOSYS; - return -1; + case UART_PARITY_EVEN: + p->c_cflag |= PARENB; + break; + case UART_PARITY_ODD: + p->c_cflag |= (PARENB | PARODD); + break; + case UART_PARITY_DISABLE: + // nothing to do + break; + default: + errno = ENOSYS; + return -1; } } @@ -838,103 +844,103 @@ static int uart_tcgetattr(int fd, struct termios *p) speed_t sp; switch (baudrate) { - case 0: - sp = B0; - break; - case 50: - sp = B50; - break; - case 75: - sp = B75; - break; - case 110: - sp = B110; - break; - case 134: - sp = B134; - break; - case 150: - sp = B150; - break; - case 200: - sp = B200; - break; - case 300: - sp = B300; - break; - case 600: - sp = B600; - break; - case 1200: - sp = B1200; - break; - case 1800: - sp = B1800; - break; - case 2400: - sp = B2400; - break; - case 4800: - sp = B4800; - break; - case 9600: - sp = B9600; - break; - case 19200: - sp = B19200; - break; - case 38400: - sp = B38400; - break; - case 57600: - sp = B57600; - break; - case 115200: - sp = B115200; - break; - case 230400: - sp = B230400; - break; - case 460800: - sp = B460800; - break; - case 500000: - sp = B500000; - break; - case 576000: - sp = B576000; - break; - case 921600: - sp = B921600; - break; - case 1000000: - sp = B1000000; - break; - case 1152000: - sp = B1152000; - break; - case 1500000: - sp = B1500000; - break; - case 2000000: - sp = B2000000; - break; - case 2500000: - sp = B2500000; - break; - case 3000000: - sp = B3000000; - break; - case 3500000: - sp = B3500000; - break; - case 4000000: - sp = B4000000; - break; - default: - p->c_cflag |= BOTHER; - sp = baudrate; - break; + case 0: + sp = B0; + break; + case 50: + sp = B50; + break; + case 75: + sp = B75; + break; + case 110: + sp = B110; + break; + case 134: + sp = B134; + break; + case 150: + sp = B150; + break; + case 200: + sp = B200; + break; + case 300: + sp = B300; + break; + case 600: + sp = B600; + break; + case 1200: + sp = B1200; + break; + case 1800: + sp = B1800; + break; + case 2400: + sp = B2400; + break; + case 4800: + sp = B4800; + break; + case 9600: + sp = B9600; + break; + case 19200: + sp = B19200; + break; + case 38400: + sp = B38400; + break; + case 57600: + sp = B57600; + break; + case 115200: + sp = B115200; + break; + case 230400: + sp = B230400; + break; + case 460800: + sp = B460800; + break; + case 500000: + sp = B500000; + break; + case 576000: + sp = B576000; + break; + case 921600: + sp = B921600; + break; + case 1000000: + sp = B1000000; + break; + case 1152000: + sp = B1152000; + break; + case 1500000: + sp = B1500000; + break; + case 2000000: + sp = B2000000; + break; + case 2500000: + sp = B2500000; + break; + case 3000000: + sp = B3000000; + break; + case 3500000: + sp = B3500000; + break; + case 4000000: + sp = B4000000; + break; + default: + p->c_cflag |= BOTHER; + sp = baudrate; + break; } p->c_ispeed = p->c_ospeed = sp; @@ -980,7 +986,7 @@ static int uart_tcflush(int fd, int select) } #endif // CONFIG_VFS_SUPPORT_TERMIOS -static const esp_vfs_t vfs = { +static const esp_vfs_t uart_vfs = { .flags = ESP_VFS_FLAG_DEFAULT, .write = &uart_write, .open = &uart_open, @@ -1004,17 +1010,12 @@ static const esp_vfs_t vfs = { #endif // CONFIG_VFS_SUPPORT_TERMIOS }; -const esp_vfs_t* esp_vfs_uart_get_vfs(void) +void uart_vfs_dev_register(void) { - return &vfs; + ESP_ERROR_CHECK(esp_vfs_register("/dev/uart", &uart_vfs, NULL)); } -void esp_vfs_dev_uart_register(void) -{ - ESP_ERROR_CHECK(esp_vfs_register("/dev/uart", &vfs, NULL)); -} - -int esp_vfs_dev_uart_port_set_rx_line_endings(int uart_num, esp_line_endings_t mode) +int uart_vfs_dev_port_set_rx_line_endings(int uart_num, esp_line_endings_t mode) { if (uart_num < 0 || uart_num >= UART_NUM) { errno = EBADF; @@ -1024,7 +1025,7 @@ int esp_vfs_dev_uart_port_set_rx_line_endings(int uart_num, esp_line_endings_t m return 0; } -int esp_vfs_dev_uart_port_set_tx_line_endings(int uart_num, esp_line_endings_t mode) +int uart_vfs_dev_port_set_tx_line_endings(int uart_num, esp_line_endings_t mode) { if (uart_num < 0 || uart_num >= UART_NUM) { errno = EBADF; @@ -1034,21 +1035,23 @@ int esp_vfs_dev_uart_port_set_tx_line_endings(int uart_num, esp_line_endings_t m return 0; } -void esp_vfs_dev_uart_set_rx_line_endings(esp_line_endings_t mode) +// Deprecated +void uart_vfs_dev_set_rx_line_endings(esp_line_endings_t mode) { for (int i = 0; i < UART_NUM; ++i) { s_ctx[i]->rx_mode = mode; } } -void esp_vfs_dev_uart_set_tx_line_endings(esp_line_endings_t mode) +// Deprecated +void uart_vfs_dev_set_tx_line_endings(esp_line_endings_t mode) { for (int i = 0; i < UART_NUM; ++i) { s_ctx[i]->tx_mode = mode; } } -void esp_vfs_dev_uart_use_nonblocking(int uart_num) +void uart_vfs_dev_use_nonblocking(int uart_num) { _lock_acquire_recursive(&s_ctx[uart_num]->read_lock); _lock_acquire_recursive(&s_ctx[uart_num]->write_lock); @@ -1058,7 +1061,7 @@ void esp_vfs_dev_uart_use_nonblocking(int uart_num) _lock_release_recursive(&s_ctx[uart_num]->read_lock); } -void esp_vfs_dev_uart_use_driver(int uart_num) +void uart_vfs_dev_use_driver(int uart_num) { _lock_acquire_recursive(&s_ctx[uart_num]->read_lock); _lock_acquire_recursive(&s_ctx[uart_num]->write_lock); @@ -1067,3 +1070,32 @@ void esp_vfs_dev_uart_use_driver(int uart_num) _lock_release_recursive(&s_ctx[uart_num]->write_lock); _lock_release_recursive(&s_ctx[uart_num]->read_lock); } + +#if CONFIG_VFS_SUPPORT_IO && CONFIG_ESP_CONSOLE_UART +ESP_SYSTEM_INIT_FN(init_vfs_uart, CORE, BIT(0), 110) +{ + esp_vfs_set_primary_dev_vfs_def_struct(&uart_vfs); + return ESP_OK; +} +#endif + +void uart_vfs_include_dev_init(void) +{ + // Linker hook function, exists to make the linker examine this file +} + +// -------------------------- esp_vfs_dev_uart_xxx ALIAS (deprecated) ---------------------------- + +void esp_vfs_dev_uart_register(void) __attribute__((alias("uart_vfs_dev_register"))); + +void esp_vfs_dev_uart_set_rx_line_endings(esp_line_endings_t mode) __attribute__((alias("uart_vfs_dev_set_rx_line_endings"))); + +void esp_vfs_dev_uart_set_tx_line_endings(esp_line_endings_t mode) __attribute__((alias("uart_vfs_dev_set_tx_line_endings"))); + +int esp_vfs_dev_uart_port_set_rx_line_endings(int uart_num, esp_line_endings_t mode) __attribute__((alias("uart_vfs_dev_port_set_rx_line_endings"))); + +int esp_vfs_dev_uart_port_set_tx_line_endings(int uart_num, esp_line_endings_t mode) __attribute__((alias("uart_vfs_dev_port_set_tx_line_endings"))); + +void esp_vfs_dev_uart_use_nonblocking(int uart_num) __attribute__((alias("uart_vfs_dev_use_nonblocking"))); + +void esp_vfs_dev_uart_use_driver(int uart_num) __attribute__((alias("uart_vfs_dev_use_driver"))); diff --git a/components/esp_driver_uart/test_apps/.build-test-rules.yml b/components/esp_driver_uart/test_apps/.build-test-rules.yml index f930c4ec13..1e0bdf7afb 100644 --- a/components/esp_driver_uart/test_apps/.build-test-rules.yml +++ b/components/esp_driver_uart/test_apps/.build-test-rules.yml @@ -17,3 +17,8 @@ components/esp_driver_uart/test_apps/uart: depends_components: - esp_driver_uart - esp_driver_gpio + +components/esp_driver_uart/test_apps/uart_vfs: + depends_components: + - esp_driver_uart + - vfs diff --git a/components/esp_driver_uart/test_apps/uart_vfs/CMakeLists.txt b/components/esp_driver_uart/test_apps/uart_vfs/CMakeLists.txt new file mode 100644 index 0000000000..f1ff90157f --- /dev/null +++ b/components/esp_driver_uart/test_apps/uart_vfs/CMakeLists.txt @@ -0,0 +1,11 @@ +# This is the project CMakeLists.txt file for the test subproject +cmake_minimum_required(VERSION 3.5) + +set(EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/unit-test-app/components") + +list(PREPEND SDKCONFIG_DEFAULTS "$ENV{IDF_PATH}/tools/test_apps/configs/sdkconfig.debug_helpers" "sdkconfig.defaults") + +set(COMPONENTS main) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(uart_vfs_test) diff --git a/components/esp_driver_uart/test_apps/uart_vfs/README.md b/components/esp_driver_uart/test_apps/uart_vfs/README.md new file mode 100644 index 0000000000..bf47d80ec6 --- /dev/null +++ b/components/esp_driver_uart/test_apps/uart_vfs/README.md @@ -0,0 +1,2 @@ +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | diff --git a/components/esp_driver_uart/test_apps/uart_vfs/main/CMakeLists.txt b/components/esp_driver_uart/test_apps/uart_vfs/main/CMakeLists.txt new file mode 100644 index 0000000000..c24a412b28 --- /dev/null +++ b/components/esp_driver_uart/test_apps/uart_vfs/main/CMakeLists.txt @@ -0,0 +1,9 @@ +set(src "test_app_main.c" + "test_vfs_uart.c" + ) + +idf_component_register(SRCS ${src} + PRIV_INCLUDE_DIRS . + PRIV_REQUIRES esp_driver_uart unity test_utils esp_psram + WHOLE_ARCHIVE + ) diff --git a/components/esp_driver_uart/test_apps/uart_vfs/main/test_app_main.c b/components/esp_driver_uart/test_apps/uart_vfs/main/test_app_main.c new file mode 100644 index 0000000000..6ee4144067 --- /dev/null +++ b/components/esp_driver_uart/test_apps/uart_vfs/main/test_app_main.c @@ -0,0 +1,28 @@ +/* + * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ + +#include "unity.h" +#include "unity_test_utils.h" +#include "esp_heap_caps.h" + +// Some resources are lazy allocated, the threadhold is left for that case +#define TEST_MEMORY_LEAK_THRESHOLD (400) + +void setUp(void) +{ + unity_utils_record_free_mem(); +} + +void tearDown(void) +{ + esp_reent_cleanup(); //clean up some of the newlib's lazy allocations + unity_utils_evaluate_leaks_direct(TEST_MEMORY_LEAK_THRESHOLD); +} + +void app_main(void) +{ + unity_run_menu(); +} diff --git a/components/vfs/test_apps/main/test_vfs_uart.c b/components/esp_driver_uart/test_apps/uart_vfs/main/test_vfs_uart.c similarity index 91% rename from components/vfs/test_apps/main/test_vfs_uart.c rename to components/esp_driver_uart/test_apps/uart_vfs/main/test_vfs_uart.c index 18403fb560..561d20cf52 100644 --- a/components/vfs/test_apps/main/test_vfs_uart.c +++ b/components/esp_driver_uart/test_apps/uart_vfs/main/test_vfs_uart.c @@ -10,15 +10,15 @@ #include #include #include +#include #include "unity.h" #include "esp_rom_uart.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/semphr.h" +#include "driver/uart_vfs.h" #include "driver/uart.h" #include "hal/uart_ll.h" -#include "esp_vfs_dev.h" -#include "esp_vfs.h" #include "esp_clk_tree.h" #include "test_utils.h" #include "sdkconfig.h" @@ -45,7 +45,7 @@ static void flush_stdin_stdout(void) esp_rom_uart_tx_wait_idle(CONFIG_ESP_CONSOLE_UART_NUM); } -TEST_CASE("can read from stdin", "[vfs]") +TEST_CASE("can read from stdin", "[vfs_uart]") { flush_stdin_stdout(); @@ -68,11 +68,10 @@ TEST_CASE("can read from stdin", "[vfs]") free(buf); } - -TEST_CASE("CRs are removed from the stdin correctly", "[vfs]") +TEST_CASE("CRs are removed from the stdin correctly", "[vfs_uart]") { - esp_vfs_dev_uart_port_set_rx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CRLF); - esp_vfs_dev_uart_port_set_tx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CRLF); + uart_vfs_dev_port_set_rx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CRLF); + uart_vfs_dev_port_set_tx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CRLF); flush_stdin_stdout(); const char* send_str = "1234567890\n\r123\r\n4\n"; @@ -155,34 +154,32 @@ static void write_task_fn(void* varg) vTaskDelete(NULL); } -TEST_CASE("can write to UART while another task is reading", "[vfs]") +TEST_CASE("can write to UART while another task is reading", "[vfs_uart]") { char out_buffer[32]; size_t out_buffer_len = sizeof(out_buffer); struct read_task_arg_t read_arg = { - .out_buffer = out_buffer, - .out_buffer_len = out_buffer_len, - .done = xSemaphoreCreateBinary() + .out_buffer = out_buffer, + .out_buffer_len = out_buffer_len, + .done = xSemaphoreCreateBinary() }; struct write_task_arg_t write_arg = { - .str = "!(@*#&(!*@&#((SDasdkjhadsl\n", - .done = xSemaphoreCreateBinary() + .str = "!(@*#&(!*@&#((SDasdkjhadsl\n", + .done = xSemaphoreCreateBinary() }; flush_stdin_stdout(); - ESP_ERROR_CHECK( uart_driver_install(CONFIG_ESP_CONSOLE_UART_NUM, - 256, 0, 0, NULL, 0) ); - esp_vfs_dev_uart_use_driver(CONFIG_ESP_CONSOLE_UART_NUM); - + ESP_ERROR_CHECK(uart_driver_install(CONFIG_ESP_CONSOLE_UART_NUM, + 256, 0, 0, NULL, 0)); + uart_vfs_dev_use_driver(CONFIG_ESP_CONSOLE_UART_NUM); xTaskCreate(&read_task_fn, "vfs_read", 4096, &read_arg, 5, NULL); vTaskDelay(10); xTaskCreate(&write_task_fn, "vfs_write", 4096, &write_arg, 6, NULL); - int res = xSemaphoreTake(write_arg.done, 100 / portTICK_PERIOD_MS); TEST_ASSERT(res); @@ -191,13 +188,14 @@ TEST_CASE("can write to UART while another task is reading", "[vfs]") TEST_ASSERT_EQUAL(0, strcmp(write_arg.str, read_arg.out_buffer)); - esp_vfs_dev_uart_use_nonblocking(CONFIG_ESP_CONSOLE_UART_NUM); + uart_vfs_dev_use_nonblocking(CONFIG_ESP_CONSOLE_UART_NUM); uart_driver_delete(CONFIG_ESP_CONSOLE_UART_NUM); vSemaphoreDelete(read_arg.done); vSemaphoreDelete(write_arg.done); + vTaskDelay(2); // wait for tasks to exit } -TEST_CASE("fcntl supported in UART VFS", "[vfs]") +TEST_CASE("fcntl supported in UART VFS", "[vfs_uart]") { int flags = fcntl(STDIN_FILENO, F_GETFL, 0); TEST_ASSERT_NOT_EQUAL(-1, flags); @@ -209,7 +207,7 @@ TEST_CASE("fcntl supported in UART VFS", "[vfs]") } #ifdef CONFIG_VFS_SUPPORT_TERMIOS -TEST_CASE("Can use termios for UART", "[vfs]") +TEST_CASE("Can use termios for UART", "[vfs_uart]") { uint32_t clk_src_hz = 0; TEST_ESP_OK(esp_clk_tree_src_get_freq_hz((soc_module_clk_t)UART_SCLK_DEFAULT, ESP_CLK_TREE_SRC_FREQ_PRECISION_CACHED, &clk_src_hz)); @@ -226,7 +224,7 @@ TEST_CASE("Can use termios for UART", "[vfs]") const int uart_fd = open("/dev/uart/1", O_RDWR); TEST_ASSERT_NOT_EQUAL_MESSAGE(uart_fd, -1, "Cannot open UART"); - esp_vfs_dev_uart_use_driver(1); + uart_vfs_dev_use_driver(1); TEST_ASSERT_EQUAL(-1, tcgetattr(uart_fd, NULL)); TEST_ASSERT_EQUAL(EINVAL, errno); @@ -343,7 +341,7 @@ TEST_CASE("Can use termios for UART", "[vfs]") memset(&tios_result, 0xFF, sizeof(struct termios)); } - esp_vfs_dev_uart_use_nonblocking(1); + uart_vfs_dev_use_nonblocking(1); close(uart_fd); uart_driver_delete(UART_NUM_1); } diff --git a/components/esp_driver_uart/test_apps/uart_vfs/partitions.csv b/components/esp_driver_uart/test_apps/uart_vfs/partitions.csv new file mode 100644 index 0000000000..b31d4e80fd --- /dev/null +++ b/components/esp_driver_uart/test_apps/uart_vfs/partitions.csv @@ -0,0 +1,5 @@ +# Name, Type, SubType, Offset, Size, Flags +# Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap +nvs, data, nvs, 0x9000, 0x6000, +factory, 0, 0, 0x10000, 1M +flash_test, data, fat, , 528K diff --git a/components/esp_driver_uart/test_apps/uart_vfs/pytest_uart_vfs.py b/components/esp_driver_uart/test_apps/uart_vfs/pytest_uart_vfs.py new file mode 100644 index 0000000000..131d8c9423 --- /dev/null +++ b/components/esp_driver_uart/test_apps/uart_vfs/pytest_uart_vfs.py @@ -0,0 +1,24 @@ +# SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD +# SPDX-License-Identifier: CC0-1.0 + +import pytest +from pytest_embedded import Dut + + +@pytest.mark.supported_targets +@pytest.mark.temp_skip_ci(targets=['esp32s3'], reason='skip due to duplication with test_uart_vfs_psram') +@pytest.mark.generic +@pytest.mark.parametrize('config', [ + 'default', 'iram', +], indirect=True) +def test_uart_vfs_default(dut: Dut) -> None: + dut.run_all_single_board_cases() + + +@pytest.mark.esp32s3 +@pytest.mark.quad_psram +@pytest.mark.parametrize('config', [ + 'default', 'iram', +], indirect=True) +def test_uart_vfs_psram(dut: Dut) -> None: + dut.run_all_single_board_cases() diff --git a/components/esp_driver_uart/test_apps/uart_vfs/sdkconfig.ci.default b/components/esp_driver_uart/test_apps/uart_vfs/sdkconfig.ci.default new file mode 100644 index 0000000000..e69de29bb2 diff --git a/components/esp_driver_uart/test_apps/uart_vfs/sdkconfig.ci.iram b/components/esp_driver_uart/test_apps/uart_vfs/sdkconfig.ci.iram new file mode 100644 index 0000000000..328d7f113c --- /dev/null +++ b/components/esp_driver_uart/test_apps/uart_vfs/sdkconfig.ci.iram @@ -0,0 +1 @@ +CONFIG_UART_ISR_IN_IRAM=y diff --git a/components/esp_driver_uart/test_apps/uart_vfs/sdkconfig.defaults b/components/esp_driver_uart/test_apps/uart_vfs/sdkconfig.defaults new file mode 100644 index 0000000000..728b21c79f --- /dev/null +++ b/components/esp_driver_uart/test_apps/uart_vfs/sdkconfig.defaults @@ -0,0 +1,9 @@ +# Enable Unity fixture support +CONFIG_UNITY_ENABLE_FIXTURE=n +CONFIG_UNITY_ENABLE_IDF_TEST_RUNNER=y + +# Custom partition table for this test app +CONFIG_PARTITION_TABLE_CUSTOM=y +CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv" + +CONFIG_ESP_TASK_WDT_INIT=n diff --git a/components/esp_driver_uart/test_apps/uart_vfs/sdkconfig.defaults.esp32s3 b/components/esp_driver_uart/test_apps/uart_vfs/sdkconfig.defaults.esp32s3 new file mode 100644 index 0000000000..e831880d07 --- /dev/null +++ b/components/esp_driver_uart/test_apps/uart_vfs/sdkconfig.defaults.esp32s3 @@ -0,0 +1,3 @@ +CONFIG_SPIRAM=y +CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL=0 +CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ_240=y diff --git a/components/esp_system/startup_funcs.c b/components/esp_system/startup_funcs.c index 8997ab6634..85cb9eee4b 100644 --- a/components/esp_system/startup_funcs.c +++ b/components/esp_system/startup_funcs.c @@ -52,11 +52,6 @@ #include "esp_private/pm_impl.h" #endif -#if CONFIG_VFS_SUPPORT_IO -#include "esp_vfs_dev.h" -#include "esp_vfs_console.h" -#endif - #include "esp_pthread.h" #include "esp_private/esp_clk.h" #include "esp_private/spi_flash_os.h" @@ -135,12 +130,6 @@ ESP_SYSTEM_INIT_FN(init_timer, CORE, BIT(0), 101) return ESP_OK; } -ESP_SYSTEM_INIT_FN(init_newlib, CORE, BIT(0), 102) -{ - esp_newlib_init(); - return ESP_OK; -} - ESP_SYSTEM_INIT_FN(init_psram_heap, CORE, BIT(0), 103) { #if CONFIG_SPIRAM_BOOT_INIT && (CONFIG_SPIRAM_USE_CAPS_ALLOC || CONFIG_SPIRAM_USE_MALLOC) @@ -174,24 +163,6 @@ ESP_SYSTEM_INIT_FN(init_newlib_time, CORE, BIT(0), 105) return ESP_OK; } -#if CONFIG_VFS_SUPPORT_IO -ESP_SYSTEM_INIT_FN(init_vfs_console, CORE, BIT(0), 110) -{ - // VFS console register. - return esp_vfs_console_register(); -} -#endif // CONFIG_VFS_SUPPORT_IO - -ESP_SYSTEM_INIT_FN(init_newlib_stdio, CORE, BIT(0), 115) -{ -#if defined(CONFIG_VFS_SUPPORT_IO) && !defined(CONFIG_ESP_CONSOLE_NONE) - esp_newlib_init_global_stdio(ESP_VFS_DEV_CONSOLE); -#else - esp_newlib_init_global_stdio(NULL); -#endif - return ESP_OK; -} - ESP_SYSTEM_INIT_FN(init_pthread, CORE, BIT(0), 120) { return esp_pthread_init(); diff --git a/components/esp_system/system_init_fn.txt b/components/esp_system/system_init_fn.txt index 3970de39f0..921bb5cbd4 100644 --- a/components/esp_system/system_init_fn.txt +++ b/components/esp_system/system_init_fn.txt @@ -37,12 +37,17 @@ CORE: 100: init_heap in components/esp_system/startup_funcs.c on BIT(0) # esp_timer early initialization is required for esp_timer_get_time to work. CORE: 101: init_timer in components/esp_system/startup_funcs.c on BIT(0) -CORE: 102: init_newlib in components/esp_system/startup_funcs.c on BIT(0) +CORE: 102: init_newlib in components/newlib/newlib_init.c on BIT(0) CORE: 103: init_psram_heap in components/esp_system/startup_funcs.c on BIT(0) CORE: 104: init_brownout in components/esp_system/startup_funcs.c on BIT(0) CORE: 105: init_newlib_time in components/esp_system/startup_funcs.c on BIT(0) -CORE: 110: init_vfs_console in components/esp_system/startup_funcs.c on BIT(0) -CORE: 115: init_newlib_stdio in components/esp_system/startup_funcs.c on BIT(0) + +# Peripheral-specific implementation operators should be filled first +# Then register vfs console, and follow by newlib stdio initialization +CORE: 110: init_vfs_uart in components/esp_driver_uart/src/uart_vfs.c on BIT(0) +CORE: 114: init_vfs_console in components/vfs/vfs_console.c on BIT(0) +CORE: 115: init_newlib_stdio in components/newlib/newlib_init.c on BIT(0) + CORE: 120: init_pthread in components/esp_system/startup_funcs.c on BIT(0) CORE: 130: init_flash in components/esp_system/startup_funcs.c on BIT(0) CORE: 140: init_virtual_efuse in components/esp_system/startup_funcs.c on BIT(0) diff --git a/components/newlib/CMakeLists.txt b/components/newlib/CMakeLists.txt index 9f30c6db68..a74220bb7b 100644 --- a/components/newlib/CMakeLists.txt +++ b/components/newlib/CMakeLists.txt @@ -55,6 +55,9 @@ list(APPEND EXTRA_LINK_FLAGS "-u newlib_include_pthread_impl") list(APPEND EXTRA_LINK_FLAGS "-u newlib_include_assert_impl") target_link_libraries(${COMPONENT_LIB} INTERFACE "${EXTRA_LINK_FLAGS}") +# Forces the linker to include newlib_init.c +target_link_libraries(${COMPONENT_LIB} INTERFACE "-u newlib_include_init_funcs") + if(CONFIG_NEWLIB_NANO_FORMAT) if(CMAKE_C_COMPILER_ID MATCHES "Clang") set(libc_dir_cmd ${CMAKE_C_COMPILER}) diff --git a/components/newlib/newlib_init.c b/components/newlib/newlib_init.c index 2c9b798c74..8072665a8a 100644 --- a/components/newlib/newlib_init.c +++ b/components/newlib/newlib_init.c @@ -20,6 +20,7 @@ #include "esp_attr.h" #include "soc/soc_caps.h" #include "esp_rom_caps.h" +#include "esp_private/startup_internal.h" #if CONFIG_IDF_TARGET_ESP32 #include "esp32/rom/libc_stubs.h" @@ -176,8 +177,22 @@ void esp_newlib_init(void) esp_newlib_locks_init(); } +ESP_SYSTEM_INIT_FN(init_newlib, CORE, BIT(0), 102) +{ + esp_newlib_init(); + return ESP_OK; +} + void esp_setup_newlib_syscalls(void) __attribute__((alias("esp_newlib_init"))); +/** + * Postponed _GLOBAL_REENT stdio FPs initialization. + * + * Can not be a part of esp_reent_init() because stdio device may not initialized yet. + * + * Called from startup code and FreeRTOS, not intended to be called from + * application code. + */ void esp_newlib_init_global_stdio(const char *stdio_dev) { if (stdio_dev == NULL) @@ -207,3 +222,18 @@ void esp_newlib_init_global_stdio(const char *stdio_dev) #endif /* ESP_ROM_NEEDS_SWSETUP_WORKAROUND */ } } + +ESP_SYSTEM_INIT_FN(init_newlib_stdio, CORE, BIT(0), 115) +{ +#if defined(CONFIG_VFS_SUPPORT_IO) && !defined(CONFIG_ESP_CONSOLE_NONE) + esp_newlib_init_global_stdio("/dev/console"); +#else + esp_newlib_init_global_stdio(NULL); +#endif + return ESP_OK; +} + +// Hook to force the linker to include this file +void newlib_include_init_funcs(void) +{ +} diff --git a/components/openthread/src/port/esp_openthread_uart.c b/components/openthread/src/port/esp_openthread_uart.c index 9c8ef64cde..cd5fe9d61c 100644 --- a/components/openthread/src/port/esp_openthread_uart.c +++ b/components/openthread/src/port/esp_openthread_uart.c @@ -20,6 +20,7 @@ #include "esp_vfs_dev.h" #include "common/logging.hpp" #include "driver/uart.h" +#include "driver/uart_vfs.h" #include "utils/uart.h" #include "esp_vfs_usb_serial_jtag.h" #include "driver/usb_serial_jtag.h" @@ -68,7 +69,7 @@ esp_err_t esp_openthread_uart_init_port(const esp_openthread_uart_config_t *conf OT_PLAT_LOG_TAG, "uart_set_pin failed"); ESP_RETURN_ON_ERROR(uart_driver_install(config->port, ESP_OPENTHREAD_UART_BUFFER_SIZE, 0, 0, NULL, 0), OT_PLAT_LOG_TAG, "uart_driver_install failed"); - esp_vfs_dev_uart_use_driver(config->port); + uart_vfs_dev_use_driver(config->port); return ESP_OK; } @@ -90,7 +91,7 @@ esp_err_t esp_openthread_host_cli_usb_init(const esp_openthread_platform_config_ ret = usb_serial_jtag_driver_install((usb_serial_jtag_driver_config_t *)&config->host_config.host_usb_config); esp_vfs_usb_serial_jtag_use_driver(); - esp_vfs_dev_uart_register(); + uart_vfs_dev_register(); return ret; } #endif @@ -113,8 +114,8 @@ esp_err_t esp_openthread_host_rcp_uart_init(const esp_openthread_platform_config ESP_RETURN_ON_ERROR(esp_openthread_uart_init_port(&config->host_config.host_uart_config), OT_PLAT_LOG_TAG, "esp_openthread_uart_init_port failed"); - esp_vfs_dev_uart_port_set_rx_line_endings(s_uart_port, ESP_LINE_ENDINGS_LF); - esp_vfs_dev_uart_port_set_tx_line_endings(s_uart_port, ESP_LINE_ENDINGS_LF); + uart_vfs_dev_port_set_rx_line_endings(s_uart_port, ESP_LINE_ENDINGS_LF); + uart_vfs_dev_port_set_tx_line_endings(s_uart_port, ESP_LINE_ENDINGS_LF); snprintf(uart_path, sizeof(uart_path), "/dev/uart/%d", s_uart_port); s_uart_fd = open(uart_path, O_RDWR | O_NONBLOCK); ESP_RETURN_ON_FALSE(s_uart_fd >= 0, ESP_FAIL, OT_PLAT_LOG_TAG, "open uart_path failed"); diff --git a/components/openthread/src/port/esp_uart_spinel_interface.cpp b/components/openthread/src/port/esp_uart_spinel_interface.cpp index ec07bd6d80..6e795b6f5f 100644 --- a/components/openthread/src/port/esp_uart_spinel_interface.cpp +++ b/components/openthread/src/port/esp_uart_spinel_interface.cpp @@ -17,7 +17,7 @@ #include "esp_openthread_common_macro.h" #include "esp_openthread_types.h" #include "esp_openthread_uart.h" -#include "esp_vfs_dev.h" +#include "driver/uart_vfs.h" #include "core/common/code_utils.hpp" #include "core/common/logging.hpp" #include "driver/uart.h" @@ -265,8 +265,8 @@ esp_err_t UartSpinelInterface::InitUart(const esp_openthread_uart_config_t &radi ESP_RETURN_ON_ERROR(esp_openthread_uart_init_port(&radio_uart_config), OT_PLAT_LOG_TAG, "esp_openthread_uart_init_port failed"); // We have a driver now installed so set up the read/write functions to use driver also. - esp_vfs_dev_uart_port_set_tx_line_endings(m_uart_config.port, ESP_LINE_ENDINGS_LF); - esp_vfs_dev_uart_port_set_rx_line_endings(m_uart_config.port, ESP_LINE_ENDINGS_LF); + uart_vfs_dev_port_set_tx_line_endings(m_uart_config.port, ESP_LINE_ENDINGS_LF); + uart_vfs_dev_port_set_rx_line_endings(m_uart_config.port, ESP_LINE_ENDINGS_LF); snprintf(uart_path, sizeof(uart_path), "/dev/uart/%d", radio_uart_config.port); m_uart_fd = open(uart_path, O_RDWR | O_NONBLOCK); diff --git a/components/vfs/CMakeLists.txt b/components/vfs/CMakeLists.txt index f46a303948..1a090fa1e9 100644 --- a/components/vfs/CMakeLists.txt +++ b/components/vfs/CMakeLists.txt @@ -6,13 +6,15 @@ endif() list(APPEND sources "vfs.c" "vfs_eventfd.c" - "vfs_uart.c" "vfs_semihost.c" - "vfs_console.c") + "vfs_console.c" + ) list(APPEND pr driver + esp_timer + # for backwards compatibility (TODO: IDF-8799) esp_driver_uart - esp_timer) + ) idf_component_register(SRCS ${sources} LDFRAGMENTS "linker.lf" @@ -31,3 +33,8 @@ endif() # Some newlib syscalls are implemented in vfs.c, make sure these are always # seen by the linker target_link_libraries(${COMPONENT_LIB} INTERFACE "-u vfs_include_syscalls_impl") + +if(CONFIG_VFS_SUPPORT_IO) + # Make sure esp_vfs_console_register gets called at startup stage + target_link_libraries(${COMPONENT_LIB} INTERFACE "-u esp_vfs_include_console_register") +endif() diff --git a/components/vfs/include/esp_private/esp_vfs_console.h b/components/vfs/include/esp_private/esp_vfs_console.h new file mode 100644 index 0000000000..4196c451f3 --- /dev/null +++ b/components/vfs/include/esp_private/esp_vfs_console.h @@ -0,0 +1,41 @@ + +/* + * SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include "sdkconfig.h" +#include "esp_vfs.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#if CONFIG_VFS_SUPPORT_IO +/** + * @brief Set the pointer of primary dev vfs. + * + * This function is called in each vfs dev driver as a system initialization function registered via ESP_SYSTEM_INIT_FN + * + * @param vfs pointer to structure esp_vfs_t + */ +void esp_vfs_set_primary_dev_vfs_def_struct(const esp_vfs_t *vfs); + +#if CONFIG_ESP_CONSOLE_SECONDARY_USB_SERIAL_JTAG +/** + * @brief Set the pointer of secondary dev vfs. + * + * This function is called in each vfs dev driver as a system initialization function registered via ESP_SYSTEM_INIT_FN + * + * @param vfs pointer to structure esp_vfs_t + */ +void esp_vfs_set_secondary_dev_vfs_def_struct(const esp_vfs_t *vfs); +#endif //CONFIG_ESP_CONSOLE_SECONDARY_USB_SERIAL_JTAG +#endif // CONFIG_VFS_SUPPORT_IO + +#ifdef __cplusplus +} +#endif diff --git a/components/vfs/include/esp_vfs_console.h b/components/vfs/include/esp_vfs_console.h index 3ac6e31add..4b24a53722 100644 --- a/components/vfs/include/esp_vfs_console.h +++ b/components/vfs/include/esp_vfs_console.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -8,12 +8,12 @@ #include "esp_err.h" -#define ESP_VFS_DEV_CONSOLE "/dev/console" - #ifdef __cplusplus extern "C" { #endif +#define ESP_VFS_DEV_CONSOLE "/dev/console" + /** * @brief add uart/usb_serial_jtag/usb_otg_acmcdc virtual filesystem driver * diff --git a/components/vfs/include/esp_vfs_dev.h b/components/vfs/include/esp_vfs_dev.h index 6eed6afc87..a6513ca27f 100644 --- a/components/vfs/include/esp_vfs_dev.h +++ b/components/vfs/include/esp_vfs_dev.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -13,12 +13,15 @@ extern "C" { #endif -/** - * @brief add /dev/uart virtual filesystem driver - * - * This function is called from startup code to enable serial output - */ -void esp_vfs_dev_uart_register(void); +void esp_vfs_dev_uart_register(void) __attribute__((deprecated("Please use uart_vfs_dev_register() instead"))); + +void esp_vfs_dev_uart_use_nonblocking(int uart_num) __attribute__((deprecated("Please use uart_vfs_dev_use_nonblocking() instead"))); + +void esp_vfs_dev_uart_use_driver(int uart_num) __attribute__((deprecated("Please use uart_vfs_dev_use_driver() instead"))); + +int esp_vfs_dev_uart_port_set_rx_line_endings(int uart_num, esp_line_endings_t mode) __attribute__((deprecated("Please use uart_vfs_dev_port_set_rx_line_endings() instead"))); + +int esp_vfs_dev_uart_port_set_tx_line_endings(int uart_num, esp_line_endings_t mode) __attribute__((deprecated("Please use uart_vfs_dev_port_set_tx_line_endings() instead"))); /** * @brief Set the line endings expected to be received on UART @@ -34,7 +37,7 @@ void esp_vfs_dev_uart_register(void); * * @param mode line endings expected on UART */ -void esp_vfs_dev_uart_set_rx_line_endings(esp_line_endings_t mode) __attribute__((deprecated("Please use esp_vfs_dev_uart_port_set_rx_line_endings"))); +void esp_vfs_dev_uart_set_rx_line_endings(esp_line_endings_t mode) __attribute__((deprecated("Please use uart_vfs_dev_port_set_rx_line_endings() instead"))); /** * @brief Set the line endings to sent to UART @@ -50,61 +53,7 @@ void esp_vfs_dev_uart_set_rx_line_endings(esp_line_endings_t mode) __attribute__ * * @param mode line endings to send to UART */ -void esp_vfs_dev_uart_set_tx_line_endings(esp_line_endings_t mode) __attribute__((deprecated("Please use esp_vfs_dev_uart_port_set_tx_line_endings"))); - -/** - * @brief Set the line endings expected to be received on specified UART - * - * This specifies the conversion between line endings received on UART and - * newlines ('\n', LF) passed into stdin: - * - * - ESP_LINE_ENDINGS_CRLF: convert CRLF to LF - * - ESP_LINE_ENDINGS_CR: convert CR to LF - * - ESP_LINE_ENDINGS_LF: no modification - * - * @note this function is not thread safe w.r.t. reading from UART - * - * @param uart_num the UART number - * @param mode line endings to send to UART - * @return 0 if successed, or -1 - * when an error (specified by errno) have occurred. - */ -int esp_vfs_dev_uart_port_set_rx_line_endings(int uart_num, esp_line_endings_t mode); - -/** - * @brief Set the line endings to sent to specified UART - * - * This specifies the conversion between newlines ('\n', LF) on stdout and line - * endings sent over UART: - * - * - ESP_LINE_ENDINGS_CRLF: convert LF to CRLF - * - ESP_LINE_ENDINGS_CR: convert LF to CR - * - ESP_LINE_ENDINGS_LF: no modification - * - * @note this function is not thread safe w.r.t. writing to UART - * - * @param uart_num the UART number - * @param mode line endings to send to UART - * @return 0 if successed, or -1 - * when an error (specified by errno) have occurred. - */ -int esp_vfs_dev_uart_port_set_tx_line_endings(int uart_num, esp_line_endings_t mode); - -/** - * @brief set VFS to use simple functions for reading and writing UART - * Read is non-blocking, write is busy waiting until TX FIFO has enough space. - * These functions are used by default. - * @param uart_num UART peripheral number - */ -void esp_vfs_dev_uart_use_nonblocking(int uart_num); - -/** - * @brief set VFS to use UART driver for reading and writing - * @note application must configure UART driver before calling these functions - * With these functions, read and write are blocking and interrupt-driven. - * @param uart_num UART peripheral number - */ -void esp_vfs_dev_uart_use_driver(int uart_num); +void esp_vfs_dev_uart_set_tx_line_endings(esp_line_endings_t mode) __attribute__((deprecated("Please use uart_vfs_dev_port_set_tx_line_endings() instead"))); /** * @brief set VFS to use USB-SERIAL-JTAG driver for reading and writing diff --git a/components/vfs/linker.lf b/components/vfs/linker.lf index 9c8e69b5f7..90d1d43f20 100644 --- a/components/vfs/linker.lf +++ b/components/vfs/linker.lf @@ -3,4 +3,3 @@ archive: libvfs.a entries: if VFS_SELECT_IN_RAM = y: vfs:esp_vfs_select_triggered_isr (noflash) - vfs_uart:select_notif_callback_isr (noflash) diff --git a/components/vfs/private_include/esp_vfs_private.h b/components/vfs/private_include/esp_vfs_private.h index 0651dc71f3..8122717314 100644 --- a/components/vfs/private_include/esp_vfs_private.h +++ b/components/vfs/private_include/esp_vfs_private.h @@ -13,7 +13,7 @@ extern "C" { #endif #if CONFIG_VFS_SELECT_IN_RAM -#define VFS_MALLOC_FLAGS MALLOC_CAP_INTERNAL +#define VFS_MALLOC_FLAGS (MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT) #else #define VFS_MALLOC_FLAGS MALLOC_CAP_DEFAULT #endif @@ -26,17 +26,6 @@ typedef struct vfs_entry_ { int offset; // index of this structure in s_vfs array } vfs_entry_t; - -/** - * @brief get pointer of uart vfs. - * - * This function is called in vfs_console in order to get the vfs implementation - * of uart. - * - * @return pointer to structure esp_vfs_t - */ -const esp_vfs_t *esp_vfs_uart_get_vfs(void); - /** * @brief get pointer of cdcacm vfs. * diff --git a/components/vfs/test_apps/main/CMakeLists.txt b/components/vfs/test_apps/main/CMakeLists.txt index efc6105105..f9a5e774c9 100644 --- a/components/vfs/test_apps/main/CMakeLists.txt +++ b/components/vfs/test_apps/main/CMakeLists.txt @@ -2,11 +2,12 @@ set(src "test_app_main.c" "test_vfs_access.c" "test_vfs_append.c" "test_vfs_eventfd.c" "test_vfs_fd.c" "test_vfs_lwip.c" "test_vfs_open.c" "test_vfs_paths.c" - "test_vfs_select.c" "test_vfs_uart.c" + "test_vfs_select.c" ) idf_component_register(SRCS ${src} PRIV_INCLUDE_DIRS . - PRIV_REQUIRES test_utils vfs fatfs spiffs unity lwip wear_levelling cmock driver + PRIV_REQUIRES test_utils vfs fatfs spiffs unity lwip wear_levelling cmock + esp_driver_gptimer esp_driver_uart WHOLE_ARCHIVE ) diff --git a/components/vfs/test_apps/main/test_vfs_select.c b/components/vfs/test_apps/main/test_vfs_select.c index 3e8c04596d..80f7720cca 100644 --- a/components/vfs/test_apps/main/test_vfs_select.c +++ b/components/vfs/test_apps/main/test_vfs_select.c @@ -10,10 +10,9 @@ #include #include "unity.h" #include "freertos/FreeRTOS.h" -#include "soc/uart_struct.h" #include "driver/uart.h" #include "esp_vfs.h" -#include "esp_vfs_dev.h" +#include "driver/uart_vfs.h" #include "esp_vfs_fat.h" #include "lwip/sockets.h" #include "lwip/netdb.h" @@ -144,14 +143,14 @@ static void init(int *uart_fd, int *socket_fd) *uart_fd = open("/dev/uart/1", O_RDWR); TEST_ASSERT_NOT_EQUAL_MESSAGE(*uart_fd, -1, "Cannot open UART"); - esp_vfs_dev_uart_use_driver(1); + uart_vfs_dev_use_driver(1); *socket_fd = socket_init(); } static void deinit(int uart_fd, int socket_fd) { - esp_vfs_dev_uart_use_nonblocking(1); + uart_vfs_dev_use_nonblocking(1); close(uart_fd); uart_driver_delete(UART_NUM_1); diff --git a/components/vfs/vfs.c b/components/vfs/vfs.c index ea0564dabf..ef66a54323 100644 --- a/components/vfs/vfs.c +++ b/components/vfs/vfs.c @@ -1381,5 +1381,5 @@ void rewinddir(DIR* pdir) void vfs_include_syscalls_impl(void) { - // Linker hook function, exists to make the linker examine this fine + // Linker hook function, exists to make the linker examine this file } diff --git a/components/vfs/vfs_console.c b/components/vfs/vfs_console.c index 1360be33cc..336feed706 100644 --- a/components/vfs/vfs_console.c +++ b/components/vfs/vfs_console.c @@ -1,24 +1,26 @@ /* - * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ -#include "esp_vfs_console.h" +#include "esp_err.h" #include "esp_rom_sys.h" #include "esp_vfs_cdcacm.h" #include "esp_vfs_private.h" #include "esp_vfs_usb_serial_jtag.h" -#include "esp_vfs_dev.h" #include "esp_private/usb_console.h" +#include "esp_vfs_console.h" +#include "esp_private/esp_vfs_console.h" #include "sdkconfig.h" +#include "esp_private/startup_internal.h" #define STRINGIFY(s) STRINGIFY2(s) #define STRINGIFY2(s) #s /** * This file is to concentrate all the vfs(UART, USB_SERIAL_JTAG, CDCACM) console into one single file. - * Get the vfs information from their component (i.e. vfs_uart.c) through `esp_vfs_usb_xxx_get_console()`, + * Get the vfs information from their component (i.e. uart_vfs.c), * which can help us to output some string to two different ports(i.e both through uart and usb_serial_jtag). * Usually, we set a port as primary and another as secondary. For primary, it is used for all the features supported by each vfs implementation, * while the secondary is only used for output. @@ -43,9 +45,11 @@ const static char *primary_path = "/dev/cdcacm"; #if CONFIG_ESP_CONSOLE_SECONDARY_USB_SERIAL_JTAG const static char *secondary_path = "/dev/secondary"; static int secondary_vfs_index; +const static esp_vfs_t *secondary_vfs = NULL; #endif // Secondary part static int primary_vfs_index; +const static esp_vfs_t *primary_vfs = NULL; static vfs_console_context_t vfs_console= {0}; @@ -186,7 +190,7 @@ static const esp_vfs_t vfs = { #endif // CONFIG_VFS_SUPPORT_TERMIOS }; -esp_err_t esp_vfs_dev_console_register(void) +static esp_err_t esp_vfs_dev_console_register(void) { return esp_vfs_register(ESP_VFS_DEV_CONSOLE, &vfs, NULL); } @@ -196,27 +200,25 @@ esp_err_t esp_vfs_console_register(void) esp_err_t err = ESP_OK; // Primary register part. #ifdef CONFIG_ESP_CONSOLE_UART - const esp_vfs_t *uart_vfs = esp_vfs_uart_get_vfs(); - err = esp_vfs_register_common(primary_path, strlen(primary_path), uart_vfs, NULL, &primary_vfs_index); + assert(primary_vfs); #elif CONFIG_ESP_CONSOLE_USB_CDC - const esp_vfs_t *cdcacm_vfs = esp_vfs_cdcacm_get_vfs(); + primary_vfs = esp_vfs_cdcacm_get_vfs(); err = esp_usb_console_init(); if (err != ESP_OK) { return err; } - err = esp_vfs_register_common(primary_path, strlen(primary_path), cdcacm_vfs, NULL, &primary_vfs_index); #elif CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG - const esp_vfs_t *usb_serial_jtag_vfs = esp_vfs_usb_serial_jtag_get_vfs(); - err = esp_vfs_register_common(primary_path, strlen(primary_path), usb_serial_jtag_vfs, NULL, &primary_vfs_index); + primary_vfs = esp_vfs_usb_serial_jtag_get_vfs(); #endif // CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG + err = esp_vfs_register_common(primary_path, strlen(primary_path), primary_vfs, NULL, &primary_vfs_index); if (err != ESP_OK) { return err; } // Secondary register part. #if CONFIG_ESP_CONSOLE_SECONDARY_USB_SERIAL_JTAG - const esp_vfs_t *usb_serial_jtag_vfs = esp_vfs_usb_serial_jtag_get_vfs(); - err = esp_vfs_register_common(secondary_path, strlen(secondary_path), usb_serial_jtag_vfs, NULL, &secondary_vfs_index); + secondary_vfs = esp_vfs_usb_serial_jtag_get_vfs(); + err = esp_vfs_register_common(secondary_path, strlen(secondary_path), secondary_vfs, NULL, &secondary_vfs_index); if(err != ESP_OK) { return err; } @@ -225,4 +227,26 @@ esp_err_t esp_vfs_console_register(void) return err; } +void esp_vfs_set_primary_dev_vfs_def_struct(const esp_vfs_t *vfs) +{ + primary_vfs = vfs; +} + +#if CONFIG_ESP_CONSOLE_SECONDARY_USB_SERIAL_JTAG +void esp_vfs_set_secondary_dev_vfs_def_struct(const esp_vfs_t *vfs) +{ + secondary_vfs = vfs; +} +#endif + +ESP_SYSTEM_INIT_FN(init_vfs_console, CORE, BIT(0), 114) +{ + return esp_vfs_console_register(); +} + #endif // CONFIG_VFS_SUPPORT_IO + +void esp_vfs_include_console_register(void) +{ + // Linker hook function, exists to make the linker examine this file +} diff --git a/docs/doxygen/Doxyfile b/docs/doxygen/Doxyfile index 035d5fa498..d92b07e169 100644 --- a/docs/doxygen/Doxyfile +++ b/docs/doxygen/Doxyfile @@ -143,6 +143,7 @@ INPUT = \ $(PROJECT_PATH)/components/esp_driver_spi/include/driver/spi_slave.h \ $(PROJECT_PATH)/components/esp_driver_tsens/include/driver/temperature_sensor.h \ $(PROJECT_PATH)/components/esp_driver_uart/include/driver/uart.h \ + $(PROJECT_PATH)/components/esp_driver_uart/include/driver/uart_vfs.h \ $(PROJECT_PATH)/components/esp_eth/include/esp_eth_com.h \ $(PROJECT_PATH)/components/esp_eth/include/esp_eth_driver.h \ $(PROJECT_PATH)/components/esp_eth/include/esp_eth_mac.h \ diff --git a/docs/en/api-reference/storage/vfs.rst b/docs/en/api-reference/storage/vfs.rst index d7a74c92ab..c50527716d 100644 --- a/docs/en/api-reference/storage/vfs.rst +++ b/docs/en/api-reference/storage/vfs.rst @@ -104,7 +104,7 @@ If you want to use :cpp:func:`select` with a file descriptor belonging to a non- :cpp:func:`end_select` might be called without a previous :cpp:func:`start_select` call in some rare circumstances. :cpp:func:`end_select` should fail gracefully if this is the case (i.e., should not crash but return an error instead). -Please refer to the reference implementation for the UART peripheral in :component_file:`vfs/vfs_uart.c` and most particularly to the functions :cpp:func:`esp_vfs_dev_uart_register`, :cpp:func:`uart_start_select`, and :cpp:func:`uart_end_select` for more information. +Please refer to the reference implementation for the UART peripheral in :component_file:`esp_driver_uart/src/uart_vfs.c` and most particularly to the functions :cpp:func:`uart_vfs_dev_register`, :cpp:func:`uart_start_select`, and :cpp:func:`uart_end_select` for more information. Please check the following examples that demonstrate the use of :cpp:func:`select` with VFS file descriptors: @@ -189,9 +189,9 @@ Writing to ``stdout`` or ``stderr`` sends characters to the UART transmit FIFO. By default, VFS uses simple functions for reading from and writing to UART. Writes busy-wait until all data is put into UART FIFO, and reads are non-blocking, returning only the data present in the FIFO. Due to this non-blocking read behavior, higher level C library calls, such as ``fscanf("%d\n", &var);``, might not have desired results. -Applications which use the UART driver can instruct VFS to use the driver's interrupt driven, blocking read and write functions instead. This can be done using a call to the ``esp_vfs_dev_uart_use_driver`` function. It is also possible to revert to the basic non-blocking functions using a call to ``esp_vfs_dev_uart_use_nonblocking``. +Applications which use the UART driver can instruct VFS to use the driver's interrupt driven, blocking read and write functions instead. This can be done using a call to the :cpp:func:`uart_vfs_dev_use_driver` function. It is also possible to revert to the basic non-blocking functions using a call to :cpp:func:`uart_vfs_dev_use_nonblocking`. -VFS also provides an optional newline conversion feature for input and output. Internally, most applications send and receive lines terminated by the LF (''\n'') character. Different terminal programs may require different line termination, such as CR or CRLF. Applications can configure this separately for input and output either via menuconfig, or by calls to the functions ``esp_vfs_dev_uart_port_set_rx_line_endings`` and ``esp_vfs_dev_uart_port_set_tx_line_endings``. +VFS also provides an optional newline conversion feature for input and output. Internally, most applications send and receive lines terminated by the LF (''\n'') character. Different terminal programs may require different line termination, such as CR or CRLF. Applications can configure this separately for input and output either via menuconfig, or by calls to the functions :cpp:func:`uart_vfs_dev_port_set_rx_line_endings` and :cpp:func:`uart_vfs_dev_port_set_tx_line_endings`. Standard Streams and FreeRTOS Tasks @@ -235,4 +235,6 @@ API Reference .. include-build-file:: inc/esp_vfs_dev.inc +.. include-build-file:: inc/uart_vfs.inc + .. include-build-file:: inc/esp_vfs_eventfd.inc diff --git a/docs/en/migration-guides/release-5.x/5.3/index.rst b/docs/en/migration-guides/release-5.x/5.3/index.rst index b4717423b3..eb01f88b11 100644 --- a/docs/en/migration-guides/release-5.x/5.3/index.rst +++ b/docs/en/migration-guides/release-5.x/5.3/index.rst @@ -7,4 +7,5 @@ Migration from 5.2 to 5.3 :maxdepth: 1 peripherals + storage system diff --git a/docs/en/migration-guides/release-5.x/5.3/storage.rst b/docs/en/migration-guides/release-5.x/5.3/storage.rst new file mode 100644 index 0000000000..77b01c0b74 --- /dev/null +++ b/docs/en/migration-guides/release-5.x/5.3/storage.rst @@ -0,0 +1,18 @@ +Storage +======= + +:link_to_translation:`zh_CN:[中文]` + +VFS +--- + +The UART implementation of VFS operators has been moved from `vfs` component to `esp_driver_uart` component. + +APIs with `esp_vfs_dev_uart_` prefix are all deprecated, replaced with new APIs in `uart_vfs.h` starting with `uart_vfs_dev_` prefix. Specifically, +- ``esp_vfs_dev_uart_register`` has been renamed to ``uart_vfs_dev_register`` +- ``esp_vfs_dev_uart_port_set_rx_line_endings`` has been renamed to ``uart_vfs_dev_port_set_rx_line_endings`` +- ``esp_vfs_dev_uart_port_set_tx_line_endings`` has been renamed to ``uart_vfs_dev_port_set_tx_line_endings`` +- ``esp_vfs_dev_uart_use_nonblocking`` has been renamed to ``uart_vfs_dev_use_nonblocking`` +- ``esp_vfs_dev_uart_use_driver`` has been renamed to ``uart_vfs_dev_use_driver`` + +For compatibility, `vfs` component still registers `esp_driver_uart` as its private dependency. In other words, you do not need to modify the CMake file of an existing project. diff --git a/docs/zh_CN/api-reference/storage/vfs.rst b/docs/zh_CN/api-reference/storage/vfs.rst index 5e29685fad..b27bbd3420 100644 --- a/docs/zh_CN/api-reference/storage/vfs.rst +++ b/docs/zh_CN/api-reference/storage/vfs.rst @@ -103,7 +103,7 @@ VFS 组件支持通过 :cpp:func:`select` 进行同步输入/输出多路复用 .. note:: 在少数情况下,在调用 :cpp:func:`end_select` 之前可能并没有调用过 :cpp:func:`start_select`。因此 :cpp:func:`end_select` 的实现必须在该情况下返回错误而不能崩溃。 -如需获取更多信息,请参考 :component_file:`vfs/vfs_uart.c` 中 UART 外设的 VFS 驱动,尤其是函数 :cpp:func:`esp_vfs_dev_uart_register`、:cpp:func:`uart_start_select` 和 :cpp:func:`uart_end_select`。 +如需获取更多信息,请参考 :component_file:`esp_driver_uart/src/uart_vfs.c` 中 UART 外设的 VFS 驱动,尤其是函数 :cpp:func:`uart_vfs_dev_register`、:cpp:func:`uart_start_select` 和 :cpp:func:`uart_end_select`。 请参考以下示例,查看如何使用 VFS 文件描述符调用 :cpp:func:`select`: @@ -189,9 +189,9 @@ VFS 对文件路径长度没有限制,但文件系统路径前缀受 ``ESP_VFS 默认情况下,VFS 使用简单的函数对 UART 进行读写操作。在所有数据放进 UART FIFO 之前,写操作将处于 busy-wait 状态,读操处于非阻塞状态,仅返回 FIFO 中已有数据。由于读操作为非阻塞,高层级 C 库函数调用(如 ``fscanf("%d\n", &var);``)可能获取不到所需结果。 -如果应用程序使用 UART 驱动,则可以调用 ``esp_vfs_dev_uart_use_driver`` 函数来指导 VFS 使用驱动中断、读写阻塞功能等,也可以调用 ``esp_vfs_dev_uart_use_nonblocking`` 来恢复非阻塞函数。 +如果应用程序使用 UART 驱动,则可以调用 :cpp:func:`uart_vfs_dev_use_driver` 函数来指导 VFS 使用驱动中断、读写阻塞功能等,也可以调用 :cpp:func:`uart_vfs_dev_use_nonblocking` 来恢复非阻塞函数。 -VFS 还为输入和输出提供换行符转换功能(可选)。多数应用程序在程序内部发送或接收以 LF (''\n'') 结尾的行,但不同的终端程序可能需要不同的换行符,比如 CR 或 CRLF。应用程序可以通过 menuconfig 或者调用 ``esp_vfs_dev_uart_port_set_rx_line_endings`` 和 ``esp_vfs_dev_uart_port_set_tx_line_endings`` 为输入输出配置换行符。 +VFS 还为输入和输出提供换行符转换功能(可选)。多数应用程序在程序内部发送或接收以 LF (''\n'') 结尾的行,但不同的终端程序可能需要不同的换行符,比如 CR 或 CRLF。应用程序可以通过 menuconfig 或者调用 :cpp:func:`uart_vfs_dev_port_set_rx_line_endings` 和 :cpp:func:`uart_vfs_dev_port_set_tx_line_endings` 为输入输出配置换行符。 标准流和 FreeRTOS 任务 @@ -235,4 +235,6 @@ API 参考 .. include-build-file:: inc/esp_vfs_dev.inc +.. include-build-file:: inc/uart_vfs.inc + .. include-build-file:: inc/esp_vfs_eventfd.inc diff --git a/docs/zh_CN/migration-guides/release-5.x/5.3/index.rst b/docs/zh_CN/migration-guides/release-5.x/5.3/index.rst index 40d26176f4..50534a4be9 100644 --- a/docs/zh_CN/migration-guides/release-5.x/5.3/index.rst +++ b/docs/zh_CN/migration-guides/release-5.x/5.3/index.rst @@ -7,4 +7,5 @@ :maxdepth: 1 peripherals + storage system diff --git a/docs/zh_CN/migration-guides/release-5.x/5.3/storage.rst b/docs/zh_CN/migration-guides/release-5.x/5.3/storage.rst new file mode 100644 index 0000000000..2f896e3900 --- /dev/null +++ b/docs/zh_CN/migration-guides/release-5.x/5.3/storage.rst @@ -0,0 +1,18 @@ +存储 +======= + +:link_to_translation:`en:[English]` + +VFS +--- + +VFS 操作符的 UART 具体实现函数从 `vfs` 组件挪到了 `esp_driver_uart` 组件中。 + +所有以 `esp_vfs_dev_uart_` 前缀开头的 API 已被弃用, 更新成在 `uart_vfs.h` 文件中定义的以 `uart_vfs_dev_` 为前缀的一组 API。具体来说, +- ``esp_vfs_dev_uart_register`` 更名为 ``uart_vfs_dev_register`` +- ``esp_vfs_dev_uart_port_set_rx_line_endings`` 更名为 ``uart_vfs_dev_port_set_rx_line_endings`` +- ``esp_vfs_dev_uart_port_set_tx_line_endings`` 更名为 ``uart_vfs_dev_port_set_tx_line_endings`` +- ``esp_vfs_dev_uart_use_nonblocking`` 更名为 ``uart_vfs_dev_use_nonblocking`` +- ``esp_vfs_dev_uart_use_driver`` 更名为 ``uart_vfs_dev_use_driver`` + +为了兼容性,`vfs` 组件依旧将 `esp_driver_uart` 注册成了其私有依赖。换句话说,你无需修改既有项目的 CMake 文件。 diff --git a/examples/common_components/protocol_examples_common/stdin_out.c b/examples/common_components/protocol_examples_common/stdin_out.c index 5fbc8a38af..9f3a5163f3 100644 --- a/examples/common_components/protocol_examples_common/stdin_out.c +++ b/examples/common_components/protocol_examples_common/stdin_out.c @@ -9,7 +9,7 @@ #include "protocol_examples_common.h" #include "esp_err.h" -#include "esp_vfs_dev.h" +#include "driver/uart_vfs.h" #include "driver/uart.h" #include "sdkconfig.h" @@ -25,10 +25,10 @@ esp_err_t example_configure_stdin_stdout(void) ESP_ERROR_CHECK( uart_driver_install( (uart_port_t)CONFIG_ESP_CONSOLE_UART_NUM, 256, 0, 0, NULL, 0) ); /* Tell VFS to use UART driver */ - esp_vfs_dev_uart_use_driver(CONFIG_ESP_CONSOLE_UART_NUM); - esp_vfs_dev_uart_port_set_rx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CR); + uart_vfs_dev_use_driver(CONFIG_ESP_CONSOLE_UART_NUM); + uart_vfs_dev_port_set_rx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CR); /* Move the caret to the beginning of the next line on '\n' */ - esp_vfs_dev_uart_port_set_tx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CRLF); + uart_vfs_dev_port_set_tx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CRLF); configured = true; return ESP_OK; } diff --git a/examples/peripherals/uart/uart_select/main/uart_select_example_main.c b/examples/peripherals/uart/uart_select/main/uart_select_example_main.c index 065837e444..7364290a3c 100644 --- a/examples/peripherals/uart/uart_select/main/uart_select_example_main.c +++ b/examples/peripherals/uart/uart_select/main/uart_select_example_main.c @@ -14,8 +14,7 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "esp_log.h" -#include "esp_vfs.h" -#include "esp_vfs_dev.h" +#include "driver/uart_vfs.h" #include "driver/uart.h" static const char* TAG = "uart_select_example"; @@ -48,7 +47,7 @@ static void uart_select_task(void *arg) } // We have a driver now installed so set up the read/write functions to use driver also. - esp_vfs_dev_uart_use_driver(0); + uart_vfs_dev_use_driver(0); while (1) { int s; diff --git a/examples/system/console/advanced/main/console_example_main.c b/examples/system/console/advanced/main/console_example_main.c index dba157f544..3151803fd9 100644 --- a/examples/system/console/advanced/main/console_example_main.c +++ b/examples/system/console/advanced/main/console_example_main.c @@ -9,10 +9,11 @@ #include #include +#include #include "esp_system.h" #include "esp_log.h" #include "esp_console.h" -#include "esp_vfs_dev.h" +#include "driver/uart_vfs.h" #include "driver/uart.h" #include "linenoise/linenoise.h" #include "argtable3/argtable3.h" @@ -90,9 +91,9 @@ static void initialize_console(void) setvbuf(stdin, NULL, _IONBF, 0); /* Minicom, screen, idf_monitor send CR when ENTER key is pressed */ - esp_vfs_dev_uart_port_set_rx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CR); + uart_vfs_dev_port_set_rx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CR); /* Move the caret to the beginning of the next line on '\n' */ - esp_vfs_dev_uart_port_set_tx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CRLF); + uart_vfs_dev_port_set_tx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CRLF); /* Configure UART. Note that REF_TICK is used so that the baud rate remains * correct while APB frequency is changing in light sleep mode. @@ -114,7 +115,7 @@ static void initialize_console(void) ESP_ERROR_CHECK( uart_param_config(CONFIG_ESP_CONSOLE_UART_NUM, &uart_config) ); /* Tell VFS to use UART driver */ - esp_vfs_dev_uart_use_driver(CONFIG_ESP_CONSOLE_UART_NUM); + uart_vfs_dev_use_driver(CONFIG_ESP_CONSOLE_UART_NUM); /* Initialize the console */ esp_console_config_t console_config = { diff --git a/examples/system/select/main/select_example.c b/examples/system/select/main/select_example.c index 4de3a00697..01f6359998 100644 --- a/examples/system/select/main/select_example.c +++ b/examples/system/select/main/select_example.c @@ -15,8 +15,7 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "esp_log.h" -#include "esp_vfs.h" -#include "esp_vfs_dev.h" +#include "driver/uart_vfs.h" #include "driver/uart.h" #include "esp_netif.h" #include "lwip/sockets.h" @@ -107,7 +106,7 @@ static void uart1_init(void) uart1_deinit(); } - esp_vfs_dev_uart_use_driver(1); + uart_vfs_dev_use_driver(1); } static void uart1_write_task(void *param) diff --git a/examples/wifi/power_save/main/get_ap_info.c b/examples/wifi/power_save/main/get_ap_info.c index 00c9558464..233c2c2a40 100644 --- a/examples/wifi/power_save/main/get_ap_info.c +++ b/examples/wifi/power_save/main/get_ap_info.c @@ -6,7 +6,7 @@ #include #include "sdkconfig.h" #include "esp_log.h" -#include "esp_vfs_dev.h" +#include "driver/uart_vfs.h" #include "driver/uart.h" #if CONFIG_EXAMPLE_GET_AP_INFO_FROM_STDIN @@ -23,10 +23,10 @@ void get_ap_info_from_stdin(void) 256, 0, 0, NULL, 0) ); /* Tell VFS to use UART driver */ - esp_vfs_dev_uart_use_driver(CONFIG_ESP_CONSOLE_UART_NUM); - esp_vfs_dev_uart_port_set_rx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CR); + uart_vfs_dev_use_driver(CONFIG_ESP_CONSOLE_UART_NUM); + uart_vfs_dev_port_set_rx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CR); /* Move the caret to the beginning of the next line on '\n' */ - esp_vfs_dev_uart_port_set_tx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CRLF); + uart_vfs_dev_port_set_tx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CRLF); ESP_LOGI(TAG, "Input SSID:"); @@ -44,7 +44,7 @@ void get_ap_info_from_stdin(void) } /* Back to use non-blocking vfs console*/ - esp_vfs_dev_uart_use_nonblocking(CONFIG_ESP_CONSOLE_UART_NUM); + uart_vfs_dev_use_nonblocking(CONFIG_ESP_CONSOLE_UART_NUM); uart_driver_delete(CONFIG_ESP_CONSOLE_UART_NUM); } #endif diff --git a/examples/zigbee/esp_zigbee_gateway/main/esp_zigbee_gateway.c b/examples/zigbee/esp_zigbee_gateway/main/esp_zigbee_gateway.c index 4c034b2e05..d1731af3c0 100644 --- a/examples/zigbee/esp_zigbee_gateway/main/esp_zigbee_gateway.c +++ b/examples/zigbee/esp_zigbee_gateway/main/esp_zigbee_gateway.c @@ -46,6 +46,7 @@ #include "esp_vfs_eventfd.h" #include "esp_vfs_dev.h" #include "esp_vfs_usb_serial_jtag.h" +#include "driver/uart_vfs.h" #include "esp_wifi.h" #include "nvs_flash.h" #include "protocol_examples_common.h" @@ -77,7 +78,7 @@ esp_err_t esp_zb_gateway_console_init(void) usb_serial_jtag_driver_config_t usb_serial_jtag_config = USB_SERIAL_JTAG_DRIVER_CONFIG_DEFAULT(); ret = usb_serial_jtag_driver_install(&usb_serial_jtag_config); esp_vfs_usb_serial_jtag_use_driver(); - esp_vfs_dev_uart_register(); + uart_vfs_dev_register(); return ret; } #endif diff --git a/tools/test_apps/protocols/network_tests/main/stdinout.c b/tools/test_apps/protocols/network_tests/main/stdinout.c index 719c8ff528..a257bea460 100644 --- a/tools/test_apps/protocols/network_tests/main/stdinout.c +++ b/tools/test_apps/protocols/network_tests/main/stdinout.c @@ -3,11 +3,12 @@ * * SPDX-License-Identifier: Apache-2.0 */ +#include #include "esp_netif.h" #include "esp_log.h" #include "driver/uart.h" +#include "driver/uart_vfs.h" #include "esp_console.h" -#include "esp_vfs_dev.h" #include "linenoise/linenoise.h" // @@ -139,10 +140,10 @@ void * netsuite_io_new(void) ESP_ERROR_CHECK( uart_driver_install( (uart_port_t)CONFIG_ESP_CONSOLE_UART_NUM, 256, 0, 0, NULL, 0) ); /* Tell VFS to use UART driver */ - esp_vfs_dev_uart_use_driver(CONFIG_ESP_CONSOLE_UART_NUM); - esp_vfs_dev_uart_port_set_rx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CR); + uart_vfs_dev_use_driver(CONFIG_ESP_CONSOLE_UART_NUM); + uart_vfs_dev_port_set_rx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CR); /* Move the caret to the beginning of the next line on '\n' */ - esp_vfs_dev_uart_port_set_tx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CRLF); + uart_vfs_dev_port_set_tx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CRLF); linenoiseSetDumbMode(1); return (void *)&s_driver_base; }