Skip to content

Commit 5ddea6d

Browse files
committed
feat(vfs): modify vfs for ESP8266
1 parent 2309630 commit 5ddea6d

File tree

12 files changed

+58
-139
lines changed

12 files changed

+58
-139
lines changed

components/esp8266/driver/uart.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -663,16 +663,12 @@ static void uart_rx_intr_handler_default(void *param)
663663
notify = UART_SELECT_ERROR_NOTIF;
664664
}
665665

666-
#ifdef CONFIG_USING_ESP_VFS
667666
if (uart_event.type != UART_EVENT_MAX && p_uart->uart_select_notif_callback) {
668667
p_uart->uart_select_notif_callback(uart_num, notify, &task_woken);
669668
if (task_woken == pdTRUE) {
670669
portYIELD_FROM_ISR();
671670
}
672671
}
673-
#else
674-
(void)notify;
675-
#endif
676672

677673
if (uart_event.type != UART_EVENT_MAX && p_uart->xQueueUart) {
678674
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)
10901086
UART_EXIT_CRITICAL();
10911087
return ESP_OK;
10921088
}
1089+
1090+
bool uart_is_driver_installed(uart_port_t uart_num)
1091+
{
1092+
return uart_num < UART_NUM_MAX && (p_uart_obj[uart_num] != NULL);
1093+
}

components/esp8266/include/driver/uart.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
extern "C" {
2121
#endif
2222

23+
#include <stdbool.h>
2324
#include "esp_err.h"
2425
#include "esp_log.h"
2526
#include "freertos/queue.h"
@@ -553,6 +554,17 @@ esp_err_t uart_get_buffered_data_len(uart_port_t uart_num, size_t *size);
553554
*/
554555
esp_err_t uart_set_rx_timeout(uart_port_t uart_num, const uint8_t tout_thresh);
555556

557+
/**
558+
* @brief Checks whether the driver is installed or not
559+
*
560+
* @param uart_num UART port number, the max port number is (UART_NUM_MAX -1).
561+
*
562+
* @return
563+
* - true driver is installed
564+
* - false driver is not installed
565+
*/
566+
bool uart_is_driver_installed(uart_port_t uart_num);
567+
556568
#ifdef __cplusplus
557569
}
558570
#endif

components/newlib/newlib/port/esp_newlib.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
#include <stdlib.h>
1919
#include <string.h>
2020
#include <unistd.h>
21-
#ifdef CONFIG_USING_ESP_VFS
2221
#include "esp_vfs_dev.h"
23-
#endif
2422

2523
#define _STR(_s) #_s
2624
#define STR(_s) _STR(_s)
@@ -56,9 +54,7 @@ int esp_newlib_init(void)
5654

5755
esp_reent_init(_global_impure_ptr);
5856

59-
#ifdef CONFIG_USING_ESP_VFS
6057
esp_vfs_dev_uart_register();
61-
#endif
6258

6359
_GLOBAL_REENT->_stdout = fopen(default_uart_dev, "w");
6460
if (!_GLOBAL_REENT->_stdout)

components/newlib/newlib/port/select.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include <sys/select.h>
1616
#include "sdkconfig.h"
1717

18-
#ifdef CONFIG_USING_ESP_VFS
1918
#include "esp_vfs.h"
2019

2120
#ifdef CONFIG_USE_ONLY_LWIP_SELECT
@@ -64,4 +63,3 @@ int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct
6463
return esp_vfs_select(nfds, readfds, writefds, errorfds, timeout);
6564
#endif
6665
}
67-
#endif /* CONFIG_USING_ESP_VFS */

components/newlib/newlib/port/syscall.c

Lines changed: 13 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -16,119 +16,17 @@
1616

1717
#include <stdio.h>
1818
#include <string.h>
19+
#include <reent.h>
1920
#include <sys/stat.h>
2021
#include <sys/errno.h>
22+
#include <sys/fcntl.h>
2123

2224
#include "esp_libc.h"
2325
#include "FreeRTOS.h"
2426
#include "esp_log.h"
2527

26-
#ifdef CONFIG_USING_ESP_VFS
27-
2828
#include "esp_vfs.h"
2929

