/*Copyright (C), 2024-2030
*文件名:	USER.c
*版  本:	Ver1.00
*日  期:	2025/04/30
*描  述:	常用函数定义
*备  注:     
**********************************************************/
#include "USER.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 == PWM1)
	{
		PWM1DUTL = dt;
		return;
	}
	if(num == PWM3)
	{	
		PWM3DUT = dt;	
		return;
	}
}
/**
	* @brief  1. 打开PWM0和PWM2，从P00输出2.7KHz，P01输出659Hz，占空比为1/2的PWM波形。
	*					
  * @param  None
  * @retval PWM0/1是12位可调周期占空比，PWM2/3是8位可调周期占空比
	*/
void PWM_INIT()
{
	/* 独立输出 */
	P00_FUN_PWM0;													//配置P00为PWM功能
	pwm_set_out_mode(PWM0, PWM_MODE_INDP);//设置PWM0独立输出
	pwm_pdiv(PWM0,9);										  //设置预分频10分频 27M/10 = 2.7M
	pwm_set_div(PWM0, 999); 							//设置PWM0的分频,输出波形频率为时钟源/(999+1) = 2.7M/1000=2.7K
	pwm_set_duty(PWM0, 500);							//设置PWM0的占空比为DUT/(DIV+1)=50%
	pwm_set_source_clk(PWM0, PWM_CLK_HRC);//设置PWM0的时钟源为HRC
	pwm_enable(PWM0);											//使能PWM0
	
	P01_FUN_PWM1;													//配置P01为PWM功能
	pwm_set_out_mode(PWM1, PWM_MODE_INDP);//设置PWM1独立输出
	pwm_pdiv(PWM1,9);										  //设置预分频10分频 27M/10 = 2.7M
	pwm_set_div(PWM1, 4095); 							//设置PWM1的分频,输出波形频率为时钟源/(4095+1) = 2.7M/4096=659.2Hz
	pwm_set_duty(PWM1, 2048);							//设置PWM1的占空比为DUT/(DIV+1)=50%
	pwm_set_source_clk(PWM1, PWM_CLK_SYS);//设置PWM1的时钟源为系统时钟
	pwm_enable(PWM1);											//使能PWM1

	P02_FUN_PWM2;													//配置P02为PWM功能
	pwm_set_out_mode(PWM2, PWM_MODE_INDP);//设置PWM2独立输出
	pwm_pdiv(PWM2,0);										  //不分频
	pwm_set_div(PWM2, 31); 								//设置PWM2的分频,输出波形频率为时钟源/(31+1) = 32K/32=1K
	pwm_set_duty(PWM2, 16);								//设置PWM2的占空比为DUT/(DIV+1)
	pwm_set_source_clk(PWM2, PWM_CLK_LRC);//设置PWM2的时钟源为LRC
	pwm_enable(PWM2);											//使能PWM2

	P03_FUN_PWM3;													//配置P03为PWM功能
	pwm_set_out_mode(PWM3, PWM_MODE_INDP);//设置PWM3独立输出
	pwm_pdiv(PWM3,0);											//不分频
	pwm_set_div(PWM3, 19); 								//设置PWM3的分频,输出波形频率为时钟源/(19+1) = 32K/2=1.6K
	pwm_set_duty(PWM3, 10);								//设置PWM3的占空比为DUT/(DIV+1)
	pwm_set_source_clk(PWM3, PWM_CLK_LRC);//设置PWM3的时钟源为LRC
	pwm_enable(PWM3);											//使能PWM3
	
	/*  互补输出 */	
//	P00_FUN_PWM0;													//配置P00为PWM功能
//	P01_FUN_PWM1;													//配置P01为PWM功能
//	pwm_set_out_mode(PWM0, PWM_MODE_COMP);//设置PWM0独立输出
//	pwm_pdiv(PWM0,9);										  //设置预分频10分频 27M/10 = 2.7M
//	pwm_set_div(PWM0, 999); 							//设置PWM0的分频,输出波形频率为时钟源/(999+1) = 2.7M/1000=2.7K
//	pwm_set_duty(PWM0, 500);							//设置PWM0的占空比为DUT/(DIV+1)=50%	
//	pwm_set_dt(PWM1, 150);								//设置PWM1的死区时间
//	pwm_set_source_clk(PWM0, PWM_CLK_HRC);//设置PWM0的时钟源为HRC
//	pwm_enable(PWM0);											//使能PWM0	
//	pwm_enable(PWM1);											//使能PWM1	
//		
//	P02_FUN_PWM2;													//配置P02为PWM功能
//	P03_FUN_PWM3;													//配置P03为PWM功能
//	pwm_set_out_mode(PWM2, PWM_MODE_COMP);//设置PWM2独立输出
//	pwm_pdiv(PWM2,0);										  //不分频
//	pwm_set_div(PWM2, 31); 								//设置PWM2的分频,输出波形频率为时钟源/(31+1) = 32K/32=1K
//	pwm_set_duty(PWM2, 16);								//设置PWM2的占空比为DUT/(DIV+1)
//	pwm_set_dt(PWM3, 5);									//设置PWM2的死区时间
//	pwm_set_source_clk(PWM2, PWM_CLK_LRC);//设置PWM2的时钟源为LRC
//	pwm_enable(PWM2);											//使能PWM2
//	pwm_enable(PWM3);											//使能PWM3
		
}
