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

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

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

/** @defgroup HAL_I2C I2C
  *
  *@{
  */

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

#define HAL_I2C_ERROR_NONE      (0x00000000U)    /**< No error              */
#define HAL_I2C_ERROR_BERR      (0x00000001U)    /**< BERR error            */
#define HAL_I2C_ERROR_ARLO      (0x00000002U)    /**< ARLO error            */
#define HAL_I2C_ERROR_AF        (0x00000004U)    /**< ACKF error            */
#define HAL_I2C_ERROR_OVR       (0x00000008U)    /**< OVR error             */
#define HAL_I2C_ERROR_DMA       (0x00000010U)    /**< DMA transfer error    */
#define HAL_I2C_ERROR_TIMEOUT   (0x00000020U)    /**< Timeout error         */
#define HAL_I2C_ERROR_SIZE      (0x00000040U)    /**< Size Management error */

/**
  *@brief  回调函数类型
  *
  */
typedef void (*HAL_I2C_CALLBACK)(void *, uint8_t, uint8_t, uint8_t);

/**
  * @}
  */

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

/**
* @brief I2C 回调ID定义
*
*/
typedef enum
{
    I2C_CALLBACK_ID_TXISR,
    I2C_CALLBACK_ID_RXISR,
} I2CCallbackIDTypeDef;

/**
* @brief I2C 模式定义
*
*/
typedef enum
{
    HAL_I2C_MODE_NONE               = 0x00,   /**< No I2C communication on going             */
    HAL_I2C_MODE_MASTER             = 0x10,   /**< I2C communication is in Master Mode       */
    HAL_I2C_MODE_SLAVE              = 0x20,   /**< I2C communication is in Slave Mode        */
    HAL_I2C_MODE_MEM                = 0x40    /**< I2C communication is in Memory Mode       */

} HAL_I2C_ModeTypeDef;

/**
  *@brief I2C通信状态枚举
  */
typedef enum
{
    HAL_I2C_STATE_RESET             = 0x00,   /**< Peripheral is not yet Initialized          */
    HAL_I2C_STATE_READY             = 0x20,   /**< Peripheral Initialized and ready for use  */
    HAL_I2C_STATE_BUSY              = 0x24,   /**< An internal process is ongoing             */
    HAL_I2C_STATE_BUSY_TX           = 0x21,   /**< Data Transmission process is ongoing   */
    HAL_I2C_STATE_BUSY_RX           = 0x22,   /**< Data Reception process is ongoing          */
    HAL_I2C_STATE_BUSY_DEVADD_READ  = 0X23,
    HAL_I2C_STATE_LISTEN            = 0x28,   /**< Address Listen Mode is ongoing             */
    HAL_I2C_STATE_BUSY_TX_LISTEN    = 0x29,   /**< Address Listen Mode and Data Transmission
                                                   process is ongoing                         */
    HAL_I2C_STATE_BUSY_RX_LISTEN    = 0x2A,   /**< Address Listen Mode and Data Reception
                                                   process is ongoing                         */
    HAL_I2C_STATE_ABORT             = 0x60,   /**< Abort user request ongoing                 */
    HAL_I2C_STATE_TIMEOUT           = 0xA0,   /**< Timeout state                              */
    HAL_I2C_STATE_ERROR             = 0xE0,    /**< Error                                     */

    HAL_I2C_STATE_BUSY_MEM_SUBADD   = 0XF0,
    HAL_I2C_STATE_BUSY_MEM_TX       = 0XF1,
    HAL_I2C_STATE_BUSY_MEM_RX       = 0XF2,
    HAL_I2C_STATE_BUSY_MEM_DEVADD_RESTART_READ     = 0XF3,
    HAL_I2C_STATE_BUSY_MEM_STOP     = 0XF4,
}HAL_I2C_StateTypeDef;


/**
  *@brief I2C模块初始化结构体定义.
  *
  */
typedef struct __I2C_HandleTypeDef
{
    I2C_TypeDef              *Instance;       /**< IIC 寄存器基址       */
    I2C_InitTypeDef          Init;           /**< 初始化结构体         */
    uint32_t                 SubAdd;         /**< 从设备地址           */
    uint32_t                 MemAdd;         /**< 访问存储器时的子地址*/
    uint32_t                 MemAddSize;     /**< 访问存储器时的子地址*/
    uint8_t                  XferFlag;       /**< 写操作需要发送*/
    uint8_t                  *pXferBuf;      /**< 传输缓存区地址       */
    uint8_t                  *pTxBuf;        /**< 发送缓冲区           */
    uint16_t                 XferSize;       /**< 传输长度             */
    uint16_t                 XferCnt;        /**< 传输计数             */
    __IO uint32_t            PreviousState;  /**< I2C communication Previous state          */
    __IO HAL_I2C_StateTypeDef     State;          /**< 状态                 */
    __IO HAL_I2C_ModeTypeDef Mode;        /**< I2C communication mode                   */
    __IO uint32_t            ErrorCode;     /**< 错误代码             */
    HAL_StatusTypeDef(*XferISR)(struct __I2C_HandleTypeDef *hi2c, uint8_t BasicITFlags, uint8_t SlvHighspeedFlags, uint8_t ITSources);  /**< I2C transfer IRQ handler function pointer */
} I2C_HandleTypeDef;

