#include "ys32md211.h"




//主频参数
#define SYS_CLK    (48000000)
#define PWM_CLK    40000                         //电机驱动频率
#define PWM_PERIOD (SYS_CLK/PWM_CLK)
#define PWM_TIM    (SYS_CLK/48/PWM_CLK)

#define  POLE                     1                  //一对极
#define START_SPEED_MIN           5000//100//500// 1000//(1000)     //启动最小速度3000  
#define LOOP_SPEED_MIN            7000//                            // 闭环速度 5000
#define LOCK_START_MIN_TIME        1300                              
#define LOCK_START_MAX_TIME        (LOCK_START_MIN_TIME+3000)
#define MOTOR_DIR     1          //正反转
#define M_TEST_FLAG  (0)         //启动测试
#define M_BMF_TIME   (9)         //检测反电动势有效次数
//*********************************************************//
#define LIMIT_SLOP_MIN       (-40)
#define LIMIT_MAX            (PWM_PERIOD)   //电机PWM最大占空比
#define LIMIT_MIN            (LIMIT_MAX/20)                 //电机PWM最小占空比
//********************************************************************//
#define COM_OVER_TIME        (12000/(1000000/PWM_CLK)) //换相超时设定
#define SFCOM_OVER_TIME      (6500/(1000000/PWM_CLK))  //SF换相超时设定
#define M_Angle              (0)                       //范围0-30     //转速高设置小，否则设置越大
#define STOP_SPEED_VALUE     (((PWM_CLK*60)/(1800*6)))  //判断停止速度250//
#define RAMP_PERIOD                (5)                 //爬坡和下坡PWM周期ms

////启动/开环pi
#define START_KP            (0.001)//0.35)      
#define START_KI            (0.00001)//(0.002)

#define  M_Control_SET      (M_key_mode) //电机控制模式
#define  M_key_mode         1            //按键  
#define  M_pwm_mode         2            // PWM
#define  M_adc_mode         3            // ADC
//启动换相方式
#define BLDC_MOTOR_START   (M_SLOPE)            
#define M_EXPERT           (1)                    
#define M_SLOPE            (0)   
#define COM_ERROR          (3)           //换相异常次数
//运行模式
#define MC_LOOP            (M_CLOSE_LOOP)                 
#define M_OPEN_LOOP        (0)               //开环运行
#define M_CLOSE_LOOP       (1)               //闭环运行
//启动方式
#define M_START_FLAG       (M_forced)       //启动方式
#define M_IPD              1                //脉冲注入
#define M_forced           0                //强拖
//  顺风启动
#define M_SF_START        (1)               //顺风开关
//******************************************************************//
#define  OPAM_EN          (0)              //运放开关使能
#if(OPAM_EN == 1)	
  #define  OPAM_GAIN      (10)
#else
  #define  OPAM_GAIN      (1)
#endif
//************************************************************//
//   电压电流检测
#define  I_BUS_R           (0.001)  //电阻大小
#define  R_DOWN			        2      //10K
#define  R_UP			          10     //20K
#define  AD_REF            (5)     //5VAD参考电压
#define  OVRE_VDD          (9)     //过压设置
#define  LOW_VDD           (5.8)   //低压设置
#define  REV_OVRE_VDD      (8.4)   //恢复过压
#define  REV_LOW_VDD       (6.2)   //恢复低压
#define  OVI_MAX           (22)
#define  Over_i            (4096/AD_REF*OVI_MAX*I_BUS_R*OPAM_GAIN)  //过流定义
#define  P_Over_VDD        (4096/AD_REF*R_DOWN/(R_DOWN+R_UP)*OVRE_VDD)   //过压定义
#define  P_LOW_VDD         (4096/AD_REF*R_DOWN/(R_DOWN+R_UP)*LOW_VDD)    //欠压定义 
#define  NOR_REV_LOW_VDD   (4096/AD_REF*R_DOWN/(R_DOWN+R_UP)*REV_LOW_VDD)    //恢复欠压
#define  NOR_REV_OVRE_VDD  (4096/AD_REF*R_DOWN/(R_DOWN+R_UP)*REV_OVRE_VDD)    //恢复过压 
#define  V_VDD_EN          (0)           //过压欠压保护使能
//************************************************************//
#define V_PWM_MODE         (1)            //根据电压调启动PWM占空比模式
//******************************************************************//
typedef enum
{
  M_INIT,
	M_PRE_POS,
	M_START,
	M_START_OFF,
	M_RUN,
	M_STOP,
	M_FAULT,
	M_BRAKE
}STATE;
//***********************************************************//
typedef struct {

	  uint8_t   Motor_state;
	 // uint8_t   PWM_EN_BIT;
	  uint8_t   EN_REV_ROTATE_BIT;
    uint8_t   hx_temp;             // : 1;  // bit0:????
    uint8_t   commutate_status;         // : 1;  // bit2:?????
    uint8_t   gpcc_inc ;           //: 1;  // bit4:????
    uint16_t  gpcc_sw_inc;         // : 2;  // bit5~6:??????
	  uint16_t  H_duty;
	  uint16_t  motor_start_intime;

	  uint8_t   back_hx_temp;

	  uint16_t  commutate;
	  uint16_t  com_common_time_60; 
	  uint8_t   commutate60_flag;
	  uint16_t  ramp_cnt;
	  unsigned   char fault;
	  unsigned   char state;
	  uint8_t   stall;

		uint8_t    speed_loop_state;
		uint32_t   set_duty;
	  uint32_t   actual_duty;
		uint32_t   actual_value; 
	  uint32_t   set_value;
		uint16_t   loop_cnt; 
		uint32_t   set_speed;          //éè???ù?è
	  uint32_t   actual_speed;       //êμ?ê?ù?è
		uint16_t   commutate_time;
		uint16_t   commutate_index;
		uint16_t   bk_commutate_index;
		uint8_t    commutate_flag;
		uint8_t    Angle;
		uint16_t   DelayAngle;
		uint8_t    common_60_inc;
		uint16_t   sum_common_time;
		uint16_t   com_common_sum_60;
		uint16_t   m_lock_time;
		uint8_t    Vali_BEM_data;
		uint16_t   Abnor_time;
		uint16_t   test_time;
		uint16_t   speed_unit;
	//	uint8_t    SF;
//		uint16_t   SF_gpcc_inc;
//		uint8_t    SF_out;
		uint16_t   STOP_SPEED_V;
		//uint16_t   PRE_DUTY;
		uint16_t   COM_OV_TIME;
//		uint16_t   SF_COM_OV;
		int16_t    SLOP_VAL;
		uint8_t    START_MODE;
} M_par;
 extern M_par  m_par_set;



//***********************************************************//
typedef struct {
	
	  uint16_t   gpcc_inc;
		uint8_t    out;
	  uint16_t   COM_OV;
    uint8_t    FLAG;
} M_SF;
 extern M_SF  SF;


void irq_ims(void);
void motor_commutate_process(void);
//void  bldc_process(void);
void  stop_mos(void);
void Start_Sensorless_BLDC(void);
void  bldc_process(void);
int16_t slope_calu(uint16_t com_data1,uint16_t com_data2);
void Electrical_Angle_Phase_Calculation(void);
void int_motor_commutate(void);
void pre_start(void);
void ipd_process(void);
void com_set_process(void);
void TIMDelay_N10us(uint16_t Times);
void TIMDelay_Nms(uint16_t Times);
void com_set_PWM(uint8_t temp);
void Fan_Stop_Func(void);
void sf_check(void);
uint16_t calculate_start_pwm(void);

