/**
  **********************************************************************************
  * @file    pwm_hal.c
  * @author  Product application department
  * @version V1.0
  * @date    2020.02.25
  * @brief   PWM模块HAL层驱动.
  * 
  @verbatim
  ===================================================================================
                                  ##### 更新 #####
  ===================================================================================
  [..] 更新时间:
  [..] 更新人:
  [..] 更新内容:
        1. nothing.
  @endverbatim
  */

#include "pwm_hal.h"

/*** 全局变量定义 ***************************************************************/
/**@ volatile
  *@{
  */

/**@
  *@}
  */

/**@ static
  *@{
  */
static PWM_HandleTypeDef *g_hpwm;
/**@
  *@}
  */
/*** 常量定义 *******************************************************************/
/**@ const
  *@{
  */
/**@
  *@}
  */
/*** 函数定义 *******************************************************************/

/**
  *@brief PWM:注册寄存器的回调函数
  *
  *@param[in] hPWM PWM句柄
  *@param[in] callback:回调函数实例
  *@param[in] index:回调函数类别
  *@return
  *- 0 :成功;
  *- 1 :失败;
  */
static uint8_t HAL_PWM_ConfigCallbackRegistry(PWM_HandleTypeDef *hpwm,
		                                                        pwm_callback_t     callback,
                                                                uint8_t            index)
{
	switch(index)
	{
		case 0: hpwm->RisingCallback   = callback;break;
		case 1: hpwm->FallingCallback = callback;break;
		case 2: hpwm->CaptureCallback  = callback;break;
		default: return 1;
	}
	
	return 0;
}

/**
 * @brief PWM获取下降沿中断标志位.
 * 
 * @param[in] hpwm
 * @return
 * -0:成功
 * -1:失败 
 */
uint8_t HAL_PWM_GetFallingInterruptFlag(PWM_HandleTypeDef *hpwm)
{
	switch(hpwm->init.channel)
	{
		case 0: if(_pwm_get_ch0_Fallingflag(hpwm->instance)) return 1;break;
		case 1: if(_pwm_get_ch1_Fallingflag(hpwm->instance)) return 1;break;
		case 2: if(_pwm_get_ch2_Fallingflag(hpwm->instance)) return 1;break;
		case 3: if(_pwm_get_ch3_Fallingflag(hpwm->instance)) return 1;break;
		case 4: if(_pwm_get_ch4_Fallingflag(hpwm->instance)) return 1;break;
		case 5: if(_pwm_get_ch5_Fallingflag(hpwm->instance)) return 1;break;
		case 6: if(_pwm_get_ch6_Fallingflag(hpwm->instance)) return 1;break;
		case 7: if(_pwm_get_ch7_Fallingflag(hpwm->instance)) return 1;break;
		default:break;
	}
	return 0;
}

/**
 * @brief PWM清除下降沿中断标志位.
 * 
 * @param[in] hpwm
 * @return
 * -0:成功
 * -1:失败 
 */
uint8_t HAL_PWM_ClrFallingInterruptFlag(PWM_HandleTypeDef *hpwm)
{
	switch(hpwm->init.channel)
	{
		case 0: if(_pwm_clr_ch0_Fallingflag(hpwm->instance)) return 1;break;
		case 1: if(_pwm_clr_ch1_Fallingflag(hpwm->instance)) return 1;break;
		case 2: if(_pwm_clr_ch2_Fallingflag(hpwm->instance)) return 1;break;
		case 3: if(_pwm_clr_ch3_Fallingflag(hpwm->instance)) return 1;break;
		case 4: if(_pwm_clr_ch4_Fallingflag(hpwm->instance)) return 1;break;
		case 5: if(_pwm_clr_ch5_Fallingflag(hpwm->instance)) return 1;break;
		case 6: if(_pwm_clr_ch6_Fallingflag(hpwm->instance)) return 1;break;
		case 7: if(_pwm_clr_ch7_Fallingflag(hpwm->instance)) return 1;break;
		default:break;
	}
	return 0;
}

/**
 * @brief PWM获取上升沿中断标志位.
 * 
 * @param[in] hpwm
 * @return
 * -0:成功
 * -1:失败 
 */