30-
int _open_r(struct _reent *r, const char *filename, int flags, int mode)
31-
{
32-
return esp_vfs_open(r, filename, flags, mode);
33-
}
34-
35-
_ssize_t _read_r(struct _reent *r, int fd, void *buf, size_t len)
36-
{
37-
return esp_vfs_read(r, fd, buf, len);
38-
}
39-
40-
_ssize_t _write_r(struct _reent *r, int fd, const void *buf, size_t len)
41-
{
42-
return esp_vfs_write(r, fd, buf, len);
43-
}
44-
45-
_off_t _lseek_r(struct _reent *r, int fd, _off_t where, int whence)
46-
{
47-
return esp_vfs_lseek(r, fd, where, whence);
48-
}
49-
50-
int _close_r(struct _reent *r, int fd)
51-
{
52-
return esp_vfs_close(r, fd);
53-
}
54-
55-
int _rename_r(struct _reent *r, const char *from, const char *to)
56-
{
57-
return esp_vfs_rename(r, from, to);
58-
}
59-
60-
int _unlink_r(struct _reent *r, const char *filename)
61-
{
62-
return esp_vfs_unlink(r, filename);
63-
}
64-
65-
int _fstat_r(struct _reent *r, int fd, struct stat *s)
66-
{
67-
return esp_vfs_fstat(r, fd, s);
68-
}
69-
70-
int _stat_r(struct _reent *r, const char *path, struct stat *st)
71-
{
72-
return esp_vfs_stat(r, path, st);
73-
}
74-
75-
#else
76-
77-
int _open_r(struct _reent *r, const char *filename, int flags, int mode)
78-
{
79-
return 0;
80-
}
81-
82-
_ssize_t _read_r(struct _reent *r, int fd, void *buf, size_t len)
83-
{
84-
return 0;
85-
}
86-
87-
_ssize_t _write_r(struct _reent *r, int fd, const void *buf, size_t len)
88-
{
89-
int i;
90-
const char *cbuf = buf;
91-
92-
for (i = 0; i < len; i++)
93-
ets_putc(cbuf[i]);
94-
95-
return len;
96-
}
97-
98-
_off_t _lseek_r(struct _reent *r, int fd, _off_t where, int whence)
99-
{
100-
return 0;
101-
}
102-
103-
int _close_r(struct _reent *r, int fd)
104-
{
105-
return 0;
106-
}
107-
108-
int _rename_r(struct _reent *r, const char *from, const char *to)
109-
{
110-
return 0;
111-
}
112-
113-
int _unlink_r(struct _reent *r, const char *filename)
114-
{
115-
return 0;
116-
}
117-
118-
int _fstat_r(struct _reent *r, int fd, struct stat *s)
119-
{
120-
s->st_mode = S_IFCHR;
121-
122-
return 0;
123-
}
124-
125-
int _stat_r(struct _reent *r, const char *path, struct stat *st)
126-
{
127-
return 0;
128-
}
129-
130-
#endif /* CONFIG_USING_ESP_VFS */
131-
13230
void *_malloc_r(struct _reent *r, size_t n)
13331
{
13432
void *return_addr = (void *)__builtin_return_address(0);
@@ -184,3 +82,14 @@ int _getpid_r(struct _reent *r)
18482
__errno_r(r) = ENOSYS;
18583
return -1;
18684
}
85+
86+
/* Replaces newlib fcntl, which has been compiled without HAVE_FCNTL */
87+
int fcntl(int fd, int cmd, ...)
88+
{
89+
va_list args;
90+
va_start(args, cmd);
91+
int arg = va_arg(args, int);
92+
va_end(args);
93+
struct _reent* r = __getreent();
94+
return _fcntl_r(r, fd, cmd, arg);
95+
}

components/vfs/test/test_vfs_select.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include <sys/param.h>
1919
#include "unity.h"
2020
#include "freertos/FreeRTOS.h"
21-
#include "soc/uart_struct.h"
2221
#include "driver/uart.h"
2322
#include "esp_vfs.h"
2423
#include "esp_vfs_dev.h"

components/vfs/vfs_semihost.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#if 0
16+
1517
#include <string.h>
1618
#include <stdbool.h>
1719
#include <stdarg.h>
@@ -214,3 +216,5 @@ esp_err_t esp_vfs_semihost_unregister(const char* base_path)
214216
ESP_LOGD(TAG, "Unregistered semihosting driver @ '%s'", base_path);
215217
return ESP_OK;
216218
}
219+
220+
#endif

components/vfs/vfs_uart.c

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,24 @@
2222
#include "esp_vfs.h"
2323
#include "esp_vfs_dev.h"
2424
#include "esp_attr.h"
25-
#include "soc/uart_periph.h"
2625
#include "driver/uart.h"
2726
#include "sdkconfig.h"
2827
#include "driver/uart_select.h"
2928
#if CONFIG_IDF_TARGET_ESP32
3029
#include "esp32/rom/uart.h"
30+
#include "soc/uart_periph.h"
3131
#elif CONFIG_IDF_TARGET_ESP32S2BETA
3232
#include "esp32s2beta/rom/uart.h"
33+
#include "soc/uart_periph.h"
34+
#elif CONFIG_IDF_TARGET_ESP8266
35+
#include "rom/uart.h"
3336
#endif
3437

38+
#define SOC_UART_NUM 2
39+
#define UART0 uart0
40+
#define UART1 uart1
41+
42+
3543
// TODO: make the number of UARTs chip dependent
3644
#define UART_NUM SOC_UART_NUM
3745