/**
  * @}
  */

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



/**
  * @}
  */

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

HAL_StatusTypeDef HAL_I2C_Init(I2C_HandleTypeDef *hiic);
HAL_StatusTypeDef HAL_I2C_DeInit(I2C_HandleTypeDef *hiic);
void HAL_I2C_MspInit(I2C_HandleTypeDef *hiic);
void HAL_I2C_MspDeInit(I2C_HandleTypeDef *hiic);  
void HAL_I2C_IRQ(I2C_HandleTypeDef *hiic);
void HAL_I2C_MasterIRQHandler(I2C_HandleTypeDef *hi2c,
                              uint8_t BasicFlags,
                              uint8_t SlaveHighspeedFlags,
                              uint8_t ItSource);
HAL_StatusTypeDef HAL_I2C_MasterTransmit(I2C_HandleTypeDef *hi2c,
                                         uint16_t DevAddress,
                                         uint8_t *pData,
                                         uint16_t Size,
                                         uint32_t Timeout);
HAL_StatusTypeDef HAL_I2C_MasterRecceive(I2C_HandleTypeDef *hi2c,
                                         uint16_t DevAddress,
                                         uint8_t *pData,
                                         uint16_t Size,
                                         uint32_t Timeout);
HAL_StatusTypeDef HAL_I2C_MasterTransmitIT(I2C_HandleTypeDef *hi2c,
                                           uint16_t DevAddress,
                                           uint8_t *pData,
                                           uint16_t Size,
                                           uint32_t Timeout);
HAL_StatusTypeDef HAL_I2C_MasterReceiveIT(I2C_HandleTypeDef *hi2c,
										  uint16_t DevAddress,
										  uint8_t *pData,
										  uint16_t Size,
										  uint32_t timeout);
void HAL_I2C_MasterMemIRQHandler(I2C_HandleTypeDef *hi2c,
                                 uint8_t BasicFlags,
                                 uint8_t SlaveHighspeedFlags,
                                 uint8_t ItSource);											  
HAL_StatusTypeDef HAL_I2C_MemWrite(I2C_HandleTypeDef *hi2c,
                                   uint16_t DevAddress,
                                   uint32_t MemAddress,
                                   uint16_t MemAddSize,
                                   uint8_t *pData,
                                   uint16_t Size,
                                   uint32_t Timeout);
HAL_StatusTypeDef HAL_I2C_MemRead(I2C_HandleTypeDef *hi2c,
                                  uint16_t DevAddress,
                                  uint32_t MemAddress,
                                  uint16_t MemAddSize,
                                  uint8_t *pData,
                                  uint16_t Size,
                                  uint32_t Timeout);
HAL_StatusTypeDef HAL_I2C_MemWriteIT(I2C_HandleTypeDef *hi2c,
                                     uint16_t DevAddress,
                                     uint32_t MemAddress,
                                     uint16_t MemAddSize,
                                     uint8_t *pData,
                                     uint16_t Size,
                                     uint32_t Timeout);
HAL_StatusTypeDef HAL_I2C_MemReadIT(I2C_HandleTypeDef *hi2c,
                                    uint16_t DevAddress,
                                    uint32_t MemAddress,
                                    uint16_t MemAddSize,
                                    uint8_t *pData,
                                    uint16_t Size,
                                    uint32_t Timeout);
void HAL_I2C_SlaveIRQHandler(I2C_HandleTypeDef *hi2c, 
	                         uint8_t BasicFlags, 
	                         uint8_t SlaveHighspeedFlags, 
	                         uint8_t ItSource);
HAL_StatusTypeDef HAL_I2C_SlaveTransmit(I2C_HandleTypeDef *hi2c,
									 uint8_t *pData,
									 uint16_t Size,
									 uint32_t Timeout);
HAL_StatusTypeDef HAL_I2C_SlaveReceive(I2C_HandleTypeDef *hi2c,
                                       uint8_t *pData,
                                       uint16_t Size,
                                       uint32_t Timeout);
HAL_StatusTypeDef HAL_I2C_SlaveTransmitIT(I2C_HandleTypeDef *hi2c,
                                          uint8_t *pData,
                                          uint16_t Size,
                                          uint32_t Timeout);
HAL_StatusTypeDef HAL_I2C_SlaveReceiveIT(I2C_HandleTypeDef *hi2c,
                                         uint8_t *pData,
                                         uint16_t Size,
                                         uint32_t Timeout);
void HAL_I2C_IRQHandler(I2C_HandleTypeDef *hi2c);
void  HAL_I2C_GPIOInit(I2C_TypeDef  *pi2c, GPIO_InitTypeDef *GPIO_Init);
void  HAL_I2C_GPIODeInit(I2C_TypeDef  *pi2c, uint16_t Pin);
GPIO_PinStateTypeDef HAL_I2C_GPIOReadPin(I2C_TypeDef *pi2c, uint16_t Pin);
void HAL_I2C_GPIOWritePin(I2C_TypeDef *pi2c, uint16_t Pin, GPIO_PinStateTypeDef PinState);
void HAL_I2C_GPIOTogglePin(I2C_TypeDef *pi2c, uint16_t Pin);

/**
  * @}
  */

/**
  *@}
  */

/**
  *@}
  */

#ifdef __cplusplus
}

#endif

#endif /* __I2C_HAL_H */

