fix(esp_vfs_console): add esp_vfs_console component

move vfs_console related init steps from vfs component to new esp_vfs_console component
This commit is contained in:
sonika.rathi 2024-01-23 12:09:57 +01:00
parent 8ce8a9e75e
commit e162903615
17 changed files with 161 additions and 151 deletions

View File

@ -31,7 +31,7 @@ idf_component_register(SRCS ${srcs}
${argtable_srcs}
INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}
PRIV_INCLUDE_DIRS private_include
REQUIRES vfs
REQUIRES vfs esp_vfs_console
PRIV_REQUIRES esp_driver_uart
esp_driver_usb_serial_jtag
)

View File

@ -0,0 +1,27 @@
/*
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include "esp_vfs.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @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);
#ifdef __cplusplus
}
#endif

View File

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@ -19,7 +19,6 @@
#include "esp_rom_uart.h"
#include "hal/uart_ll.h"
#include "soc/soc_caps.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"
@ -1010,6 +1009,11 @@ static const esp_vfs_t uart_vfs = {
#endif // CONFIG_VFS_SUPPORT_TERMIOS
};
const esp_vfs_t *esp_vfs_uart_get_vfs(void)
{
return &uart_vfs;
}
void uart_vfs_dev_register(void)
{
ESP_ERROR_CHECK(esp_vfs_register("/dev/uart", &uart_vfs, NULL));
@ -1074,7 +1078,7 @@ void uart_vfs_dev_use_driver(int uart_num)
#if 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);
uart_vfs_dev_register();
return ESP_OK;
}
#endif

View File

@ -0,0 +1,27 @@
/*
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include "esp_vfs.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief get pointer of usb_serial_jtag vfs.
*
* This function is called in vfs_console in order to get the vfs implementation
* of usb_serial_jtag.
*
* @return pointer to structure esp_vfs_t
*/
const esp_vfs_t *esp_vfs_usb_serial_jtag_get_vfs(void);
#ifdef __cplusplus
}
#endif

View File

@ -18,7 +18,6 @@
#include "esp_vfs.h"
#include "esp_vfs_dev.h" // Old headers for the aliasing functions
#include "esp_vfs_usb_serial_jtag.h" // Old headers for the aliasing functions
#include "esp_private/esp_vfs_console.h"
#include "esp_attr.h"
#include "esp_log.h"
#include "sdkconfig.h"
@ -395,7 +394,7 @@ esp_err_t usb_serial_jtag_vfs_register(void)
#if CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
ESP_SYSTEM_INIT_FN(init_vfs_usj, CORE, BIT(0), 111)
{
esp_vfs_set_primary_dev_vfs_def_struct(&usj_vfs);
usb_serial_jtag_vfs_register();
return ESP_OK;
}
#endif
@ -403,7 +402,8 @@ ESP_SYSTEM_INIT_FN(init_vfs_usj, CORE, BIT(0), 111)
#if CONFIG_ESP_CONSOLE_SECONDARY_USB_SERIAL_JTAG
ESP_SYSTEM_INIT_FN(init_vfs_usj_sec, CORE, BIT(0), 112)
{
esp_vfs_set_secondary_dev_vfs_def_struct(&usj_vfs);
// "/dev/seccondary_usb_serial_jtag" unfortunately is too long for vfs
esp_vfs_register("/dev/secondary", &usj_vfs, NULL);
return ESP_OK;
}
#endif

View File

@ -50,7 +50,7 @@ CORE: 105: init_newlib_time in components/esp_system/startup_funcs.c on BIT(0)
CORE: 110: init_vfs_uart in components/esp_driver_uart/src/uart_vfs.c on BIT(0)
CORE: 111: init_vfs_usj in components/esp_driver_usb_serial_jtag/src/usb_serial_jtag_vfs.c on BIT(0)
CORE: 112: init_vfs_usj_sec in components/esp_driver_usb_serial_jtag/src/usb_serial_jtag_vfs.c on BIT(0)
CORE: 114: init_vfs_console in components/vfs/vfs_console.c on BIT(0)
CORE: 114: init_vfs_console in components/esp_vfs_console/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/pthread/pthread.c on BIT(0)

View File

