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

#ifdef __cplusplus
extern "C"
{
#endif
/* Includes ------------------------------------------------------------------*/
#include "hal_def.h"
#include "SSI_drv.h"
/**@defgroup HAL HAL driver
  *
  *@{
  */

/** @defgroup HAL_SSI SSI
  *
  *@{
  */

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

  #define HAL_SSI_ERROR_NONE ((uint32_t)0x00000000) /**< No error             */
  #define HAL_SSI_ERROR_MODF ((uint32_t)0x00000001) /**< MODF error           */
  #define HAL_SSI_ERROR_CRC ((uint32_t)0x00000002)  /**< CRC error            */
  #define HAL_SSI_ERROR_OVR ((uint32_t)0x00000004)  /**< OVR error            */
  #define HAL_SSI_ERROR_FRE ((uint32_t)0x00000008)  /**< FRE error            */
  #define HAL_SSI_ERROR_DMA ((uint32_t)0x00000010)  /**< DMA transfer error   */
  #define HAL_SSI_ERROR_FLAG ((uint32_t)0x00000020) /**< Flag: RXNE,TXE, BSY  */

  #define ssi_timeout 0xffffff
  #define SSI_PAGE_SIZE 0x100
  #define SSI_SECTOR_SIZE 0x1000
  #define SSI_DATA_LEN SSI_PAGE_SIZE
  /**
  * @}
  */

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

  /**
  *@brief 回调函数ID.
  *
  */
  typedef enum
  {
    SSI_CALLBACK_ID_TXISR,
    SSI_CALLBACK_ID_RXISR,
  } SSICallbackIDTypeDef;
  /**
  *@brief 状态.
  *
  */
  typedef enum
  {
    HAL_SSI_STATE_RESET = 0x00,      /**< SSI not yet initialized or disabled                */
    HAL_SSI_STATE_READY = 0x01,      /**< SSI initialized and ready for use                  */
    HAL_SSI_STATE_BUSY = 0x02,       /**< SSI process is ongoing                             */
    HAL_SSI_STATE_BUSY_TX = 0x12,    /**< Data Transmission process is ongoing               */
    HAL_SSI_STATE_BUSY_RX = 0x22,    /**< Data Reception process is ongoing                  */
    HAL_SSI_STATE_BUSY_TX_RX = 0x32, /**< Data Transmission and Reception process is ongoing */
    HAL_SSI_STATE_ERROR = 0x03       /**< SSI error state                                    */
  } HAL_SSI_StateTypeDef;

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

  /**
  * @}
  */

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

  /**
  * @}
  */

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

  extern HAL_StatusTypeDef HAL_SSI_DeInit(SSI_HandleTypeDef *hssi);
  extern HAL_StatusTypeDef HAL_SSI_Init(SSI_HandleTypeDef *hssi, uint32_t Timeout);
  extern HAL_StatusTypeDef HAL_SSI_Read_ID(SSI_HandleTypeDef *hssi, uint8_t mode, uint16_t *ID, uint32_t Timeout);
  extern HAL_StatusTypeDef HAL_SSI_EFlash_Sector_Erase(SSI_HandleTypeDef *hssi, uint8_t mode, uint32_t addr, uint32_t Timeout);
  extern HAL_StatusTypeDef HAL_SSI_EFlash_Read(SSI_HandleTypeDef *hssi, uint8_t mode, uint32_t addr, uint8_t *buf, uint32_t len, uint32_t Timeout);
  extern HAL_StatusTypeDef HAL_SSI_EFlash_Program(SSI_HandleTypeDef *hssi, uint8_t mode, uint32_t addr, uint8_t *buf, uint32_t len, uint32_t Timeout);

  extern HAL_StatusTypeDef HAL_SSI_EFlash_QPI_Enter(SSI_HandleTypeDef *hssi, uint32_t Timeout);
  extern HAL_StatusTypeDef HAL_SSI_EFlash_QPI_EXIT(SSI_HandleTypeDef *hssi, uint32_t Timeout);
  extern HAL_StatusTypeDef HAL_SSI_EFlash_QPI_Sector_Erase(SSI_HandleTypeDef *hssi, uint32_t addr, uint32_t Timeout);
  extern HAL_StatusTypeDef HAL_SSI_EFlash_QPI_Read(SSI_HandleTypeDef *hssi, uint32_t addr, uint8_t *buf, uint32_t len, uint32_t Timeout);
  extern HAL_StatusTypeDef HAL_SSI_EFlash_QPI_Program(SSI_HandleTypeDef *hssi, uint32_t addr, uint8_t *buf, uint32_t len, uint32_t Timeout);

  extern HAL_StatusTypeDef HAL_XIP_Enter(SSI_HandleTypeDef *hssi, uint32_t Timeout);
  extern HAL_StatusTypeDef HAL_XIP_EXIT(SSI_HandleTypeDef *hssi, uint32_t Timeout);
  extern HAL_StatusTypeDef HAL_SSI_FLASH_Reset(SSI_HandleTypeDef *hssi, uint32_t Timeout);

  extern void HAL_SSI_STD_DMA_Trig(SSI_HandleTypeDef *hssi, unsigned char cmd, unsigned int addr, int dmaConf);
  extern void HAL_SSI_DUAL_DMA_Trig(SSI_HandleTypeDef *hssi, unsigned char cmd, unsigned int addr, int dmaConf);
  extern void HAL_SSI_QUAD_DMA_Trig(SSI_HandleTypeDef *hssi, unsigned char cmd, unsigned int addr, int dmaConf);
  extern void HAL_SSI_QPI_DMA_Trig(SSI_HandleTypeDef *hssi, unsigned char cmd, unsigned int addr, int dmaConf);

  extern HAL_StatusTypeDef HAL_SSI_EFlash_Write_Enable(SSI_HandleTypeDef *hssi, uint32_t Timeout);
  extern HAL_StatusTypeDef HAL_SSI_EFlash_QPI_Write_Enable(SSI_HandleTypeDef *hssi, uint32_t Timeout);

  extern void HAL_SSI_DMA_Transmit_level(SSI_HandleTypeDef *hssi, unsigned char level);
  extern void HAL_SSI_DMA_Receive_level(SSI_HandleTypeDef *hssi, unsigned char level);
  extern HAL_StatusTypeDef HAL_SSI_WAIT_IDLE(SSI_HandleTypeDef *hssi, uint32_t Timeout);
	
  extern void HAL_SSI_Standard_Init(SSI_TypeDef *SSIx);
  extern void HAL_SSI_STD_DMA_Program(SSI_TypeDef *SSIx, uint8_t* psend, uint32_t addr, uint32_t length, bool binten);
  extern void HAL_SSI_STD_DMA_Read(SSI_TypeDef *SSIx,  uint8_t* pread, uint32_t addr, uint32_t length, bool binten);

  /**
  * @}
  */

  /**
  *@}
  */

  /**
  *@}
  */

#ifdef __cplusplus
}

#endif

#endif /* __SSI_HAL_H */
