/**
******************************************************************************************************************************************************************************************
*@file                       cpu_timer.c
*@author         
*@data                       
*@version                    v1.0
*@par Copyright(c):
*
*@par History:
*     version:author,date,desc
*     v1.0   :create this file
*
*******************************************************************************************************************************************************************************************
*/
#include "cpu_timer.h"
#include "cpu_deepsleep.h"

#define TIMER_PWM_Set			                    ((uint32_t)0x08)
#define TIMER_PWM_Reset		                        (~TIMER_PWM_Set)

#define TIMER_MODE_Set		                        ((uint32_t)0x02)
    
#define TIMER_ENABLE_Set			                ((uint32_t)0x01)
#define TIMER_ENABLE_Reset		                    (~TIMER_ENABLE_Set)

#define TIMER_Interrupt_Mask_Set			        ((uint32_t)0x04)
#define TIMER_Interrupt_Mask_Reset		            (~TIMER_Interrupt_Mask_Set)

#define TIMER_Interrupt_Status_Set		        	((uint32_t)0x01)
#define TIMER_Interrupt_Status_Reset		        (~TIMER_Interrupt_Status_Set)

/*******************************************************************************
* 函数名   : timer_init.
* 说明     : timer 的初始化
* 参数     : -timerx,     		  timer分为,TIMER0、TIMER1、TIMER2、TIMER3
             -timer_init_struct,  timer的初始化参数配置,参考 timer_init_type_def 定义
* 返回值   : None.
* 示例     : timer_init(TIMER0,&timer_init_struct);
*******************************************************************************/
void timer_init(timer_type_def* timerx, timer_init_type_def* timer_init_struct)
{
	timer_cmd(timerx,DISABLE);
	
	timerx->LoadCount = timer_init_struct->timer_period;
	timerx->ControlReg |= TIMER_CONTROL_REG_TIMER_MODE_Mask;
	
	timer_cmd(timerx, ENABLE);
}

/*******************************************************************************
* 函数名   : timer_pwm_init. 注意：只有TIMER0、TIMER1才能产生PWM
* 说明     : timer_pwm的初始化
* 参数     : -pwmx,     		  pwmx分为 PWM_0、PWM_1
             -timer_init_struct,  timer的初始化参数配置,参考 timer_init_type_def 定义
* 返回值   : None.
* 示例     : timer_pwm_init(PWM_0,&timer_init_struct);
*******************************************************************************/
void timer_pwm_init(uint8_t pwmx, timer_init_type_def* timer_init_struct)
{
	if( pwmx == 0 )
	{
		TIMER0_M -> Timer1ControlReg = 0x00000000;
		
		TIMER0_M -> Timer1LoadCount = (double)timer_init_struct->TIMER_PWM_Hz* ( 1 - (double)timer_init_struct->TIMER_PWM_duty_cycle / (double)100 ) - 1;
		TIMER0_M -> Timer1LoadCount2 = (double)timer_init_struct->TIMER_PWM_Hz * ( (double)timer_init_struct->TIMER_PWM_duty_cycle / (double)100 ) - 1;
		
		TIMER0_M -> Timer1ControlReg |= BIT1 | BIT3;
		TIMER0_M -> Timer1ControlReg |= BIT0;
	}
	else if( pwmx == 1 )
	{
		TIMER0_M -> Timer2ControlReg = 0x00000000;
		
		TIMER0_M -> Timer2LoadCount = (double)timer_init_struct->TIMER_PWM_Hz* ( 1 - (double)timer_init_struct->TIMER_PWM_duty_cycle / (double)100 ) - 1;
		TIMER0_M -> Timer2LoadCount2 = (double)timer_init_struct->TIMER_PWM_Hz * ( (double)timer_init_struct->TIMER_PWM_duty_cycle / (double)100 ) - 1;
		
		TIMER0_M -> Timer2ControlReg |= BIT1 | BIT3;
		TIMER0_M -> Timer2ControlReg |= BIT0;
	}
	timer_cmd(TIMER0, DISABLE);
	timer_cmd(TIMER1, DISABLE);
}


// fix note 用呼吸灯例程重新测试验证
void pwm_start(uint8_t pwmx, timer_init_type_def* timer_init_struct)
{
	if( pwmx == 0 )
	{
		timer_cmd(TIMER0, DISABLE);
		TIMER0_M->Timer1ControlReg = 0x00000000;
		TIMER0_M->Timer1LoadCount  = timer_init_struct->TIMER_PWM_Hz * (100 - timer_init_struct->TIMER_PWM_duty_cycle )/100;
		TIMER0_M->Timer1LoadCount2 = timer_init_struct->TIMER_PWM_Hz * timer_init_struct->TIMER_PWM_duty_cycle / 100;
		TIMER0_M->Timer1ControlReg |= BIT1 | BIT3;
		TIMER0_M->Timer1ControlReg |= BIT0;
		timer_cmd(TIMER0, ENABLE);
	}
	else if( pwmx == 1 )
	{
		timer_cmd(TIMER0, DISABLE);
		TIMER0_M->Timer2ControlReg = 0x00000000;
		TIMER0_M->Timer2LoadCount  = timer_init_struct->TIMER_PWM_Hz * (100 - timer_init_struct->TIMER_PWM_duty_cycle )/100 ;
		TIMER0_M->Timer2LoadCount2 = timer_init_struct->TIMER_PWM_Hz * timer_init_struct->TIMER_PWM_duty_cycle / 100;
		TIMER0_M->Timer2ControlReg |= BIT1 | BIT3;
		TIMER0_M->Timer2ControlReg |= BIT0;
		timer_cmd(TIMER0, ENABLE);
	}
	cpu_prevent_sleep_set(TIMER0_ONGOING);
}


void pwm_stop(uint8_t PWMx)
{
	if( PWMx == 0 )
	{
		timer_cmd(TIMER0, DISABLE);
		TIMER0_M -> Timer1ControlReg = 0x00000000;
		TIMER0_M -> Timer1LoadCount  = 0;

	}
	else if( PWMx == 1 )
	{
		timer_cmd(TIMER0, DISABLE);
		TIMER0_M -> Timer2ControlReg = 0x00000000;
		TIMER0_M -> Timer2LoadCount  = 0;
	}
	
	cpu_prevent_sleep_clear(TIMER0_ONGOING);
}


/*******************************************************************************
* 函数名   : timer_get_current_count.
* 说明     : timer得到当前计数值
* 参数     : -timerx,     timer分为,TIMER0、TIMER1、TIMER2、TIMER3
* 返回值   : None.
* 示例     : timer_get_current_count(TIMER0);
*******************************************************************************/
uint32_t timer_get_current_count(timer_type_def * timerx)
{
	return timerx->CurrentValue;
}

/*******************************************************************************
* 函数名   : timer_cmd.
* 说明     : timer使能
* 参数     : -timerx,     timer分为,TIMER0、TIMER1、TIMER2、TIMER3
             -new_state,  使能或者不使能,参考 functional_state_t 定义
* 返回值   : None.
* 示例     : timer_cmd(TIMER0,ENABLE);
*******************************************************************************/
void timer_cmd(timer_type_def* timerx, functional_state_t new_state)
{
	if(new_state != DISABLE)
	{
		timerx->ControlReg |= TIMER_CONTROL_REG_TIMER_ENABLE_Mask;
	}
	else
	{
		timerx->ControlReg &= ~TIMER_CONTROL_REG_TIMER_ENABLE_Mask;
	}
}

/*******************************************************************************
* 函数名   : timer_it_config.
* 说明     : timer中断配置
* 参数     : -timerx,     timer分为,TIMER0、TIMER1、TIMER2、TIMER3
             -new_state,  使能或者不使能,参考 functional_state_t 定义
* 返回值   : None.
* 示例     : timer_it_config(TIMER0,ENABLE);
*******************************************************************************/
void timer_it_config(timer_type_def* timerx,functional_state_t new_state)
{
	if(new_state != DISABLE)
		timerx->ControlReg &= ~TIMER_CONTROL_REG_TIMER_INTERRUPT_MASK_Mask;
	else
		timerx->ControlReg |= TIMER_CONTROL_REG_TIMER_INTERRUPT_MASK_Mask;
}

/*******************************************************************************
* 函数名   : timer_get_it_status.
* 说明     : 获取timer中断状态
* 参数     : -timerx,     			timer分为,TIMER0、TIMER1、TIMER2、TIMER3
* 返回值   : -bitstatus = SET.		定时器产生中断
			 -bitstatus = RESET.	定时器产生未中断
* 示例     : timer_get_it_status(TIMER0);
*******************************************************************************/
itstatus timer_get_it_status(timer_type_def* timerx)
{
	itstatus bitstatus = RESET; 
  

  if ((timerx->IntStatus & TIMER_INT_STATUS_INTSTATUS_Mask) != RESET)
  {
    bitstatus = SET;
  }
  else
  {
    bitstatus = RESET;
  }
  return bitstatus;
}

/*******************************************************************************
* 函数名   : timer_clear_it_pending_bit.
* 说明     : 清除timer中断位
* 参数     : -timerx,     timer分为,TIMER0、TIMER1、TIMER2、TIMER3
* 返回值   : None.		
* 示例     : timer_clear_it_pending_bit(TIMER0);
*******************************************************************************/
void timer_clear_it_pending_bit(timer_type_def* timerx)
{
	uint32_t clr = timerx->EOI;
}

/*******************************************************************************
* 函数名   : timer_set_period.
* 说明     : 设置timer周期
* 参数     : -timerx,     timer分为,TIMER0、TIMER1、TIMER2、TIMER3
			 -Period.	  周期	
* 返回值   : None.		
* 示例     : timer_set_period(TIMER0,22222);
*******************************************************************************/
void timer_set_period(timer_type_def* timerx,uint32_t Period)
{
	timerx->LoadCount = Period;
}


void timer_set_pwm_period(timer_type_def* timerx,uint32_t pwm_period)
{
	/*if(timerx == TIMER0_0)
		TIMER0->Timer1LoadCount2 = pwm_period;
	else if(timerx == TIMER0_1)
		TIMER0->Timer2LoadCount2 = pwm_period;*/
}

flag_status timer_get_flag_status(timer_type_def* timerx)
{
	flag_status bitstatus = RESET; 
  /*

  if ((timerx->IntStatus & TIMER_Interrupt_Status_Set) != RESET)
  {
    bitstatus = SET;
  }
  else
  {
    bitstatus = RESET;
  }*/
  return bitstatus;
}


void timer_clear_flag(timer_type_def* timerx)
{
	//uint32_t clr = timerx->EOI;
}



#define __SYSTEM_CLOCK   (16000000UL)  

void timer0_pwm_set_duty(char duty)
{
	timer_init_type_def TIMER_init;
	cpu_prevent_sleep_set(TIMER0_ONGOING);
	TIMER_init.TIMER_PWM_Hz = __SYSTEM_CLOCK / 1000;
	TIMER_init.TIMER_PWM_duty_cycle=duty;
	timer_pwm_init(0,&TIMER_init);
	timer_cmd(TIMER0, ENABLE);
	cpu_prevent_sleep_clear(TIMER0_ONGOING);
	
	return;
}