@ -389,7 +389,7 @@ TEST_CASE_MULTIPLE_STAGES("reset reason ESP_RST_BROWNOUT after brownout event",
#include "xt_instr_macros.h"
#include "xtensa/config/specreg.h"
static int size_stack = 1024 * 3;
static int size_stack = 1024 * 4;
static StackType_t *start_addr_stack;
static int fibonacci(int n, void* func(void))

View File

@ -0,0 +1,22 @@
idf_build_get_property(target IDF_TARGET)
if(${target} STREQUAL "linux")
return() # This component is not supported by the POSIX/Linux simulator
endif()
set(srcs "vfs_console.c")
idf_component_register(SRCS ${srcs}
INCLUDE_DIRS include
PRIV_REQUIRES vfs esp_driver_uart esp_driver_usb_serial_jtag
)
if(CONFIG_ESP_CONSOLE_USB_CDC)
target_sources(${COMPONENT_LIB} PRIVATE "vfs_cdcacm.c")
endif()
if(CONFIG_VFS_SUPPORT_IO)
target_link_libraries(${COMPONENT_LIB} PUBLIC idf::vfs)
# Make sure esp_vfs_console_register gets called at startup stage
target_link_libraries(${COMPONENT_LIB} INTERFACE "-u esp_vfs_include_console_register")
endif()

View File

@ -0,0 +1,27 @@
/*
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include "esp_vfs.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief get pointer of cdcacm vfs.
*
* This function is called in vfs_console in order to get the vfs implementation
* of cdcacm.
*
* @return pointer to structure esp_vfs_t
*/
const esp_vfs_t *esp_vfs_cdcacm_get_vfs(void);
#ifdef __cplusplus
}
#endif

View File

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/

View File