uint8_t HAL_PWM_GetRisingInterruptFlag(PWM_HandleTypeDef *hpwm)
{
	switch(hpwm->init.channel)
	{
		case 0: if(_pwm_get_ch0_Risingflag(hpwm->instance)) return 1;break;
		case 1: if(_pwm_get_ch1_Risingflag(hpwm->instance)) return 1;break;
		case 2: if(_pwm_get_ch2_Risingflag(hpwm->instance)) return 1;break;
		case 3: if(_pwm_get_ch3_Risingflag(hpwm->instance)) return 1;break;
		case 4: if(_pwm_get_ch4_Risingflag(hpwm->instance)) return 1;break;
		case 5: if(_pwm_get_ch5_Risingflag(hpwm->instance)) return 1;break;
		case 6: if(_pwm_get_ch6_Risingflag(hpwm->instance)) return 1;break;
		case 7: if(_pwm_get_ch7_Risingflag(hpwm->instance)) return 1;break;
		default:break;
	}
	return 0;
}


/**
 * @brief PWM清除上升沿中断标志位.
 * 
 * @param[in] hpwm
 * @return
 * -0:成功
 * -1:失败 
 */
uint8_t HAL_PWM_ClrRisingInterruptFlag(PWM_HandleTypeDef *hpwm)
{
	switch(hpwm->init.channel)
	{
		case 0: if(_pwm_clr_ch0_Risingflag(hpwm->instance)) return 1;break;
		case 1: if(_pwm_clr_ch1_Risingflag(hpwm->instance)) return 1;break;
		case 2: if(_pwm_clr_ch2_Risingflag(hpwm->instance)) return 1;break;
		case 3: if(_pwm_clr_ch3_Risingflag(hpwm->instance)) return 1;break;
		case 4: if(_pwm_clr_ch4_Risingflag(hpwm->instance)) return 1;break;
		case 5: if(_pwm_clr_ch5_Risingflag(hpwm->instance)) return 1;break;
		case 6: if(_pwm_clr_ch6_Risingflag(hpwm->instance)) return 1;break;
		case 7: if(_pwm_clr_ch7_Risingflag(hpwm->instance)) return 1;break;
		default:break;
	}
	return 0;
}

/**
 * @brief PWM获取捕获中断标志位.
 * 
 * @param[in] hpwm
 * @return
 * -0:成功
 * -1:失败 
 */
uint8_t HAL_PWM_GetCaptureInterruptFlag(PWM_HandleTypeDef *hpwm)
{
	switch(hpwm->init.channel)
	{
		case 0: if(_pwm_get_ch0_captureflag(hpwm->instance)) return 1;break;
		case 1: if(_pwm_get_ch1_captureflag(hpwm->instance)) return 1;break;
		case 2: if(_pwm_get_ch2_captureflag(hpwm->instance)) return 1;break;
		case 3: if(_pwm_get_ch3_captureflag(hpwm->instance)) return 1;break;
		case 4: if(_pwm_get_ch4_captureflag(hpwm->instance)) return 1;break;
		case 5: if(_pwm_get_ch5_captureflag(hpwm->instance)) return 1;break;
		case 6: if(_pwm_get_ch6_captureflag(hpwm->instance)) return 1;break;
		case 7: if(_pwm_get_ch7_captureflag(hpwm->instance)) return 1;break;
		default:break;
	}
	return 0;
}


/**
 * @brief PWM清除捕获中断标志位.
 * 
 * @param[in] hpwm
 * @return
 * -0:成功
 * -1:失败 
 */	
uint8_t HAL_PWM_ClrCaptureInterruptFlag(PWM_HandleTypeDef *hpwm)
{
	switch(hpwm->init.channel)
	{
		case 0: if(_pwm_clr_ch0_captureflag(hpwm->instance)) return 1;break;
		case 1: if(_pwm_clr_ch1_captureflag(hpwm->instance)) return 1;break;
		case 2: if(_pwm_clr_ch2_captureflag(hpwm->instance)) return 1;break;
		case 3: if(_pwm_clr_ch3_captureflag(hpwm->instance)) return 1;break;
		case 4: if(_pwm_clr_ch4_captureflag(hpwm->instance)) return 1;break;
		case 5: if(_pwm_clr_ch5_captureflag(hpwm->instance)) return 1;break;
		case 6: if(_pwm_clr_ch6_captureflag(hpwm->instance)) return 1;break;
		case 7: if(_pwm_clr_ch7_captureflag(hpwm->instance)) return 1;break;
		default:break;
	}
	return 0;
}

