219 lines
6.9 KiB
C
219 lines
6.9 KiB
C
/**
|
||
* @file uarts.h
|
||
* @brief Header file for UARTs module.
|
||
*
|
||
* This file contains the definitions and function prototypes for UARTs module.
|
||
* The UARTs module provides functions for creating and managing UART instances,
|
||
* enabling reception, sending data, and handling interrupts.
|
||
*/
|
||
|
||
#ifndef __UARTS_H__
|
||
#define __UARTS_H__
|
||
|
||
#include "lib.h"
|
||
#include "main.h"
|
||
// 串口中断用于接收超时的参数
|
||
#define RX_TIMEOUT_TICK (1U) /* 10ms的tick */
|
||
#define RX_TIMEOUT_MSEC (20U / RX_TIMEOUT_TICK) /* 20毫秒需要的tick,可以根据需求添加其他时间更短的宏 */
|
||
|
||
/**
|
||
* @brief Enumeration for UART status.
|
||
*/
|
||
typedef enum
|
||
{
|
||
UART_OK = 0x00u, /**< The action was successful. */
|
||
UART_ERROR = 0xFFu /**< Generic error. */
|
||
} uart_status_e;
|
||
|
||
typedef enum
|
||
{
|
||
// 无错误
|
||
UART_NO_ERROR = BIT0,
|
||
// 奇偶校验错误中断
|
||
UART_PARITY_ERROR = BIT1,
|
||
// 帧错误中断
|
||
UART_FRAME_ERROR = BIT2,
|
||
// 噪声错误中断
|
||
UART_NOISE_ERROR = BIT3,
|
||
// 溢出错误中断
|
||
UART_OVERRUN_ERROR = BIT4,
|
||
} uarts_interupt_error_e; ///< UART中断错误枚举
|
||
|
||
typedef struct
|
||
{
|
||
uarts_interupt_error_e err; ///< 错误标志
|
||
uint16_t index; ///< 接收到的第几个字节
|
||
} uarts_interupt_error_t;
|
||
|
||
/**
|
||
* @brief Callback function type for UART receive interrupt.
|
||
*
|
||
* This function type is used to define the callback function for UART receive interrupt.
|
||
* The callback function is called when data is received on the UART.
|
||
*
|
||
* @param uart_index The index of the UART.
|
||
* @param uart_error The error code.
|
||
* @param data The received data.
|
||
* @param len The length of the received data.
|
||
*/
|
||
typedef void (*rx_interupt_cb_t)(uint8_t uart_index, uint8_t *data, uint16_t len);
|
||
|
||
/**
|
||
* @brief Callback function type for UART transmit complete.
|
||
*
|
||
* This function type is used to define the callback function for UART transmit complete.
|
||
* The callback function is called when the UART transmission is complete.
|
||
*/
|
||
typedef void (*tx_complete_cb_t)(void);
|
||
|
||
/**
|
||
* @brief Structure representing a UART instance.
|
||
*/
|
||
typedef struct
|
||
{
|
||
uint8_t uart_index; /**< The index of the UART. */
|
||
USART_TypeDef *huart; /**< The UART peripheral. */
|
||
DMA_TypeDef *dma; /**< The DMA peripheral. */
|
||
uint32_t dma_rx_channel; /**< The DMA receive channel. */
|
||
uint32_t dma_tx_channel; /**< The DMA transmit channel. */
|
||
|
||
//*******************RX*************************/
|
||
BOOL rx_cd_en; /**< Flag indicating if carrier detect is enabled. */
|
||
BOOL rx_dma_en; /**< Flag indicating if DMA reception is enabled. */
|
||
BOOL rx_err_en; /**< Flag indicating if error interrupt is enabled. */
|
||
__IO BOOL rx_interupt_timeout; /**< Flag indicating if receive interrupt timeout. */
|
||
__IO uint8_t rx_interupt_cnt; /**< The receive interrupt count. */
|
||
uint8_t *rxbuf; /**< The receive buffer. */
|
||
uint16_t rxsize; /**< The size of the receive buffer. */
|
||
uarts_interupt_error_t *rx_error; /**< The receive error. */
|
||
uint16_t rx_error_count; /**< The receive error count. */
|
||
__IO uint16_t rx_index; /**< The receive data index. */
|
||
|
||
//*******************TX*************************/
|
||
|
||
BOOL tx_dma_en; /**< Flag indicating if DMA transmission is enabled. */
|
||
uint8_t *txbuf; /**< The transmit buffer. */
|
||
uint16_t txsize; /**< The size of the transmit buffer. */
|
||
uint16_t tx_index; /**< The transmit data index. */
|
||
__IO BOOL tx_dma_ok; /**< Flag indicating if DMA transmission is complete. */
|
||
|
||
rx_interupt_cb_t rx_interupt_cb; /**< The receive interrupt callback function. */
|
||
tx_complete_cb_t tx_complete_cb; /**< The transmit complete callback function. */
|
||
} uart_t;
|
||
|
||
/**
|
||
* @brief Creates a UART instance.
|
||
*
|
||
* This function creates a UART instance with the specified parameters.
|
||
*
|
||
* @param huart The UART peripheral.
|
||
* @param rx_dma_en Flag indicating if DMA reception is enabled.
|
||
* @param rxsize The size of the receive buffer.
|
||
* @param rx_cb The receive interrupt callback function.
|
||
* @param tx_dma_en Flag indicating if DMA transmission is enabled.
|
||
* @param txsize The size of the transmit buffer.
|
||
* @param tx_complete_cb The transmit complete callback function.
|
||
* @return The created UART instance.
|
||
*/
|
||
extern uart_t *uart_create(USART_TypeDef *huart, BOOL rx_dma_en, uint16_t rxsize, rx_interupt_cb_t rx_cb,
|
||
BOOL tx_dma_en, uint16_t txsize, tx_complete_cb_t tx_complete_cb);
|
||
|
||
/**
|
||
* @brief Frees the resources of a UART instance.
|
||
*
|
||
* This function frees the resources allocated for a UART instance.
|
||
*
|
||
* @param uart The UART instance to free.
|
||
*/
|
||
extern void uart_free(uart_t *uart);
|
||
|
||
/**
|
||
* @brief Initializes a UART instance.
|
||
*
|
||
* This function initializes the specified UART instance with the specified parameters.
|
||
*
|
||
* @param uart The UART instance.
|
||
* @param baudrate The baudrate.
|
||
*/
|
||
extern void uart_set_baudrate(USART_TypeDef *uart, uint32_t baudrate);
|
||
|
||
/**
|
||
* @brief Enables UART reception.
|
||
*
|
||
* This function enables reception on the specified UART instance.
|
||
*
|
||
* @param uart The UART instance.
|
||
*/
|
||
extern void uart_recv_en(uart_t *uart, BOOL rx_err_en);
|
||
|
||
/**
|
||
* @brief Sends data over UART.
|
||
*
|
||
* This function sends the specified data over the specified UART instance.
|
||
*
|
||
* @param uart The UART instance.
|
||
* @param data The data to send.
|
||
* @param len The length of the data.
|
||
*/
|
||
extern void uart_send_data(uart_t *uart, uint8_t *data, uint16_t len);
|
||
|
||
/**
|
||
* @brief UART receive carrier detect callback.
|
||
*
|
||
* This function is the callback for UART receive carrier detect.
|
||
*
|
||
* @param uart The UART instance.
|
||
*/
|
||
extern void uart_rx_cd_callback(uart_t *uart);
|
||
|
||
/**
|
||
* @brief UART receive timeout timer.
|
||
*
|
||
* This function is the timer callback for UART receive timeout.
|
||
*
|
||
* @param uart The UART instance.
|
||
*/
|
||
extern void uart_rx_timeout_timer(uart_t *uart);
|
||
/**
|
||
* @brief UART receive interrupt callback.
|
||
*
|
||
* This function is the interrupt callback for UART receive interrupt.
|
||
*
|
||
* @param uart The UART instance.
|
||
*/
|
||
extern void uart_reception_callback(uart_t *uart);
|
||
|
||
/**
|
||
* @brief Get the UART error count.
|
||
*
|
||
* This function returns the number of errors that have occurred on the specified UART instance.
|
||
*
|
||
* @param uart The UART instance.
|
||
* @return The number of errors.
|
||
*/
|
||
extern uint16_t uart_get_error_count(uart_t *uart);
|
||
|
||
/**
|
||
* @brief Get UART interrupt error.
|
||
*
|
||
* This function gets the UART interrupt error.
|
||
*
|
||
* @param uart The UART instance.
|
||
* @param count The error count.
|
||
* @return The error.
|
||
*/
|
||
extern uarts_interupt_error_t *uart_get_error(uart_t *uart);
|
||
|
||
extern void uart_data_storage_reset(uart_t *uart);
|
||
|
||
/**
|
||
* @brief DMA receive interrupt callback.
|
||
*
|
||
* This function is the interrupt callback for DMA receive interrupt.
|
||
*
|
||
* @param uart The UART instance.
|
||
*/
|
||
extern void uart_dma_reception_callback(uart_t *uart);
|
||
|
||
#endif ///< __UARTS_H__
|