From 31689633bc0f3ffb6dd1e4eade77c3c94ccad1e3 Mon Sep 17 00:00:00 2001 From: Jiaxuan Weng <1391548050@qq.com> Date: Fri, 12 Jan 2024 11:21:08 +0800 Subject: [PATCH] refactor: esp serial using ringbuffer class for rx --- porting/el_serial.h | 4 ++ porting/espressif/el_network_esp.cpp | 7 +-- porting/espressif/el_serial_esp.cpp | 68 +++++----------------------- porting/espressif/el_serial_esp.h | 6 +-- porting/himax/we2/el_serial_we2.cpp | 3 -- sscma/definations.hpp | 2 +- sscma/main_task.hpp | 13 ++++-- 7 files changed, 31 insertions(+), 72 deletions(-) diff --git a/porting/el_serial.h b/porting/el_serial.h index b1fa8f6c..55b5721d 100644 --- a/porting/el_serial.h +++ b/porting/el_serial.h @@ -28,6 +28,10 @@ #include "porting/el_transport.h" +#include "core/el_debug.h" +#include "core/el_types.h" +#include "core/utils/el_ringbuffer.hpp" + namespace edgelab { class Serial : public Transport { diff --git a/porting/espressif/el_network_esp.cpp b/porting/espressif/el_network_esp.cpp index 72a008b0..55b76f9b 100644 --- a/porting/espressif/el_network_esp.cpp +++ b/porting/espressif/el_network_esp.cpp @@ -209,7 +209,8 @@ el_err_code_t NetworkEsp::connect(mqtt_server_config_t mqtt_cfg, topic_cb_t cb) .broker = { .address = { .hostname = mqtt_cfg.address, - .transport = (mqtt_cfg.use_ssl) ? MQTT_TRANSPORT_OVER_SSL : MQTT_TRANSPORT_OVER_TCP, + .transport = (mqtt_cfg.use_ssl == 1) ? + MQTT_TRANSPORT_OVER_SSL : MQTT_TRANSPORT_OVER_TCP, .port = mqtt_cfg.port, } }, @@ -220,10 +221,6 @@ el_err_code_t NetworkEsp::connect(mqtt_server_config_t mqtt_cfg, topic_cb_t cb) .password = mqtt_cfg.password } }, - .task = { - .priority = 3, - .stack_size = 8192 - } }; if (mqtt_cfg.use_ssl) { esp_mqtt_cfg.broker.verification.certificate = (const char *)ca_crt; diff --git a/porting/espressif/el_serial_esp.cpp b/porting/espressif/el_serial_esp.cpp index 21c2822a..b426dcae 100644 --- a/porting/espressif/el_serial_esp.cpp +++ b/porting/espressif/el_serial_esp.cpp @@ -29,18 +29,13 @@ #include -#include "core/el_debug.h" -#include "core/el_types.h" - namespace edgelab { SerialEsp::SerialEsp(usb_serial_jtag_driver_config_t driver_config) : _driver_config(driver_config), _send_lock(), _size(driver_config.rx_buffer_size), - _buffer(nullptr), - _head(0), - _tail(0) {} + _rb_rx(nullptr) {} SerialEsp::~SerialEsp() { deinit(); } @@ -53,9 +48,10 @@ el_err_code_t SerialEsp::init() { if (!this->_is_present) [[unlikely]] return EL_EPERM; - _buffer = new char[_size]{}; + if (!this->_rb_rx) [[likely]] + this->_rb_rx = new lwRingBuffer{_size}; - EL_ASSERT(_buffer); + EL_ASSERT(this->_rb_rx); return EL_OK; } @@ -63,13 +59,7 @@ el_err_code_t SerialEsp::init() { el_err_code_t SerialEsp::deinit() { this->_is_present = !(usb_serial_jtag_driver_uninstall() == ESP_OK); - if (_buffer) { - delete[] _buffer; - _buffer = nullptr; - } - - _head = 0; - _tail = 0; + delete this->_rb_rx; return !this->_is_present ? EL_OK : EL_EIO; } @@ -95,48 +85,14 @@ char SerialEsp::get_char() { std::size_t SerialEsp::get_line(char* buffer, size_t size, const char delim) { if (!this->_is_present) return 0; - { - std::size_t len = 0; - std::size_t read = 0; - auto head = _head; - auto tail = _tail; - auto remain = head < tail ? tail - head : _size - (head - tail); - - do { - len += read = usb_serial_jtag_read_bytes(_buffer + head, 1, 1 / portTICK_PERIOD_MS); - head = (head + read) % _size; - } while (read); - - // the store operation makes get_line thread-unsafe - if (len > remain) [[unlikely]] - _tail = (head + 1) % _size; - - _head = head; - } - - std::size_t len = 0; - std::size_t const len_max = size - 1; - auto found = false; - auto head = _head; - auto tail = _tail; - auto prev = tail; - - if (head == tail) return 0; - - for (; (!found) & (head != tail) & (len < len_max); tail = (tail + 1) % _size, ++len) { - char c = _buffer[tail]; - if (c == '\0') - break; - else if (c == delim) - found = true; - } - - if (!found) return 0; - - for (std::size_t i = 0; i < len; ++i) buffer[i] = _buffer[(prev + i) % _size]; - _tail = tail; + size_t rlen = 0; + char rbuf[32] = {0}; // Most commands are less than 32 bytes long + do { + rlen = usb_serial_jtag_read_bytes(rbuf, sizeof(rbuf), 1 / portTICK_PERIOD_MS); + this->_rb_rx->put(rbuf, rlen); + } while (rlen > 0); - return len; + return this->_rb_rx->extract(delim, buffer, size); } std::size_t SerialEsp::read_bytes(char* buffer, size_t size) { diff --git a/porting/espressif/el_serial_esp.h b/porting/espressif/el_serial_esp.h index cc0b5f0f..10ea6a1b 100644 --- a/porting/espressif/el_serial_esp.h +++ b/porting/espressif/el_serial_esp.h @@ -53,10 +53,8 @@ class SerialEsp final : public Serial { usb_serial_jtag_driver_config_t _driver_config; Mutex _send_lock; - const std::size_t _size; - char* _buffer; - volatile std::size_t _head; - volatile std::size_t _tail; + std::size_t _size; + lwRingBuffer* _rb_rx; }; } // namespace edgelab diff --git a/porting/himax/we2/el_serial_we2.cpp b/porting/himax/we2/el_serial_we2.cpp index c3864243..a86f9858 100644 --- a/porting/himax/we2/el_serial_we2.cpp +++ b/porting/himax/we2/el_serial_we2.cpp @@ -33,9 +33,6 @@ extern "C" { #include #include -#include "core/el_debug.h" -#include "core/el_types.h" -#include "core/utils/el_ringbuffer.hpp" #include "el_config_porting.h" namespace edgelab { diff --git a/sscma/definations.hpp b/sscma/definations.hpp index e642eb19..bf04a9c4 100644 --- a/sscma/definations.hpp +++ b/sscma/definations.hpp @@ -14,7 +14,7 @@ #endif #define SSCMA_REPL_SUPERVISOR_NAME "sscma#supervisor" -#define SSCMA_REPL_SUPERVISOR_STACK_SIZE 8192U +#define SSCMA_REPL_SUPERVISOR_STACK_SIZE 6144U #ifndef SSCMA_REPL_SUPERVISOR_PRIO #define SSCMA_REPL_SUPERVISOR_PRIO 1 #endif diff --git a/sscma/main_task.hpp b/sscma/main_task.hpp index 1d44296d..073d49cc 100644 --- a/sscma/main_task.hpp +++ b/sscma/main_task.hpp @@ -323,13 +323,20 @@ void register_commands() { void wait_for_inputs() { // mark the system status as ready - static_resource->executor->add_task([](const std::atomic&) { static_resource->is_ready.store(true); }); + static_resource->executor->add_task( + [](const std::atomic&) { static_resource->is_ready.store(true); } + ); EL_LOGI("[SSCMA] AT server is ready to use :)"); - auto transports = std::forward_list{static_resource->serial, static_resource->mqtt, static_resource->wire}; - char* buf = reinterpret_cast(el_aligned_malloc_once(16, SSCMA_CMD_MAX_LENGTH + 1)); + auto transports = std::forward_list{ + static_resource->serial, + static_resource->mqtt, + static_resource->wire + }; + char* buf = reinterpret_cast(el_aligned_malloc_once(16, SSCMA_CMD_MAX_LENGTH + 1)); std::memset(buf, 0, SSCMA_CMD_MAX_LENGTH + 1); + Loop: std::for_each(transports.begin(), transports.end(), [&buf](Transport* transport) { if (transport && *transport && transport->get_line(buf, SSCMA_CMD_MAX_LENGTH)) {