Add basic support for termios.h

Closes https://github.com/espressif/esp-idf/issues/2063
This commit is contained in:
Roland Dobai
2018-08-14 13:39:30 +02:00
parent 499d06746b
commit 5395435816
7 changed files with 1087 additions and 7 deletions
+132
View File
@@ -15,6 +15,9 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/termios.h>
#include <sys/errno.h>
#include "unity.h"
#include "rom/uart.h"
#include "soc/uart_struct.h"
@@ -23,6 +26,7 @@
#include "freertos/semphr.h"
#include "driver/uart.h"
#include "esp_vfs_dev.h"
#include "esp_vfs.h"
#include "sdkconfig.h"
static void fwrite_str_loopback(const char* str, size_t size)
@@ -198,3 +202,131 @@ TEST_CASE("can write to UART while another task is reading", "[vfs]")
vSemaphoreDelete(read_arg.done);
vSemaphoreDelete(write_arg.done);
}
#ifdef CONFIG_SUPPORT_TERMIOS
TEST_CASE("Can use termios for UART", "[vfs]")
{
uart_config_t uart_config = {
.baud_rate = 115200,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE
};
uart_param_config(UART_NUM_1, &uart_config);
uart_driver_install(UART_NUM_1, 256, 256, 0, NULL, 0);
const int uart_fd = open("/dev/uart/1", O_RDWR);
TEST_ASSERT_NOT_EQUAL_MESSAGE(uart_fd, -1, "Cannot open UART");
esp_vfs_dev_uart_use_driver(1);
TEST_ASSERT_EQUAL(-1, tcgetattr(uart_fd, NULL));
TEST_ASSERT_EQUAL(EINVAL, errno);
struct termios tios, tios_result;
TEST_ASSERT_EQUAL(-1, tcgetattr(-1, &tios));
TEST_ASSERT_EQUAL(EBADF, errno);
TEST_ASSERT_EQUAL(0, tcgetattr(uart_fd, &tios));
TEST_ASSERT_EQUAL(0, tcsetattr(uart_fd, TCSADRAIN, &tios));
TEST_ASSERT_EQUAL(0, tcsetattr(uart_fd, TCSAFLUSH, &tios));
tios.c_iflag |= IGNCR;
TEST_ASSERT_EQUAL(0, tcsetattr(uart_fd, TCSANOW, &tios));
tios.c_iflag &= (~IGNCR);
TEST_ASSERT_EQUAL(0, tcgetattr(uart_fd, &tios_result));
TEST_ASSERT_EQUAL(IGNCR, tios_result.c_iflag & IGNCR);
memset(&tios_result, 0xFF, sizeof(struct termios));
tios.c_iflag |= ICRNL;
TEST_ASSERT_EQUAL(0, tcsetattr(uart_fd, TCSANOW, &tios));
tios.c_iflag &= (~ICRNL);
TEST_ASSERT_EQUAL(0, tcgetattr(uart_fd, &tios_result));
TEST_ASSERT_EQUAL(ICRNL, tios_result.c_iflag & ICRNL);
memset(&tios_result, 0xFF, sizeof(struct termios));
{
uart_word_length_t data_bit;
uart_stop_bits_t stop_bits;
uart_parity_t parity_mode;
tios.c_cflag &= (~CSIZE);
tios.c_cflag &= (~CSTOPB);
tios.c_cflag &= (~PARENB);
tios.c_cflag |= CS6;
TEST_ASSERT_EQUAL(0, tcsetattr(uart_fd, TCSANOW, &tios));
tios.c_cflag &= (~CSIZE);
TEST_ASSERT_EQUAL(0, tcgetattr(uart_fd, &tios_result));
TEST_ASSERT_EQUAL(CS6, tios_result.c_cflag & CS6);
TEST_ASSERT_EQUAL(ESP_OK, uart_get_word_length(UART_NUM_1, &data_bit));
TEST_ASSERT_EQUAL(UART_DATA_6_BITS, data_bit);
TEST_ASSERT_EQUAL(0, tios_result.c_cflag & CSTOPB);
TEST_ASSERT_EQUAL(ESP_OK, uart_get_stop_bits(UART_NUM_1, &stop_bits));
TEST_ASSERT_EQUAL(UART_STOP_BITS_1, stop_bits);
TEST_ASSERT_EQUAL(ESP_OK, uart_get_parity(UART_NUM_1, &parity_mode));
TEST_ASSERT_EQUAL(UART_PARITY_DISABLE, parity_mode);
memset(&tios_result, 0xFF, sizeof(struct termios));
}
{
uart_stop_bits_t stop_bits;
uart_parity_t parity_mode;
tios.c_cflag |= CSTOPB;
tios.c_cflag |= (PARENB | PARODD);
TEST_ASSERT_EQUAL(0, tcsetattr(uart_fd, TCSANOW, &tios));
tios.c_cflag &= (~(CSTOPB | PARENB | PARODD));
TEST_ASSERT_EQUAL(0, tcgetattr(uart_fd, &tios_result));
TEST_ASSERT_EQUAL(CSTOPB, tios_result.c_cflag & CSTOPB);
TEST_ASSERT_EQUAL(ESP_OK, uart_get_stop_bits(UART_NUM_1, &stop_bits));
TEST_ASSERT_EQUAL(UART_STOP_BITS_2, stop_bits);
TEST_ASSERT_EQUAL(ESP_OK, uart_get_parity(UART_NUM_1, &parity_mode));
TEST_ASSERT_EQUAL(UART_PARITY_ODD, parity_mode);
memset(&tios_result, 0xFF, sizeof(struct termios));
}
{
uint32_t baudrate;
tios.c_cflag &= (~BOTHER);
tios.c_cflag |= CBAUD;
tios.c_ispeed = tios.c_ospeed = B38400;
TEST_ASSERT_EQUAL(0, tcsetattr(uart_fd, TCSANOW, &tios));
TEST_ASSERT_EQUAL(0, tcgetattr(uart_fd, &tios_result));
TEST_ASSERT_EQUAL(CBAUD, tios_result.c_cflag & CBAUD);
TEST_ASSERT_EQUAL(B38400, tios_result.c_ispeed);
TEST_ASSERT_EQUAL(B38400, tios_result.c_ospeed);
TEST_ASSERT_EQUAL(ESP_OK, uart_get_baudrate(UART_NUM_1, &baudrate));
TEST_ASSERT_EQUAL(38400, baudrate);
tios.c_cflag |= CBAUDEX;
tios.c_ispeed = tios.c_ospeed = B230400;
TEST_ASSERT_EQUAL(0, tcsetattr(uart_fd, TCSANOW, &tios));
TEST_ASSERT_EQUAL(0, tcgetattr(uart_fd, &tios_result));
TEST_ASSERT_EQUAL(BOTHER, tios_result.c_cflag & BOTHER);
// Setting the speed to 230400 will set it actually to 230423
TEST_ASSERT_EQUAL(230423, tios_result.c_ispeed);
TEST_ASSERT_EQUAL(230423, tios_result.c_ospeed);
TEST_ASSERT_EQUAL(ESP_OK, uart_get_baudrate(UART_NUM_1, &baudrate));
TEST_ASSERT_EQUAL(230423, baudrate);
tios.c_cflag |= BOTHER;
tios.c_ispeed = tios.c_ospeed = 213;
TEST_ASSERT_EQUAL(0, tcsetattr(uart_fd, TCSANOW, &tios));
TEST_ASSERT_EQUAL(0, tcgetattr(uart_fd, &tios_result));
TEST_ASSERT_EQUAL(BOTHER, tios_result.c_cflag & BOTHER);
TEST_ASSERT_EQUAL(213, tios_result.c_ispeed);
TEST_ASSERT_EQUAL(213, tios_result.c_ospeed);
TEST_ASSERT_EQUAL(ESP_OK, uart_get_baudrate(UART_NUM_1, &baudrate));
TEST_ASSERT_EQUAL(213, baudrate);
memset(&tios_result, 0xFF, sizeof(struct termios));
}
esp_vfs_dev_uart_use_nonblocking(1);
close(uart_fd);
uart_driver_delete(UART_NUM_1);
}
#endif // CONFIG_SUPPORT_TERMIOS