Skip to content
Snippets Groups Projects
Commit 6a3ae9d0 authored by fxk8y's avatar fxk8y :spider:
Browse files

Finishing reader task

parent aff63ed2
No related branches found
No related tags found
No related merge requests found
......@@ -26,6 +26,16 @@ static const char* TAG = "CyanBus";
namespace SiliconTorch {
// namespace Impl {
//
// typedef struct {
// bool crcOK;
// uint16_t length;
// uint8_t payload[CyanBus::MTU];
// } CyanData;
//
// }
namespace CyanBus {
// CyanBus envelope header
......@@ -85,17 +95,19 @@ namespace SiliconTorch {
rxEN(true);
readerTaskHandle = new SiliconTorch::Util::LambdaTask([&](){ readerTask(); });
readerTaskHandle = new Util::LambdaTask([&](){ readerTask(); });
packetQ = xQueueCreate(3, sizeof(CyanData));
callbackTaskHandle = new SiliconTorch::Util::LambdaTask([&](){ callbackTask(); });
// packetQ = xQueueCreate(3, sizeof(Impl::CyanData));
packetQ = xQueueCreate(3, 4096);
packetTaskHandle = new Util::LambdaTask([&](){ packetTask(); });
}
void CyanBus::readerTask() {
uint32_t bufLEN = MTU + 32; // header[6] + length[2] + payload[MTU] + CRC[4] + some slack…
uint32_t bufLEN = MTU;
uint8_t* buffer = new uint8_t[bufLEN]; // our packet buffer
uint8_t* bufPTR = buffer; // points inside our buffer; used for envelope parsing…
// uint16_t payloadLEN = 0;
std::memset(buffer, 0x00, bufLEN);
......@@ -122,9 +134,12 @@ namespace SiliconTorch {
enum FSM { SearchHeader, ReadLength, ReadPayload, CheckCRC };
/* enum FSM { SearchHeader, ReadLength, ReadPayload, CheckCRC };
FSM fsm = SearchHeader;
uint8_t headerLength = std::strlen(HEADER);
uint32_t headerByteNum = 0; */
uart_event_t event;
while (true) {
......@@ -140,26 +155,56 @@ namespace SiliconTorch {
// received plain data
case UART_DATA: {
uint32_t bytesRead = bufPTR - buffer;
if (bytesRead + event.size > bufLEN) {
ESP_LOGE(TAG, "Buffer#[ %d ] Overflow: Got %d bytes without a break", bufLEN, bytesRead + event.size);
if (event.size < bufLEN) { // read into buffer and just start over
uart_read_bytes((uart_port_t)uartChannel, buffer, event.size, portMAX_DELAY);
bufPTR = buffer;
} else { // kill it with 🔥
uart_flush_input((uart_port_t)uartChannel);
bufPTR = buffer;
}
} else {
uart_read_bytes((uart_port_t)uartChannel, bufPTR, event.size, portMAX_DELAY);
bufPTR += event.size;
}
break;
}
// FIFO overflow
case UART_FIFO_OVF: {
ESP_LOGW(TAG, "FIFO overflow! 😡");
ESP_LOGE(TAG, "FIFO overflow! 😡");
uart_flush_input((uart_port_t)uartChannel);
bufPTR = buffer;
break;
}
// internal buffer full
case UART_BUFFER_FULL: {
ESP_LOGW(TAG, "Buffer full! 😡");
ESP_LOGE(TAG, "Internal Buffer overflow! 😡");
uart_flush_input((uart_port_t)uartChannel);
bufPTR = buffer;
break;
}
// got an RX break signal
case UART_BREAK: {
ESP_LOGI(TAG, "BREAK received 🛑");
ESP_LOGI(TAG, "Received BREAK 🛑 after reading %d bytes", bufPTR - buffer);
// TODO: handle message end!!!
xQueueSendToBack(packetQ, buffer, portMAX_DELAY);
bufPTR = buffer;
break;
}
......@@ -180,8 +225,27 @@ namespace SiliconTorch {
delete buffer;
}
void CyanBus::callbackTask() {
void CyanBus::packetTask() {
ESP_LOGW(TAG, "TODO: implement callback task!");
uint8_t* buffer = new uint8_t[MTU];
while (true) {
if (xQueueReceive(packetQ, buffer, (TickType_t)portMAX_DELAY)) {
// TODO: handle packet!
char strbuf[17];
strbuf[16] = 0x00;
std::memcpy(strbuf, buffer, 16);
ESP_LOGI(TAG, "PACKET DEBUG: Starts with [ %s ]", strbuf);
// TODO: really handle packet!
}
}
}
void CyanBus::setBaudRate(uint32_t baudRate) {
......
......@@ -25,13 +25,6 @@ namespace SiliconTorch {
extern const uint8_t* const HEADER;
typedef struct {
bool crc;
uint16_t length;
uint8_t payload[MTU];
} CyanData;
class CyanBus {
public:
CyanBus(uint8_t tx, uint8_t rx, uint8_t de, uint8_t re, uint32_t baudRate = 115200, uint8_t uartChannel = 1);
......@@ -60,10 +53,10 @@ namespace SiliconTorch {
void readerTask();
void callbackTask();
void packetTask();
SiliconTorch::Util::LambdaTask* readerTaskHandle = NULL;
SiliconTorch::Util::LambdaTask* callbackTaskHandle = NULL;
Util::LambdaTask* readerTaskHandle = NULL;
Util::LambdaTask* packetTaskHandle = NULL;
};
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment