#include "pwm.h"

void InitPort(void)
{
	//HAL_StatusTypeDef ret ;
	EPORT_InitTypeDef eport_init;

	HAL_IOCTRL_Swapint(IOCTRL_SWAP0, ENABLE);

	eport_init.Instance = EPORT2;
	eport_init.pins = EPORT_PIN16;
	eport_init.dir = GPIO_DIR_OUT;
	eport_init.it_status = DISABLE;
	eport_init.it_type = EPORT_RISINGFALLING_TRIGGER;
	eport_init.pull_up_status = ENABLE;
	eport_init.ioctrl_init.id = IOCTRL_GINTL;
	eport_init.ioctrl_init.ds = DS_NULL;
	eport_init.ioctrl_init.sr = SR_NULL;
	eport_init.ioctrl_init.is = IS_NULL;
	eport_init.ioctrl_init.pins = eport_init.pins;
	eport_init.ioctrl_init.ie = IE_INPUT_NULL;
	eport_init.ioctrl_init.ps = PS_PULL_UP;
	HAL_EPORT_Init(&eport_init);
	 
	_eport_epdr_pins_load_clr(EPORT2, EPORT_PIN16);
	_eport_epdr_pins_load_set(EPORT2, EPORT_PIN16);
}

PWM_HandleTypeDef hpwm;
void ConfigBeep(u8 status)
{
	if(status)
	{
		HAL_PWM_CHxCmd(&hpwm,ENABLE);
	}else 
	{
		HAL_PWM_CHxCmd(&hpwm,DISABLE);
	}	
}
void InitBeep(void)
{
	hpwm.instance = PWM;
	hpwm.init.channel = 7;
	hpwm.init.direction = PWM_OUTPUT;
	hpwm.init.out.deadzone = 0;
	hpwm.init.out.prescaler = 99;
	hpwm.init.out.clkdiv = PWM_CLK_DIV_1;
	hpwm.init.out.period = 20;
	hpwm.init.out.width = 10;//duty/5;
	hpwm.init.out.autoload = ENABLE;
	hpwm.init.out.timerInv= DISABLE;
	hpwm.init.out.pullup = ENABLE;
	hpwm.init.out.level = BIT_RESET;
	hpwm.init.out.timerInterrupt = DISABLE;//ENABLE;
	HAL_PWM_Init(&hpwm);	
	//DelayMS(1000);
	//HAL_PWM_CHxCmd(&hpwm,DISABLE);
	ConfigBeep(0);
}

PWM_HandleTypeDef hBlPwm;
void ConfigBacklight(u8 duty)
{
	if(duty)
	{
		HAL_PWM_CHxCmd(&hBlPwm,ENABLE);
	}else 
	{
		HAL_PWM_CHxCmd(&hBlPwm,DISABLE);
	}	
}

void InitBacklight(void)
{
	hBlPwm.instance = PWM;
	hBlPwm.init.channel = 0;
	hBlPwm.init.direction = PWM_OUTPUT;
	hBlPwm.init.out.deadzone = 0;
	hBlPwm.init.out.prescaler = 4;	//125MHz/5 = 25MHz
	hBlPwm.init.out.clkdiv = PWM_CLK_DIV_1;
	hBlPwm.init.out.period = 799;//25MHz/800 = 31.25KHz
	hBlPwm.init.out.width = 599;
	hBlPwm.init.out.autoload = ENABLE;
	hBlPwm.init.out.timerInv= DISABLE;
	hBlPwm.init.out.pullup = DISABLE;
	hBlPwm.init.out.level = BIT_RESET;
	hBlPwm.init.out.timerInterrupt = DISABLE;
	HAL_PWM_Init(&hBlPwm);	
	ConfigBacklight(0);
}
