2017-08-15 08:23:23 +00:00
|
|
|
// Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
|
2016-10-26 06:05:56 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2016-10-25 14:01:50 +00:00
|
|
|
#include <string.h>
|
2017-08-15 08:23:23 +00:00
|
|
|
#include <stdbool.h>
|
2017-08-16 06:58:06 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/fcntl.h>
|
2018-05-03 08:41:10 +00:00
|
|
|
#include <sys/param.h>
|
2016-10-25 14:01:50 +00:00
|
|
|
#include "esp_vfs.h"
|
2017-08-15 08:23:23 +00:00
|
|
|
#include "esp_vfs_dev.h"
|
2016-10-25 14:01:50 +00:00
|
|
|
#include "esp_attr.h"
|
|
|
|
#include "soc/uart_struct.h"
|
2017-08-15 08:23:23 +00:00
|
|
|
#include "driver/uart.h"
|
2016-10-28 08:16:12 +00:00
|
|
|
#include "sdkconfig.h"
|
2018-05-03 08:41:10 +00:00
|
|
|
#include "driver/uart_select.h"
|
2016-10-25 14:01:50 +00:00
|
|
|
|
2017-08-15 08:23:23 +00:00
|
|
|
// TODO: make the number of UARTs chip dependent
|
|
|
|
#define UART_NUM 3
|
2016-10-25 14:01:50 +00:00
|
|
|
|
2017-08-15 08:23:23 +00:00
|
|
|
// Token signifying that no character is available
|
|
|
|
#define NONE -1
|
|
|
|
|
|
|
|
// UART write bytes function type
|
|
|
|
typedef void (*tx_func_t)(int, int);
|
|
|
|
// UART read bytes function type
|
|
|
|
typedef int (*rx_func_t)(int);
|
|
|
|
|
|
|
|
// Basic functions for sending and receiving bytes over UART
|
|
|
|
static void uart_tx_char(int fd, int c);
|
|
|
|
static int uart_rx_char(int fd);
|
|
|
|
|
|
|
|
// Functions for sending and receiving bytes which use UART driver
|
|
|
|
static void uart_tx_char_via_driver(int fd, int c);
|
|
|
|
static int uart_rx_char_via_driver(int fd);
|
|
|
|
|
|
|
|
// Pointers to UART peripherals
|
|
|
|
static uart_dev_t* s_uarts[UART_NUM] = {&UART0, &UART1, &UART2};
|
|
|
|
// per-UART locks, lazily initialized
|
2017-08-29 00:23:40 +00:00
|
|
|
static _lock_t s_uart_read_locks[UART_NUM];
|
|
|
|
static _lock_t s_uart_write_locks[UART_NUM];
|
2017-08-15 08:23:23 +00:00
|
|
|
// One-character buffer used for newline conversion code, per UART
|
|
|
|
static int s_peek_char[UART_NUM] = { NONE, NONE, NONE };
|
2017-08-16 06:58:06 +00:00
|
|
|
// Per-UART non-blocking flag. Note: default implementation does not honor this
|
|
|
|
// flag, all reads are non-blocking. This option becomes effective if UART
|
|
|
|
// driver is used.
|
|
|
|
static bool s_non_blocking[UART_NUM];
|
2017-08-15 08:23:23 +00:00
|
|
|
|
2018-05-29 09:01:25 +00:00
|
|
|
/* Lock ensuring that uart_select is used from only one task at the time */
|
|
|
|
static _lock_t s_one_select_lock;
|
|
|
|
|
|
|
|
static SemaphoreHandle_t *_signal_sem = NULL;
|
2018-05-03 08:41:10 +00:00
|
|
|
static fd_set *_readfds = NULL;
|
|
|
|
static fd_set *_writefds = NULL;
|
|
|
|
static fd_set *_errorfds = NULL;
|
|
|
|
static fd_set *_readfds_orig = NULL;
|
|
|
|
static fd_set *_writefds_orig = NULL;
|
|
|
|
static fd_set *_errorfds_orig = NULL;
|
|
|
|
|
2017-08-15 08:23:23 +00:00
|
|
|
// Newline conversion mode when transmitting
|
|
|
|
static esp_line_endings_t s_tx_mode =
|
|
|
|
#if CONFIG_NEWLIB_STDOUT_LINE_ENDING_CRLF
|
|
|
|
ESP_LINE_ENDINGS_CRLF;
|
|
|
|
#elif CONFIG_NEWLIB_STDOUT_LINE_ENDING_CR
|
|
|
|
ESP_LINE_ENDINGS_CR;
|
|
|
|
#else
|
|
|
|
ESP_LINE_ENDINGS_LF;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Newline conversion mode when receiving
|
|
|
|
static esp_line_endings_t s_rx_mode =
|
|
|
|
#if CONFIG_NEWLIB_STDIN_LINE_ENDING_CRLF
|
|
|
|
ESP_LINE_ENDINGS_CRLF;
|
|
|
|
#elif CONFIG_NEWLIB_STDIN_LINE_ENDING_CR
|
|
|
|
ESP_LINE_ENDINGS_CR;
|
|
|
|
#else
|
|
|
|
ESP_LINE_ENDINGS_LF;
|
|
|
|
#endif
|
|
|
|
|
2018-05-29 09:01:25 +00:00
|
|
|
static void uart_end_select();
|
|
|
|
|
2017-08-15 08:23:23 +00:00
|
|
|
// Functions used to write bytes to UART. Default to "basic" functions.
|
|
|
|
static tx_func_t s_uart_tx_func[UART_NUM] = {
|
|
|
|
&uart_tx_char, &uart_tx_char, &uart_tx_char
|
|
|
|
};
|
|
|
|
|
|
|
|
// Functions used to read bytes from UART. Default to "basic" functions.
|
|
|
|
static rx_func_t s_uart_rx_func[UART_NUM] = {
|
|
|
|
&uart_rx_char, &uart_rx_char, &uart_rx_char
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static int uart_open(const char * path, int flags, int mode)
|
2016-10-25 14:01:50 +00:00
|
|
|
{
|
|
|
|
// this is fairly primitive, we should check if file is opened read only,
|
|
|
|
// and error out if write is requested
|
2018-03-29 12:18:10 +00:00
|
|
|
int fd = -1;
|
|
|
|
|
2016-10-25 14:01:50 +00:00
|
|
|
if (strcmp(path, "/0") == 0) {
|
2018-03-29 12:18:10 +00:00
|
|
|
fd = 0;
|
2016-10-25 14:01:50 +00:00
|
|
|
} else if (strcmp(path, "/1") == 0) {
|
2018-03-29 12:18:10 +00:00
|
|
|
fd = 1;
|
2016-10-25 14:01:50 +00:00
|
|
|
} else if (strcmp(path, "/2") == 0) {
|
2018-03-29 12:18:10 +00:00
|
|
|
fd = 2;
|
|
|
|
} else {
|
|
|
|
errno = ENOENT;
|
|
|
|
return fd;
|
2016-10-25 14:01:50 +00:00
|
|
|
}
|
2018-03-29 12:18:10 +00:00
|
|
|
|
|
|
|
s_non_blocking[fd] = ((flags & O_NONBLOCK) == O_NONBLOCK);
|
|
|
|
|
|
|
|
return fd;
|
2016-10-25 14:01:50 +00:00
|
|
|
}
|
|
|
|
|
2017-08-15 08:23:23 +00:00
|
|
|
static void uart_tx_char(int fd, int c)
|
2016-10-25 14:01:50 +00:00
|
|
|
{
|
2017-08-15 08:23:23 +00:00
|
|
|
uart_dev_t* uart = s_uarts[fd];
|
2016-10-25 14:01:50 +00:00
|
|
|
while (uart->status.txfifo_cnt >= 127) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
uart->fifo.rw_byte = c;
|
|
|
|
}
|
|
|
|
|
2017-08-15 08:23:23 +00:00
|
|
|
static void uart_tx_char_via_driver(int fd, int c)
|
|
|
|
{
|
|
|
|
char ch = (char) c;
|
|
|
|
uart_write_bytes(fd, &ch, 1);
|
|
|
|
}
|
2016-10-25 14:01:50 +00:00
|
|
|
|
2017-08-15 08:23:23 +00:00
|
|
|
static int uart_rx_char(int fd)
|
|
|
|
{
|
|
|
|
uart_dev_t* uart = s_uarts[fd];
|
|
|
|
if (uart->status.rxfifo_cnt == 0) {
|
|
|
|
return NONE;
|
|
|
|
}
|
|
|
|
return uart->fifo.rw_byte;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int uart_rx_char_via_driver(int fd)
|
|
|
|
{
|
|
|
|
uint8_t c;
|
2017-08-16 06:58:06 +00:00
|
|
|
int timeout = s_non_blocking[fd] ? 0 : portMAX_DELAY;
|
|
|
|
int n = uart_read_bytes(fd, &c, 1, timeout);
|
|
|
|
if (n <= 0) {
|
2017-08-15 08:23:23 +00:00
|
|
|
return NONE;
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t uart_write(int fd, const void * data, size_t size)
|
2016-10-25 14:01:50 +00:00
|
|
|
{
|
|
|
|
assert(fd >=0 && fd < 3);
|
|
|
|
const char *data_c = (const char *)data;
|
2017-08-15 08:23:23 +00:00
|
|
|
/* Even though newlib does stream locking on each individual stream, we need
|
2016-10-26 06:05:56 +00:00
|
|
|
* a dedicated UART lock if two streams (stdout and stderr) point to the
|
|
|
|
* same UART.
|
|
|
|
*/
|
2017-08-29 00:23:40 +00:00
|
|
|
_lock_acquire_recursive(&s_uart_write_locks[fd]);
|
2016-10-25 14:01:50 +00:00
|
|
|
for (size_t i = 0; i < size; i++) {
|
2017-08-15 08:23:23 +00:00
|
|
|
int c = data_c[i];
|
|
|
|
if (c == '\n' && s_tx_mode != ESP_LINE_ENDINGS_LF) {
|
|
|
|
s_uart_tx_func[fd](fd, '\r');
|
|
|
|
if (s_tx_mode == ESP_LINE_ENDINGS_CR) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-10-25 14:01:50 +00:00
|
|
|
}
|
2017-08-15 08:23:23 +00:00
|
|
|
s_uart_tx_func[fd](fd, c);
|
2016-10-25 14:01:50 +00:00
|
|
|
}
|
2017-08-29 00:23:40 +00:00
|
|
|
_lock_release_recursive(&s_uart_write_locks[fd]);
|
2016-10-25 14:01:50 +00:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2017-08-15 08:23:23 +00:00
|
|
|
/* Helper function which returns a previous character or reads a new one from
|
|
|
|
* UART. Previous character can be returned ("pushed back") using
|
|
|
|
* uart_return_char function.
|
|
|
|
*/
|
|
|
|
static int uart_read_char(int fd)
|
|
|
|
{
|
|
|
|
/* return character from peek buffer, if it is there */
|
|
|
|
if (s_peek_char[fd] != NONE) {
|
|
|
|
int c = s_peek_char[fd];
|
|
|
|
s_peek_char[fd] = NONE;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
return s_uart_rx_func[fd](fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Push back a character; it will be returned by next call to uart_read_char */
|
|
|
|
static void uart_return_char(int fd, int c)
|
|
|
|
{
|
|
|
|
assert(s_peek_char[fd] == NONE);
|
|
|
|
s_peek_char[fd] = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t uart_read(int fd, void* data, size_t size)
|
2017-01-09 14:50:42 +00:00
|
|
|
{
|
|
|
|
assert(fd >=0 && fd < 3);
|
2017-08-15 08:23:23 +00:00
|
|
|
char *data_c = (char *) data;
|
2017-01-09 14:50:42 +00:00
|
|
|
size_t received = 0;
|
2017-08-29 00:23:40 +00:00
|
|
|
_lock_acquire_recursive(&s_uart_read_locks[fd]);
|
2017-08-15 08:23:23 +00:00
|
|
|
while (received < size) {
|
|
|
|
int c = uart_read_char(fd);
|
|
|
|
if (c == '\r') {
|
|
|
|
if (s_rx_mode == ESP_LINE_ENDINGS_CR) {
|
|
|
|
c = '\n';
|
|
|
|
} else if (s_rx_mode == ESP_LINE_ENDINGS_CRLF) {
|
|
|
|
/* look ahead */
|
|
|
|
int c2 = uart_read_char(fd);
|
|
|
|
if (c2 == NONE) {
|
|
|
|
/* could not look ahead, put the current character back */
|
|
|
|
uart_return_char(fd, c);
|
2017-01-09 14:50:42 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-08-15 08:23:23 +00:00
|
|
|
if (c2 == '\n') {
|
|
|
|
/* this was \r\n sequence. discard \r, return \n */
|
|
|
|
c = '\n';
|
|
|
|
} else {
|
|
|
|
/* \r followed by something else. put the second char back,
|
|
|
|
* it will be processed on next iteration. return \r now.
|
|
|
|
*/
|
|
|
|
uart_return_char(fd, c2);
|
|
|
|
}
|
2017-01-09 14:50:42 +00:00
|
|
|
}
|
2017-08-16 06:58:06 +00:00
|
|
|
} else if (c == NONE) {
|
|
|
|
break;
|
2017-01-09 14:50:42 +00:00
|
|
|
}
|
2017-08-15 08:23:23 +00:00
|
|
|
data_c[received] = (char) c;
|
2017-01-09 14:50:42 +00:00
|
|
|
++received;
|
2017-08-15 08:23:23 +00:00
|
|
|
if (c == '\n') {
|
|
|
|
break;
|
|
|
|
}
|
2017-01-09 14:50:42 +00:00
|
|
|
}
|
2017-08-29 00:23:40 +00:00
|
|
|
_lock_release_recursive(&s_uart_read_locks[fd]);
|
2017-01-09 14:50:42 +00:00
|
|
|
if (received > 0) {
|
|
|
|
return received;
|
|
|
|
}
|
|
|
|
errno = EWOULDBLOCK;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-08-15 08:23:23 +00:00
|
|
|
static int uart_fstat(int fd, struct stat * st)
|
2016-10-25 14:01:50 +00:00
|
|
|
{
|
|
|
|
assert(fd >=0 && fd < 3);
|
|
|
|
st->st_mode = S_IFCHR;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-15 08:23:23 +00:00
|
|
|
static int uart_close(int fd)
|
2016-10-25 14:01:50 +00:00
|
|
|
{
|
|
|
|
assert(fd >=0 && fd < 3);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-16 06:58:06 +00:00
|
|
|
static int uart_fcntl(int fd, int cmd, va_list args)
|
|
|
|
{
|
|
|
|
assert(fd >=0 && fd < 3);
|
|
|
|
int result = 0;
|
|
|
|
if (cmd == F_GETFL) {
|
|
|
|
if (s_non_blocking[fd]) {
|
|
|
|
result |= O_NONBLOCK;
|
|
|
|
}
|
|
|
|
} else if (cmd == F_SETFL) {
|
|
|
|
int arg = va_arg(args, int);
|
|
|
|
s_non_blocking[fd] = (arg & O_NONBLOCK) != 0;
|
|
|
|
} else {
|
|
|
|
// unsupported operation
|
|
|
|
result = -1;
|
|
|
|
errno = ENOSYS;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-05-04 09:44:38 +00:00
|
|
|
static int uart_access(const char *path, int amode)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
if (strcmp(path, "/0") == 0 || strcmp(path, "/1") == 0 || strcmp(path, "/2") == 0) {
|
|
|
|
if (F_OK == amode) {
|
|
|
|
ret = 0; //path exists
|
|
|
|
} else {
|
|
|
|
if ((((amode & R_OK) == R_OK) || ((amode & W_OK) == W_OK)) && ((amode & X_OK) != X_OK)) {
|
|
|
|
ret = 0; //path is readable and/or writable but not executable
|
|
|
|
} else {
|
|
|
|
errno = EACCES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
errno = ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-05-03 08:41:10 +00:00
|
|
|
static void select_notif_callback(uart_port_t uart_num, uart_select_notif_t uart_select_notif, BaseType_t *task_woken)
|
|
|
|
{
|
|
|
|
switch (uart_select_notif) {
|
|
|
|
case UART_SELECT_READ_NOTIF:
|
|
|
|
if (FD_ISSET(uart_num, _readfds_orig)) {
|
|
|
|
FD_SET(uart_num, _readfds);
|
2018-05-29 09:01:25 +00:00
|
|
|
esp_vfs_select_triggered_isr(_signal_sem, task_woken);
|
2018-05-03 08:41:10 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case UART_SELECT_WRITE_NOTIF:
|
|
|
|
if (FD_ISSET(uart_num, _writefds_orig)) {
|
|
|
|
FD_SET(uart_num, _writefds);
|
2018-05-29 09:01:25 +00:00
|
|
|
esp_vfs_select_triggered_isr(_signal_sem, task_woken);
|
2018-05-03 08:41:10 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case UART_SELECT_ERROR_NOTIF:
|
|
|
|
if (FD_ISSET(uart_num, _errorfds_orig)) {
|
|
|
|
FD_SET(uart_num, _errorfds);
|
2018-05-29 09:01:25 +00:00
|
|
|
esp_vfs_select_triggered_isr(_signal_sem, task_woken);
|
2018-05-03 08:41:10 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-29 09:01:25 +00:00
|
|
|
static esp_err_t uart_start_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, SemaphoreHandle_t *signal_sem)
|
2018-05-03 08:41:10 +00:00
|
|
|
{
|
2018-05-29 09:01:25 +00:00
|
|
|
if (_lock_try_acquire(&s_one_select_lock)) {
|
|
|
|
return ESP_ERR_INVALID_STATE;
|
|
|
|
}
|
|
|
|
|
2018-05-03 08:41:10 +00:00
|
|
|
const int max_fds = MIN(nfds, UART_NUM);
|
|
|
|
|
2018-06-07 06:10:52 +00:00
|
|
|
portENTER_CRITICAL(uart_get_selectlock());
|
2018-05-03 08:41:10 +00:00
|
|
|
|
2018-05-29 09:01:25 +00:00
|
|
|
if (_readfds || _writefds || _errorfds || _readfds_orig || _writefds_orig || _errorfds_orig || _signal_sem) {
|
2018-06-07 06:10:52 +00:00
|
|
|
portEXIT_CRITICAL(uart_get_selectlock());
|
2018-05-29 09:01:25 +00:00
|
|
|
uart_end_select();
|
2018-05-03 08:41:10 +00:00
|
|
|
return ESP_ERR_INVALID_STATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((_readfds_orig = malloc(sizeof(fd_set))) == NULL) {
|
2018-06-07 06:10:52 +00:00
|
|
|
portEXIT_CRITICAL(uart_get_selectlock());
|
2018-05-29 09:01:25 +00:00
|
|
|
uart_end_select();
|
2018-05-03 08:41:10 +00:00
|
|
|
return ESP_ERR_NO_MEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((_writefds_orig = malloc(sizeof(fd_set))) == NULL) {
|
2018-06-07 06:10:52 +00:00
|
|
|
portEXIT_CRITICAL(uart_get_selectlock());
|
2018-05-29 09:01:25 +00:00
|
|
|
uart_end_select();
|
2018-05-03 08:41:10 +00:00
|
|
|
return ESP_ERR_NO_MEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((_errorfds_orig = malloc(sizeof(fd_set))) == NULL) {
|
2018-06-07 06:10:52 +00:00
|
|
|
portEXIT_CRITICAL(uart_get_selectlock());
|
2018-05-29 09:01:25 +00:00
|
|
|
uart_end_select();
|
2018-05-03 08:41:10 +00:00
|
|
|
return ESP_ERR_NO_MEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
//uart_set_select_notif_callback set the callbacks in UART ISR
|
|
|
|
for (int i = 0; i < max_fds; ++i) {
|
|
|
|
if (FD_ISSET(i, readfds) || FD_ISSET(i, writefds) || FD_ISSET(i, exceptfds)) {
|
|
|
|
uart_set_select_notif_callback(i, select_notif_callback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-29 09:01:25 +00:00
|
|
|
_signal_sem = signal_sem;
|
|
|
|
|
2018-05-03 08:41:10 +00:00
|
|
|
_readfds = readfds;
|
|
|
|
_writefds = writefds;
|
|
|
|
_errorfds = exceptfds;
|
|
|
|
|
|
|
|
*_readfds_orig = *readfds;
|
|
|
|
*_writefds_orig = *writefds;
|
|
|
|
*_errorfds_orig = *exceptfds;
|
|
|
|
|
|
|
|
FD_ZERO(readfds);
|
|
|
|
FD_ZERO(writefds);
|
|
|
|
FD_ZERO(exceptfds);
|
|
|
|
|
2018-11-27 11:53:19 +00:00
|
|
|
for (int i = 0; i < max_fds; ++i) {
|
|
|
|
if (FD_ISSET(i, _readfds_orig)) {
|
|
|
|
size_t buffered_size;
|
|
|
|
if (uart_get_buffered_data_len(i, &buffered_size) == ESP_OK && buffered_size > 0) {
|
|
|
|
// signalize immediately when data is buffered
|
|
|
|
FD_SET(i, _readfds);
|
|
|
|
esp_vfs_select_triggered(_signal_sem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-07 06:10:52 +00:00
|
|
|
portEXIT_CRITICAL(uart_get_selectlock());
|
2018-05-29 09:01:25 +00:00
|
|
|
// s_one_select_lock is not released on successfull exit - will be
|
|
|
|
// released in uart_end_select()
|
2018-05-03 08:41:10 +00:00
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uart_end_select()
|
|
|
|
{
|
2018-06-07 06:10:52 +00:00
|
|
|
portENTER_CRITICAL(uart_get_selectlock());
|
2018-05-03 08:41:10 +00:00
|
|
|
for (int i = 0; i < UART_NUM; ++i) {
|
|
|
|
uart_set_select_notif_callback(i, NULL);
|
|
|
|
}
|
|
|
|
|
2018-05-29 09:01:25 +00:00
|
|
|
_signal_sem = NULL;
|
|
|
|
|
2018-05-03 08:41:10 +00:00
|
|
|
_readfds = NULL;
|
|
|
|
_writefds = NULL;
|
|
|
|
_errorfds = NULL;
|
|
|
|
|
|
|
|
if (_readfds_orig) {
|
|
|
|
free(_readfds_orig);
|
|
|
|
_readfds_orig = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_writefds_orig) {
|
|
|
|
free(_writefds_orig);
|
|
|
|
_writefds_orig = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_errorfds_orig) {
|
|
|
|
free(_errorfds_orig);
|
|
|
|
_errorfds_orig = NULL;
|
|
|
|
}
|
2018-06-07 06:10:52 +00:00
|
|
|
portEXIT_CRITICAL(uart_get_selectlock());
|
2018-05-29 09:01:25 +00:00
|
|
|
_lock_release(&s_one_select_lock);
|
2018-05-03 08:41:10 +00:00
|
|
|
}
|
|
|
|
|
2016-10-25 14:01:50 +00:00
|
|
|
void esp_vfs_dev_uart_register()
|
|
|
|
{
|
|
|
|
esp_vfs_t vfs = {
|
|
|
|
.flags = ESP_VFS_FLAG_DEFAULT,
|
|
|
|
.write = &uart_write,
|
|
|
|
.open = &uart_open,
|
|
|
|
.fstat = &uart_fstat,
|
|
|
|
.close = &uart_close,
|
2017-01-09 14:50:42 +00:00
|
|
|
.read = &uart_read,
|
2018-05-04 09:44:38 +00:00
|
|
|
.fcntl = &uart_fcntl,
|
|
|
|
.access = &uart_access,
|
2018-05-03 08:41:10 +00:00
|
|
|
.start_select = &uart_start_select,
|
|
|
|
.end_select = &uart_end_select,
|
2016-10-25 14:01:50 +00:00
|
|
|
};
|
|
|
|
ESP_ERROR_CHECK(esp_vfs_register("/dev/uart", &vfs, NULL));
|
|
|
|
}
|
2017-08-15 08:23:23 +00:00
|
|
|
|
|
|
|
void esp_vfs_dev_uart_set_rx_line_endings(esp_line_endings_t mode)
|
|
|
|
{
|
|
|
|
s_rx_mode = mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void esp_vfs_dev_uart_set_tx_line_endings(esp_line_endings_t mode)
|
|
|
|
{
|
|
|
|
s_tx_mode = mode;
|
|
|
|
}
|
|
|
|
|
2018-05-07 07:01:56 +00:00
|
|
|
void esp_vfs_dev_uart_use_nonblocking(int uart_num)
|
2017-08-15 08:23:23 +00:00
|
|
|
{
|
2018-05-07 07:01:56 +00:00
|
|
|
_lock_acquire_recursive(&s_uart_read_locks[uart_num]);
|
|
|
|
_lock_acquire_recursive(&s_uart_write_locks[uart_num]);
|
|
|
|
s_uart_tx_func[uart_num] = uart_tx_char;
|
|
|
|
s_uart_rx_func[uart_num] = uart_rx_char;
|
|
|
|
_lock_release_recursive(&s_uart_write_locks[uart_num]);
|
|
|
|
_lock_release_recursive(&s_uart_read_locks[uart_num]);
|
2017-08-15 08:23:23 +00:00
|
|
|
}
|
|
|
|
|
2018-05-07 07:01:56 +00:00
|
|
|
void esp_vfs_dev_uart_use_driver(int uart_num)
|
2017-08-15 08:23:23 +00:00
|
|
|
{
|
2018-05-07 07:01:56 +00:00
|
|
|
_lock_acquire_recursive(&s_uart_read_locks[uart_num]);
|
|
|
|
_lock_acquire_recursive(&s_uart_write_locks[uart_num]);
|
|
|
|
s_uart_tx_func[uart_num] = uart_tx_char_via_driver;
|
|
|
|
s_uart_rx_func[uart_num] = uart_rx_char_via_driver;
|
|
|
|
_lock_release_recursive(&s_uart_write_locks[uart_num]);
|
|
|
|
_lock_release_recursive(&s_uart_read_locks[uart_num]);
|
2017-08-15 08:23:23 +00:00
|
|
|
}
|