/**
  ******************************************************************************
  * @file    spi_hal.h
  * @author  Product application department
  * @version V1.0
  * @date    2020.02.13
  * @brief   Header file of SPI HAL module.
  *
  ******************************************************************************
  */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __SPI_HAL_H
#define __SPI_HAL_H

#ifdef __cplusplus
extern "C"
{
#endif
/* Includes ------------------------------------------------------------------*/
#include "hal_def.h"
#include "spi_drv.h"

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

/** @defgroup HAL_SPI SPI
  *
  *@{
  */

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

  #define HAL_SPI_ERROR_NONE ((uint32_t)0x00000000) /**< No error             */
  #define HAL_SPI_ERROR_MODF ((uint32_t)0x00000001) /**< MODF error           */
  #define HAL_SPI_ERROR_CRC ((uint32_t)0x00000002)  /**< CRC error            */
  #define HAL_SPI_ERROR_OVR ((uint32_t)0x00000004)  /**< OVR error            */
  #define HAL_SPI_ERROR_FRE ((uint32_t)0x00000008)  /**< FRE error            */
  #define HAL_SPI_ERROR_DMA ((uint32_t)0x00000010)  /**< DMA transfer error   */
  #define HAL_SPI_ERROR_FLAG ((uint32_t)0x00000020) /**< Flag: RXNE,TXE, BSY  */

  /**
  * @}
  */

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

  /**
  *@brief 回调函数ID.
  *
  */
  typedef enum
  {
    SPI_CALLBACK_ID_TXISR,
    SPI_CALLBACK_ID_RXISR,
  } SPICallbackIDTypeDef;
  /**
  *@brief 状态.
  *
  */
  typedef enum
  {
    HAL_SPI_STATE_RESET = 0x00,      /**< SPI not yet initialized or disabled                */
    HAL_SPI_STATE_READY = 0x01,      /**< SPI initialized and ready for use                  */
    HAL_SPI_STATE_BUSY = 0x02,       /**< SPI process is ongoing                             */
    HAL_SPI_STATE_BUSY_TX = 0x12,    /**< Data Transmission process is ongoing               */
    HAL_SPI_STATE_BUSY_RX = 0x22,    /**< Data Reception process is ongoing                  */
    HAL_SPI_STATE_BUSY_TX_RX = 0x32, /**< Data Transmission and Reception process is ongoing */
    HAL_SPI_STATE_ERROR = 0x03       /**< SPI error state                                    */
  } HAL_SPI_StateTypeDef;

  typedef void (*HAL_SPI_CALLBACK)(void *);

  /**
  *@brief SPI句柄结构体定义.
  *
  */
  typedef struct
  {
    SPI_TypeDef *Instance;             /**< SPI设备实例指针  */
    SPI_InitTypeDef Init;              /**< 初始化结构体     */
    uint8_t *pRxBuf;                   /**< 待接收缓存区指针 */
    uint16_t RxSize;                   /**< 待接收数据长度   */
    uint16_t RxCnt;                    /**< 待接收数据计数   */
    uint8_t *pTxBuf;                   /**< 待发送缓存区指针 */
    uint16_t TxSize;                   /**< 待发送数据长度   */
    uint16_t TxCnt;                    /**< 待发送数据计数   */
    HAL_SPI_StateTypeDef State;        /**< 模块状态         */
    __IO uint32_t ErrorCode;           /**< 错误代码         */
    HAL_SPI_CALLBACK XferCpltCallback; /**< 传输完成回调函数 */
    HAL_SPI_CALLBACK ErrCallback;      /**< 错误处理回调函数 */
    HAL_SPI_CALLBACK TxIsrCallback;    /**< 发送中断回调函数 */
    HAL_SPI_CALLBACK RxIsrCallback;    /**< 接收中断回调函数 */
  } SPI_HandleTypeDef;

  /**
  * @}
  */

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

  /**
  * @}
  */

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

  void HAL_SPI_MspDeInit(SPI_HandleTypeDef *hspi);
  void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi);
  HAL_StatusTypeDef HAL_SPI_DeInit(SPI_HandleTypeDef *hspi);
  HAL_StatusTypeDef HAL_SPI_Init(SPI_HandleTypeDef *hspi);
  HAL_StatusTypeDef HAL_SPI_Transmit(SPI_HandleTypeDef *hspi,
                                     uint8_t *pData,
                                     uint16_t Size,
                                     uint32_t Timeout);
  HAL_StatusTypeDef HAL_SPI_Receive(SPI_HandleTypeDef *hspi,
                                    uint8_t *pData,
                                    uint16_t Size,
                                    uint32_t Timeout);
  HAL_StatusTypeDef HAL_SPI_TransmitReceive(SPI_HandleTypeDef *hspi,
                                            uint8_t *pTxData,
                                            uint8_t *pRxData,
                                            uint16_t Size,
                                            uint32_t Timeout);
  HAL_StatusTypeDef HAL_SPI_TransmitIT(SPI_HandleTypeDef *hspi,
                                       uint8_t *pData,
                                       uint16_t Size,
                                       uint32_t Timeout);
  HAL_StatusTypeDef HAL_SPI_ReceiveIT(SPI_HandleTypeDef *hspi,
                                      uint8_t *pData,
                                      uint16_t Size,
                                      uint32_t Timeout);
  HAL_StatusTypeDef HAL_SPI_TransmitEDMAC(SPI_HandleTypeDef *hspi,
                                          EDMAC_TypeDef *pemdac,
                                          uint8_t *pData,
                                          uint16_t Size,
                                          uint32_t Timeout);
  HAL_StatusTypeDef HAL_SPI_ReceiveEDMAC(SPI_HandleTypeDef *hspi,
                                         EDMAC_TypeDef *pemdac,
                                         uint8_t *pData,
                                         uint16_t Size,
                                         uint32_t Timeout);
  void HAL_SPI_IRQHandler(SPI_HandleTypeDef *hspi);
  HAL_StatusTypeDef HAL_SPI_RegisterCallback(SPI_HandleTypeDef *hspi,
                                             uint8_t CallbackID,
                                             HAL_SPI_CALLBACK CallbackISRFun);
  HAL_StatusTypeDef HAL_SPI_GetFlag(SPI_HandleTypeDef *hspi, SPI_FlagIndexTypeDef FlagIndex, uint8_t *pflag);
  HAL_StatusTypeDef HAL_SPI_ClrFlag(SPI_HandleTypeDef *hspi, SPI_FlagIndexTypeDef FlagIndex);
  HAL_StatusTypeDef HAL_SPI_WaitonFlagTimeout(SPI_HandleTypeDef *hspi,
                                              SPI_FlagIndexTypeDef index,
                                              FlagStatusTypeDef status,
                                              uint32_t timeout);

  void HAL_SPI_GPIOInit(SPI_TypeDef *pspi, GPIO_InitTypeDef *GPIO_Init);
  void HAL_SPI_GPIODeInit(SPI_TypeDef *pspi, uint16_t Pin);
  GPIO_PinStateTypeDef HAL_SPI_GPIOReadPin(SPI_TypeDef *pspi, uint16_t Pin);
  void HAL_SPI_GPIOWritePin(SPI_TypeDef *pspi, uint16_t Pin, GPIO_PinStateTypeDef PinState);
  void HAL_SPI_GPIOTogglePin(SPI_TypeDef *pspi, uint16_t Pin);
  void HAL_SPI_Disable(SPI_TypeDef *hspi);
  void HAL_SPI_En(SPI_HandleTypeDef *hspi);
  void HAL_SPI_Dis(SPI_HandleTypeDef *hspi);
  void HAL_SPI_ClrRxFifo(SPI_HandleTypeDef *hspi);
  void HAL_SPI_ClrTxFifo(SPI_HandleTypeDef *hspi);
  void HAL_SPI_EnIt(SPI_HandleTypeDef *hspi);
  void HAL_SPI_DisIt(SPI_HandleTypeDef *hspi);
  void HAL_SPI_EnRxDma(SPI_HandleTypeDef *hspi);
  void HAL_SPI_DisRxDma(SPI_HandleTypeDef *hspi);
  void HAL_SPI_EnTxDma(SPI_HandleTypeDef *hspi);
  void HAL_SPI_DisTxDma(SPI_HandleTypeDef *hspi);

  /**
  * @}
  */

  /**
  *@}
  */

  /**
  *@}
  */

#ifdef __cplusplus
}

#endif

#endif /* __SPI_HAL_H */