/**
 * @brief pwm通道使能
 *
 * @param[in] hpwm
 * @param[in] status 使能状态
 * -ENABLE
 * -DISBALE
 * @return
 * -0:成功
 * -1:失败
 */
uint8_t HAL_PWM_CHxCmd(PWM_HandleTypeDef* hpwm,FunctionalStateTypeDef status)
{
	return DRV_PWM_CHxCmd(hpwm->instance,hpwm->init.channel,status);	
}

/**
 * @brief pwm通道翻转使能
 *
 * @param[in] hpwm
 * @param[in] status 使能状态
 * -ENABLE
 * -DISBALE
 * @return
 * -0:成功
 * -1:失败
 */
uint8_t HAL_PWM_CHxTimerInverteCmd(PWM_HandleTypeDef* hpwm,FunctionalStateTypeDef status)
{
	return DRV_PWM_CHxTimerInverteCmd(hpwm->instance,hpwm->init.channel,status);
}



/**
 * @brief pwm通道死区使能
 *
 * @param[in] hpwm
 * @param[in] status 使能状态
 * -ENABLE
 * -DISBALE
 * @return
 * -0:成功
 * -1:失败
 */
uint8_t HAL_PWM_CHxDeadZoneCmd(PWM_HandleTypeDef* hpwm,FunctionalStateTypeDef status)
{
	return DRV_PWM_CHxDeadZoneCmd(hpwm->instance,hpwm->init.channel,status);
}


/**
 * @brief pwm通道设置死区翻转参数
 *
 * @param[in] hpwm pwm句柄
 * @param[in] value 死区翻转值
 * @return
 * -0:成功
 * -1:失败
 */
uint8_t HAL_PWM_ConfigCHxDeadZone(PWM_HandleTypeDef* hpwm,uint8_t value)
{
   return DRV_PWM_ConfigCHxDeadZone(hpwm->instance,hpwm->init.channel,value); 	
}

/**
 * @brief PWM配置预分频系数
 * 
 * @param[in] hpwm  pwm句柄
 * @param[in] value 预分频值
 * @return NONE
 */
uint8_t HAL_PWM_ConfigCHxPerscaler(PWM_HandleTypeDef* hpwm,uint8_t value)
{
	 return DRV_PWM_ConfigCHxPerscaler(hpwm->instance,hpwm->init.channel,value);
}

/**
 * @brief PWM配置分频系数
 * 
 * @param[in] hpwm
 * @param[in] div 分频系数
 * @return NONE
 */
uint8_t HAL_PWM_ConfigCHxClkDiv(PWM_HandleTypeDef* hpwm,PWM_CLKDIV div)
{
	 return DRV_PWM_ConfigCHxClkDiv(hpwm->instance,hpwm->init.channel,div);
}

/**
 * @brief pwm通道模式配置
 *
 * @param[in] hpwm pwm句柄
 * @param[in] status 计数器auto load模式
 * -ENABLE
 * -DISABLE
 * @return
 * -0:成功
 * -1:失败
 */

uint8_t HAL_PWM_CHxAutoLoadMode(PWM_HandleTypeDef* hpwm,FunctionalStateTypeDef status)
{
	return DRV_PWM_CHxAutoLoadMode(hpwm->instance,hpwm->init.channel,status);
}


/**
 * @brief 设置pwm周期
 *
 * @param[in] hpwm pwm句柄
 * @param[in] cnt 一个timer周期内产生的PWM波形个数
 * @return
 * -0:成功
 * -1:失败
 */
uint8_t HAL_PWM_ConfigCHxPeriod(PWM_HandleTypeDef* hpwm,uint16_t cnt)
{
	return DRV_PWM_ConfigCHxPeriod(hpwm->instance,hpwm->init.channel,cnt);	
}

/**
 * @brief 获取pwm周期
 *
 * @param[in] hpwm pwm句柄
 * @retval 周期值
 */

uint16_t HAL_PWM_GetCHxPeriod(PWM_HandleTypeDef* hpwm)
{
	return DRV_PWM_GetCHxPeriod(hpwm->instance,hpwm->init.channel);	
}

