/** * @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" /** * @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 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 Enumeration for UART status. */ typedef enum { UART_OK = 0x00u, /**< The action was successful. */ UART_ERROR = 0xFFu /**< Generic error. */ } uart_status_e; /** * @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. */ uint16_t rx_index; /**< The receive data index. */ BOOL rx_dma_en; /**< Flag indicating if DMA reception is enabled. */ uint8_t *rxbuf; /**< The receive buffer. */ uint16_t rxsize; /**< The size of the receive buffer. */ uint16_t tx_index; /**< The transmit data index. */ 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. */ __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 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); /** * @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 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 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__