@ -160,7 +160,6 @@ static ssize_t cdcacm_read(int fd, void *data, size_t size)
xSemaphoreTake(s_rx_semaphore, portMAX_DELAY);
}
if (s_rx_mode == ESP_LINE_ENDINGS_CR || s_rx_mode == ESP_LINE_ENDINGS_LF) {
/* This is easy. Just receive, and if needed replace \r by \n. */
received = esp_usb_console_read_buf(data_c, size);
@ -266,7 +265,6 @@ static int cdcacm_disable_blocking(void)
return 0;
}
static int cdcacm_fcntl(int fd, int cmd, int arg)
{
assert(fd == 0);

View File

@ -4,13 +4,15 @@
* SPDX-License-Identifier: Apache-2.0
*/
#include <fcntl.h>
#include "esp_err.h"
#include "esp_rom_sys.h"
#include "esp_vfs_cdcacm.h"
#include "esp_vfs_private.h"
#include "esp_private/esp_vfs_cdcacm.h"
#include "driver/esp_private/usb_serial_jtag_vfs.h"
#include "driver/esp_private/uart_vfs.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"
@ -31,41 +33,30 @@ typedef struct {
} vfs_console_context_t;
#if CONFIG_VFS_SUPPORT_IO
// Primary register part.
#ifdef CONFIG_ESP_CONSOLE_UART
const static char *primary_path = "/dev/uart";
#elif CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
const static char *primary_path = "/dev/usbserjtag";
#elif CONFIG_ESP_CONSOLE_USB_CDC
const static char *primary_path = "/dev/cdcacm";
#endif
// Secondary register part.
#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};
static vfs_console_context_t vfs_console = {0};
int console_open(const char * path, int flags, int mode)
{
// Primary port open
#if CONFIG_ESP_CONSOLE_UART
vfs_console.fd_primary = get_vfs_for_path(primary_path)->vfs.open("/"STRINGIFY(CONFIG_ESP_CONSOLE_UART_NUM), flags, mode);
vfs_console.fd_primary = open("/dev/uart/"STRINGIFY(CONFIG_ESP_CONSOLE_UART_NUM), flags, mode);
#elif CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
vfs_console.fd_primary = get_vfs_for_path(primary_path)->vfs.open("/", flags, mode);
vfs_console.fd_primary = open("/dev/usbserjtag", flags, mode);
#elif CONFIG_ESP_CONSOLE_USB_CDC
vfs_console.fd_primary = esp_vfs_cdcacm_get_vfs()->open("/", flags, mode);
vfs_console.fd_primary = open("/dev/cdcacm", flags, mode);
#endif
// Secondary port open
#if CONFIG_ESP_CONSOLE_SECONDARY_USB_SERIAL_JTAG
vfs_console.fd_secondary = get_vfs_for_path(secondary_path)->vfs.open("/", flags, mode);
vfs_console.fd_secondary = open("/dev/secondary", flags, mode);
#endif
return 0;
}
@ -73,70 +64,70 @@ int console_open(const char * path, int flags, int mode)
ssize_t console_write(int fd, const void *data, size_t size)
{
// All function calls are to primary, except from write and close, which will be forwarded to both primary and secondary.
get_vfs_for_index(primary_vfs_index)->vfs.write(vfs_console.fd_primary, data, size);
write(vfs_console.fd_primary, data, size);
#if CONFIG_ESP_CONSOLE_SECONDARY_USB_SERIAL_JTAG
get_vfs_for_index(secondary_vfs_index)->vfs.write(vfs_console.fd_secondary, data, size);
write(vfs_console.fd_secondary, data, size);
#endif
return size;
}
int console_fstat(int fd, struct stat * st)
{
return get_vfs_for_index(primary_vfs_index)->vfs.fstat(fd, st);
return fstat(fd, st);
}
int console_close(int fd)
{
// All function calls are to primary, except from write and close, which will be forwarded to both primary and secondary.
get_vfs_for_index(primary_vfs_index)->vfs.close(vfs_console.fd_primary);
close(vfs_console.fd_primary);
#if CONFIG_ESP_CONSOLE_SECONDARY_USB_SERIAL_JTAG
get_vfs_for_index(secondary_vfs_index)->vfs.close(vfs_console.fd_secondary);
close(vfs_console.fd_secondary);
#endif
return 0;
}
ssize_t console_read(int fd, void * dst, size_t size)
{
return get_vfs_for_index(primary_vfs_index)->vfs.read(vfs_console.fd_primary, dst, size);
return read(vfs_console.fd_primary, dst, size);
}
int console_fcntl(int fd, int cmd, int arg)
{
return get_vfs_for_index(primary_vfs_index)->vfs.fcntl(vfs_console.fd_primary, cmd, arg);
return fcntl(vfs_console.fd_primary, cmd, arg);
}
int console_fsync(int fd)
{
return get_vfs_for_index(primary_vfs_index)->vfs.fsync(vfs_console.fd_primary);
return fsync(vfs_console.fd_primary);
}
#ifdef CONFIG_VFS_SUPPORT_DIR
int console_access(const char *path, int amode)
{
// currently only UART support DIR.
return get_vfs_for_index(primary_vfs_index)->vfs.access("/"STRINGIFY(CONFIG_ESP_CONSOLE_UART_NUM), amode);
return access("/dev/uart/"STRINGIFY(CONFIG_ESP_CONSOLE_UART_NUM), amode);
}
#endif // CONFIG_VFS_SUPPORT_DIR
#ifdef CONFIG_VFS_SUPPORT_SELECT
static esp_err_t console_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 vfs_entry_t* vfs_entry = get_vfs_for_index(primary_vfs_index);
// start_select is not guaranteed be implemented even though CONFIG_VFS_SUPPORT_SELECT is enabled in sdkconfig
if (vfs_entry && vfs_entry->vfs.start_select) {
return vfs_entry->vfs.start_select(nfds, readfds, writefds, exceptfds, select_sem, end_select_args);
if (primary_vfs->start_select) {
return primary_vfs->start_select(nfds, readfds, writefds, exceptfds, select_sem, end_select_args);
}
return ESP_ERR_NOT_SUPPORTED;
}
esp_err_t console_end_select(void *end_select_args)
{
const vfs_entry_t* vfs_entry = get_vfs_for_index(primary_vfs_index);
// end_select is not guaranteed be implemented even though CONFIG_VFS_SUPPORT_SELECT is enabled in sdkconfig
if (vfs_entry && vfs_entry->vfs.end_select) {
return vfs_entry->vfs.end_select(end_select_args);
if (primary_vfs->end_select) {
return primary_vfs->end_select(end_select_args);
}
return ESP_ERR_NOT_SUPPORTED;
}
@ -146,22 +137,22 @@ esp_err_t console_end_select(void *end_select_args)
int console_tcsetattr(int fd, int optional_actions, const struct termios *p)
{
return get_vfs_for_index(primary_vfs_index)->vfs.tcsetattr(vfs_console.fd_primary, optional_actions, p);
return tcsetattr(vfs_console.fd_primary, optional_actions, p);
}
int console_tcgetattr(int fd, struct termios *p)
{
return get_vfs_for_index(primary_vfs_index)->vfs.tcgetattr(vfs_console.fd_primary, p);
return tcgetattr(vfs_console.fd_primary, p);
}
int console_tcdrain(int fd)
{
return get_vfs_for_index(primary_vfs_index)->vfs.tcdrain(vfs_console.fd_primary);
return tcdrain(vfs_console.fd_primary);
}
int console_tcflush(int fd, int select)
{
return get_vfs_for_index(primary_vfs_index)->vfs.tcflush(vfs_console.fd_primary, select);
return tcflush(vfs_console.fd_primary, select);
}
#endif // CONFIG_VFS_SUPPORT_TERMIOS
@ -197,47 +188,32 @@ static esp_err_t esp_vfs_dev_console_register(void)
esp_err_t esp_vfs_console_register(void)
{
esp_err_t err = ESP_OK;
// Primary register part.
#if CONFIG_ESP_CONSOLE_UART || CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
assert(primary_vfs);
// Primary vfs part.
#if CONFIG_ESP_CONSOLE_UART
primary_vfs = esp_vfs_uart_get_vfs();
#elif CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
primary_vfs = esp_vfs_usb_serial_jtag_get_vfs();
#elif CONFIG_ESP_CONSOLE_USB_CDC
primary_vfs = esp_vfs_cdcacm_get_vfs();
err = esp_usb_console_init();
if (err != ESP_OK) {
return err;
}
#endif
#if !CONFIG_ESP_CONSOLE_NONE
err = esp_vfs_register_common(primary_path, strlen(primary_path), primary_vfs, NULL, &primary_vfs_index);
err = esp_vfs_dev_cdcacm_register();
if (err != ESP_OK) {
return err;
}
#endif // !CONFIG_ESP_CONSOLE_NONE
// Secondary register part.
#if CONFIG_ESP_CONSOLE_SECONDARY_USB_SERIAL_JTAG
assert(secondary_vfs);
err = esp_vfs_register_common(secondary_path, strlen(secondary_path), secondary_vfs, NULL, &secondary_vfs_index);
if(err != ESP_OK) {
return err;
}
#endif
// Secondary vfs part.
#if CONFIG_ESP_CONSOLE_SECONDARY_USB_SERIAL_JTAG
secondary_vfs = esp_vfs_usb_serial_jtag_get_vfs();
#endif
err = esp_vfs_dev_console_register();
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();

View File

@ -7,12 +7,11 @@ endif()
list(APPEND sources "vfs.c"
"vfs_eventfd.c"
"vfs_semihost.c"
"vfs_console.c"
)
list(APPEND pr esp_timer
# for backwards compatibility (TODO: IDF-8799)
esp_driver_uart esp_driver_usb_serial_jtag
esp_driver_uart esp_driver_usb_serial_jtag esp_vfs_console
)
idf_component_register(SRCS ${sources}
@ -21,15 +20,6 @@ idf_component_register(SRCS ${sources}
PRIV_INCLUDE_DIRS private_include
PRIV_REQUIRES ${pr})
if(CONFIG_ESP_CONSOLE_USB_CDC)
target_sources(${COMPONENT_LIB} PRIVATE "vfs_cdcacm.c")
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()

View File

@ -1,41 +0,0 @@
/*
* 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

View File

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@ -26,26 +26,6 @@ typedef struct vfs_entry_ {
int offset; // index of this structure in s_vfs array
} vfs_entry_t;
/**
* @brief get pointer of cdcacm vfs.
*
* This function is called in vfs_console in order to get the vfs implementation
* of cdcacm.
*
* @return pointer to structure esp_vfs_t
*/
const esp_vfs_t *esp_vfs_cdcacm_get_vfs(void);
/**
* @brief get pointer of usb_serial_jtag vfs.
*
* This function is called in vfs_console in order to get the vfs implementation
* of usb_serial_jtag.
*
* @return pointer to structure esp_vfs_nonblocking_console_t
*/
const esp_vfs_t *esp_vfs_usb_serial_jtag_get_vfs(void);
/**
* Register a virtual filesystem.
*

View File

@ -183,7 +183,7 @@ void app_main(void)
ESP_LOGI(TAG, "starting event sources");
// Create the event source task with the same priority as the current task
xTaskCreate(task_event_source, "task_event_source", 2048, NULL, uxTaskPriorityGet(NULL), NULL);
xTaskCreate(task_event_source, "task_event_source", 4096, NULL, uxTaskPriorityGet(NULL), NULL);
ESP_ERROR_CHECK(esp_timer_start_periodic(TIMER, TIMER_PERIOD));