/**
  ******************************************************************************
  * @file    pwm_hal.h
  * @author  Product application department
  * @version V1.0
  * @date    2020.02.25
  * @brief   Header file of PWM HAL module.
  * 
  ******************************************************************************
  */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __PWM_HAL_H
#define __PWM_HAL_H

#ifdef __cplusplus
extern "C"
{
#endif

/* Includes ------------------------------------------------------------------*/
#include "pwm_drv.h"
#include "hal_def.h"
#include "ioctrl_drv.h"

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

  /** @defgroup HAL_PWM PWM
  *
  *@{
  */

  /*** 宏定义 *******************************************************************/
  /** @defgroup PWM_Exported_Macros Exported Macros
  *
  * @{
  */
  /**
  * @brief PWM 回调函数类型定义
  *
  */
  typedef void (*pwm_callback_t)(void *);

  /**
  * @}
  */

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

  /**
  * @brief PWM 方向枚举类型
  *
  */
  typedef enum
  {
    PWM_INPUT = 0,
    PWM_OUTPUT,
  }PWM_DirectionTypeDef;

  /**
  * @brief PWM 输出初始化结构体定义
  *
  */
  typedef struct
  {
    uint8_t deadzone;                      /**< PWM:死区翻转值      */
    uint8_t prescaler;                     /**< PWM:PWM预分频值     */
    uint8_t clkdiv;                        /**< PWM:时钟分频系数    */
    uint16_t period;                       /**< PWM:频率周期        */
    uint16_t width;                        /**< PWM:占空比宽度      */
    FunctionalStateTypeDef timerInv;       /**< PWM:通道翻转使能	 */
    FunctionalStateTypeDef autoload;       /**< PWM:自动加载模式使能*/
    FunctionalStateTypeDef pullup;         /**< PWM:拉高使能        */
    BitActionTypeDef level;                /**< PWM:初始电平        */
    FunctionalStateTypeDef timerInterrupt; /**< PWM:产生period个波形后是否产生中断*/
  } PWMOutput_TypeDef;

  /**
  * @brief PWM 输出初始化结构体定义
  *
  */
  typedef struct
  {
    uint8_t deadzone;                /**< PWM:死区翻转值      */
    uint8_t prescaler;               /**< PWM:PWM预分频值     */
    uint8_t clkdiv;                  /**< PWM:时钟分频系数    */
    uint16_t period;                 /**< PWM:频率周期        */
    uint32_t edgeinterrupt;          /**< PWM:边沿中断        */
    FunctionalStateTypeDef CHxInv;   /**< PWM:通道翻转使能	 */
    FunctionalStateTypeDef autoload; /**< PWM:自动加载模式使能*/
  } PWMInput_TypeDef;

  /**
  * @brief PWM GPIO初始化结构体定义
  *
  */
  typedef struct
  {
    FunctionalStateTypeDef pullup; /**< PWM:拉高使能 	   */
    BitActionTypeDef level;        /**< PWM:初始电平        */
  } PWMGpio_TypeDef;

  /**
  * @brief PWM 初始化结构体定义
  *
  */
  typedef struct
  {
    uint8_t channel;   /**< PWM通道：0-3        */
    uint8_t direction; /**< PWM方向：0-1        */
    union {
      PWMInput_TypeDef in;   /**< PWM输入捕获参数配置 */
      PWMOutput_TypeDef out; /**< PWM输出波形参数配置 */
      PWMGpio_TypeDef gpio;  /**< PWMGPIO功能参数配置 */
    };
  } PWM_InitTypeDef;

  /**
  * @brief PWM  句柄结构体定义
  *
  */
  typedef struct
  {
    PWM_TypeDef *instance;          /**< pwm实例寄存器 */
    PWM_InitTypeDef init;           /**< pwm初始化结构体 */
    pwm_callback_t RisingCallback;  /**< 上升沿中断回调函数*/
    pwm_callback_t FallingCallback; /**< 下降沿中断回调函数*/
    pwm_callback_t CaptureCallback; /**< 捕获中断回调函数*/
  } PWM_HandleTypeDef;

  /**
  * @}
  */

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

  /**
  * @}
  */

  /*** 函数声明 ******************************************************************/
  /** @defgroup PWM_Exported_Functions Exported Functions
  * @{
  */
  extern uint8_t HAL_PWM_GetFallingInterruptFlag(PWM_HandleTypeDef *hpwm);

  extern uint8_t HAL_PWM_ClrFallingInterruptFlag(PWM_HandleTypeDef *hpwm);

  extern uint8_t HAL_PWM_GetRisingInterruptFlag(PWM_HandleTypeDef *hpwm);

  extern uint8_t HAL_PWM_ClrRisingInterruptFlag(PWM_HandleTypeDef *hpwm);

  extern uint8_t HAL_PWM_GetCaptureInterruptFlag(PWM_HandleTypeDef *hpwm);

  extern uint8_t HAL_PWM_ClrCaptureInterruptFlag(PWM_HandleTypeDef *hpwm);

  extern uint8_t HAL_PWM_CHxCmd(PWM_HandleTypeDef *hpwm, FunctionalStateTypeDef status);

  extern uint8_t HAL_PWM_CHxTimerInverteCmd(PWM_HandleTypeDef *hpwm, FunctionalStateTypeDef status);

  extern uint8_t HAL_PWM_CHxDeadZoneCmd(PWM_HandleTypeDef *hpwm, FunctionalStateTypeDef status);

  extern uint8_t HAL_PWM_ConfigCHxDeadZone(PWM_HandleTypeDef *hpwm, uint8_t value);

  extern uint8_t HAL_PWM_ConfigCHxPerscaler(PWM_HandleTypeDef *hpwm, uint8_t value);

  extern uint8_t HAL_PWM_ConfigCHxClkDiv(PWM_HandleTypeDef *hpwm, PWM_CLKDIV div);

  extern uint8_t HAL_PWM_CHxAutoLoadMode(PWM_HandleTypeDef *hpwm, FunctionalStateTypeDef status);

  extern uint8_t HAL_PWM_ConfigCHxPeriod(PWM_HandleTypeDef *hpwm, uint16_t cnt);

  extern uint16_t HAL_PWM_GetCHxPeriod(PWM_HandleTypeDef *hpwm);

  extern uint8_t HAL_PWM_ConfigCHxWidth(PWM_HandleTypeDef *hpwm, uint16_t cnt);

  extern uint16_t HAL_PWM_GetCHxWidth(PWM_HandleTypeDef *hpwm);

  extern uint16_t HAL_PWM_GetCHxTimerCounter(PWM_HandleTypeDef *hpwm);

  extern uint8_t HAL_PWM_CHxInverteCmd(PWM_HandleTypeDef *hpwm, FunctionalStateTypeDef status);

  extern uint8_t HAL_PWM_CHxRisingInterruptCmd(PWM_HandleTypeDef *hpwm, FunctionalStateTypeDef status);

  extern uint8_t HAL_PWM_CHxFallinginterruptCmd(PWM_HandleTypeDef *hpwm, FunctionalStateTypeDef status);

  extern uint8_t HAL_PWM_CHxCaptureCmd(PWM_HandleTypeDef *hpwm, FunctionalStateTypeDef status);

  extern uint8_t HAL_PWM_ConfigCHxEdgeinterrupt(PWM_HandleTypeDef *hpwm, uint32_t edge);

  extern uint8_t HAL_PWM_SetCHxDirection(PWM_HandleTypeDef *hpwm, uint8_t dir);

  extern uint8_t HAL_PWM_CHxPullupCmd(PWM_HandleTypeDef *hpwm, FunctionalStateTypeDef status);

  extern uint8_t HAL_PWM_SetCHxLevel(PWM_HandleTypeDef *hpwm, uint8_t level);

  extern uint8_t HAL_PWM_GetCHxLevel(PWM_HandleTypeDef *hpwm);

  extern void HAL_PWM_RisingCallback(void *hpwm);

  extern void HAL_PWM_FallingCallback(void *hpwm);

  extern void HAL_PWM_CaptureCallback(void *hpwm);

  extern void HAL_PWM_TimerCallback(void *hpwm);

  extern uint8_t HAL_PWM_Init(PWM_HandleTypeDef *hpwm);

  /**
  * @}
  */

  /**
  *@}
  */

  /**
  *@}
  */

#ifdef __cplusplus
}
#endif

#endif /* __PWM_HAL_H */

