Files
firmware/components/softUART/include/SoftUARTManager.h
2026-05-19 23:13:25 -04:00

60 lines
1.9 KiB
C++

#ifndef SOFT_UART_MANAGER_H
#define SOFT_UART_MANAGER_H
#include "IPhysicalLayer.h"
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "driver/uart.h"
#include "driver/gpio.h"
#include "soc/gpio_num.h"
#include <cstdint>
#include <cstddef>
#define SUART_NUM_CHANNELS 3
#define SUART_BAUD_RATE 115200
#define SUART_RX_BUF_SIZE 1024
#define SUART_TX_DONE_WAIT_TICKS pdMS_TO_TICKS(5000)
#define SUART_CHANNEL_NOT_READY 0x0
#define SUART_CHANNEL_READY 0x1
#define SUART_CHANNEL_LISTENING 0x2
typedef struct {
uart_port_t port;
gpio_num_t tx_gpio;
gpio_num_t rx_gpio;
uint8_t status;
SemaphoreHandle_t tx_done_sem;
} hw_channel_t;
class SoftUARTManager : public IPhysicalLayer {
public:
explicit SoftUARTManager(uint8_t num_channels);
~SoftUARTManager() override;
esp_err_t send(uint8_t* data, size_t size, void* config, uint8_t channel_num) override;
esp_err_t receive(uint8_t* recv_buf, size_t size, size_t* output_size, uint8_t channel_num) override;
esp_err_t start_receiving(uint8_t channel_num) override;
esp_err_t wait_until_send_complete(uint8_t channel_num) override;
private:
uint8_t num_channels;
hw_channel_t hw[SUART_NUM_CHANNELS];
uint8_t ch_status[SUART_NUM_CHANNELS];
esp_err_t init_hw_channel(uint8_t idx);
void deinit_hw_channel(uint8_t idx);
esp_err_t send_hw(uint8_t idx, uint8_t* data, size_t size);
esp_err_t receive_hw(uint8_t idx, uint8_t* buf, size_t size, size_t* out);
static constexpr gpio_num_t HW_TX[SUART_NUM_CHANNELS] = { GPIO_NUM_4, GPIO_NUM_5, GPIO_NUM_11 };
static constexpr gpio_num_t HW_RX[SUART_NUM_CHANNELS] = { GPIO_NUM_3, GPIO_NUM_6, GPIO_NUM_12 };
static constexpr uart_port_t HW_PORT[SUART_NUM_CHANNELS] = { UART_NUM_0, UART_NUM_1, UART_NUM_2 };
};
#endif // SOFT_UART_MANAGER_H