VFS Terminal cleanup

This commit is contained in:
David Cermak committed 2021-10-04 11:32:55 +02:00
1 parent 9f4fe3a177
commit 59c3837345
11 files changed
+268 -264

No files matched your search

+25 -76
View File
@@ -25,22 +25,13 @@ static const char *TAG = "uart_terminal";
namespace esp_modem {
/**
* @brief Uart Resource is a platform specific struct which is implemented separately
*/
struct uart_resource {
explicit uart_resource(const esp_modem_dte_config *config);
explicit uart_resource(const esp_modem_dte_config *config, struct QueueDefinition** event_queue);
~uart_resource();
bool get_event(uart_event_t &event, uint32_t time_ms) {
return xQueueReceive(event_queue, &event, pdMS_TO_TICKS(time_ms));
}
void reset_events() {
uart_flush_input(port);
xQueueReset(event_queue);
}
uart_port_t port; /*!< UART port */
QueueHandle_t event_queue; /*!< UART event queue handle */
uart_port_t port;
};
struct uart_task {
@@ -57,64 +48,13 @@ struct uart_task {
TaskHandle_t task_handle; /*!< UART event task handle */
};
uart_resource::~uart_resource()
{
if (port >= UART_NUM_0 && port < UART_NUM_MAX) {
uart_driver_delete(port);
}
}
uart_resource::uart_resource(const esp_modem_dte_config *config) :
port(-1)
{
esp_err_t res;
/* Config UART */
uart_config_t uart_config = {};
uart_config.baud_rate = config->uart_config.baud_rate;
uart_config.data_bits = config->uart_config.data_bits;
uart_config.parity = config->uart_config.parity;
uart_config.stop_bits = config->uart_config.stop_bits;
uart_config.flow_ctrl = (config->uart_config.flow_control == ESP_MODEM_FLOW_CONTROL_HW) ? UART_HW_FLOWCTRL_CTS_RTS
: UART_HW_FLOWCTRL_DISABLE;
uart_config.source_clk = UART_SCLK_APB;
throw_if_esp_fail(uart_param_config(config->uart_config.port_num, &uart_config), "config uart parameter failed");
if (config->uart_config.flow_control == ESP_MODEM_FLOW_CONTROL_HW) {
res = uart_set_pin(config->uart_config.port_num, config->uart_config.tx_io_num, config->uart_config.rx_io_num,
config->uart_config.rts_io_num, config->uart_config.cts_io_num);
} else {
res = uart_set_pin(config->uart_config.port_num, config->uart_config.tx_io_num, config->uart_config.rx_io_num,
UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
}
throw_if_esp_fail(res, "config uart gpio failed");
/* Set flow control threshold */
if (config->uart_config.flow_control == ESP_MODEM_FLOW_CONTROL_HW) {
res = uart_set_hw_flow_ctrl(config->uart_config.port_num, UART_HW_FLOWCTRL_CTS_RTS, UART_FIFO_LEN - 8);
} else if (config->uart_config.flow_control == ESP_MODEM_FLOW_CONTROL_SW) {
res = uart_set_sw_flow_ctrl(config->uart_config.port_num, true, 8, UART_FIFO_LEN - 8);
}
throw_if_esp_fail(res, "config uart flow control failed");
/* Install UART driver and get event queue used inside driver */
res = uart_driver_install(config->uart_config.port_num, config->uart_config.rx_buffer_size, config->uart_config.tx_buffer_size,
config->uart_config.event_queue_size, &(event_queue), 0);
throw_if_esp_fail(res, "install uart driver failed");
throw_if_esp_fail(uart_set_rx_timeout(config->uart_config.port_num, 1), "set rx timeout failed");
throw_if_esp_fail(uart_set_rx_full_threshold(config->uart_config.port_num, 64), "config rx full threshold failed");
/* mark UART as initialized */
port = config->uart_config.port_num;
}
class uart_terminal : public Terminal {
public:
explicit uart_terminal(const esp_modem_dte_config *config) :
uart(config), signal(),
task_handle(config->uart_config.event_task_stack_size, config->uart_config.event_task_priority, this, s_task) {}
event_queue(), uart(config, &event_queue), signal(),
task_handle(config->task_stack_size, config->task_priority, this, s_task) {}
~uart_terminal() override = default;
@@ -139,16 +79,25 @@ private:
static void s_task(void *task_param) {
auto t = static_cast<uart_terminal *>(task_param);
t->task();
vTaskDelete(NULL);
vTaskDelete(nullptr);
}
void task();
bool get_event(uart_event_t &event, uint32_t time_ms) {
return xQueueReceive(event_queue, &event, pdMS_TO_TICKS(time_ms));
}
void reset_events() {
uart_flush_input(uart.port);
xQueueReset(event_queue);
}
static const size_t TASK_INIT = BIT0;
static const size_t TASK_START = BIT1;
static const size_t TASK_STOP = BIT2;
static const size_t TASK_PARAMS = BIT3;
QueueHandle_t event_queue;
uart_resource uart;
SignalGroup signal;
uart_task task_handle;
@@ -172,7 +121,7 @@ void uart_terminal::task() {
return; // exits to the static method where the task gets deleted
}
while (signal.is_any(TASK_START)) {
if (uart.get_event(event, 100)) {
if (get_event(event, 100)) {
if (signal.is_any(TASK_PARAMS)) {
on_data_priv = on_data;
signal.clear(TASK_PARAMS);
@@ -190,13 +139,13 @@ void uart_terminal::task() {
ESP_LOGW(TAG, "HW FIFO Overflow");
if (on_error)
on_error(terminal_error::BUFFER_OVERFLOW);
uart.reset_events();
reset_events();
break;
case UART_BUFFER_FULL:
ESP_LOGW(TAG, "Ring Buffer Full");
if (on_error)
on_error(terminal_error::BUFFER_OVERFLOW);
uart.reset_events();
reset_events();
break;
case UART_BREAK:
ESP_LOGW(TAG, "Rx Break");
@@ -224,11 +173,11 @@ void uart_terminal::task() {
int uart_terminal::read(uint8_t *data, size_t len) {
size_t length = 0;
uart_get_buffered_data_len(uart.port, &length);
if (esp_random() < UINT32_MAX/4 && length > 32) {
printf("ahoj!\n");
length -= length/4;
}
size_t new_size = length/2;
// if (esp_random() < UINT32_MAX/4 && length > 32) {
// printf("ahoj!\n");
// length -= length/4;
// }
// size_t new_size = length/2;
length = std::min(len, length);
if (length > 0) {
return uart_read_bytes(uart.port, data, length, portMAX_DELAY);