ESP32-S3 firmware acting as BMC for another board, controlling an ATX power supply and providing access to UART. Co-authored with GLM-5
139 lines
3.4 KiB
C
139 lines
3.4 KiB
C
#ifndef UART_HANDLER_H
|
|
#define UART_HANDLER_H
|
|
|
|
#include "esp_err.h"
|
|
#include "driver/uart.h"
|
|
|
|
// ============================================================================
|
|
// Configuration
|
|
// ============================================================================
|
|
|
|
// Default UART settings (can be changed at runtime)
|
|
typedef struct {
|
|
uart_port_t port;
|
|
int baud_rate;
|
|
uart_word_length_t data_bits;
|
|
uart_parity_t parity;
|
|
uart_stop_bits_t stop_bits;
|
|
uart_hw_flowcontrol_t flow_ctrl;
|
|
} bmc_uart_config_t;
|
|
|
|
// ============================================================================
|
|
// Function Declarations
|
|
// ============================================================================
|
|
|
|
/**
|
|
* @brief Initialize the UART handler
|
|
*
|
|
* Sets up UART with default configuration and starts the receive task.
|
|
*
|
|
* @return ESP_OK on success, error code otherwise
|
|
*/
|
|
esp_err_t uart_handler_init(void);
|
|
|
|
/**
|
|
* @brief Deinitialize the UART handler
|
|
*
|
|
* Stops the receive task and releases UART resources.
|
|
*
|
|
* @return ESP_OK on success, error code otherwise
|
|
*/
|
|
esp_err_t uart_handler_deinit(void);
|
|
|
|
/**
|
|
* @brief Get current UART configuration
|
|
*
|
|
* @param config Pointer to store the configuration
|
|
* @return ESP_OK on success, error code otherwise
|
|
*/
|
|
esp_err_t uart_get_config(bmc_uart_config_t *config);
|
|
|
|
/**
|
|
* @brief Set UART configuration
|
|
*
|
|
* @param config New configuration to apply
|
|
* @return ESP_OK on success, error code otherwise
|
|
*/
|
|
esp_err_t uart_set_config(const bmc_uart_config_t *config);
|
|
|
|
/**
|
|
* @brief Read data from UART buffer
|
|
*
|
|
* Non-blocking read from the internal ring buffer.
|
|
*
|
|
* @param buf Buffer to store received data
|
|
* @param len Maximum bytes to read
|
|
* @param timeout_ms Timeout in milliseconds
|
|
* @return Number of bytes read, or -1 on error
|
|
*/
|
|
int uart_read_data(uint8_t *buf, size_t len, uint32_t timeout_ms);
|
|
|
|
/**
|
|
* @brief Write data to UART
|
|
*
|
|
* @param buf Data to send
|
|
* @param len Number of bytes to send
|
|
* @return Number of bytes written, or -1 on error
|
|
*/
|
|
int uart_write_data(const uint8_t *buf, size_t len);
|
|
|
|
/**
|
|
* @brief Check if data is available in receive buffer
|
|
*
|
|
* @return Number of bytes available, or -1 on error
|
|
*/
|
|
int uart_data_available(void);
|
|
|
|
/**
|
|
* @brief Flush UART buffers
|
|
*
|
|
* @return ESP_OK on success, error code otherwise
|
|
*/
|
|
esp_err_t uart_flush_buffers(void);
|
|
|
|
/**
|
|
* @brief Set baud rate
|
|
*
|
|
* @param baud_rate New baud rate
|
|
* @return ESP_OK on success, error code otherwise
|
|
*/
|
|
esp_err_t uart_set_baud_rate(int baud_rate);
|
|
|
|
/**
|
|
* @brief Get current baud rate
|
|
*
|
|
* @return Current baud rate
|
|
*/
|
|
int uart_get_baud_rate(void);
|
|
|
|
// ============================================================================
|
|
// WebSocket Bridge Functions
|
|
// ============================================================================
|
|
|
|
/**
|
|
* @brief Callback type for UART data received
|
|
*
|
|
* @param data Received data
|
|
* @param len Length of data
|
|
*/
|
|
typedef void (*uart_rx_callback_t)(const uint8_t *data, size_t len);
|
|
|
|
/**
|
|
* @brief Register a callback for received UART data
|
|
*
|
|
* Used for WebSocket bridge to forward data to connected clients.
|
|
*
|
|
* @param callback Function to call when data is received
|
|
* @return ESP_OK on success, error code otherwise
|
|
*/
|
|
esp_err_t uart_register_rx_callback(uart_rx_callback_t callback);
|
|
|
|
/**
|
|
* @brief Unregister the UART receive callback
|
|
*
|
|
* @return ESP_OK on success, error code otherwise
|
|
*/
|
|
esp_err_t uart_unregister_rx_callback(void);
|
|
|
|
#endif // UART_HANDLER_H
|