mirror of
https://github.com/espressif/esp-idf.git
synced 2026-05-19 23:45:28 +02:00
refactor(uart_vfs): Move uart implementation of vfs to esp_driver_uart
Deprecated esp_vfs_dev_uart_xxx APIs vfs_uart test case moved to esp_driver_uart test_apps Astyle fixed for uart_vfs
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
*
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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.
|
||||
*
|
||||
|
||||
@@ -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
|
||||
)
|
||||
|
||||
@@ -10,10 +10,9 @@
|
||||
#include <sys/param.h>
|
||||
#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);
|
||||
|
||||
|
||||
@@ -1,350 +0,0 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/termios.h>
|
||||
#include <sys/errno.h>
|
||||
#include "unity.h"
|
||||
#include "esp_rom_uart.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/semphr.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"
|
||||
|
||||
static void fwrite_str_loopback(const char* str, size_t size)
|
||||
{
|
||||
esp_rom_uart_tx_wait_idle(CONFIG_ESP_CONSOLE_UART_NUM);
|
||||
uart_ll_set_loop_back(&UART0, 1);
|
||||
fwrite(str, 1, size, stdout);
|
||||
fflush(stdout);
|
||||
esp_rom_uart_tx_wait_idle(CONFIG_ESP_CONSOLE_UART_NUM);
|
||||
vTaskDelay(2 / portTICK_PERIOD_MS);
|
||||
uart_ll_set_loop_back(&UART0, 0);
|
||||
}
|
||||
|
||||
static void flush_stdin_stdout(void)
|
||||
{
|
||||
vTaskDelay(10 / portTICK_PERIOD_MS);
|
||||
char bitbucket[UART_HW_FIFO_LEN(CONFIG_ESP_CONSOLE_UART_NUM)];
|
||||
while (fread(bitbucket, 1, UART_HW_FIFO_LEN(CONFIG_ESP_CONSOLE_UART_NUM), stdin) > 0) {
|
||||
;
|
||||
}
|
||||
fflush(stdout);
|
||||
esp_rom_uart_tx_wait_idle(CONFIG_ESP_CONSOLE_UART_NUM);
|
||||
}
|
||||
|
||||
TEST_CASE("can read from stdin", "[vfs]")
|
||||
{
|
||||
flush_stdin_stdout();
|
||||
|
||||
const size_t count = 12;
|
||||
srand(count);
|
||||
char* data = (char*) calloc(1, count * 8 + 2);
|
||||
char* buf = (char*) calloc(1, count * 8 + 2);
|
||||
char* p = data;
|
||||
for (size_t i = 0; i < count; ++i) {
|
||||
p += sprintf(p, "%08x", rand());
|
||||
}
|
||||
p += sprintf(p, "\n");
|
||||
size_t len = p - data;
|
||||
fwrite_str_loopback(data, len);
|
||||
size_t cb = fread(buf, 1, len, stdin);
|
||||
TEST_ASSERT_EQUAL(len, cb);
|
||||
TEST_ASSERT_EQUAL_UINT8_ARRAY(data, buf, len);
|
||||
|
||||
free(data);
|
||||
free(buf);
|
||||
}
|
||||
|
||||
|
||||
TEST_CASE("CRs are removed from the stdin correctly", "[vfs]")
|
||||
{
|
||||
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);
|
||||
|
||||
flush_stdin_stdout();
|
||||
const char* send_str = "1234567890\n\r123\r\n4\n";
|
||||
/* with CONFIG_NEWLIB_STDOUT_ADDCR, the following will be sent on the wire.
|
||||
* (last character of each part is marked with a hat)
|
||||
*
|
||||
* 1234567890\r\n\r123\r\r\n4\r\n
|
||||
* ^ ^^ ^
|
||||
*/
|
||||
char buf[128];
|
||||
char* dst = buf;
|
||||
|
||||
fwrite_str_loopback(send_str, 11); // send up to the first \n
|
||||
size_t rb = fread(dst, 1, 5, stdin); // read first 5
|
||||
TEST_ASSERT_EQUAL(5, rb);
|
||||
dst += rb;
|
||||
|
||||
rb = fread(dst, 1, 6, stdin); // ask for 6
|
||||
TEST_ASSERT_EQUAL(6, rb); // get 6
|
||||
|
||||
TEST_ASSERT_EQUAL_UINT8_ARRAY("1234567890\n", buf, 11);
|
||||
dst += rb;
|
||||
|
||||
rb = fread(dst, 1, 2, stdin); // any more characters?
|
||||
TEST_ASSERT_EQUAL(0, rb); // nothing
|
||||
|
||||
fwrite_str_loopback(send_str + 11, 1); // send the '\r'
|
||||
vTaskDelay(10 / portTICK_PERIOD_MS);
|
||||
|
||||
rb = fread(dst, 1, 2, stdin); // try to get somthing
|
||||
TEST_ASSERT_EQUAL(0, rb); // still nothing (\r is buffered)
|
||||
|
||||
fwrite_str_loopback(send_str + 12, 1); // Now send the '1'
|
||||
vTaskDelay(10 / portTICK_PERIOD_MS);
|
||||
rb = fread(dst, 1, 2, stdin); // try again
|
||||
TEST_ASSERT_EQUAL(2, rb); // get two characters
|
||||
TEST_ASSERT_EQUAL_UINT8_ARRAY("\r1", dst, 2);
|
||||
dst += rb;
|
||||
|
||||
fwrite_str_loopback(send_str + 13, 6); // Send the rest
|
||||
vTaskDelay(10 / portTICK_PERIOD_MS);
|
||||
|
||||
rb = fread(dst, 1, 4, stdin); // consume "23\r\n"
|
||||
TEST_ASSERT_EQUAL(4, rb);
|
||||
TEST_ASSERT_EQUAL_UINT8_ARRAY("23\r\n", dst, 4);
|
||||
dst += rb;
|
||||
|
||||
rb = fread(dst, 1, 4, stdin); // ask for more than the remainder
|
||||
TEST_ASSERT_EQUAL(2, rb);
|
||||
TEST_ASSERT_EQUAL_UINT8_ARRAY("4\n", dst, 2);
|
||||
}
|
||||
|
||||
struct read_task_arg_t {
|
||||
char* out_buffer;
|
||||
size_t out_buffer_len;
|
||||
SemaphoreHandle_t ready;
|
||||
SemaphoreHandle_t done;
|
||||
};
|
||||
|
||||
struct write_task_arg_t {
|
||||
const char* str;
|
||||
SemaphoreHandle_t done;
|
||||
};
|
||||
|
||||
static void read_task_fn(void* varg)
|
||||
{
|
||||
struct read_task_arg_t* parg = (struct read_task_arg_t*) varg;
|
||||
parg->out_buffer[0] = 0;
|
||||
|
||||
fgets(parg->out_buffer, parg->out_buffer_len, stdin);
|
||||
xSemaphoreGive(parg->done);
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
static void write_task_fn(void* varg)
|
||||
{
|
||||
struct write_task_arg_t* parg = (struct write_task_arg_t*) varg;
|
||||
fwrite_str_loopback(parg->str, strlen(parg->str));
|
||||
xSemaphoreGive(parg->done);
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
TEST_CASE("can write to UART while another task is reading", "[vfs]")
|
||||
{
|
||||
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()
|
||||
};
|
||||
|
||||
struct write_task_arg_t write_arg = {
|
||||
.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);
|
||||
|
||||
|
||||
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);
|
||||
|
||||
res = xSemaphoreTake(read_arg.done, 100 / portTICK_PERIOD_MS);
|
||||
TEST_ASSERT(res);
|
||||
|
||||
TEST_ASSERT_EQUAL(0, strcmp(write_arg.str, read_arg.out_buffer));
|
||||
|
||||
esp_vfs_dev_uart_use_nonblocking(CONFIG_ESP_CONSOLE_UART_NUM);
|
||||
uart_driver_delete(CONFIG_ESP_CONSOLE_UART_NUM);
|
||||
vSemaphoreDelete(read_arg.done);
|
||||
vSemaphoreDelete(write_arg.done);
|
||||
}
|
||||
|
||||
TEST_CASE("fcntl supported in UART VFS", "[vfs]")
|
||||
{
|
||||
int flags = fcntl(STDIN_FILENO, F_GETFL, 0);
|
||||
TEST_ASSERT_NOT_EQUAL(-1, flags);
|
||||
int res = fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
|
||||
TEST_ASSERT_NOT_EQUAL(-1, res);
|
||||
/* revert */
|
||||
res = fcntl(STDIN_FILENO, F_SETFL, flags);
|
||||
TEST_ASSERT_NOT_EQUAL(-1, res);
|
||||
}
|
||||
|
||||
#ifdef CONFIG_VFS_SUPPORT_TERMIOS
|
||||
TEST_CASE("Can use termios for UART", "[vfs]")
|
||||
{
|
||||
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));
|
||||
uart_config_t uart_config = {
|
||||
.baud_rate = 115200,
|
||||
.data_bits = UART_DATA_8_BITS,
|
||||
.parity = UART_PARITY_DISABLE,
|
||||
.stop_bits = UART_STOP_BITS_1,
|
||||
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
|
||||
.source_clk = UART_SCLK_DEFAULT,
|
||||
};
|
||||
uart_driver_install(UART_NUM_1, 256, 256, 0, NULL, 0);
|
||||
uart_param_config(UART_NUM_1, &uart_config);
|
||||
|
||||
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);
|
||||
|
||||
TEST_ASSERT_EQUAL(-1, tcgetattr(uart_fd, NULL));
|
||||
TEST_ASSERT_EQUAL(EINVAL, errno);
|
||||
|
||||
struct termios tios, tios_result;
|
||||
|
||||
TEST_ASSERT_EQUAL(-1, tcgetattr(-1, &tios));
|
||||
TEST_ASSERT_EQUAL(EBADF, errno);
|
||||
|
||||
TEST_ASSERT_EQUAL(0, tcgetattr(uart_fd, &tios));
|
||||
|
||||
TEST_ASSERT_EQUAL(0, tcsetattr(uart_fd, TCSADRAIN, &tios));
|
||||
TEST_ASSERT_EQUAL(0, tcsetattr(uart_fd, TCSAFLUSH, &tios));
|
||||
|
||||
tios.c_iflag |= IGNCR;
|
||||
TEST_ASSERT_EQUAL(0, tcsetattr(uart_fd, TCSANOW, &tios));
|
||||
tios.c_iflag &= (~IGNCR);
|
||||
TEST_ASSERT_EQUAL(0, tcgetattr(uart_fd, &tios_result));
|
||||
TEST_ASSERT_EQUAL(IGNCR, tios_result.c_iflag & IGNCR);
|
||||
memset(&tios_result, 0xFF, sizeof(struct termios));
|
||||
|
||||
tios.c_iflag |= ICRNL;
|
||||
TEST_ASSERT_EQUAL(0, tcsetattr(uart_fd, TCSANOW, &tios));
|
||||
tios.c_iflag &= (~ICRNL);
|
||||
TEST_ASSERT_EQUAL(0, tcgetattr(uart_fd, &tios_result));
|
||||
TEST_ASSERT_EQUAL(ICRNL, tios_result.c_iflag & ICRNL);
|
||||
memset(&tios_result, 0xFF, sizeof(struct termios));
|
||||
|
||||
{
|
||||
uart_word_length_t data_bit;
|
||||
uart_stop_bits_t stop_bits;
|
||||
uart_parity_t parity_mode;
|
||||
|
||||
tios.c_cflag &= (~CSIZE);
|
||||
tios.c_cflag &= (~CSTOPB);
|
||||
tios.c_cflag &= (~PARENB);
|
||||
tios.c_cflag |= CS6;
|
||||
TEST_ASSERT_EQUAL(0, tcsetattr(uart_fd, TCSANOW, &tios));
|
||||
tios.c_cflag &= (~CSIZE);
|
||||
TEST_ASSERT_EQUAL(0, tcgetattr(uart_fd, &tios_result));
|
||||
TEST_ASSERT_EQUAL(CS6, tios_result.c_cflag & CS6);
|
||||
TEST_ASSERT_EQUAL(ESP_OK, uart_get_word_length(UART_NUM_1, &data_bit));
|
||||
TEST_ASSERT_EQUAL(UART_DATA_6_BITS, data_bit);
|
||||
TEST_ASSERT_EQUAL(0, tios_result.c_cflag & CSTOPB);
|
||||
TEST_ASSERT_EQUAL(ESP_OK, uart_get_stop_bits(UART_NUM_1, &stop_bits));
|
||||
TEST_ASSERT_EQUAL(UART_STOP_BITS_1, stop_bits);
|
||||
TEST_ASSERT_EQUAL(ESP_OK, uart_get_parity(UART_NUM_1, &parity_mode));
|
||||
TEST_ASSERT_EQUAL(UART_PARITY_DISABLE, parity_mode);
|
||||
memset(&tios_result, 0xFF, sizeof(struct termios));
|
||||
}
|
||||
|
||||
{
|
||||
uart_stop_bits_t stop_bits;
|
||||
uart_parity_t parity_mode;
|
||||
|
||||
tios.c_cflag |= CSTOPB;
|
||||
tios.c_cflag |= (PARENB | PARODD);
|
||||
TEST_ASSERT_EQUAL(0, tcsetattr(uart_fd, TCSANOW, &tios));
|
||||
tios.c_cflag &= (~(CSTOPB | PARENB | PARODD));
|
||||
TEST_ASSERT_EQUAL(0, tcgetattr(uart_fd, &tios_result));
|
||||
TEST_ASSERT_EQUAL(CSTOPB, tios_result.c_cflag & CSTOPB);
|
||||
TEST_ASSERT_EQUAL(ESP_OK, uart_get_stop_bits(UART_NUM_1, &stop_bits));
|
||||
TEST_ASSERT_EQUAL(UART_STOP_BITS_2, stop_bits);
|
||||
TEST_ASSERT_EQUAL(ESP_OK, uart_get_parity(UART_NUM_1, &parity_mode));
|
||||
TEST_ASSERT_EQUAL(UART_PARITY_ODD, parity_mode);
|
||||
memset(&tios_result, 0xFF, sizeof(struct termios));
|
||||
}
|
||||
|
||||
{
|
||||
uint32_t baudrate;
|
||||
|
||||
tios.c_cflag &= (~BOTHER);
|
||||
tios.c_cflag |= CBAUD;
|
||||
tios.c_ispeed = tios.c_ospeed = B38400;
|
||||
TEST_ASSERT_EQUAL(0, tcsetattr(uart_fd, TCSANOW, &tios));
|
||||
TEST_ASSERT_EQUAL(0, tcgetattr(uart_fd, &tios_result));
|
||||
TEST_ASSERT_EQUAL(CBAUD, tios_result.c_cflag & CBAUD);
|
||||
TEST_ASSERT_EQUAL(ESP_OK, uart_get_baudrate(UART_NUM_1, &baudrate));
|
||||
TEST_ASSERT_INT32_WITHIN(2, 38400, baudrate);
|
||||
if (clk_src_hz == 40000000) {
|
||||
// Setting the speed to 38400 will set it actually to 38401
|
||||
// Note: can't use TEST_ASSERT_INT32_WITHIN here because B38400 == 15
|
||||
TEST_ASSERT_EQUAL(38401, tios_result.c_ispeed);
|
||||
TEST_ASSERT_EQUAL(38401, tios_result.c_ospeed);
|
||||
} else {
|
||||
TEST_ASSERT_EQUAL(B38400, tios_result.c_ispeed);
|
||||
TEST_ASSERT_EQUAL(B38400, tios_result.c_ospeed);
|
||||
}
|
||||
|
||||
tios.c_cflag |= CBAUDEX;
|
||||
tios.c_ispeed = tios.c_ospeed = B230400;
|
||||
TEST_ASSERT_EQUAL(0, tcsetattr(uart_fd, TCSANOW, &tios));
|
||||
TEST_ASSERT_EQUAL(0, tcgetattr(uart_fd, &tios_result));
|
||||
TEST_ASSERT_EQUAL(BOTHER, tios_result.c_cflag & BOTHER);
|
||||
TEST_ASSERT_EQUAL(ESP_OK, uart_get_baudrate(UART_NUM_1, &baudrate));
|
||||
// Setting the speed to 230400 will set it actually to something else,
|
||||
// depending on the default clock source
|
||||
TEST_ASSERT_INT32_WITHIN(100, 230400, tios_result.c_ispeed);
|
||||
TEST_ASSERT_INT32_WITHIN(100, 230400, tios_result.c_ospeed);
|
||||
TEST_ASSERT_INT32_WITHIN(100, 230400, baudrate);
|
||||
|
||||
tios.c_cflag |= BOTHER;
|
||||
tios.c_ispeed = tios.c_ospeed = 42321;
|
||||
TEST_ASSERT_EQUAL(0, tcsetattr(uart_fd, TCSANOW, &tios));
|
||||
TEST_ASSERT_EQUAL(0, tcgetattr(uart_fd, &tios_result));
|
||||
TEST_ASSERT_EQUAL(BOTHER, tios_result.c_cflag & BOTHER);
|
||||
TEST_ASSERT_EQUAL(ESP_OK, uart_get_baudrate(UART_NUM_1, &baudrate));
|
||||
// Setting the speed to 230400 will set it actually to something else,
|
||||
// depending on the default clock source
|
||||
TEST_ASSERT_INT32_WITHIN(10, 42321, tios_result.c_ispeed);
|
||||
TEST_ASSERT_INT32_WITHIN(10, 42321, tios_result.c_ospeed);
|
||||
TEST_ASSERT_INT32_WITHIN(10, 42321, baudrate);
|
||||
|
||||
memset(&tios_result, 0xFF, sizeof(struct termios));
|
||||
}
|
||||
|
||||
esp_vfs_dev_uart_use_nonblocking(1);
|
||||
close(uart_fd);
|
||||
uart_driver_delete(UART_NUM_1);
|
||||
}
|
||||
#endif // CONFIG_VFS_SUPPORT_TERMIOS
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user