feat(vfs): modify vfs for ESP8266

This commit is contained in:
Dong Heng
2020-02-27 12:56:59 +08:00
parent 230963030d
commit 5ddea6d655
12 changed files with 58 additions and 139 deletions

View File

@ -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);
}

View File

@ -20,6 +20,7 @@
extern "C" {
#endif
#include <stdbool.h>
#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

View File

@ -18,9 +18,7 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#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)

View File

@ -15,7 +15,6 @@
#include <sys/select.h>
#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 */

View File

@ -16,119 +16,17 @@
#include <stdio.h>
#include <string.h>
#include <reent.h>
#include <sys/stat.h>
#include <sys/errno.h>
#include <sys/fcntl.h>
#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);
}

View File

@ -18,7 +18,6 @@
#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"

View File

@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#if 0
#include <string.h>
#include <stdbool.h>
#include <stdarg.h>
@ -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

View File

@ -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);

View File

@ -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

View File

@ -1,4 +1,3 @@
CONFIG_USING_ESP_VFS=y
CONFIG_SUPPRESS_SELECT_DEBUG_OUTPUT=y
CONFIG_SUPPORT_TERMIOS=y

View File

@ -1,4 +1,3 @@
CONFIG_USING_ESP_VFS=y
CONFIG_SUPPRESS_SELECT_DEBUG_OUTPUT=y
CONFIG_SUPPORT_TERMIOS=y

View File

@ -1,8 +1,3 @@
#
# Virtual file system
#
CONFIG_USING_ESP_VFS=y
#
# PThreads
#