/**
  ******************************************************************************
  * @file    uart_hal.h
  * @author  Product application department
  * @version V1.0
  * @date    2020.02.12
  * @brief   Header file of UART HAL module..
  ******************************************************************************
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __UART_HAL_H
#define __UART_HAL_H

#ifdef __cplusplus
extern "C"
{
#endif

/* Includes ------------------------------------------------------------------*/
#include "hal_def.h"
#include "uart_drv.h"


/**@defgroup HAL HAL driver
  *
  *@{
  */

/** @defgroup HAL_UART UART
  *
  *@{
  */

/*** 宏定义 *******************************************************************/
/** @defgroup UART_Exported_Macros Exported Macros
  *
  * @{
  */

  #define HAL_UART_ERROR_NONE      ((uint32_t)0x00)    /**< No error            */
  #define HAL_UART_ERROR_PE        ((uint32_t)0x01)    /**< Parity error        */
  #define HAL_UART_ERROR_NE        ((uint32_t)0x02)    /**< Noise error         */
  #define HAL_UART_ERROR_FE        ((uint32_t)0x04)    /**< frame error         */
  #define HAL_UART_ERROR_ORE       ((uint32_t)0x08)    /**< Overrun error       */
  #define HAL_UART_ERROR_DMA       ((uint32_t)0x10)    /**< DMA transfer error  */

/**
  * @}
  */

/*** 结构体、枚举变量定义 *****************************************************/
/** @defgroup UART_Exported_Types Exported Types
  *
  * @{
  */

/**
  * @brief HAL UART State structures definition
  */
typedef enum
{
  HAL_UART_STATE_RESET			   = 0x00,	  /**< Peripheral is not yet Initialized
												   Value is allowed for gState and RxState */
  HAL_UART_STATE_READY			   = 0x20,	  /**< Peripheral Initialized and ready for use
												   Value is allowed for gState and RxState */
  HAL_UART_STATE_BUSY			   = 0x24,	  /**< an internal process is ongoing
												   Value is allowed for gState only */
  HAL_UART_STATE_BUSY_TX		   = 0x21,	  /**< Data Transmission process is ongoing
												   Value is allowed for gState only */
  HAL_UART_STATE_BUSY_RX		   = 0x22,	  /**< Data Reception process is ongoing
												   Value is allowed for RxState only */
  HAL_UART_STATE_BUSY_TX_RX 	   = 0x23,	  /**< Data Transmission and Reception process is ongoing
												   Not to be used for neither gState nor RxState.
												   Value is result of combination (Or) between gState and RxState values */
  HAL_UART_STATE_TIMEOUT		   = 0xA0,	  /**< Timeout state
												   Value is allowed for gState only */
  HAL_UART_STATE_ERROR			   = 0xE0	  /**< Error
												   Value is allowed for gState only */
} HAL_UART_StateTypeDef;

/**
  *@brief UART 句柄结构体定义
  */
typedef struct
{
UART_TypeDef                  *Instance;		/**< UART register base address		  */
UART_InitTypeDef              Init; 			/**< UART communciation parameters		*/
uint8_t                       *pTxBuffPtr;      /**< Pointer to UART Tx transfer Buffer */
uint16_t                      TxXferSize;       /**< UART Tx Transfer size              */
uint16_t                      TxXferCount;      /**< UART Tx Transfer Counter           */
uint8_t                       *pRxBuffPtr;      /**< Pointer to UART Rx transfer Buffer */
uint16_t                      RxXferSize;       /**< UART Rx Transfer size              */
uint16_t                      RxXferCount;      /**< UART Rx Transfer Counter           */
HAL_UART_StateTypeDef         State;		    /**< UART communciation state			*/
uint32_t                      ErrorCode;        /**< UART error code					*/
//HAL_LockTypeDef               Lock;           /**< Locking object                     */
}UART_HandleTypeDef,*pUART_HandleTypeDef;

/**
* @brief UART GPIO结构体定义
*
*/
typedef struct
{
   uint8_t Pin;
   uint8_t Dir;
   uint8_t PullMode;
   uint8_t OutputMode;
   uint8_t InputMode;
   uint8_t SlewRateSel;
   uint8_t DriveStrength;
}UART_GPIOInitTypeDef;

/**
  * @}
  */

/*** 全局变量声明 **************************************************************/
/** @defgroup UART_Exported_Variables Exported Variables
  *
  * @{
  */



/**
  * @}
  */

/*** 函数声明 ******************************************************************/
/** @defgroup UART_Exported_Functions Exported Functions
  * @{
  */

HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart);
HAL_StatusTypeDef HAL_UART_DeInit(UART_HandleTypeDef *huart);
void HAL_UART_MUARTnit(UART_HandleTypeDef *huart);
void HAL_UART_MspDeInit(UART_HandleTypeDef *huart);
HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout);
HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout);
HAL_StatusTypeDef HAL_UART_TransmitIT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
HAL_StatusTypeDef HAL_UART_ReceiveIT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
HAL_StatusTypeDef HAL_UART_TransmitDMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
HAL_StatusTypeDef HAL_UART_ReceiveDMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
void HAL_UART_IRQHandler(UART_HandleTypeDef *huart);
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart);
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart);
void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart);
HAL_UART_StateTypeDef HAL_UART_GetState(UART_HandleTypeDef *huart);
uint32_t HAL_UART_GetError(UART_HandleTypeDef *huart);
void HAL_UART_DisItTcie(UART_HandleTypeDef *huart);
void HAL_UART_DisItTie(UART_HandleTypeDef *huart);
void HAL_UART_DisItRe(UART_HandleTypeDef *huart);

void HAL_UART_EnRxDma(UART_HandleTypeDef *huart);
void HAL_UART_DisRxDma(UART_HandleTypeDef *huart);
void HAL_UART_EnTxDma(UART_HandleTypeDef *huart);
void HAL_UART_DisTxDma(UART_HandleTypeDef *huart);
void HAL_UART_EnRxFifo(UART_HandleTypeDef *huart);
void HAL_UART_DisRxFifo(UART_HandleTypeDef *huart);
void HAL_UART_EnTxFifo(UART_HandleTypeDef *huart);
void HAL_UART_DisTxFifo(UART_HandleTypeDef *huart);

void  HAL_UART_GPIOInit(UART_TypeDef  *puart, GPIO_InitTypeDef *GPIO_Init);
void  HAL_UART_GPIODeInit(UART_TypeDef  *puart, UART_PinTypeDef Pin);
GPIO_PinStateTypeDef HAL_UART_GPIOReadPin(UART_TypeDef *puart, UART_PinTypeDef Pin);
void HAL_UART_GPIOWritePin(UART_TypeDef *puart, UART_PinTypeDef Pin, GPIO_PinStateTypeDef PinState);
void HAL_UART_GPIOTogglePin(UART_TypeDef *puart, UART_PinTypeDef Pin);

void HAL_UART_SendByteIT(UART_TypeDef *UARTx, UINT8 SendByte);
INT8 HAL_UART_RecvByteIT(UART_TypeDef *UARTx, UINT8 *dat);
void HAL_UART_ISR_IRQHandler(UART_TypeDef *UARTx);

/**
  * @}
  */

/**
  *@}
  */

/**
  *@}
  */

#ifdef __cplusplus
}
#endif

#endif /* __UART_HAL_H */
