From 5ddea6d655ec93e87161fe08140505ee17decb19 Mon Sep 17 00:00:00 2001 From: Dong Heng Date: Thu, 27 Feb 2020 12:56:59 +0800 Subject: [PATCH] feat(vfs): modify vfs for ESP8266 --- components/esp8266/driver/uart.c | 9 +- components/esp8266/include/driver/uart.h | 12 ++ components/newlib/newlib/port/esp_newlib.c | 4 - components/newlib/newlib/port/select.c | 2 - components/newlib/newlib/port/syscall.c | 117 ++---------------- components/vfs/test/test_vfs_select.c | 1 - components/vfs/vfs_semihost.c | 4 + components/vfs/vfs_uart.c | 39 +++--- .../protocol_examples_common/stdin_out.c | 2 - examples/peripherals/uart_select/sdkconfig.ci | 1 - .../uart_select/sdkconfig.defaults | 1 - .../system/factory-test/sdkconfig.defaults | 5 - 12 files changed, 58 insertions(+), 139 deletions(-) diff --git a/components/esp8266/driver/uart.c b/components/esp8266/driver/uart.c index 3d3cb518..f43c1357 100644 --- a/components/esp8266/driver/uart.c +++ b/components/esp8266/driver/uart.c @@ -663,16 +663,12 @@ static void uart_rx_intr_handler_default(void *param) notify = UART_SELECT_ERROR_NOTIF; } -#ifdef CONFIG_USING_ESP_VFS if (uart_event.type != UART_EVENT_MAX && p_uart->uart_select_notif_callback) { p_uart->uart_select_notif_callback(uart_num, notify, &task_woken); if (task_woken == pdTRUE) { portYIELD_FROM_ISR(); } } -#else - (void)notify; -#endif if (uart_event.type != UART_EVENT_MAX && p_uart->xQueueUart) { if (pdFALSE == xQueueSendFromISR(p_uart->xQueueUart, (void *)&uart_event, &task_woken)) { @@ -1090,3 +1086,8 @@ esp_err_t uart_set_rx_timeout(uart_port_t uart_num, const uint8_t tout_thresh) UART_EXIT_CRITICAL(); return ESP_OK; } + +bool uart_is_driver_installed(uart_port_t uart_num) +{ + return uart_num < UART_NUM_MAX && (p_uart_obj[uart_num] != NULL); +} diff --git a/components/esp8266/include/driver/uart.h b/components/esp8266/include/driver/uart.h index 39f256a3..ed144cd5 100644 --- a/components/esp8266/include/driver/uart.h +++ b/components/esp8266/include/driver/uart.h @@ -20,6 +20,7 @@ extern "C" { #endif +#include #include "esp_err.h" #include "esp_log.h" #include "freertos/queue.h" @@ -553,6 +554,17 @@ esp_err_t uart_get_buffered_data_len(uart_port_t uart_num, size_t *size); */ esp_err_t uart_set_rx_timeout(uart_port_t uart_num, const uint8_t tout_thresh); +/** + * @brief Checks whether the driver is installed or not + * + * @param uart_num UART port number, the max port number is (UART_NUM_MAX -1). + * + * @return + * - true driver is installed + * - false driver is not installed + */ +bool uart_is_driver_installed(uart_port_t uart_num); + #ifdef __cplusplus } #endif diff --git a/components/newlib/newlib/port/esp_newlib.c b/components/newlib/newlib/port/esp_newlib.c index de5a479b..63c9c254 100644 --- a/components/newlib/newlib/port/esp_newlib.c +++ b/components/newlib/newlib/port/esp_newlib.c @@ -18,9 +18,7 @@ #include #include #include -#ifdef CONFIG_USING_ESP_VFS #include "esp_vfs_dev.h" -#endif #define _STR(_s) #_s #define STR(_s) _STR(_s) @@ -56,9 +54,7 @@ int esp_newlib_init(void) esp_reent_init(_global_impure_ptr); -#ifdef CONFIG_USING_ESP_VFS esp_vfs_dev_uart_register(); -#endif _GLOBAL_REENT->_stdout = fopen(default_uart_dev, "w"); if (!_GLOBAL_REENT->_stdout) diff --git a/components/newlib/newlib/port/select.c b/components/newlib/newlib/port/select.c index 1f641e2a..536fe5bc 100644 --- a/components/newlib/newlib/port/select.c +++ b/components/newlib/newlib/port/select.c @@ -15,7 +15,6 @@ #include #include "sdkconfig.h" -#ifdef CONFIG_USING_ESP_VFS #include "esp_vfs.h" #ifdef CONFIG_USE_ONLY_LWIP_SELECT @@ -64,4 +63,3 @@ int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct return esp_vfs_select(nfds, readfds, writefds, errorfds, timeout); #endif } -#endif /* CONFIG_USING_ESP_VFS */ diff --git a/components/newlib/newlib/port/syscall.c b/components/newlib/newlib/port/syscall.c index c82fc6c9..50c7375f 100644 --- a/components/newlib/newlib/port/syscall.c +++ b/components/newlib/newlib/port/syscall.c @@ -16,119 +16,17 @@ #include #include +#include #include #include +#include #include "esp_libc.h" #include "FreeRTOS.h" #include "esp_log.h" -#ifdef CONFIG_USING_ESP_VFS - #include "esp_vfs.h" -int _open_r(struct _reent *r, const char *filename, int flags, int mode) -{ - return esp_vfs_open(r, filename, flags, mode); -} - -_ssize_t _read_r(struct _reent *r, int fd, void *buf, size_t len) -{ - return esp_vfs_read(r, fd, buf, len); -} - -_ssize_t _write_r(struct _reent *r, int fd, const void *buf, size_t len) -{ - return esp_vfs_write(r, fd, buf, len); -} - -_off_t _lseek_r(struct _reent *r, int fd, _off_t where, int whence) -{ - return esp_vfs_lseek(r, fd, where, whence); -} - -int _close_r(struct _reent *r, int fd) -{ - return esp_vfs_close(r, fd); -} - -int _rename_r(struct _reent *r, const char *from, const char *to) -{ - return esp_vfs_rename(r, from, to); -} - -int _unlink_r(struct _reent *r, const char *filename) -{ - return esp_vfs_unlink(r, filename); -} - -int _fstat_r(struct _reent *r, int fd, struct stat *s) -{ - return esp_vfs_fstat(r, fd, s); -} - -int _stat_r(struct _reent *r, const char *path, struct stat *st) -{ - return esp_vfs_stat(r, path, st); -} - -#else - -int _open_r(struct _reent *r, const char *filename, int flags, int mode) -{ - return 0; -} - -_ssize_t _read_r(struct _reent *r, int fd, void *buf, size_t len) -{ - return 0; -} - -_ssize_t _write_r(struct _reent *r, int fd, const void *buf, size_t len) -{ - int i; - const char *cbuf = buf; - - for (i = 0; i < len; i++) - ets_putc(cbuf[i]); - - return len; -} - -_off_t _lseek_r(struct _reent *r, int fd, _off_t where, int whence) -{ - return 0; -} - -int _close_r(struct _reent *r, int fd) -{ - return 0; -} - -int _rename_r(struct _reent *r, const char *from, const char *to) -{ - return 0; -} - -int _unlink_r(struct _reent *r, const char *filename) -{ - return 0; -} - -int _fstat_r(struct _reent *r, int fd, struct stat *s) -{ - s->st_mode = S_IFCHR; - - return 0; -} - -int _stat_r(struct _reent *r, const char *path, struct stat *st) -{ - return 0; -} - -#endif /* CONFIG_USING_ESP_VFS */ - void *_malloc_r(struct _reent *r, size_t n) { void *return_addr = (void *)__builtin_return_address(0); @@ -184,3 +82,14 @@ int _getpid_r(struct _reent *r) __errno_r(r) = ENOSYS; return -1; } + +/* Replaces newlib fcntl, which has been compiled without HAVE_FCNTL */ +int fcntl(int fd, int cmd, ...) +{ + va_list args; + va_start(args, cmd); + int arg = va_arg(args, int); + va_end(args); + struct _reent* r = __getreent(); + return _fcntl_r(r, fd, cmd, arg); +} diff --git a/components/vfs/test/test_vfs_select.c b/components/vfs/test/test_vfs_select.c index 6255aadc..1c96af74 100644 --- a/components/vfs/test/test_vfs_select.c +++ b/components/vfs/test/test_vfs_select.c @@ -18,7 +18,6 @@ #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" diff --git a/components/vfs/vfs_semihost.c b/components/vfs/vfs_semihost.c index d97fa918..ae02826f 100644 --- a/components/vfs/vfs_semihost.c +++ b/components/vfs/vfs_semihost.c @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if 0 + #include #include #include @@ -214,3 +216,5 @@ esp_err_t esp_vfs_semihost_unregister(const char* base_path) ESP_LOGD(TAG, "Unregistered semihosting driver @ '%s'", base_path); return ESP_OK; } + +#endif diff --git a/components/vfs/vfs_uart.c b/components/vfs/vfs_uart.c index dc430b82..75897caa 100644 --- a/components/vfs/vfs_uart.c +++ b/components/vfs/vfs_uart.c @@ -22,16 +22,24 @@ #include "esp_vfs.h" #include "esp_vfs_dev.h" #include "esp_attr.h" -#include "soc/uart_periph.h" #include "driver/uart.h" #include "sdkconfig.h" #include "driver/uart_select.h" #if CONFIG_IDF_TARGET_ESP32 #include "esp32/rom/uart.h" +#include "soc/uart_periph.h" #elif CONFIG_IDF_TARGET_ESP32S2BETA #include "esp32s2beta/rom/uart.h" +#include "soc/uart_periph.h" +#elif CONFIG_IDF_TARGET_ESP8266 +#include "rom/uart.h" #endif +#define SOC_UART_NUM 2 +#define UART0 uart0 +#define UART1 uart1 + + // TODO: make the number of UARTs chip dependent #define UART_NUM SOC_UART_NUM @@ -128,7 +136,6 @@ typedef struct { static uart_select_args_t **s_registered_selects = NULL; static int s_registered_select_num = 0; -static portMUX_TYPE s_registered_select_lock = portMUX_INITIALIZER_UNLOCKED; static esp_err_t uart_end_select(void *end_select_args); @@ -142,8 +149,6 @@ static int uart_open(const char * path, int flags, int mode) fd = 0; } else if (strcmp(path, "/1") == 0) { fd = 1; - } else if (strcmp(path, "/2") == 0) { - fd = 2; } else { errno = ENOENT; return fd; @@ -164,6 +169,8 @@ static void uart_tx_char(int fd, int c) uart->fifo.rw_byte = c; #elif CONFIG_IDF_TARGET_ESP32S2BETA uart->ahb_fifo.rw_byte = c; +#elif CONFIG_IDF_TARGET_ESP8266 + uart->fifo.rw_byte = c; #endif } @@ -183,6 +190,8 @@ static int uart_rx_char(int fd) return uart->fifo.rw_byte; #elif CONFIG_IDF_TARGET_ESP32S2BETA return READ_PERI_REG(UART_FIFO_AHB_REG(fd)); +#elif CONFIG_IDF_TARGET_ESP8266 + return uart->fifo.rw_byte; #endif } @@ -354,7 +363,7 @@ static esp_err_t register_select(uart_select_args_t *args) esp_err_t ret = ESP_ERR_INVALID_ARG; if (args) { - portENTER_CRITICAL(&s_registered_select_lock); + portENTER_CRITICAL(); const int new_size = s_registered_select_num + 1; if ((s_registered_selects = realloc(s_registered_selects, new_size * sizeof(uart_select_args_t *))) == NULL) { ret = ESP_ERR_NO_MEM; @@ -363,7 +372,7 @@ static esp_err_t register_select(uart_select_args_t *args) s_registered_select_num = new_size; ret = ESP_OK; } - portEXIT_CRITICAL(&s_registered_select_lock); + portEXIT_CRITICAL(); } return ret; @@ -374,7 +383,7 @@ static esp_err_t unregister_select(uart_select_args_t *args) esp_err_t ret = ESP_OK; if (args) { ret = ESP_ERR_INVALID_STATE; - portENTER_CRITICAL(&s_registered_select_lock); + portENTER_CRITICAL(); for (int i = 0; i < s_registered_select_num; ++i) { if (s_registered_selects[i] == args) { const int new_size = s_registered_select_num - 1; @@ -391,14 +400,14 @@ static esp_err_t unregister_select(uart_select_args_t *args) break; } } - portEXIT_CRITICAL(&s_registered_select_lock); + portEXIT_CRITICAL(); } return ret; } static void select_notif_callback_isr(uart_port_t uart_num, uart_select_notif_t uart_select_notif, BaseType_t *task_woken) { - portENTER_CRITICAL_ISR(&s_registered_select_lock); + portENTER_CRITICAL(); for (int i = 0; i < s_registered_select_num; ++i) { uart_select_args_t *args = s_registered_selects[i]; if (args) { @@ -424,7 +433,7 @@ static void select_notif_callback_isr(uart_port_t uart_num, uart_select_notif_t } } } - portEXIT_CRITICAL_ISR(&s_registered_select_lock); + portEXIT_CRITICAL(); } static esp_err_t uart_start_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, @@ -458,7 +467,7 @@ static esp_err_t uart_start_select(int nfds, fd_set *readfds, fd_set *writefds, FD_ZERO(writefds); FD_ZERO(exceptfds); - portENTER_CRITICAL(uart_get_selectlock()); + portENTER_CRITICAL(); //uart_set_select_notif_callback sets the callbacks in UART ISR for (int i = 0; i < max_fds; ++i) { @@ -480,12 +489,12 @@ static esp_err_t uart_start_select(int nfds, fd_set *readfds, fd_set *writefds, esp_err_t ret = register_select(args); if (ret != ESP_OK) { - portEXIT_CRITICAL(uart_get_selectlock()); + portEXIT_CRITICAL(); free(args); return ret; } - portEXIT_CRITICAL(uart_get_selectlock()); + portEXIT_CRITICAL(); *end_select_args = args; return ESP_OK; @@ -495,12 +504,12 @@ static esp_err_t uart_end_select(void *end_select_args) { uart_select_args_t *args = end_select_args; - portENTER_CRITICAL(uart_get_selectlock()); + portENTER_CRITICAL(); esp_err_t ret = unregister_select(args); for (int i = 0; i < UART_NUM; ++i) { uart_set_select_notif_callback(i, NULL); } - portEXIT_CRITICAL(uart_get_selectlock()); + portEXIT_CRITICAL(); if (args) { free(args); diff --git a/examples/common_components/protocol_examples_common/stdin_out.c b/examples/common_components/protocol_examples_common/stdin_out.c index 1c54026e..10cc2167 100644 --- a/examples/common_components/protocol_examples_common/stdin_out.c +++ b/examples/common_components/protocol_examples_common/stdin_out.c @@ -13,7 +13,6 @@ #include "driver/uart.h" #include "sdkconfig.h" -#ifdef CONFIG_USING_ESP_VFS esp_err_t example_configure_stdin_stdout(void) { // Initialize VFS & UART so we can use std::cout/cin @@ -29,4 +28,3 @@ esp_err_t example_configure_stdin_stdout(void) esp_vfs_dev_uart_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF); return ESP_OK; } -#endif diff --git a/examples/peripherals/uart_select/sdkconfig.ci b/examples/peripherals/uart_select/sdkconfig.ci index 68875e06..e912f5b4 100644 --- a/examples/peripherals/uart_select/sdkconfig.ci +++ b/examples/peripherals/uart_select/sdkconfig.ci @@ -1,4 +1,3 @@ -CONFIG_USING_ESP_VFS=y CONFIG_SUPPRESS_SELECT_DEBUG_OUTPUT=y CONFIG_SUPPORT_TERMIOS=y diff --git a/examples/peripherals/uart_select/sdkconfig.defaults b/examples/peripherals/uart_select/sdkconfig.defaults index b9359a4e..5f02e6f3 100644 --- a/examples/peripherals/uart_select/sdkconfig.defaults +++ b/examples/peripherals/uart_select/sdkconfig.defaults @@ -1,4 +1,3 @@ -CONFIG_USING_ESP_VFS=y CONFIG_SUPPRESS_SELECT_DEBUG_OUTPUT=y CONFIG_SUPPORT_TERMIOS=y diff --git a/examples/system/factory-test/sdkconfig.defaults b/examples/system/factory-test/sdkconfig.defaults index 5ca96d27..78e63880 100644 --- a/examples/system/factory-test/sdkconfig.defaults +++ b/examples/system/factory-test/sdkconfig.defaults @@ -1,8 +1,3 @@ -# -# Virtual file system -# -CONFIG_USING_ESP_VFS=y - # # PThreads #