/**
 * @brief 设置pwm脉冲宽度
 *
 * @param[in] hpwm pwm句柄
 * @param[in] cnt 计数器值
 * @return
 * -0:成功
 * -1:失败
 */
uint8_t HAL_PWM_ConfigCHxWidth(PWM_HandleTypeDef* hpwm,uint16_t cnt)
{
	return DRV_PWM_ConfigCHxWidth(hpwm->instance,hpwm->init.channel,cnt);	
}

/**
 * @brief 获取pwm脉冲宽度
 *
 * @param[in] hpwm pwm句柄
 * @retval 脉冲宽度
 */
uint16_t HAL_PWM_GetCHxWidth(PWM_HandleTypeDef *hpwm)
{
	return DRV_PWM_GetCHxWidth(hpwm->instance,hpwm->init.channel);
}

/**
 * @brief 获取pwm通道时钟计数器值
 *
 * @param[in] hpwm pwm句柄
 * @retval 通道时钟计数器值
 */

uint16_t HAL_PWM_GetCHxTimerCounter(PWM_HandleTypeDef *hpwm)
{
		return DRV_PWM_GetCHxTimerCounter(hpwm->instance,hpwm->init.channel);
}


/**
 * @brief pwm通道翻转使能
 *
 * @param[in] hpwm pwm句柄
 * @param[in] status 使能状态
 * -ENABLE
 * -DISBALE
 * @return
 * -0:成功
 * -1:失败
 */

uint8_t HAL_PWM_CHxInverteCmd(PWM_HandleTypeDef *hpwm,FunctionalStateTypeDef status)
{
	return DRV_PWM_CHxInverteCmd(hpwm->instance,hpwm->init.channel,status);	
}

/**
 * @brief pwm通道上升沿中断使能
 *
 * @param[in] hpwm pwm句柄
 * @param[in] status 使能状态
 * -ENABLE
 * -DISBALE
 * @return
 * -0:成功
 * -1:失败
 */

uint8_t HAL_PWM_CHxRisingInterruptCmd(PWM_HandleTypeDef *hpwm,FunctionalStateTypeDef status)
{
	return DRV_PWM_CHxRisingInterruptCmd(hpwm->instance,hpwm->init.channel,status);	
}

/**
 * @brief pwm通道下降沿中断使能
 *
 * @param[in] hpwm pwm句柄
 * @param[in] chx  pwm通道号
 * @param[in] status 使能状态
 * -ENABLE
 * -DISBALE
 * @return
 * -0:成功
 * -1:失败
 */
uint8_t HAL_PWM_CHxFallinginterruptCmd(PWM_HandleTypeDef *hpwm,FunctionalStateTypeDef status)
{
	return DRV_PWM_CHxFallinginterruptCmd(hpwm->instance,hpwm->init.channel,status);	
}

/**
 * @brief pwm通道捕获中断使能
 *
 * @param[in] hpwm pwm句柄
 * @param[in] chx  pwm通道号
 * @param[in] status 使能状态
 * -ENABLE
 * -DISBALE
 * @return
 * -0:成功
 * -1:失败
 */
uint8_t HAL_PWM_CHxCaptureCmd(PWM_HandleTypeDef *hpwm,FunctionalStateTypeDef status)
{
	return DRV_PWM_CHxCaptureCmd(hpwm->instance,hpwm->init.channel,status);	
}

/**
 * @brief pwm通道设置边沿触发
 *
 * @param[in] hpwm pwm句柄
 * @param[in] chx  pwm通道号
 * @param[in] value 边沿触发标志
 * -PWM_PCCRX_CHX_RLINT
 * -PWM_PCCRX_CHX_FLINT
 * -PWM_PCCRX_CHX_CAPEN
 * @return
 * -0:成功
 * -1:失败
 */
uint8_t HAL_PWM_ConfigCHxEdgeinterrupt(PWM_HandleTypeDef *hpwm,uint32_t edge)
{
	return DRV_PWM_ConfigCHxEdgeinterrupt(hpwm->instance,hpwm->init.channel,edge);	
}

/**
 * @brief pwm通道设置方向属性
 *
 * @param[in] hpwm pwm句柄
 * @param[in] chx  pwm通道号
 * @param[in] dir  方向
 * -0:输入
 * -1:输出
 * @return
 * -0:成功
 * -1:失败
 */