@@ -128,7 +136,6 @@ typedef struct {
128136

129137
static uart_select_args_t **s_registered_selects = NULL;
130138
static int s_registered_select_num = 0;
131-
static portMUX_TYPE s_registered_select_lock = portMUX_INITIALIZER_UNLOCKED;
132139

133140
static esp_err_t uart_end_select(void *end_select_args);
134141

@@ -142,8 +149,6 @@ static int uart_open(const char * path, int flags, int mode)
142149
fd = 0;
143150
} else if (strcmp(path, "/1") == 0) {
144151
fd = 1;
145-
} else if (strcmp(path, "/2") == 0) {
146-
fd = 2;
147152
} else {
148153
errno = ENOENT;
149154
return fd;
@@ -164,6 +169,8 @@ static void uart_tx_char(int fd, int c)
164169
uart->fifo.rw_byte = c;
165170
#elif CONFIG_IDF_TARGET_ESP32S2BETA
166171
uart->ahb_fifo.rw_byte = c;
172+
#elif CONFIG_IDF_TARGET_ESP8266
173+
uart->fifo.rw_byte = c;
167174
#endif
168175
}
169176

@@ -183,6 +190,8 @@ static int uart_rx_char(int fd)
183190
return uart->fifo.rw_byte;
184191
#elif CONFIG_IDF_TARGET_ESP32S2BETA
185192
return READ_PERI_REG(UART_FIFO_AHB_REG(fd));
193+
#elif CONFIG_IDF_TARGET_ESP8266
194+
return uart->fifo.rw_byte;
186195
#endif
187196
}
188197

@@ -354,7 +363,7 @@ static esp_err_t register_select(uart_select_args_t *args)
354363
esp_err_t ret = ESP_ERR_INVALID_ARG;
355364

356365
if (args) {
357-
portENTER_CRITICAL(&s_registered_select_lock);
366+
portENTER_CRITICAL();
358367
const int new_size = s_registered_select_num + 1;
359368
if ((s_registered_selects = realloc(s_registered_selects, new_size * sizeof(uart_select_args_t *))) == NULL) {
360369
ret = ESP_ERR_NO_MEM;
@@ -363,7 +372,7 @@ static esp_err_t register_select(uart_select_args_t *args)
363372
s_registered_select_num = new_size;
364373
ret = ESP_OK;
365374
}
366-
portEXIT_CRITICAL(&s_registered_select_lock);
375+
portEXIT_CRITICAL();
367376
}
368377

369378
return ret;
@@ -374,7 +383,7 @@ static esp_err_t unregister_select(uart_select_args_t *args)
374383
esp_err_t ret = ESP_OK;
375384
if (args) {
376385
ret = ESP_ERR_INVALID_STATE;
377-
portENTER_CRITICAL(&s_registered_select_lock);
386+
portENTER_CRITICAL();
378387
for (int i = 0; i < s_registered_select_num; ++i) {
379388
if (s_registered_selects[i] == args) {
380389
const int new_size = s_registered_select_num - 1;
@@ -391,14 +400,14 @@ static esp_err_t unregister_select(uart_select_args_t *args)
391400
break;
392401
}
393402
}
394-
portEXIT_CRITICAL(&s_registered_select_lock);
403+
portEXIT_CRITICAL();
395404
}
396405
return ret;
397406
}
398407

399408
static void select_notif_callback_isr(uart_port_t uart_num, uart_select_notif_t uart_select_notif, BaseType_t *task_woken)
400409
{
401-
portENTER_CRITICAL_ISR(&s_registered_select_lock);
410+
portENTER_CRITICAL();
402411
for (int i = 0; i < s_registered_select_num; ++i) {
403412
uart_select_args_t *args = s_registered_selects[i];
404413
if (args) {
@@ -424,7 +433,7 @@ static void select_notif_callback_isr(uart_port_t uart_num, uart_select_notif_t
424433
}
425434
}
426435
}
427-
portEXIT_CRITICAL_ISR(&s_registered_select_lock);
436+
portEXIT_CRITICAL();
428437
}
429438

430439
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,
458467
FD_ZERO(writefds);
459468
FD_ZERO(exceptfds);
460469

461-
portENTER_CRITICAL(uart_get_selectlock());
470+
portENTER_CRITICAL();
462471

463472
//uart_set_select_notif_callback sets the callbacks in UART ISR
464473
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,
480489

481490
esp_err_t ret = register_select(args);
482491
if (ret != ESP_OK) {
483-
portEXIT_CRITICAL(uart_get_selectlock());
492+
portEXIT_CRITICAL();
484493
free(args);
485494
return ret;
486495
}
487496

488-
portEXIT_CRITICAL(uart_get_selectlock());
497+
portEXIT_CRITICAL();
489498

490499
*end_select_args = args;
491500
return ESP_OK;
@@ -495,12 +504,12 @@ static esp_err_t uart_end_select(void *end_select_args)
495504
{
496505
uart_select_args_t *args = end_select_args;
497506

498-
portENTER_CRITICAL(uart_get_selectlock());
507+
portENTER_CRITICAL();
499508
esp_err_t ret = unregister_select(args);
500509
for (int i = 0; i < UART_NUM; ++i) {
501510
uart_set_select_notif_callback(i, NULL);
502511
}
503-
portEXIT_CRITICAL(uart_get_selectlock());
512+
portEXIT_CRITICAL();
504513

505514
if (args) {
506515
free(args);

0 commit comments

Comments
 (0)