4996b451d9
集成CH390驱动、LwIP协议栈和FreeRTOS多任务透传框架,确保TCP Server/Client与UART链路按配置稳定联动。
152 lines
3.9 KiB
C
152 lines
3.9 KiB
C
/**
|
|
* @file uart_trans.h
|
|
* @brief UART transparent transmission module for TCP2UART
|
|
*
|
|
* - UART2 <-> TCP Server (via StreamBuffer)
|
|
* - UART3 <-> TCP Client (via StreamBuffer)
|
|
* - DMA + IDLE interrupt for efficient reception
|
|
*/
|
|
|
|
#ifndef __UART_TRANS_H__
|
|
#define __UART_TRANS_H__
|
|
|
|
#include <stdint.h>
|
|
#include <stdbool.h>
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
/* UART channel definitions */
|
|
typedef enum {
|
|
UART_CHANNEL_SERVER = 0, /* UART2 - TCP Server channel */
|
|
UART_CHANNEL_CLIENT = 1, /* UART3 - TCP Client channel */
|
|
UART_CHANNEL_MAX
|
|
} uart_channel_t;
|
|
|
|
/* DMA buffer sizes */
|
|
#define UART_RX_DMA_BUFFER_SIZE 256
|
|
#define UART_TX_DMA_BUFFER_SIZE 256
|
|
|
|
/* UART configuration */
|
|
typedef struct {
|
|
uint32_t baudrate;
|
|
uint8_t data_bits; /* 8 or 9 */
|
|
uint8_t stop_bits; /* 1 or 2 */
|
|
uint8_t parity; /* 0=None, 1=Odd, 2=Even */
|
|
} uart_config_t;
|
|
|
|
/* Default configurations */
|
|
#define UART_DEFAULT_BAUDRATE 115200
|
|
#define UART_DEFAULT_DATA_BITS 8
|
|
#define UART_DEFAULT_STOP_BITS 1
|
|
#define UART_DEFAULT_PARITY 0
|
|
|
|
/* UART statistics */
|
|
typedef struct {
|
|
uint32_t rx_bytes;
|
|
uint32_t tx_bytes;
|
|
uint32_t rx_packets;
|
|
uint32_t tx_packets;
|
|
uint32_t errors;
|
|
} uart_stats_t;
|
|
|
|
/**
|
|
* @brief Initialize UART transparent transmission module
|
|
* @return 0 on success, negative on error
|
|
*/
|
|
int uart_trans_init(void);
|
|
|
|
/**
|
|
* @brief Configure UART channel parameters
|
|
* @param channel UART channel (SERVER or CLIENT)
|
|
* @param config UART configuration
|
|
* @return 0 on success, negative on error
|
|
*/
|
|
int uart_trans_config(uart_channel_t channel, const uart_config_t *config);
|
|
|
|
/**
|
|
* @brief Start UART reception (enable DMA + IDLE interrupt)
|
|
* @param channel UART channel
|
|
* @return 0 on success, negative on error
|
|
*/
|
|
int uart_trans_start(uart_channel_t channel);
|
|
|
|
/**
|
|
* @brief Stop UART reception
|
|
* @param channel UART channel
|
|
* @return 0 on success, negative on error
|
|
*/
|
|
int uart_trans_stop(uart_channel_t channel);
|
|
|
|
/**
|
|
* @brief Set StreamBuffer handles for TCP integration
|
|
* @param channel UART channel
|
|
* @param rx_stream StreamBuffer to receive data from TCP (for UART TX)
|
|
* @param tx_stream StreamBuffer to send data to TCP (from UART RX)
|
|
*/
|
|
void uart_trans_set_streams(uart_channel_t channel,
|
|
void *rx_stream,
|
|
void *tx_stream);
|
|
|
|
/**
|
|
* @brief Get UART statistics
|
|
* @param channel UART channel
|
|
* @param stats Pointer to statistics structure
|
|
*/
|
|
void uart_trans_get_stats(uart_channel_t channel, uart_stats_t *stats);
|
|
|
|
/**
|
|
* @brief Reset UART statistics
|
|
* @param channel UART channel
|
|
*/
|
|
void uart_trans_reset_stats(uart_channel_t channel);
|
|
|
|
/**
|
|
* @brief UART IDLE interrupt handler - call from stm32f1xx_it.c
|
|
* @param channel UART channel
|
|
*
|
|
* Usage in stm32f1xx_it.c USART2_IRQHandler:
|
|
* if (__HAL_UART_GET_FLAG(&huart2, UART_FLAG_IDLE)) {
|
|
* __HAL_UART_CLEAR_IDLEFLAG(&huart2);
|
|
* uart_trans_idle_handler(UART_CHANNEL_SERVER);
|
|
* }
|
|
*/
|
|
void uart_trans_idle_handler(uart_channel_t channel);
|
|
|
|
/**
|
|
* @brief UART DMA RX half complete callback - call from HAL callback
|
|
* @param channel UART channel
|
|
*/
|
|
void uart_trans_rx_half_cplt_handler(uart_channel_t channel);
|
|
|
|
/**
|
|
* @brief UART DMA RX complete callback - call from HAL callback
|
|
* @param channel UART channel
|
|
*/
|
|
void uart_trans_rx_cplt_handler(uart_channel_t channel);
|
|
|
|
/**
|
|
* @brief UART DMA TX complete callback - call from HAL callback
|
|
* @param channel UART channel
|
|
*/
|
|
void uart_trans_tx_cplt_handler(uart_channel_t channel);
|
|
|
|
/**
|
|
* @brief Server transparent transmission task (UART2 <-> TCP Server)
|
|
* @param argument Task argument (unused)
|
|
*/
|
|
void uart_server_trans_task(void *argument);
|
|
|
|
/**
|
|
* @brief Client transparent transmission task (UART3 <-> TCP Client)
|
|
* @param argument Task argument (unused)
|
|
*/
|
|
void uart_client_trans_task(void *argument);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif /* __UART_TRANS_H__ */
|