/*Copyright (C), 2024-2030
*文件名:	PWM.c
*版  本:	Ver1.00
*日  期:	2025/04/30
*描  述:	常用函数定义
*备  注:     
**********************************************************/
#include "PWM.h"
#include "BL51F003T.h"	
#include "stdio.h"
/**********************************************************/
/**
	* @brief  使能PWMn
  * @param  num: PWM0-PWM7
  * @retval None
	*/
void pwm_enable(PWM_NUM_ENUM num)
{
	PWMEN |= (0x01<<num);
}

/**
	* @brief  关闭PWMn
  * @param  num: PWM0-PWM7
  * @retval None
	*/
void pwm_disable(PWM_NUM_ENUM num)
{
	PWMEN &= (~(0x01<<num));
}

/**
	* @brief  使能PWMn的中断
  * @param  num: PWM0-PWM7
  * @retval None
	*/
void pwm_ie_enable(PWM_NUM_ENUM num)
{
	PWMIE |= (0x01<<num);
}

/**
	* @brief  关闭PWMn的中断
  * @param  num: PWM0-PWM7
  * @retval None
	*/
void pwm_ie_disable(PWM_NUM_ENUM num)
{
	PWMIE &= (~(0x01<<num));
}

/**
	* @brief  读取PWMn的中断标志
  * @param  num: PWM0-PWM7
  * @retval None
	*/
char pwm_get_flag(PWM_NUM_ENUM num)
{
	return PWMFLG&(0x01<<num);
}	


/**
	* @brief  设置PWMn的时钟源
  * @param  num: PWM0-PWM7
	*         clk: 时钟源
  * @retval None
	*/
void pwm_set_source_clk(PWM_NUM_ENUM num, PWM_CLK_ENUM clk )
{
	PWM_N_ENUM n;
	
	if((clk<0)||(clk>3))
	{
		/* error : out of range */
		return;
	}
	
	if((num == PWM0)||(num == PWM1))
	{
		n = PWM0_1;
	}
	else if((num == PWM2)||(num == PWM3))
	{
		n = PWM2_3; 
	}
	else if((num == PWM4)||(num == PWM5))
	{
		n = PWM4_5;
	}

	
	//pwm_set_index(n);
	if((num == PWM0)||(num == PWM1))
	{
		PWM0CFG &= 0xFC;
		PWM0CFG |= clk;
	}
	if((num == PWM2)||(num == PWM3))
	{
		PWM2CFG &= 0xFC;
		PWM2CFG |= clk;
	}
}

/**
	* @brief  设置PWMn的预分频
  * @param  num: PWM0-PWM7
	*         pdiv: 预分频值(0-15)
  * @retval None
	*/
void pwm_pdiv(PWM_NUM_ENUM num, char pdiv)
{
	PWM_N_ENUM n;
	
	if((pdiv<0)||(pdiv>15))
	{
		/* error : out of range */
		return;
	}
	
	if((num == PWM0)||(num == PWM1))
	{
		n = PWM0_1;
		PWM0PDIV = pdiv;
	}
	else if((num == PWM2)||(num == PWM3))
	{
		n = PWM2_3;
		PWM2PDIV = pdiv;		
	}
}

/**
	* @brief  设置PWMn的输出模式
  * @param  num: PWM0-PWM7
	*         mode: 输出模式(独立/互补)
  * @retval None
	*/
void pwm_set_out_mode(PWM_NUM_ENUM num, PWM_MODE_ENUM mode)
{
	PWM_N_ENUM n;
	
	if((num == PWM0)||(num == PWM1))
	{
		n = PWM0_1;
	}
	else if((num == PWM2)||(num == PWM3))
	{
		n = PWM2_3;
	}
	else if((num == PWM4)||(num == PWM5))
	{
		n = PWM4_5;
	}

	//pwm_set_index(n);
	
	if(mode == PWM_MODE_INDP)
	{
		if(num == PWM0)
		{
			PWM0CFG &= 0xF7;
		}
		if(num == PWM2)
		{
			PWM2CFG &= 0xF7;
		}
	}
	else if(mode == PWM_MODE_COMP)
	{
		if(num == PWM0)
		{
			PWM0CFG |= 0x08; 
		}
		if(num == PWM2)
		{
			PWM2CFG |= 0x08; 
		}
	}
}

/**
	* @brief  设置PWMn的分频
  * @param  num: PWM0-PWM7
	*         div: 分频值(0-4095)
  * @retval None
	*/
void pwm_set_div(PWM_NUM_ENUM num, int div)
{
	if((div<0) || (div>4095))
	{
		/* out of range */
		return;
	}
	
	//pwm_set_index(num);
	if(num == PWM0)
	{
		PWM0DIVH = (div>>8)&0xFF;
		PWM0DIVL = div&0xFF;
	}
	if(num == PWM1)
	{	
		PWM1DIVH = (div>>8)&0xFF;
		PWM1DIVL = div&0xFF;
	}
	if(num == PWM2)
	{	
		PWM2DIV = div&0xFF;
	}
	if(num == PWM3)
	{	
		PWM3DIV = div&0xFF;
	}
	if(num == PWM4)
	{
		PWM4DIVH = (div>>8)&0xFF;
		PWM4DIVL = div&0xFF;
	}
	if(num == PWM5)
	{	
		PWM5DIVH = (div>>8)&0xFF;
		PWM5DIVL = div&0xFF;
	}
}

/**
	* @brief  设置PWMn的占空比
  * @param  num: PWM0-PWM7
	*         duty: duty值(0-4095)
  * @retval None
	*/
void pwm_set_duty(PWM_NUM_ENUM num, int duty)
{
	if((duty<0) || (duty>4095))
	{
		/* out of range */
		return;
	}
	
	//pwm_set_index(num);
	if(num == PWM0)
	{
		PWM0DUTH = (duty>>8)&0xFF;
		PWM0DUTL = duty&0xFF;
	}
	if(num == PWM1)
	{
		PWM1DUTH = (duty>>8)&0xFF;
		PWM1DUTL = duty&0xFF;
	}
	if(num == PWM2)
	{
		PWM2DUT = duty&0xFF;
	}
	if(num == PWM3)
	{
		PWM3DUT = duty&0xFF;
	}
	if(num == PWM4)
	{
		PWM4DUTH = (duty>>8)&0xFF;
		PWM4DUTL = duty&0xFF;
	}
	if(num == PWM5)
	{
		PWM5DUTH = (duty>>8)&0xFF;
		PWM5DUTL = duty&0xFF;
	}
}

/**
	* @brief  设置PWMn的死区时间(仅用在互补输出模式)
  * @param  num: PWM1/PWM3/PWM5/PWM7
	*         dt: duty值(0-255)
  * @retval None
	*/
void pwm_set_dt(PWM_NUM_ENUM num, int dt)
{
	if((dt<0) || (dt>255))
	{
		/* out of range */
		return;
	}
	
	if((num == PWM0)||(num == PWM2)||(num == PWM4))
	{
		return;
	}
	
	//pwm_set_index(num);
	PWM0DUTL = dt;	
}
