/**
  *****************************************************************************
  *           Copyright(c) 2020 China Core Co. Ltd.
  *                    All Rights Reserved
  *****************************************************************************
  * @file    edma_hal.h
  * @author  Product application department
  * @version V1.0
  * @date    2020.02.13
  * @brief   Header file of EDMA HAL module.
  *
  ******************************************************************************
  */

/* Define to prevent recursive inclusion -------------------------------------*/

#ifndef EDMA_HAL_H_
#define EDMA_HAL_H_

#include "hal_def.h"
#include "edma_drv.h"

#ifdef __cplusplus
extern "C"
{
#endif

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

/** @defgroup HAL_EDMA EDMA
  *
  *@{
  */

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

 #define HAL_TIMEOUT_EDMA_ABORT              ((uint32_t)1000)  /**< 1s */

#define HAL_EDMAC_WRITE_BUFFER_INC           0/**< EDMAC Write Buffer Address Increment */
#define HAL_EDMAC_WRITE_BUFFER_DEC           1/**< EDMAC Write Buffer Address Decrement */
#define HAL_EDMAC_WRITE_BUFFER_NO_CHANGE     2/**< EDMAC Write Buffer Address No Change */

#define HAL_EDMAC_READ_BUFFER_INC            0/**< EDMAC Read Buffer Address Increment */
#define HAL_EDMAC_READ_BUFFER_DEC            1/**< EDMAC Read Buffer Address Decrement */
#define HAL_EDMAC_READ_BUFFER_NO_CHANGE      2/**< EDMAC Read Buffer Address No Change */

/**
  * @}
  */

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

/**
  * @brief  HAL edmac state structure definition
  */
typedef enum
{
  HAL_EDMAC_STATE_RESET             = 0x00,  /**< EDMAC not yet initialized or disabled */
  HAL_EDMAC_STATE_READY             = 0x01,  /**< EDMAC initialized and ready for use   */
  HAL_EDMAC_STATE_READY_MEM0        = 0x11,  /**< EDMAC Mem0 process success            */
  HAL_EDMAC_STATE_READY_MEM1        = 0x21,  /**< EDMAC Mem1 process success            */
  HAL_EDMAC_STATE_READY_HALF_MEM0   = 0x31,  /**< EDMAC Mem0 Half process success       */
  HAL_EDMAC_STATE_READY_HALF_MEM1   = 0x41,  /**< EDMAC Mem1 Half process success       */
  HAL_EDMAC_STATE_BUSY              = 0x02,  /**< EDMAC process is ongoing              */
  HAL_EDMAC_STATE_BUSY_MEM0         = 0x12,  /**< EDMAC Mem0 process is ongoing         */
  HAL_EDMAC_STATE_BUSY_MEM1         = 0x22,  /**< EDMAC Mem1 process is ongoing         */
  HAL_EDMAC_STATE_TIMEOUT           = 0x03,  /**< EDMAC timeout state                   */
  HAL_EDMAC_STATE_ERROR             = 0x04,  /**< EDMAC error state                     */
}HAL_EDMAC_StateTypeDef;

/**
 * @brief DMAC Transfer type of DMA controller
 */
typedef enum
{
    EDMAC_SRAM_TO_SRAM                    = ((0UL)),	/**< From SRAM to SRAM  */
    EDMAC_PERIPHERAL_TO_SRAM              = ((1UL)),	/**< From Peripheral to SRAM */
    EDMAC_SRAM_TO_PERIPHERAL              = ((2UL)),	/**< From SRAM to Peripheral */
    EDMAC_BOTH                            = ((3UL)),	/**< From SRAM to Peripheral Then From Peripheral to SRAM */

} EDMAC_FlowControlTypeDef;

/**
  *@brief EDMAC初始化结构体定义.
  *
  */
typedef struct
{
   FunctionalStateTypeDef compare_en;    /**<数据比较使能或禁止
                                                 *- ENABLE
                                                 *- DISABLE
                                                       */
   FunctionalStateTypeDef compare_skip;  /**<数据比较跳过使能或禁止
                                                 *- ENABLE
                                                 *- DISABLE
                                                       */
   FunctionalStateTypeDef preload_en;    /**<数据预加载使能或禁止
                                                 *- ENABLE
                                                 *- DISABLE
                                                       */
   uint8_t read_buf_inc;  /**<设备地址自动增加
                                                 *- HAL_EDMAC_READ_BUFFER_INC
                                                 *- HAL_EDMAC_READ_BUFFER_DEC
                                                 *- HAL_EDMAC_READ_BUFFER_NO_CHANGE
                                                 */
   uint8_t write_buf_inc; /**<内存地址自动增加
                                                 *- HAL_EDMAC_WRITE_BUFFER_INC
                                                 *- HAL_EDMAC_WRITE_BUFFER_DEC
                                                 *- HAL_EDMAC_WRITE_BUFFER_NO_CHANGE

                                                 */
   uint8_t perip_num;              /**<设备标识
                                                 *- EDMAC_CHANNEL_PERIPHERAL_SPI1
                                                 *- EDMAC_CHANNEL_PERIPHERAL_SPI2
                                                 *- EDMAC_CHANNEL_PERIPHERAL_SPI3
                                                 *- EDMAC_CHANNEL_PERIPHERAL_USI1
                                                 *- EDMAC_CHANNEL_PERIPHERAL_USI2
                                                 *- EDMAC_CHANNEL_PERIPHERAL_USI3
                                                 *- EDMAC_CHANNEL_PERIPHERAL_AES
                                                 *- EDMAC_CHANNEL_PERIPHERAL_DES
                                                 *- EDMAC_CHANNEL_PERIPHERAL_SM1
                                                 *- EDMAC_CHANNEL_PERIPHERAL_SC11
                                                 *- EDMAC_CHANNEL_PERIPHERAL_SC12
                                                 *- EDMAC_CHANNEL_PERIPHERAL_SHA
                                                 *- EDMAC_CHANNEL_PERIPHERAL_SM4
                                                 *- EDMAC_CHANNEL_PERIPHERAL_SSF33
                                                        */
   uint8_t transfer_type;                   /**<传输类型
                                                 *- EDMAC_SRAM_TO_SRAM
                                                 *- EDMAC_PERIPHERAL_TO_SRAM
                                                 *- EDMAC_SRAM_TO_PERIPHERAL
                                                 *- EDMAC_BOTH
                                                        */
   

   uint32_t read_addr;              /**<读取地址     */
   uint32_t write_addr;             /**<写入地址     */
   uint32_t len;                   /**<数据传输总数 */
}EDMAC_InitTypeDef;



typedef void (*edmac_callback_t)(void);

/**
  *@brief EDMAC句柄结构体定义.
  *
  */
typedef struct
{
    EDMAC_TypeDef *instance; 	/**< 设备实例指针    */
	
    EDMAC_InitTypeDef init;          /**< 初始化结构体    */

//    void *parent;                   /**< 当前持有者      */

    __IO  HAL_LockTypeDef lock;   /**< 互斥状态        */
    __IO  HAL_EDMAC_StateTypeDef state;  /**< 模块状态        */
    __IO  uint32_t   err;           /**< 错误代码        */
	
    /*回调函数*/
    edmac_callback_t min_done_callback;  /**< min done传输完成回调函数 */
    edmac_callback_t maj_done_callback;  /**< maj done传输完成回调函数 */
    edmac_callback_t err_callback;  /**< 错误处理回调函数 */
}EDMAC_HandleTypeDef;

/**
  * @}
  */

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



/**
  * @}
  */

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

extern HAL_StatusTypeDef HAL_EDMAC_Init(EDMAC_HandleTypeDef *hedmac);
extern HAL_StatusTypeDef HAL_EDMAC_Deinit(EDMAC_HandleTypeDef *hedmac);
extern HAL_StatusTypeDef HAL_EDMAC_Start(EDMAC_HandleTypeDef *hedmac, uint32_t ReadAddress, uint32_t WriteAddress, uint16_t DataLength);
extern HAL_StatusTypeDef HAL_EDMAC_Start_IT(EDMAC_HandleTypeDef *hedmac, uint32_t ReadAddress, uint32_t WriteAddress, uint16_t DataLength);
extern HAL_StatusTypeDef HAL_EDMAC_Abort(EDMAC_HandleTypeDef *hedmac);
extern HAL_StatusTypeDef HAL_EDMAC_PollForTransfer(EDMAC_HandleTypeDef *hedmac, uint32_t CompleteLevel, uint32_t Timeout);
extern HAL_StatusTypeDef HAL_EDMAC_PollForTransfer_IT(EDMAC_HandleTypeDef *hedmac, uint32_t CompleteLevel, uint32_t Timeout);
extern HAL_EDMAC_StateTypeDef HAL_EDMAC_GetState(EDMAC_HandleTypeDef *hedmac);
extern void HAL_EDMAC_IRQHandler(EDMAC_HandleTypeDef *hedmac);

/**
  * @}
  */

/**
  *@}
  */

/**
  *@}
  */


#ifdef __cplusplus
}
#endif



#endif /* EDMA_HAL_H_ */


/************************ (C) COPYRIGHT C*Core *****END OF FILE****/


