
#include "motor.h"
#include "pwm.h"

#include "key.h"
#include "COM.h"
#include "adc.h"
#include "M_speed.h"
uint8_t  spd_ctrl_ms_cnt;
extern uint8_t   com_index;
void int_motor_commutate(void)
{

	   m_par_set.EN_REV_ROTATE_BIT=MOTOR_DIR;
		 m_par_set.hx_temp=0;
		 m_par_set.gpcc_sw_inc=0;
	
		 m_par_set.commutate_status=0;
		 m_par_set.back_hx_temp=0;
		 m_par_set.commutate=STOP_SPEED_VALUE;
		 m_par_set.stall=0;
		 m_par_set.commutate60_flag=0;
		 m_par_set.commutate_time=0;
		 m_par_set.commutate_index=1;
	   m_par_set.com_common_time_60=STOP_SPEED_VALUE+10;//25;
	   m_par_set.sum_common_time=0;
	   m_par_set.common_60_inc=0;
	   m_par_set.m_lock_time=0;
	   m_par_set.Vali_BEM_data=0;
	   m_par_set.DelayAngle=1;
	   SF.FLAG=0;
	   m_par_set.STOP_SPEED_V=STOP_SPEED_VALUE;
	   m_par_set.speed_unit=100;
		 //m_par_set.PRE_DUTY=FSS_PRE_DUTY;
		 m_par_set.COM_OV_TIME=COM_OVER_TIME;
     SF.COM_OV=SFCOM_OVER_TIME;
     m_par_set.SLOP_VAL=LIMIT_SLOP_MIN;
		 m_par_set.START_MODE=BLDC_MOTOR_START;
		 m_par_set.Angle=M_Angle;
}
//**********************************************//
void Start_Sensorless_BLDC(void){

  if(m_par_set.H_duty>=(LIMIT_MAX*4/5))//||(m_par_set.actual_speed>m_par_set.set_speed))
			SF.FLAG=0;
	if(SF.FLAG==RESET)//(m_par_set.Motor_state!=M_STOP)
	{
	   #if(V_PWM_MODE == 1)	
	      m_par_set.H_duty = calculate_start_pwm();
		    m_par_set.H_duty=FSS_START_DUTY-m_par_set.H_duty;
		    if(m_par_set.H_duty<START_PWM_MINDUTY)
				   m_par_set.H_duty=START_PWM_MINDUTY;
				if(m_par_set.H_duty>FSS_START_DUTY)
					 m_par_set.H_duty=FSS_START_DUTY;
		 #else
		       m_par_set.H_duty = FSS_START_DUTY;
		  #endif
	}
  if(SF.FLAG!=RESET)//(m_par_set.Motor_state==M_STOP)
	{
		 if(m_par_set.H_duty<FSS_START_DUTY)
		    m_par_set.H_duty = FSS_START_DUTY;
		 if(m_par_set.H_duty>=(LIMIT_MAX*4/6))
		    m_par_set.H_duty =(LIMIT_MAX*4/6);
	}
	PWM_SET_DUTY(m_par_set.H_duty)
	PWM_START()
}