uint8_t HAL_PWM_SetCHxDirection(PWM_HandleTypeDef *hpwm,uint8_t dir)
{
	return DRV_PWM_SetCHxDirection(hpwm->instance,hpwm->init.channel,dir);	
}

/**
 * @brief pwm通道拉高使能
 *
 * @param[in] hpwm pwm句柄
 * @param[in] status 使能状态
 * -ENABLE
 * -DISBALE
 * @return
 * -0:成功
 * -1:失败
 */

uint8_t HAL_PWM_CHxPullupCmd(PWM_HandleTypeDef *hpwm,FunctionalStateTypeDef status)
{
	return 	DRV_PWM_CHxPullupCmd(hpwm->instance,hpwm->init.channel,status);
}

/**
 * @brief pwm通道设置电平级别
 *
 * @param[in] hpwm pwm句柄
 * @param[in] level:电平状态
 * -0:reset
 * -1:set
 * @return
 * -0:成功
 * -1:失败
 */
uint8_t HAL_PWM_SetCHxLevel(PWM_HandleTypeDef *hpwm,uint8_t level)
{
		return DRV_PWM_SetCHxLevel(hpwm->instance,hpwm->init.channel,level);
}

/**
 * @brief pwm通道设置电平级别
 *
 * @param[in] hpwm pwm句柄
 * @param[in] chx  pwm通道号
 * @return
 * -0:低电平
 * -1:高电平
 */
uint8_t HAL_PWM_GetCHxLevel(PWM_HandleTypeDef *hpwm)
{
		return DRV_PWM_GetCHxLevel(hpwm->instance,hpwm->init.channel);
}

void __weak HAL_PWM_IRQHandler(void)
{
	printf("IRQ PIFR\r\n");
	if(g_hpwm->RisingCallback ||\
	   g_hpwm->FallingCallback||\
	   g_hpwm->CaptureCallback)
	{
		if(g_hpwm->RisingCallback)
		{
			g_hpwm->RisingCallback((void *)g_hpwm);
		}
		
		if(g_hpwm->FallingCallback)
		{
			g_hpwm->FallingCallback((void *)g_hpwm);
		}

		if(g_hpwm->CaptureCallback)
		{
			g_hpwm->CaptureCallback((void *)g_hpwm);
		}
		
		return;
	}

	//上升沿或下降沿产生中断:前提是上升沿和下降沿中断打开，捕获中断打开
	if(HAL_PWM_GetCaptureInterruptFlag(g_hpwm))
	{
		HAL_PWM_ClrCaptureInterruptFlag(g_hpwm);
	}

    //如果仅开启上升沿中断
    if(HAL_PWM_GetRisingInterruptFlag(g_hpwm))
    {
		HAL_PWM_ClrRisingInterruptFlag(g_hpwm);
	}

	//如果仅开启下升沿中断
	if(HAL_PWM_GetFallingInterruptFlag(g_hpwm))
	{
		HAL_PWM_ClrFallingInterruptFlag(g_hpwm);
	}
}

void __weak HAL_PWM_RisingCallback(void *hpwm)
{
}

void __weak HAL_PWM_FallingCallback(void *hpwm)
{
}

void __weak HAL_PWM_CaptureCallback(void *hpwm)
{
}

/**
 * @brief PWM输入初始化.
 * 
 * @param[in] hpwm
 * @return
 * -0:成功
 * -1:失败 
 */

