Skip to content

Commit

Permalink
refactor: esp serial using ringbuffer class for rx
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxwelltoo committed Jan 12, 2024
1 parent 0945801 commit 3168963
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 72 deletions.
4 changes: 4 additions & 0 deletions porting/el_serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 2 additions & 5 deletions porting/espressif/el_network_esp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
},
Expand All @@ -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;
Expand Down
68 changes: 12 additions & 56 deletions porting/espressif/el_serial_esp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,13 @@

#include <cctype>

#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(); }

Expand All @@ -53,23 +48,18 @@ 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;
}

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;
}
Expand All @@ -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) {
Expand Down
6 changes: 2 additions & 4 deletions porting/espressif/el_serial_esp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions porting/himax/we2/el_serial_we2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ extern "C" {
#include <cctype>
#include <cstdint>

#include "core/el_debug.h"
#include "core/el_types.h"
#include "core/utils/el_ringbuffer.hpp"
#include "el_config_porting.h"
namespace edgelab {

Expand Down
2 changes: 1 addition & 1 deletion sscma/definations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 10 additions & 3 deletions sscma/main_task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool>&) { static_resource->is_ready.store(true); });
static_resource->executor->add_task(
[](const std::atomic<bool>&) { static_resource->is_ready.store(true); }
);

EL_LOGI("[SSCMA] AT server is ready to use :)");

auto transports = std::forward_list<Transport*>{static_resource->serial, static_resource->mqtt, static_resource->wire};
char* buf = reinterpret_cast<char*>(el_aligned_malloc_once(16, SSCMA_CMD_MAX_LENGTH + 1));
auto transports = std::forward_list<Transport*>{
static_resource->serial,
static_resource->mqtt,
static_resource->wire
};
char* buf = reinterpret_cast<char*>(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)) {
Expand Down

0 comments on commit 3168963

Please sign in to comment.