//*****************************************************//
void Fan_Stop_Func(void){

	//m_par_set.H_duty = 0;
	PWM_SET_DUTY(0)
//	m_par_set.PWM_EN_BIT=0;//PWM_EN_BIT = 0;

}
//*******************************************************//
void Fan_SoftStart_end(void){
	
	spd_ctrl_ms_cnt++;
	if(spd_ctrl_ms_cnt == FAN_SOFTSTART_DELAY){
		spd_ctrl_ms_cnt = 0;
		if(m_par_set.H_duty >= FSS_END_DUTY){
			m_par_set.Motor_state=M_RUN;
			//m_par_set.H_duty=0x195;
		}else{
			m_par_set.H_duty++;//=PWM_DUTY_0PERCENT(M_PWM_ADDDUTY);// PWM_DUTY_PERCENT(1);
			//if((m_par_set.H_duty<=PWM_DUTY_PERCENT(16))||(m_par_set.H_duty>PWM_DUTY_PERCENT(18)))
			PWM_SET_DUTY(m_par_set.H_duty)
		}
		
	}
}
//****************************************************//
void  stop_mos(void)
{
   
	  PWM_STOP();
	  WH_OFF
	  VH_OFF
	  UH_OFF
	  WL_OFF
	  VL_OFF
	  UL_OFF		   

}
//****************************************************//
void motor_stop(void)
{
   
		int_motor_commutate();
		Fan_Stop_Func();
		stop_mos();
	   COMP12_InputHysteresisCmd(ENABLE);
		//COMP_Config(); 
    TIM_Cmd(TIM1, DISABLE);

}
/***************************************************
函数名称：uint16_t calculate_start_pwm(void)
功能：    启动PWM点空比根据电压计算
备注：
**************************************************/
uint16_t calculate_start_pwm(void)
{
	 uint16_t   AD_bat_com;
   AD_bat_com=AD_s.bat_data-M_WMIX_VDD*(4096/AD_REF*R_DOWN/(R_DOWN+R_UP));
	 AD_bat_com=AD_bat_com/PWM_COM;
	 return AD_bat_com; 

}
/***************************************************
函数名称：mc_start_test(void)
功能：    电机启停测试函数
备注：    
****************************************************/
void  m_test(void)
{
    m_par_set.test_time++;
		if(m_par_set.test_time>2000)
		 {
				motor_stop();
			  m_par_set.motor_start_intime=0;
		 }
		if(m_par_set.test_time>4000)
		{
				m_par_set.test_time=0;
				m_par_set.Motor_state=M_PRE_POS;
			  if((m_par_set.H_duty>=(LIMIT_MAX*4/5))||(m_par_set.actual_speed>m_par_set.set_speed))
					 SF.FLAG=0;
		}

}
//*************************************************//
void  bldc_process(void)
{    
    
	  	
   switch(m_par_set.Motor_state)
	 {
		   case M_INIT:
				     m_par_set.test_time=0;
			       if(SF.FLAG==0)
						 {
			         //adc_int();
				       motor_stop();
             }
						  m_par_set.set_speed =0;//6000;
             m_par_set.motor_start_intime=0;						 
			 break;
  		 case M_PRE_POS:
				     
			 			 TIM_Cmd(TIM17, DISABLE);
			       speed_int();
			       if(SF.FLAG)
						 {
							 m_par_set.Motor_state=M_START_OFF;
						   com_set_PWM(m_par_set.hx_temp);
							 Start_Sensorless_BLDC();
							 SF.FLAG=0;
							 break; 
						 }
			       m_par_set.motor_start_intime++;
			       if(m_par_set.motor_start_intime<=2)
				     {
							 COMP12_InputHysteresisCmd(DISABLE);
							 TIM_Cmd(TIM1, ENABLE);
							 #if(M_START_FLAG == 0)	
							  pre_start();
							 #endif
							 m_par_set.Motor_state=M_START;
				     }
        
			 break;
			 case M_START:
	           	
              m_par_set.motor_start_intime++;  
//			        if(m_par_set.motor_start_intime<M_PRESTART_TIME)
//							  break;   
			        if(m_par_set.motor_start_intime==M_PRESTART_TIME)//FAN_RESETstop_TIME)
					    {
								 #if(M_START_FLAG == 0)
					       stop_mos();
								 #else
					       clr_pid_i();
								 ipd_process();
								 #endif
					    }
			
						  if(m_par_set.motor_start_intime==M_PRESTOPSTART_TIME)//&&(m_par_set.motor_start_intime<FAN_RESET_TIME))
						  {
								   
								 m_par_set.hx_temp=com_index;
								 com_set_PWM(com_index);
							   Start_Sensorless_BLDC();
						  }
				      if(m_par_set.motor_start_intime>M_RESET_TIME)
				      {
						     m_par_set.Motor_state=M_START_OFF;//	M_RUN;		//M_START_OFF;//				 						 
						
						  }				 
			 break;
		   case M_START_OFF:
				    Fan_SoftStart_end();
			     //m_par_set.Motor_state=M_RUN;
			 break;
		   case M_RUN:
				   #if(M_Control_SET == 1)
			         if(m_par_set.speed_loop_state == 0) 
					     {
						     if(read_key_state()==1)
						       m_par_set.actual_duty= PWM_DUTY_PERCENT(40);
							   if(read_key_state()==2)
						       m_par_set.actual_duty= PWM_DUTY_PERCENT(50);
							   if(read_key_state()==3)
						       m_par_set.actual_duty= PWM_DUTY_PERCENT(90);
							   if(read_key_state()==4)
						       m_par_set.actual_duty= LIMIT_MAX;//PWM_DUTY_PERCENT(100);
					   }
					   else
					   {  
						    if(read_key_state()==1)
				          m_par_set.set_speed =40000;//6000;	
							  if(read_key_state()==2)
				          m_par_set.set_speed =70000;//6000;
							  if(read_key_state()==3)
				          m_par_set.set_speed =90000;//6000;
							 if(read_key_state()==4)
				          m_par_set.set_speed =130000;//6000;
					  }
			     #else
				   
             
					 #endif	
           #if(M_TEST_FLAG == 1)	
				       m_test();
			     // #else
				  	   //6000;	
           #endif 						
			 break;
	     case M_STOP:
				   COMP12_InputHysteresisCmd(ENABLE);
				   Fan_Stop_Func();
			     stop_mos();	
			     #if(M_SF_START == 1)	 
			     SF.FLAG=1;
           TIM_Cmd(TIM17, ENABLE);	
			     #else
			     m_par_set.Motor_state=M_INIT;
           #endif 			 
			break;
	
		 case 6: 
			     
			break;
		default:
	
			break;
	}		

}

//************************************************************************//