static uint8_t HAL_PWM_InputInit(PWM_HandleTypeDef *hpwm)
{

	DRV_PWM_CHxCmd(hpwm->instance,hpwm->init.channel,DISABLE);
	//配置死区
	if(hpwm->init.in.deadzone)
	{
		DRV_PWM_ConfigCHxDeadZone(hpwm->instance,hpwm->init.channel,hpwm->init.in.deadzone);
		DRV_PWM_CHxDeadZoneCmd(hpwm->instance,hpwm->init.channel,ENABLE);
	}
	else
	{
		DRV_PWM_CHxDeadZoneCmd(hpwm->instance,hpwm->init.channel,DISABLE);
	}
    //配置预分频系数
	DRV_PWM_ConfigCHxPerscaler(hpwm->instance,hpwm->init.channel,hpwm->init.in.prescaler);

	//配置分频系数
	DRV_PWM_ConfigCHxClkDiv(hpwm->instance,hpwm->init.channel,hpwm->init.in.clkdiv);

	//配置PWM载入值模式
	DRV_PWM_CHxAutoLoadMode(hpwm->instance,hpwm->init.channel,hpwm->init.in.autoload);

	//配置PWM捕获周期
	DRV_PWM_ConfigCHxPeriod(hpwm->instance,hpwm->init.channel,hpwm->init.in.period);

	//配置PWM方向
	DRV_PWM_SetCHxDirection(hpwm->instance,hpwm->init.channel,hpwm->init.direction);

	//配置PWM通道翻转
	DRV_PWM_CHxInverteCmd(hpwm->instance,hpwm->init.channel,hpwm->init.in.CHxInv);

	//配置边沿触发中断
	DRV_PWM_ConfigCHxEdgeinterrupt(hpwm->instance,hpwm->init.channel,hpwm->init.in.edgeinterrupt);

	//注册回调函数
	if(hpwm->init.in.edgeinterrupt)
	{
		HAL_PWM_ConfigCallbackRegistry(hpwm,HAL_PWM_RisingCallback,0);
		HAL_PWM_ConfigCallbackRegistry(hpwm,HAL_PWM_FallingCallback,1);
		HAL_PWM_ConfigCallbackRegistry(hpwm,HAL_PWM_CaptureCallback,2);
		
		//配置中断优先级
		switch(hpwm->init.channel)
		{
			case 0:NVIC_Init(3, 3, PWM0_IRQn, 2);break;
			case 1:NVIC_Init(3, 3, PWM1_IRQn, 2);break;
			case 2:NVIC_Init(3, 3, PWM2_IRQn, 2);break;
			case 3:NVIC_Init(3, 3, PWM3_IRQn, 2);break;
			case 4:NVIC_Init(3, 3, PWM4_IRQn, 2);break;
			case 5:NVIC_Init(3, 3, PWM5_IRQn, 2);break;
			case 6:NVIC_Init(3, 3, PWM6_IRQn, 2);break;
			case 7:NVIC_Init(3, 3, PWM7_IRQn, 2);break;
			default:return 1;
		}	
	}

	DRV_PWM_CHxCmd(hpwm->instance,hpwm->init.channel,ENABLE);
	return 0;
	
}

/**
 * @brief PWM输出初始化.
 * 
 * @param[in] hpwm
 * @return
 * -0:成功
 * -1:失败 
 */

