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

Begin restructuring the whole thing…

parent 98722ea7
No related branches found
No related tags found
No related merge requests found
......@@ -9,6 +9,7 @@
#include "esp_log.h"
#include "driver/gpio.h"
#include "driver/uart.h"
#include "freertos/queue.h"
// project specific
#include "LambdaTask.hpp"
......@@ -31,92 +32,92 @@ namespace SiliconTorch {
namespace CyanBus {
CyanBus::CyanBus(SiliconTorch::Impl::RS485 reader) : reader(reader) {
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) {
readerTaskHandle = new SiliconTorch::Util::LambdaTask([&](){ readerTask(); });
}
void CyanBus::readerTask() {
const uint8_t* header = (const uint8_t*)"fxCyan";
uint8_t* buffer = new uint8_t[CyanBusMTU];
uint8_t* bufPTR = buffer;
uint16_t packetLength = 0;
uint64_t bitMask = 0;
enum FSM { SearchHeader, ReadLenght, ReadBody, CheckCRC };
FSM fsm = SearchHeader;
bitMask |= 1 << tx;
bitMask |= 1 << de;
bitMask |= 1 << re;
while (true) {
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,
};
switch (fsm) {
case SearchHeader: {
// configure outputs
gpio_config(&gpio);
txrxEN(false);
reader.read(bufPTR, 1);
gpio.pin_bit_mask = 1 << rx;
gpio.mode = GPIO_MODE_INPUT;
if (bufPTR[0] == header[bufPTR - buffer]) {
// got the right char
bufPTR++;
// configure inputs
gpio_config(&gpio);
if (bufPTR - buffer >= sizeof(header)) {
fsm = ReadLenght;
}
} else {
// start over
bufPTR = buffer;
}
break;
}
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,
};
case ReadLenght: {
reader.read(bufPTR, 2);
uart_port_t _ch = (uart_port_t)uartChannel;
packetLength = (bufPTR[0] << 8) | bufPTR[1];
bufPTR += 2;
uart_driver_install(_ch, CyanBusMTU, 0, 0, &uartEvQ, 0);
uart_param_config(_ch, &uartConfig);
uart_set_pin(_ch, tx, rx, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
uart_set_mode(_ch, UART_MODE_UART);
uart_set_rx_timeout(_ch, 0); // TODO: timeout…??
ESP_LOGW(TAG, "Got a packet with length[ %d ] \\o/", packetLength);
// TOOD: receive only ATM!
txEN(false);
rxEN(true);
fsm = ReadBody;
break;
}
// readerTaskHandle = new SiliconTorch::Util::LambdaTask([&](){ readerTask(); });
case ReadBody: {
reader.read(bufPTR, packetLength + 4); // +4 = CRC
}
fsm = CheckCRC;
void CyanBus::readerTask() {
// const uint8_t* header = (const uint8_t*)"fxCyan";
}
break;
}
case CheckCRC: {
// TODO: check the CRC!
void CyanBus::setBaudRate(uint32_t baudRate) {
uart_set_baudrate((uart_port_t)uartChannel, baudRate);
}
// If it matches: handle packet!
// But if it not matches?
// search whole packet for header? (how would we implement this???)
// or just discard? (leads to possible desynchronisation!!!)
uint32_t CyanBus::getBaudRate() {
uint32_t baudRate = 0;
uart_get_baudrate((uart_port_t)uartChannel, &baudRate);
return baudRate;
}
break;
}
}
}
delete buffer;
void CyanBus::txEN(bool state) {
gpio_set_level((gpio_num_t)de, state);
}
void CyanBus::setBaudRate(uint32_t baudRate) {
ESP_LOGE(TAG, "Operation unsupported: setBaudRate(…)");
void CyanBus::rxEN(bool state) {
gpio_set_level((gpio_num_t)re, state ^ 1);
}
uint32_t CyanBus::getBaudRate() {
ESP_LOGE(TAG, "Operation unsupported: getBaudRate()");
return 1337;
void CyanBus::txrxEN(bool state) {
txEN(state);
rxEN(state);
}
}
}
......
......@@ -5,8 +5,10 @@
#include <cinttypes>
#include <functional>
// ESP32 specific
#include "freertos/queue.h"
// project specific
#include "RS485.hpp"
#include "LambdaTask.hpp"
......@@ -16,16 +18,29 @@ namespace SiliconTorch {
class CyanBus {
public:
CyanBus(SiliconTorch::Impl::RS485 reader);
CyanBus(uint8_t tx, uint8_t rx, uint8_t de, uint8_t re, uint32_t baudRate = 115200, uint8_t uartChannel = 1);
// TODO: How do we wanna set baudRates when we only have a reader?!
// TODO: Elaborate on this topic!
uint32_t getBaudRate();
void setBaudRate(uint32_t baudRate);
private:
SiliconTorch::Impl::RS485 reader;
QueueHandle_t uartEvQ;
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();
......
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