Chapter 2. API Reference
VFS also provides an optional newline conversion feature for input and output. Internally, most applications send
and receive lines terminated by the LF (‘’n’’) character. Different terminal programs may require differ-
ent line termination, such as CR or CRLF. Applications can configure this separately for input and output ei-
ther via menuconfig, or by calls to the functions esp_vfs_dev_uart_port_set_rx_line_endings and
esp_vfs_dev_uart_port_set_tx_line_endings.
Standard streams and FreeRTOS tasks FILE objects for stdin, stdout, and stderr are shared between
all FreeRTOS tasks, but the pointers to these objects are stored in per-task struct _reent.
The following code is transferred to fprintf(__getreent()->_stderr, "42\n"); by the preprocessor:
fprintf(stderr, "42\n");
The __getreent() function returns a per-task pointer to struct _reent in newlib libc. This structure is
allocated on the TCB of each task. When a task is initialized, _stdin, _stdout, and _stderr members of
struct _reent are set to the values of _stdin, _stdout, and _stderr of _GLOBAL_REENT (i.e., the
structure which is used before FreeRTOS is started).
Such a design has the following consequences:
• It is possible to set stdin, stdout, and stderr for any given task without affecting other tasks, e.g., by
doing stdin = fopen("/dev/uart/1", "r").
• Closing default stdin, stdout, or stderr using fclose will close the FILE stream object, which will
affect all other tasks.
• To change the default stdin, stdout, stderr streams for new tasks, modify _GLOBAL_REENT-
>_stdin (_stdout, _stderr) before creating the task.
Event fds
eventfd() call is a powerful tool to notify a select() based loop of custom events. The eventfd() imple-
mentation in ESP-IDF is generally the same as described in man(2) eventfd except for:
• esp_vfs_eventfd_register() has to be called before calling eventfd()
• Options EFD_CLOEXEC, EFD_NONBLOCK and EFD_SEMAPHORE are not supported in flags.
• Option EFD_SUPPORT_ISR has been added in flags. This flag is required to read and the write the eventfd
in an interrupt handler.
Note that creating an eventfd with EFD_SUPPORT_ISR will cause interrupts to be temporarily disabled when
reading, writing the file and during the beginning and the ending of the select() when this file is set.
API Reference
Header File
• components/vfs/include/esp_vfs.h
Functions
ssize_t esp_vfs_write(struct _reent *r, int fd, const void *data, size_t size)
These functions are to be used in newlib syscall table. They will be called by newlib when it needs to use any
of the syscalls.
off_t esp_vfs_lseek(struct _reent *r, int fd, off_t size, int mode)
ssize_t esp_vfs_read(struct _reent *r, int fd, void *dst, size_t size)
int esp_vfs_open(struct _reent *r, const char *path, int flags, int mode)
int esp_vfs_close(struct _reent *r, int fd)
int esp_vfs_fstat(struct _reent *r, int fd, struct stat *st)
int esp_vfs_stat(struct _reent *r, const char *path, struct stat *st)
Espressif Systems 772
Submit Document Feedback
Release v4.4