static uint8_t HAL_PWM_OutputInit(PWM_HandleTypeDef *hpwm)
{

	//关闭PWM
	DRV_PWM_CHxCmd(hpwm->instance,hpwm->init.channel,DISABLE);

	//配置死区
	if(hpwm->init.out.deadzone)
	{
		DRV_PWM_ConfigCHxDeadZone(hpwm->instance,hpwm->init.channel,hpwm->init.out.deadzone);
		DRV_PWM_CHxDeadZoneCmd(hpwm->instance,hpwm->init.channel,ENABLE);
		//配置PWM1 PWM3 PWM5 PWM7
		switch(hpwm->init.channel+1)
		{
			case 0: HAL_IOCTRL_Swap(IOCTRL_SWAP16,ENABLE);break;
			case 1: HAL_IOCTRL_Swap(IOCTRL_SWAP17,ENABLE);break;
			case 2: HAL_IOCTRL_Swap(IOCTRL_SWAP18,ENABLE);break;
			case 3: HAL_IOCTRL_Swap(IOCTRL_SWAP19,ENABLE);break;
			case 4: HAL_IOCTRL_Swap(IOCTRL_SWAP20,ENABLE);break;
			case 5: HAL_IOCTRL_Swap(IOCTRL_SWAP21,ENABLE);break;
			case 6: HAL_IOCTRL_Swap(IOCTRL_SWAP22,ENABLE);break;
			case 7: HAL_IOCTRL_Swap(IOCTRL_SWAP23,ENABLE);break;
			default:return 1;
		}
		//配置PWM方向
		DRV_PWM_SetCHxDirection(hpwm->instance,hpwm->init.channel+1,hpwm->init.direction);

		//配置PWM时钟翻转
		DRV_PWM_CHxTimerInverteCmd(hpwm->instance,hpwm->init.channel+1,hpwm->init.out.timerInv);

		//配置PWM拉高使能
		DRV_PWM_CHxPullupCmd(hpwm->instance,hpwm->init.channel+1,hpwm->init.out.pullup);

		//配置PWM初始电平
		DRV_PWM_SetCHxLevel(hpwm->instance,hpwm->init.channel+1,hpwm->init.out.level);
		
		//配置timer中断
		DRV_PWM_CHxTimerInterruptCmd(hpwm->instance,hpwm->init.channel+1,hpwm->init.out.timerInterrupt);

		//打开PWM
//		DRV_PWM_CHxCmd(hpwm->instance,hpwm->init.channel+1,ENABLE);
	}
	else
	{
		DRV_PWM_CHxDeadZoneCmd(hpwm->instance,hpwm->init.channel,DISABLE);
	}
	//配置分频系数
	DRV_PWM_ConfigCHxClkDiv(hpwm->instance,hpwm->init.channel,hpwm->init.out.clkdiv);
	
	//配置分频系数
	DRV_PWM_ConfigCHxPerscaler(hpwm->instance,hpwm->init.channel,hpwm->init.out.prescaler);

	//配置PWM载入值模式
	DRV_PWM_CHxAutoLoadMode(hpwm->instance,hpwm->init.channel,hpwm->init.out.autoload);

	//配置PWM波形脉冲宽度
	DRV_PWM_ConfigCHxWidth(hpwm->instance,hpwm->init.channel,hpwm->init.out.width);

	//配置PWM波形周期频率
	DRV_PWM_ConfigCHxPeriod(hpwm->instance,hpwm->init.channel,hpwm->init.out.period);

	//配置PWM方向
	DRV_PWM_SetCHxDirection(hpwm->instance,hpwm->init.channel,hpwm->init.direction);

	//配置PWM时钟翻转
	DRV_PWM_CHxTimerInverteCmd(hpwm->instance,hpwm->init.channel,hpwm->init.out.timerInv);

	//配置PWM拉高使能
	DRV_PWM_CHxPullupCmd(hpwm->instance,hpwm->init.channel,hpwm->init.out.pullup);

	//配置PWM初始电平
	DRV_PWM_SetCHxLevel(hpwm->instance,hpwm->init.channel,hpwm->init.out.level);
	
	//配置timer中断
	DRV_PWM_CHxTimerInterruptCmd(hpwm->instance,hpwm->init.channel,hpwm->init.out.timerInterrupt);


	//打开PWM
	DRV_PWM_CHxCmd(hpwm->instance,hpwm->init.channel,DISABLE);
	return 0;
}


/**
 * @brief 初始化PWM模块.
 * 
 * @param[in] hpwm
 * @return
 * -0:成功
 * -1:失败 
 */
uint8_t HAL_PWM_Init(PWM_HandleTypeDef *hpwm)
{

	//配置全局句柄
	g_hpwm = hpwm;
	//PWM管脚复用
	switch(hpwm->init.channel)
	{
		case 0: HAL_IOCTRL_Swap(IOCTRL_SWAP16,ENABLE);break;
		case 1: HAL_IOCTRL_Swap(IOCTRL_SWAP17,ENABLE);break;
		case 2: HAL_IOCTRL_Swap(IOCTRL_SWAP18,ENABLE);break;
		case 3: HAL_IOCTRL_Swap(IOCTRL_SWAP19,ENABLE);break;
		case 4: HAL_IOCTRL_Swap(IOCTRL_SWAP20,ENABLE);break;
		case 5: HAL_IOCTRL_Swap(IOCTRL_SWAP21,ENABLE);break;
		case 6: HAL_IOCTRL_Swap(IOCTRL_SWAP22,ENABLE);break;
		case 7: HAL_IOCTRL_Swap(IOCTRL_SWAP23,ENABLE);break;
		default:return 1;
	}
	
	if(hpwm->init.direction == PWM_INPUT) //输入
	{
		return HAL_PWM_InputInit(hpwm);				
	}
	else  //输出
	{
		return HAL_PWM_OutputInit(hpwm);		
	}
}

