/*******************************************************************************
*
*
* File:          hal_uart.h
* Description
* Version      V0.1
* Date         2021-11-24
* Author       kc
*******************************************************************************/

#ifndef __HAL_UART_H__
#define __HAL_UART_H__

/*******************************************************************************
* 1.Included files
*******************************************************************************/
#include "stdint.h"
#include "tau_common.h"

/*******************************************************************************
* 2.Global constant and macro definitions using #define
*******************************************************************************/

/*******************************************************************************
* 3.Global structures, unions and enumerations using typedef
*******************************************************************************/
typedef enum
{
    HAL_UART_STOPBIT_1   = 0,
    HAL_UART_STOPBIT_2   = 1
} hal_uart_stopbit_e;

typedef enum
{
    HAL_UART_PARITY_NO    = 0,
    HAL_UART_PARITY_ODD   = 0x01,
    HAL_UART_PARITY_EVEN  = 0x03,
} hal_uart_parity_e;

typedef enum
{
    HAL_UART_DATAWIDTH_6 = 1,
    HAL_UART_DATAWIDTH_7 = 2,
    HAL_UART_DATAWIDTH_8 = 3
} hal_uart_datawidth_e;


typedef struct
{
    uint32_t baudrate;
    hal_uart_stopbit_e stopbits;
    hal_uart_datawidth_e data_width;
    hal_uart_parity_e parity;
} hal_uart_config_t;


typedef struct _hal_uart_handle_t
{
    hal_uart_config_t      uart_config;
    void (* txdmacallback)(void);
    void (* rxdmacallback)(void);
} hal_uart_handle_t;


typedef enum
{
    HAL_UART_OK       = 0x00U,
    HAL_UART_ERROR    = 0x01U,
    HAL_UART_BUSY     = 0x02U,
    HAL_UART_TIMEOUT  = 0x03U
} hal_uart_status;


/*******************************************************************************
* 4.Global variable extern declarations
*******************************************************************************/

/*******************************************************************************
* 5.Global function prototypes
*******************************************************************************/
/**
* @brief  初始化设置uart 传输的波特率、位宽等参数
* @param  hal_uart_handle_t
* @retval hal_uart_status
*/
hal_uart_status hal_uart_init(hal_uart_handle_t *huart);

/**
* @brief  关闭uart口
* @param  hal_uart_handle_t
* @retval hal_uart_status
*/
hal_uart_status hal_uart_deinit(hal_uart_handle_t *huart);

/**
* @brief  阻塞式发送数据
* @param  hal_uart_handle_t
* @param  pdata:传输数据指针
* @param  size:传输数据大小
* @retval hal_uart_status
*/
hal_uart_status hal_uart_transmit_blocking(hal_uart_handle_t *huart, uint8_t *pdata, uint16_t size);

/**
* @brief  阻塞式接收数据
* @param  hal_uart_handle_t
* @param  pdata:传输数据指针
* @param  size:传输数据大小
* @retval hal_uart_status
*/
hal_uart_status hal_uart_receive_blocking(hal_uart_handle_t *huart, uint8_t *pdata, uint16_t size);

/**
* @brief  使用DMA发送数据，TX和RX共用一个DMA 通道，所以需要TX/RX传输完后才能进行RX/TX的传输
* @param  hal_uart_handle_t
* @param  pdata:传输数据指针
* @param  size:传输数据大小
* @retval hal_uart_status
*/
hal_uart_status hal_uart_transmit_dma(hal_uart_handle_t *huart, uint8_t *pdata, uint16_t size);

/**
* @brief  使用DMA发送数据，TX和RX共用一个DMA 通道，所以需要TX/RX传输完后才能进行RX/TX的传输
* @param  hal_uart_handle_t
* @param  pdata:传输数据指针
* @param  size:传输数据大小
* @retval hal_uart_status
*/
hal_uart_status hal_uart_receive_dma(hal_uart_handle_t *huart, uint8_t *pdata, uint16_t size);

#endif /* __HAL_UART_H__ */
