From 3925b16fb02424f51ca0d4f9be788eff47ba3522 Mon Sep 17 00:00:00 2001 From: Jochen Vothknecht <jochen3120@gmail.com> Date: Mon, 18 Apr 2022 20:38:23 +0200 Subject: [PATCH] Massive clean-up before further development --- .../{CyanBusBREAK.cpp => CyanBus.cpp} | 58 ++--- .../{CyanBusBREAK.hpp => CyanBus.hpp} | 2 +- CLC-qthing/SiliconTorch/CyanBusTOUT.cpp | 217 ------------------ CLC-qthing/SiliconTorch/CyanBusTOUT.hpp | 62 ----- CLC-qthing/device_main.cpp | 7 +- SiliconTorch/CyanBus.py | 37 ++- 6 files changed, 58 insertions(+), 325 deletions(-) rename CLC-qthing/SiliconTorch/{CyanBusBREAK.cpp => CyanBus.cpp} (86%) rename CLC-qthing/SiliconTorch/{CyanBusBREAK.hpp => CyanBus.hpp} (97%) delete mode 100644 CLC-qthing/SiliconTorch/CyanBusTOUT.cpp delete mode 100644 CLC-qthing/SiliconTorch/CyanBusTOUT.hpp diff --git a/CLC-qthing/SiliconTorch/CyanBusBREAK.cpp b/CLC-qthing/SiliconTorch/CyanBus.cpp similarity index 86% rename from CLC-qthing/SiliconTorch/CyanBusBREAK.cpp rename to CLC-qthing/SiliconTorch/CyanBus.cpp index 2ab8e76..d9c8175 100644 --- a/CLC-qthing/SiliconTorch/CyanBusBREAK.cpp +++ b/CLC-qthing/SiliconTorch/CyanBus.cpp @@ -1,4 +1,4 @@ -#include "CyanBusBREAK.hpp" +#include "CyanBus.hpp" // C++ system level #include <cstring> // memset @@ -36,7 +36,7 @@ namespace SiliconTorch { // // } - namespace CyanBusBREAK { + namespace CyanBus { // CyanBus envelope header const uint8_t* const HEADER = (const uint8_t*)"fxCyan"; @@ -93,7 +93,7 @@ namespace SiliconTorch { ESP_ERROR_CHECK(uart_set_mode(_ch, UART_MODE_UART)); // WARNING: The following call breaks EVERYTHING! - // uart_set_rx_timeout(_ch, 0); + uart_set_rx_timeout(_ch, 5); // TOOD: receive only ATM! @@ -120,12 +120,6 @@ namespace SiliconTorch { std::memset(buffer, 0x00, bufLEN); - // Warning: this is flawed! sizeof(buffer) returns the pointer size, not the array length! - // TODO: Remove next commit or so!!! - // ESP_LOGW(TAG, "bufLEN = %d BufferSize[MTU * 1.2] = %d", bufLEN, (int)sizeof(buffer)); - - - /* enum FSM { SearchHeader, ReadLength, ReadPayload, CheckCRC }; FSM fsm = SearchHeader; @@ -192,30 +186,6 @@ namespace SiliconTorch { break; } - // FIFO overflow - case UART_FIFO_OVF: { - ESP_LOGE(TAG, "FIFO overflow! 😡"); - - uart_flush_input(_ch); - bufPTR = buffer; - std::memset(buffer, 0x00, bufLEN); // TODO: needed…? - - - break; - } - - // internal buffer full - case UART_BUFFER_FULL: { - ESP_LOGE(TAG, "Internal Buffer overflow! 😡"); - - uart_flush_input(_ch); - bufPTR = buffer; - std::memset(buffer, 0x00, bufLEN); // TODO: needed…? - - - break; - } - // got an RX break signal case UART_BREAK: { ESP_LOGI(TAG, "Received BREAK 🛑 after reading %d bytes", bufPTR - buffer); @@ -239,8 +209,26 @@ namespace SiliconTorch { } default: { - // pattern detection, parity and other stuff is unused atm and will surface here - // just ignore everything unknown + + char* eventName; + + switch (event.type) { + case UART_DATA: { eventName = (char*)"UART_DATA"; break; } + case UART_BREAK: { eventName = (char*)"UART_BREAK"; break; } + case UART_BUFFER_FULL: { eventName = (char*)"UART_BUFFER_FULL"; break; } + case UART_FIFO_OVF: { eventName = (char*)"UART_FIFO_OVF"; break; } + case UART_FRAME_ERR: { eventName = (char*)"UART_FRAME_ERR"; break; } + case UART_PARITY_ERR: { eventName = (char*)"UART_PARITY_ERR"; break; } + case UART_DATA_BREAK: { eventName = (char*)"UART_DATA_BREAK"; break; } + case UART_PATTERN_DET: { eventName = (char*)"UART_PATTERN_DET"; break; } + default: { eventName = (char*)"_UNKNOWN_"; break; } + } + + ESP_LOGE(TAG, "Unexpected event[ %s ]!", eventName); + + uart_flush_input(_ch); + bufPTR = buffer; + std::memset(buffer, 0x00, bufLEN); } } diff --git a/CLC-qthing/SiliconTorch/CyanBusBREAK.hpp b/CLC-qthing/SiliconTorch/CyanBus.hpp similarity index 97% rename from CLC-qthing/SiliconTorch/CyanBusBREAK.hpp rename to CLC-qthing/SiliconTorch/CyanBus.hpp index 6392ef0..b10cc65 100644 --- a/CLC-qthing/SiliconTorch/CyanBusBREAK.hpp +++ b/CLC-qthing/SiliconTorch/CyanBus.hpp @@ -15,7 +15,7 @@ namespace SiliconTorch { - namespace CyanBusBREAK { + namespace CyanBus { // CyanBus payload MTU // This leads to ~54 P/s @ 2 MBaud at 100% load diff --git a/CLC-qthing/SiliconTorch/CyanBusTOUT.cpp b/CLC-qthing/SiliconTorch/CyanBusTOUT.cpp deleted file mode 100644 index 17257d5..0000000 --- a/CLC-qthing/SiliconTorch/CyanBusTOUT.cpp +++ /dev/null @@ -1,217 +0,0 @@ -#include "CyanBusTOUT.hpp" - -// C++ system level -#include <cstring> // memset -#include <cstdlib> // TODO: is this for memcpy? -#include <cinttypes> -#include <functional> - -// ESP32 specific -#include "esp_log.h" -#include "driver/gpio.h" -#include "driver/uart.h" -#include "freertos/FreeRTOS.h" -#include "freertos/queue.h" - -// project specific -#include "LambdaTask.hpp" - -// qthing stuff -// #include "" - - - -static const char* TAG = "CyanBus"; - -#include "esp_err.h" -namespace SiliconTorch { - -// namespace Impl { -// -// typedef struct { -// bool crcOK; -// uint16_t length; -// uint8_t payload[CyanBus::MTU]; -// } CyanData; -// -// } - - namespace CyanBusTOUT { - - // CyanBus envelope header - const uint8_t* const HEADER = (const uint8_t*)"fxCyan"; - - - CyanBus::CyanBus(uint8_t tx, uint8_t rx, uint8_t de, uint8_t re, uint32_t baudRate, uint8_t uartChannel) : tx(tx), rx(rx), de(de), re(re), uartChannel(uartChannel) { - - - uint64_t bitMask = 0; - - bitMask |= 1 << tx; - bitMask |= 1 << de; - bitMask |= 1 << re; - - gpio_config_t gpio = { - .pin_bit_mask = bitMask, - .mode = GPIO_MODE_OUTPUT, - .pull_up_en = GPIO_PULLUP_DISABLE, - .pull_down_en = GPIO_PULLDOWN_DISABLE, - .intr_type = GPIO_INTR_DISABLE, - }; - - // configure outputs - gpio_config(&gpio); - txrxEN(false); - - gpio.pin_bit_mask = 1 << rx; - gpio.mode = GPIO_MODE_INPUT; - - // configure inputs - gpio_config(&gpio); - - - uart_config_t uartConfig = { - .baud_rate = (int)baudRate, - .data_bits = UART_DATA_8_BITS, - .parity = UART_PARITY_DISABLE, - .stop_bits = UART_STOP_BITS_1, - .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, - .rx_flow_ctrl_thresh = 0, - .use_ref_tick = false, - // .source_clk = UART_SCLK_APB, - }; - - - uart_port_t _ch = (uart_port_t)uartChannel; - - ESP_ERROR_CHECK(uart_param_config(_ch, &uartConfig)); - ESP_ERROR_CHECK(uart_set_pin(_ch, tx, rx, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE)); - //uart_intr_config(_ch); - - ESP_ERROR_CHECK(uart_driver_install(_ch, MTU, 0, 0, NULL, 0)); - - ESP_ERROR_CHECK(uart_set_mode(_ch, UART_MODE_UART)); - uart_set_rx_timeout(_ch, 0); // TODO: timeout…?? - - - // TOOD: receive only ATM! - txEN(false); - rxEN(true); - - - readerTaskHandle = new Util::LambdaTask([&](){ readerTask(); }); - - // packetQ = xQueueCreate(3, sizeof(Impl::CyanData)); - packetQ = xQueueCreate(3, 4096); - packetTaskHandle = new Util::LambdaTask([&](){ packetTask(); }); - } - - void CyanBus::readerTask() { - - uart_port_t _ch = (uart_port_t)uartChannel; - - 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); - - - /* enum FSM { SearchHeader, ReadLength, ReadPayload, CheckCRC }; - FSM fsm = SearchHeader; - - uint8_t headerLength = std::strlen(HEADER); - uint32_t headerByteNum = 0; */ - - auto Q = packetQ; - - while (true) { - - uint32_t packetLEN = bufPTR - buffer; - - // what happens when we try to read 0 bytes??? - auto bytes = uart_read_bytes(_ch, bufPTR, MTU - packetLEN, 2); // 5 = ms Tout; TODO: make configurable and smaller - - if (bytes > 0) { - - bufPTR += bytes; - - } else { - - if (bufPTR - buffer > 0) { - ESP_LOGI(TAG, "Received Tout Ⱐafter reading %d bytes", bufPTR - buffer); - // xQueueSendToBack(Q, buffer, portMAX_DELAY); - - - - char strbuf[17]; - strbuf[16] = 0x00; - - std::memcpy(strbuf, buffer, 16); - - ESP_LOGI(TAG, "PACKET DEBUG: Starts with [ %s ]", buffer); - - - - bufPTR = buffer; - } - } - } - - delete buffer; - } - - void CyanBus::packetTask() { - ESP_LOGW(TAG, "TODO: implement callback task!"); - - uint8_t* buffer = new uint8_t[MTU]; - - auto Q = packetQ; - while (true) { - - if (xQueueReceive(Q, 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) { - uart_set_baudrate((uart_port_t)uartChannel, baudRate); - } - - uint32_t CyanBus::getBaudRate() { - uint32_t baudRate = 0; - uart_get_baudrate((uart_port_t)uartChannel, &baudRate); - return baudRate; - } - - - void CyanBus::txEN(bool state) { - gpio_set_level((gpio_num_t)de, state); - } - - void CyanBus::rxEN(bool state) { - gpio_set_level((gpio_num_t)re, state ^ 1); - } - - void CyanBus::txrxEN(bool state) { - txEN(state); - rxEN(state); - } - - } -} - - - diff --git a/CLC-qthing/SiliconTorch/CyanBusTOUT.hpp b/CLC-qthing/SiliconTorch/CyanBusTOUT.hpp deleted file mode 100644 index aabcfb9..0000000 --- a/CLC-qthing/SiliconTorch/CyanBusTOUT.hpp +++ /dev/null @@ -1,62 +0,0 @@ -#pragma once - -// C++ system level -#include <string> -#include <cinttypes> -#include <functional> - -// ESP32 specific -#include "freertos/FreeRTOS.h" -#include "freertos/queue.h" - -// project specific -#include "LambdaTask.hpp" - - -namespace SiliconTorch { - - namespace CyanBusTOUT { - - // CyanBus payload MTU - // This leads to ~54 P/s @ 2 MBaud at 100% load - constexpr uint32_t MTU = 4096; - - // CyanBus envelope header - extern const uint8_t* const HEADER; - - - class CyanBus { - public: - CyanBus(uint8_t tx, uint8_t rx, uint8_t de, uint8_t re, uint32_t baudRate = 115200, uint8_t uartChannel = 1); - - uint32_t getBaudRate(); - void setBaudRate(uint32_t baudRate); - - private: - - QueueHandle_t packetQ; - - uint8_t uartChannel; - uint8_t tx, rx, de, re; - - void txEN(bool state); - void rxEN(bool state); - void txrxEN(bool state); - - - // Receive only ATM! (make public after sound implementation…) - void write(std::string& data); - void write(const uint8_t* data, uint32_t length); - - - - - void readerTask(); - void packetTask(); - - Util::LambdaTask* readerTaskHandle = NULL; - Util::LambdaTask* packetTaskHandle = NULL; - - }; - } -} diff --git a/CLC-qthing/device_main.cpp b/CLC-qthing/device_main.cpp index d42ebfe..8016e81 100644 --- a/CLC-qthing/device_main.cpp +++ b/CLC-qthing/device_main.cpp @@ -15,13 +15,12 @@ //#include "SpiderLib/SNTP.hpp" // #include "SpiderLib/Util.hpp" #include "SiliconTorch/CyanBusCRC.hpp" -#include "SiliconTorch/CyanBusBREAK.hpp" -// #include "SiliconTorch/CyanBusTOUT.hpp" +#include "SiliconTorch/CyanBus.hpp" // ### END LIBS ### -SiliconTorch::CyanBusBREAK::CyanBus* cyanBus = NULL; +SiliconTorch::CyanBus::CyanBus* cyanBus = NULL; qthing::Config cfg; @@ -35,7 +34,7 @@ void device_main() { - cyanBus = new SiliconTorch::CyanBusBREAK::CyanBus(13, 14, 12, 15); // Pinout of CyanStripe + cyanBus = new SiliconTorch::CyanBus::CyanBus(13, 14, 12, 15); // Pinout of CyanStripe // TODO: ??? diff --git a/SiliconTorch/CyanBus.py b/SiliconTorch/CyanBus.py index b4bfee5..b7750b7 100644 --- a/SiliconTorch/CyanBus.py +++ b/SiliconTorch/CyanBus.py @@ -47,13 +47,38 @@ class CyanBus: -# Debugging stuff… -def loopRawPacket(data: bytes = b'__Hello, World!__', port: str = '/dev/ttyUSB0', baud: int = 115200): - from time import sleep +class DebugSender: - while True: - CyanBus.sendRawPacket(data=data, port=port, baud=baud) - sleep(1) + def __init__(self, port: str = '/dev/ttyUSB0', baud: int = 115200): + self.port = port + self.baud = baud + + def sendData(self, data: bytes): + with serial.Serial(port=self.port, baudrate=self.baud, bytesize=8, stopbits=serial.STOPBITS_ONE) as ser: + ser.write(data) + ser.flush() + + def sendBreak(self): + with serial.Serial(port=self.port, baudrate=self.baud, bytesize=8, stopbits=serial.STOPBITS_ONE) as ser: + ser.baudrate = self.baud/2 + ser.write(bytes([0])) + ser.flush() + ser.baudrate = self.baud + + def sendPacket(self, payload: bytes): + CyanBus.sendPacket(payload=payload, port=self.port, baud=self.baud) + + def sendGarbagePacket(self, payload: bytes, garbage: bytes = b'GaRbAgE'): + self.sendData(garbage) + self.sendPacket(payload) + + def loopGarbagePacket(self, payload: bytes = b'Hallo, Welt!', garbage: bytes = b'GaRbAgE'): + from time import sleep + + while True: + self.sendGarbagePacket(payload, garbage) + sleep(1) +cb = DebugSender(port='/dev/ttyUSB5') -- GitLab