CANTram Framework 1.0.1
Dieses Framework ermöglicht die Nutzung der CANTram Hardware, einer Mikrocontroller basierten mdoularen Steuerung.
Loading...
Searching...
No Matches
ESP32_UART.h
1#ifndef ESP32_UART_H
2#define ESP32_UART_H
3
4#include <driver/uart.h>
5#include "UARTInterface.h"
6#include "UARTCore.h"
7
8/*
9 @brief Class to manage UART Objects on ESP32
10*/
11class ESP32_UART : public UARTCore {
12 public:
13
14 enum uartState {
15 NONE,
16 INSTALLED,
17 CONFIGURED,
18 STARTED
19 };
20 ESP32_UART() = default;
21 virtual bool reset() override;
22
23 bool install(
24 int uart_num,
25 uint8_t tx_pin,
26 uint8_t rx_pin,
27 uint16_t bufferSize
28 ) override; //Prepare the driver and allocate memory
29
30 bool applyConfig(); //Start the driver
31 bool applyPins(int tx_pin, int rx_pin); //Set the TX and RX pins
32 bool applyPins();
33 void setUARTPort(uart_port_t uartPort) { _uartPort = uartPort; }
34
35 bool send(const char buffer[], size_t size) override;
36 size_t read(char buffer[]) override;
37 size_t available() override;
38 void flush() override;
39 private:
40 uartState _state = NONE;
41 QueueHandle_t uart_queue = nullptr;
42 uart_port_t _uartPort=DEFAULT_REF;
43
44 uart_word_length_t toEsp32DataBits(UARTCore::DataBits bits) {
45 switch (bits) {
46 case UARTCore::DataBits::UART_DATA_5_BITS: return uart_word_length_t(UART_DATA_5_BITS);
47 case UARTCore::DataBits::UART_DATA_6_BITS: return uart_word_length_t(UART_DATA_6_BITS);
48 case UARTCore::DataBits::UART_DATA_7_BITS: return uart_word_length_t(UART_DATA_7_BITS);
49 case UARTCore::DataBits::UART_DATA_8_BITS: return uart_word_length_t(UART_DATA_8_BITS);
50 default: return uart_word_length_t(UART_DATA_8_BITS); // fallback or handle error
51 }
52 }
53
54 uart_parity_t toEsp32Parity(UARTCore::Parity parity) {
55 switch (parity) {
56 case UARTCore::Parity::UART_PARITY_NONE: return uart_parity_t(UART_PARITY_DISABLE);
57 case UARTCore::Parity::UART_PARITY_EVEN: return uart_parity_t(UART_PARITY_EVEN);
58 case UARTCore::Parity::UART_PARITY_ODD: return uart_parity_t(UART_PARITY_ODD);
59 default: return uart_parity_t(UART_PARITY_DISABLE); // fallback or handle error
60 }
61 }
62
63 uart_stop_bits_t toEsp32StopBits(UARTCore::StopBits stopBits) {
64 switch (stopBits) {
65 case UARTCore::StopBits::UART_STOP_BITS_1: return uart_stop_bits_t(UART_STOP_BITS_1);
66 case UARTCore::StopBits::UART_STOP_BITS_1_5: return uart_stop_bits_t(UART_STOP_BITS_1_5);
67 case UARTCore::StopBits::UART_STOP_BITS_2: return uart_stop_bits_t(UART_STOP_BITS_2);
68 default: return uart_stop_bits_t(UART_STOP_BITS_1); // fallback or handle error
69 }
70 }
71
72
73};
74
75#endif
Abstract base for UART hardware cores and provider interface.
virtual bool reset() override
Reset the ESP32 UART instance.
Definition ESP32_UART.cpp:12
bool applyConfig()
Configure UART communication parameters.
Definition ESP32_UART.cpp:75
void flush() override
Flush UART RX and TX buffers.
Definition ESP32_UART.cpp:192
bool install(int uart_num, uint8_t tx_pin, uint8_t rx_pin, uint16_t bufferSize) override
Install the UART driver and prepare the instance.
Definition ESP32_UART.cpp:45
size_t read(char buffer[]) override
Read available bytes from UART into buffer.
Definition ESP32_UART.cpp:161
bool send(const char buffer[], size_t size) override
Send a buffer of bytes over UART.
Definition ESP32_UART.cpp:141
size_t available() override
Get number of bytes available to read.
Definition ESP32_UART.cpp:180
bool applyPins()
Apply previously stored TX/RX pin mapping.
Definition ESP32_UART.cpp:129
Base class for platform-specific UART implementations.
Definition UARTCore.h:22
DataBits
Possible data bit widths for UART communication.
Definition UARTCore.h:28
Parity
Parity configuration options.
Definition UARTCore.h:38
StopBits
Stop bit configuration options.
Definition UARTCore.h:47