mirror of
https://github.com/espressif/esp-idf
synced 2025-03-10 01:29:21 -04:00
vfs_uart: refactor to have static context structure
This commit is contained in:
parent
91ae40e2ff
commit
00b33a8e14
@ -37,6 +37,22 @@
|
|||||||
// Token signifying that no character is available
|
// Token signifying that no character is available
|
||||||
#define NONE -1
|
#define NONE -1
|
||||||
|
|
||||||
|
#if CONFIG_NEWLIB_STDOUT_LINE_ENDING_CRLF
|
||||||
|
# define DEFAULT_TX_MODE ESP_LINE_ENDINGS_CRLF
|
||||||
|
#elif CONFIG_NEWLIB_STDOUT_LINE_ENDING_CR
|
||||||
|
# define DEFAULT_TX_MODE ESP_LINE_ENDINGS_CR
|
||||||
|
#else
|
||||||
|
# define DEFAULT_TX_MODE ESP_LINE_ENDINGS_LF
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if CONFIG_NEWLIB_STDIN_LINE_ENDING_CRLF
|
||||||
|
# define DEFAULT_RX_MODE ESP_LINE_ENDINGS_CRLF
|
||||||
|
#elif CONFIG_NEWLIB_STDIN_LINE_ENDING_CR
|
||||||
|
# define DEFAULT_RX_MODE ESP_LINE_ENDINGS_CR
|
||||||
|
#else
|
||||||
|
# define DEFAULT_RX_MODE ESP_LINE_ENDINGS_LF
|
||||||
|
#endif
|
||||||
|
|
||||||
// UART write bytes function type
|
// UART write bytes function type
|
||||||
typedef void (*tx_func_t)(int, int);
|
typedef void (*tx_func_t)(int, int);
|
||||||
// UART read bytes function type
|
// UART read bytes function type
|
||||||
@ -50,33 +66,55 @@ static int uart_rx_char(int fd);
|
|||||||
static void uart_tx_char_via_driver(int fd, int c);
|
static void uart_tx_char_via_driver(int fd, int c);
|
||||||
static int uart_rx_char_via_driver(int fd);
|
static int uart_rx_char_via_driver(int fd);
|
||||||
|
|
||||||
// Pointers to UART peripherals
|
typedef struct {
|
||||||
static uart_dev_t* s_uarts[UART_NUM] = {
|
// Pointers to UART peripherals
|
||||||
&UART0,
|
uart_dev_t* uart;
|
||||||
&UART1,
|
// One-character buffer used for newline conversion code, per UART
|
||||||
|
int peek_char;
|
||||||
|
// per-UART locks, lazily initialized
|
||||||
|
_lock_t read_lock;
|
||||||
|
_lock_t write_lock;
|
||||||
|
// 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.
|
||||||
|
bool non_blocking;
|
||||||
|
// Newline conversion mode when transmitting
|
||||||
|
esp_line_endings_t tx_mode;
|
||||||
|
// Newline conversion mode when receiving
|
||||||
|
esp_line_endings_t rx_mode;
|
||||||
|
// Functions used to write bytes to UART. Default to "basic" functions.
|
||||||
|
tx_func_t tx_func;
|
||||||
|
// Functions used to read bytes from UART. Default to "basic" functions.
|
||||||
|
rx_func_t rx_func;
|
||||||
|
} vfs_uart_context_t;
|
||||||
|
|
||||||
|
#define VFS_CTX_DEFAULT_VAL(uart_dev) (vfs_uart_context_t) {\
|
||||||
|
.uart = (uart_dev),\
|
||||||
|
.peek_char = NONE,\
|
||||||
|
.tx_mode = DEFAULT_TX_MODE,\
|
||||||
|
.rx_mode = DEFAULT_RX_MODE,\
|
||||||
|
.tx_func = uart_tx_char,\
|
||||||
|
.rx_func = uart_rx_char,\
|
||||||
|
}
|
||||||
|
|
||||||
|
//If the context should be dynamically initialized, remove this structure
|
||||||
|
//and point s_ctx to allocated data.
|
||||||
|
static vfs_uart_context_t s_context[UART_NUM] = {
|
||||||
|
VFS_CTX_DEFAULT_VAL(&UART0),
|
||||||
|
VFS_CTX_DEFAULT_VAL(&UART1),
|
||||||
#if UART_NUM > 2
|
#if UART_NUM > 2
|
||||||
&UART2
|
VFS_CTX_DEFAULT_VAL(&UART2),
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
// One-character buffer used for newline conversion code, per UART
|
static vfs_uart_context_t* s_ctx[UART_NUM] = {
|
||||||
static int s_peek_char[UART_NUM] = {
|
&s_context[0],
|
||||||
NONE,
|
&s_context[1],
|
||||||
NONE,
|
|
||||||
#if UART_NUM > 2
|
#if UART_NUM > 2
|
||||||
NONE
|
&s_context[2],
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
// per-UART locks, lazily initialized
|
|
||||||
static _lock_t s_uart_read_locks[UART_NUM];
|
|
||||||
static _lock_t s_uart_write_locks[UART_NUM];
|
|
||||||
|
|
||||||
// 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];
|
|
||||||
|
|
||||||
/* Lock ensuring that uart_select is used from only one task at the time */
|
/* Lock ensuring that uart_select is used from only one task at the time */
|
||||||
static _lock_t s_one_select_lock;
|
static _lock_t s_one_select_lock;
|
||||||
|
|
||||||
@ -88,46 +126,9 @@ static fd_set *_readfds_orig = NULL;
|
|||||||
static fd_set *_writefds_orig = NULL;
|
static fd_set *_writefds_orig = NULL;
|
||||||
static fd_set *_errorfds_orig = NULL;
|
static fd_set *_errorfds_orig = NULL;
|
||||||
|
|
||||||
// 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[UART_NUM] = { [0 ... UART_NUM-1] =
|
|
||||||
#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
|
|
||||||
};
|
|
||||||
|
|
||||||
static void uart_end_select();
|
static void uart_end_select();
|
||||||
|
|
||||||
// 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,
|
|
||||||
#if UART_NUM > 2
|
|
||||||
&uart_tx_char
|
|
||||||
#endif
|
|
||||||
};
|
|
||||||
|
|
||||||
// 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,
|
|
||||||
#if UART_NUM > 2
|
|
||||||
&uart_rx_char
|
|
||||||
#endif
|
|
||||||
};
|
|
||||||
|
|
||||||
static int uart_open(const char * path, int flags, int mode)
|
static int uart_open(const char * path, int flags, int mode)
|
||||||
{
|
{
|
||||||
@ -146,14 +147,14 @@ static int uart_open(const char * path, int flags, int mode)
|
|||||||
return fd;
|
return fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
s_non_blocking[fd] = ((flags & O_NONBLOCK) == O_NONBLOCK);
|
s_ctx[fd]->non_blocking = ((flags & O_NONBLOCK) == O_NONBLOCK);
|
||||||
|
|
||||||
return fd;
|
return fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void uart_tx_char(int fd, int c)
|
static void uart_tx_char(int fd, int c)
|
||||||
{
|
{
|
||||||
uart_dev_t* uart = s_uarts[fd];
|
uart_dev_t* uart = s_ctx[fd]->uart;
|
||||||
while (uart->status.txfifo_cnt >= 127) {
|
while (uart->status.txfifo_cnt >= 127) {
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
@ -172,7 +173,7 @@ static void uart_tx_char_via_driver(int fd, int c)
|
|||||||
|
|
||||||
static int uart_rx_char(int fd)
|
static int uart_rx_char(int fd)
|
||||||
{
|
{
|
||||||
uart_dev_t* uart = s_uarts[fd];
|
uart_dev_t* uart = s_ctx[fd]->uart;
|
||||||
if (uart->status.rxfifo_cnt == 0) {
|
if (uart->status.rxfifo_cnt == 0) {
|
||||||
return NONE;
|
return NONE;
|
||||||
}
|
}
|
||||||
@ -186,7 +187,7 @@ static int uart_rx_char(int fd)
|
|||||||
static int uart_rx_char_via_driver(int fd)
|
static int uart_rx_char_via_driver(int fd)
|
||||||
{
|
{
|
||||||
uint8_t c;
|
uint8_t c;
|
||||||
int timeout = s_non_blocking[fd] ? 0 : portMAX_DELAY;
|
int timeout = s_ctx[fd]->non_blocking ? 0 : portMAX_DELAY;
|
||||||
int n = uart_read_bytes(fd, &c, 1, timeout);
|
int n = uart_read_bytes(fd, &c, 1, timeout);
|
||||||
if (n <= 0) {
|
if (n <= 0) {
|
||||||
return NONE;
|
return NONE;
|
||||||
@ -202,18 +203,18 @@ static ssize_t uart_write(int fd, const void * data, size_t size)
|
|||||||
* a dedicated UART lock if two streams (stdout and stderr) point to the
|
* a dedicated UART lock if two streams (stdout and stderr) point to the
|
||||||
* same UART.
|
* same UART.
|
||||||
*/
|
*/
|
||||||
_lock_acquire_recursive(&s_uart_write_locks[fd]);
|
_lock_acquire_recursive(&s_ctx[fd]->write_lock);
|
||||||
for (size_t i = 0; i < size; i++) {
|
for (size_t i = 0; i < size; i++) {
|
||||||
int c = data_c[i];
|
int c = data_c[i];
|
||||||
if (c == '\n' && s_tx_mode != ESP_LINE_ENDINGS_LF) {
|
if (c == '\n' && s_ctx[fd]->tx_mode != ESP_LINE_ENDINGS_LF) {
|
||||||
s_uart_tx_func[fd](fd, '\r');
|
s_ctx[fd]->tx_func(fd, '\r');
|
||||||
if (s_tx_mode == ESP_LINE_ENDINGS_CR) {
|
if (s_ctx[fd]->tx_mode == ESP_LINE_ENDINGS_CR) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
s_uart_tx_func[fd](fd, c);
|
s_ctx[fd]->tx_func(fd, c);
|
||||||
}
|
}
|
||||||
_lock_release_recursive(&s_uart_write_locks[fd]);
|
_lock_release_recursive(&s_ctx[fd]->write_lock);
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -224,19 +225,19 @@ static ssize_t uart_write(int fd, const void * data, size_t size)
|
|||||||
static int uart_read_char(int fd)
|
static int uart_read_char(int fd)
|
||||||
{
|
{
|
||||||
/* return character from peek buffer, if it is there */
|
/* return character from peek buffer, if it is there */
|
||||||
if (s_peek_char[fd] != NONE) {
|
if (s_ctx[fd]->peek_char != NONE) {
|
||||||
int c = s_peek_char[fd];
|
int c = s_ctx[fd]->peek_char;
|
||||||
s_peek_char[fd] = NONE;
|
s_ctx[fd]->peek_char = NONE;
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
return s_uart_rx_func[fd](fd);
|
return s_ctx[fd]->rx_func(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Push back a character; it will be returned by next call to uart_read_char */
|
/* Push back a character; it will be returned by next call to uart_read_char */
|
||||||
static void uart_return_char(int fd, int c)
|
static void uart_return_char(int fd, int c)
|
||||||
{
|
{
|
||||||
assert(s_peek_char[fd] == NONE);
|
assert(s_ctx[fd]->peek_char == NONE);
|
||||||
s_peek_char[fd] = c;
|
s_ctx[fd]->peek_char = c;
|
||||||
}
|
}
|
||||||
|
|
||||||
static ssize_t uart_read(int fd, void* data, size_t size)
|
static ssize_t uart_read(int fd, void* data, size_t size)
|
||||||
@ -244,13 +245,13 @@ static ssize_t uart_read(int fd, void* data, size_t size)
|
|||||||
assert(fd >=0 && fd < 3);
|
assert(fd >=0 && fd < 3);
|
||||||
char *data_c = (char *) data;
|
char *data_c = (char *) data;
|
||||||
size_t received = 0;
|
size_t received = 0;
|
||||||
_lock_acquire_recursive(&s_uart_read_locks[fd]);
|
_lock_acquire_recursive(&s_ctx[fd]->read_lock);
|
||||||
while (received < size) {
|
while (received < size) {
|
||||||
int c = uart_read_char(fd);
|
int c = uart_read_char(fd);
|
||||||
if (c == '\r') {
|
if (c == '\r') {
|
||||||
if (s_rx_mode[fd] == ESP_LINE_ENDINGS_CR) {
|
if (s_ctx[fd]->rx_mode == ESP_LINE_ENDINGS_CR) {
|
||||||
c = '\n';
|
c = '\n';
|
||||||
} else if (s_rx_mode[fd] == ESP_LINE_ENDINGS_CRLF) {
|
} else if (s_ctx[fd]->rx_mode == ESP_LINE_ENDINGS_CRLF) {
|
||||||
/* look ahead */
|
/* look ahead */
|
||||||
int c2 = uart_read_char(fd);
|
int c2 = uart_read_char(fd);
|
||||||
if (c2 == NONE) {
|
if (c2 == NONE) {
|
||||||
@ -277,7 +278,7 @@ static ssize_t uart_read(int fd, void* data, size_t size)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_lock_release_recursive(&s_uart_read_locks[fd]);
|
_lock_release_recursive(&s_ctx[fd]->read_lock);
|
||||||
if (received > 0) {
|
if (received > 0) {
|
||||||
return received;
|
return received;
|
||||||
}
|
}
|
||||||
@ -303,11 +304,11 @@ static int uart_fcntl(int fd, int cmd, int arg)
|
|||||||
assert(fd >=0 && fd < 3);
|
assert(fd >=0 && fd < 3);
|
||||||
int result = 0;
|
int result = 0;
|
||||||
if (cmd == F_GETFL) {
|
if (cmd == F_GETFL) {
|
||||||
if (s_non_blocking[fd]) {
|
if (s_ctx[fd]->non_blocking) {
|
||||||
result |= O_NONBLOCK;
|
result |= O_NONBLOCK;
|
||||||
}
|
}
|
||||||
} else if (cmd == F_SETFL) {
|
} else if (cmd == F_SETFL) {
|
||||||
s_non_blocking[fd] = (arg & O_NONBLOCK) != 0;
|
s_ctx[fd]->non_blocking = (arg & O_NONBLOCK) != 0;
|
||||||
} else {
|
} else {
|
||||||
// unsupported operation
|
// unsupported operation
|
||||||
result = -1;
|
result = -1;
|
||||||
@ -340,9 +341,9 @@ static int uart_access(const char *path, int amode)
|
|||||||
static int uart_fsync(int fd)
|
static int uart_fsync(int fd)
|
||||||
{
|
{
|
||||||
assert(fd >= 0 && fd < 3);
|
assert(fd >= 0 && fd < 3);
|
||||||
_lock_acquire_recursive(&s_uart_write_locks[fd]);
|
_lock_acquire_recursive(&s_ctx[fd]->write_lock);
|
||||||
uart_tx_wait_idle((uint8_t) fd);
|
uart_tx_wait_idle((uint8_t) fd);
|
||||||
_lock_release_recursive(&s_uart_write_locks[fd]);
|
_lock_release_recursive(&s_ctx[fd]->write_lock);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -511,11 +512,11 @@ static int uart_tcsetattr(int fd, int optional_actions, const struct termios *p)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (p->c_iflag & IGNCR) {
|
if (p->c_iflag & IGNCR) {
|
||||||
s_rx_mode[fd] = ESP_LINE_ENDINGS_CRLF;
|
s_ctx[fd]->rx_mode = ESP_LINE_ENDINGS_CRLF;
|
||||||
} else if (p->c_iflag & ICRNL) {
|
} else if (p->c_iflag & ICRNL) {
|
||||||
s_rx_mode[fd] = ESP_LINE_ENDINGS_CR;
|
s_ctx[fd]->rx_mode = ESP_LINE_ENDINGS_CR;
|
||||||
} else {
|
} else {
|
||||||
s_rx_mode[fd] = ESP_LINE_ENDINGS_LF;
|
s_ctx[fd]->rx_mode = ESP_LINE_ENDINGS_LF;
|
||||||
}
|
}
|
||||||
|
|
||||||
// output line endings are not supported because there is no alternative in termios for converting LF to CR
|
// output line endings are not supported because there is no alternative in termios for converting LF to CR
|
||||||
@ -694,9 +695,9 @@ static int uart_tcgetattr(int fd, struct termios *p)
|
|||||||
|
|
||||||
memset(p, 0, sizeof(struct termios));
|
memset(p, 0, sizeof(struct termios));
|
||||||
|
|
||||||
if (s_rx_mode[fd] == ESP_LINE_ENDINGS_CRLF) {
|
if (s_ctx[fd]->rx_mode == ESP_LINE_ENDINGS_CRLF) {
|
||||||
p->c_iflag |= IGNCR;
|
p->c_iflag |= IGNCR;
|
||||||
} else if (s_rx_mode[fd] == ESP_LINE_ENDINGS_CR) {
|
} else if (s_ctx[fd]->rx_mode == ESP_LINE_ENDINGS_CR) {
|
||||||
p->c_iflag |= ICRNL;
|
p->c_iflag |= ICRNL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -953,31 +954,33 @@ void esp_vfs_dev_uart_register()
|
|||||||
void esp_vfs_dev_uart_set_rx_line_endings(esp_line_endings_t mode)
|
void esp_vfs_dev_uart_set_rx_line_endings(esp_line_endings_t mode)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < UART_NUM; ++i) {
|
for (int i = 0; i < UART_NUM; ++i) {
|
||||||
s_rx_mode[i] = mode;
|
s_ctx[i]->rx_mode = mode;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void esp_vfs_dev_uart_set_tx_line_endings(esp_line_endings_t mode)
|
void esp_vfs_dev_uart_set_tx_line_endings(esp_line_endings_t mode)
|
||||||
{
|
{
|
||||||
s_tx_mode = mode;
|
for (int i = 0; i < UART_NUM; ++i) {
|
||||||
|
s_ctx[i]->tx_mode = mode;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void esp_vfs_dev_uart_use_nonblocking(int uart_num)
|
void esp_vfs_dev_uart_use_nonblocking(int uart_num)
|
||||||
{
|
{
|
||||||
_lock_acquire_recursive(&s_uart_read_locks[uart_num]);
|
_lock_acquire_recursive(&s_ctx[uart_num]->read_lock);
|
||||||
_lock_acquire_recursive(&s_uart_write_locks[uart_num]);
|
_lock_acquire_recursive(&s_ctx[uart_num]->write_lock);
|
||||||
s_uart_tx_func[uart_num] = uart_tx_char;
|
s_ctx[uart_num]->tx_func = uart_tx_char;
|
||||||
s_uart_rx_func[uart_num] = uart_rx_char;
|
s_ctx[uart_num]->rx_func = uart_rx_char;
|
||||||
_lock_release_recursive(&s_uart_write_locks[uart_num]);
|
_lock_release_recursive(&s_ctx[uart_num]->write_lock);
|
||||||
_lock_release_recursive(&s_uart_read_locks[uart_num]);
|
_lock_release_recursive(&s_ctx[uart_num]->read_lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
void esp_vfs_dev_uart_use_driver(int uart_num)
|
void esp_vfs_dev_uart_use_driver(int uart_num)
|
||||||
{
|
{
|
||||||
_lock_acquire_recursive(&s_uart_read_locks[uart_num]);
|
_lock_acquire_recursive(&s_ctx[uart_num]->read_lock);
|
||||||
_lock_acquire_recursive(&s_uart_write_locks[uart_num]);
|
_lock_acquire_recursive(&s_ctx[uart_num]->write_lock);
|
||||||
s_uart_tx_func[uart_num] = uart_tx_char_via_driver;
|
s_ctx[uart_num]->tx_func = uart_tx_char_via_driver;
|
||||||
s_uart_rx_func[uart_num] = uart_rx_char_via_driver;
|
s_ctx[uart_num]->rx_func = uart_rx_char_via_driver;
|
||||||
_lock_release_recursive(&s_uart_write_locks[uart_num]);
|
_lock_release_recursive(&s_ctx[uart_num]->write_lock);
|
||||||
_lock_release_recursive(&s_uart_read_locks[uart_num]);
|
_lock_release_recursive(&s_ctx[uart_num]->read_lock);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user