/****************************************************************************
* 文件名: def.h
* 内容简述: 预处理头文件	
* 
* 文件历史:
* 版本号  日期       作者    说明
*
*/

#ifndef DEF_H_
#define DEF_H_
#include "main.h"
//#include "hc32f4xx.h"
//#include "hc32_ll.h"

#define LL_PERIPH_SEL               (LL_PERIPH_GPIO | LL_PERIPH_EFM | LL_PERIPH_FCG | LL_PERIPH_PWC_CLK_RMU |\
                                         LL_PERIPH_SRAM)
#define EXAMPLE_PERIPH_WP           (LL_PERIPH_EFM | LL_PERIPH_FCG | LL_PERIPH_SRAM)


#define TS_MAX 			            51200//38400//200k  PWM最大周期 
#define TS_MIN                  7314//21943//350k  PWM最小周期 
#define DEAD_TIME_VAR_MAX       2051


#define PWM1_OPEN() 		CLR_REG32_BIT(CM_EMB0->SOE,1)//使能PWM输出
#define PWM1_CLOSE() 		SET_REG32_BIT(CM_EMB0->SOE,1)//禁止PWM输出

#define SR1_OPEN() 			CLR_REG32_BIT(CM_EMB2->SOE,1)//使能PWM输出
#define SR1_CLOSE() 		SET_REG32_BIT(CM_EMB2->SOE,1)//禁止PWM输出

#define PWM2_OPEN() 		CLR_REG32_BIT(CM_EMB1->SOE,1)//使能PWM输出
#define PWM2_CLOSE() 		SET_REG32_BIT(CM_EMB1->SOE,1)//禁止PWM输出

#define SR2_OPEN() 			CLR_REG32_BIT(CM_EMB3->SOE,1)//使能PWM输出
#define SR2_CLOSE() 		SET_REG32_BIT(CM_EMB3->SOE,1)//禁止PWM输出

#define PWM_OPEN() 			PWM1_OPEN();PWM2_OPEN()		//使能PWM输出
#define PWM_CLOSE() 		PWM1_CLOSE();PWM2_CLOSE()	//禁止PWM输出

#define SR_OPEN() 	    SR1_OPEN();SR2_OPEN()			//使能PWM输出
#define SR_CLOSE() 	    SR1_CLOSE();SR2_CLOSE()		//禁止PWM输出

#define HRPWM_PHASE1_ENABLE   (CLR_REG32_BIT(CM_EMB0->SOE,1))
#define HRPWM_PHASE1_DISABLE  (SET_REG32_BIT(CM_EMB0->SOE,1))

#define HRPWM_PHASE2_ENABLE   (CLR_REG32_BIT(CM_EMB1->SOE,1))
#define HRPWM_PHASE2_DISABLE  (SET_REG32_BIT(CM_EMB1->SOE,1))

//#define HRPWM_PAHSE1_ALL      (HRPWM_UNIT1 | HRPWM_UNIT5 | HRPWM_UNIT3)
//#define HRPWM_PAHSE2_ALL      (HRPWM_UNIT6 | HRPWM_UNIT2 | HRPWM_UNIT4)
//#define HRPWM_SR_ALL          (HRPWM_UNIT4 | HRPWM_UNIT3)

//#define LLC_PWM_DISABLE_IDLEMODE_IMMD_ALL            (SET_REG32_BIT(CM_HRPWM_COMMON->SSTAIDLR, HRPWM_UNIT_ALL))   //imm
//#define LLC_PWM_DISABLE_IDLEMODE_ALL                 (SET_REG32_BIT(CM_HRPWM_COMMON->SSTADIDLR, HRPWM_UNIT_ALL))
//#define LLC_PWM_DISABLE_IDLEMODE_PHASEA              (SET_REG32_BIT(CM_HRPWM_COMMON->SSTADIDLR, HRPWM_PAHSE1_ALL))
//#define LLC_PWM_DISABLE_IDLEMODE_PHASEB              (SET_REG32_BIT(CM_HRPWM_COMMON->SSTADIDLR, HRPWM_PAHSE2_ALL))

//#define LLC_PWM_ENABLE_IDLEMODE_ALL                  (WRITE_REG32(CM_HRPWM_COMMON->SSTARUNR1, 0x0FFF))
//#define LLC_PWM_ENABLE_IDLEMODE_ALL_NOT_SR           (WRITE_REG32(CM_HRPWM_COMMON->SSTARUNR1, 0x0F0F))
//#define CHB_LLC_PWM_ENABLE_IDLEMODE_NOT_SR           (WRITE_REG32(CM_HRPWM_COMMON->SSTARUNR1, 0x0909))
//#define LLC_PWM_ENABLE_IDLEMODE_PHASEA               (WRITE_REG32(CM_HRPWM_COMMON->SSTARUNR1, 0x0333))
//#define LLC_PWM_ENABLE_IDLEMODE_PHASEA_NOT_SR        (WRITE_REG32(CM_HRPWM_COMMON->SSTARUNR1, 0x0303))
//#define LLC_PWM_ENABLE_IDLEMODE_PHASEB               (WRITE_REG32(CM_HRPWM_COMMON->SSTARUNR1, 0x0CCC))
//#define LLC_PWM_ENABLE_IDLEMODE_PHASEB_NOT_SR        (WRITE_REG32(CM_HRPWM_COMMON->SSTARUNR1, 0x0C0C))
//#define ILLLC_HAL_SRDriverEnable                     (WRITE_REG32(CM_HRPWM_COMMON->SSTARUNR1, 0x00F0))
////#define ILLLC_HAL_SRDriverEnable                     (SET_REG32_BIT(CM_HRPWM_COMMON->SSTARUNR1, 0x0000))
//#define ILLLC_HAL_SRDriverDisable                    (SET_REG32_BIT(CM_HRPWM_COMMON->SSTADIDLR, HRPWM_SR_ALL))

#define LLC_HAL_SRA_PIN      			GPIO_PIN_05
#define LLC_HAL_SRA_Portx 			  GPIO_PORT_A

#define LLC_HAL_SRB_PIN      			GPIO_PIN_10
#define LLC_HAL_SRB_Portx 			GPIO_PORT_B

#define LLC_HAL_RELAY_PIN      		GPIO_PIN_14
#define LLC_HAL_RELAY_Portx  	GPIO_PORT_C

#define LLC_HAL_FAN_PIN       GPIO_PIN_01
#define LLC_HAL_FAN_Portx  	  GPIO_PORT_A
//SR A HW enable
//#define SRA_HAL_ENABLE     SET_REG32_BIT(CM_GPIO->PORRA , (1L << 5))
////SR A HW disable
//#define SRA_HAL_DISABLE    SET_REG32_BIT(CM_GPIO->POSRA , (1L << 5))

////SR B HW enable
//#define SRB_HAL_ENABLE     SET_REG32_BIT(CM_GPIO->PORRB , (1L << 10))

////SR B HW enable
//#define SRB_HAL_DISABLE    SET_REG32_BIT(CM_GPIO->POSRB , (1L << 10))


//RELAY_ON
#define RELAY_ENABLE       SET_REG32_BIT(CM_GPIO->POSRC , (1L << 14))
#define RELAY_DISABLE      SET_REG32_BIT(CM_GPIO->PORRC , (1L << 14))

//LED 
#define LLC_HAL_LED1_PIN      			GPIO_PIN_12
#define LLC_HAL_LED1_Portx 			    GPIO_PORT_A

#define LLC_HAL_LED2_PIN      			GPIO_PIN_11
#define LLC_HAL_LED2_Portx 			    GPIO_PORT_C

/*****************************************EMB*************************/
#define EMB_HW_OVP         						EMB_FLAG_PORT1	
#define EMB_HW_OCP         						EMB_FLAG_PORT2
#define EMB_HW_PRICURRT1_OCP          EMB_FLAG_PORT4	
#define EMB_HW_PRICURRT2_OCP          EMB_FLAG_PORT3
#define EMB_CMP_OCP         					EMB_FLAG_CMP3
#define EMB_CMP_PRICURRT1_OCP         EMB_FLAG_CMP1	
#define EMB_CMP_PRICURRT2_OCP         EMB_FLAG_CMP2
#define EMB_HW_FAULT_ALL              (EMB_FLAG_PORT1 | EMB_FLAG_PORT2 | EMB_FLAG_PORT3 | EMB_FLAG_PORT4)
 
#define HRPWM_PWM_Out_EN()  HRPWM_PWM_ChAOutputEnable(HRPWM_UNIT_1);\
														HRPWM_PWM_ChBOutputEnable(HRPWM_UNIT_1);\
														HRPWM_PWM_ChAOutputEnable(HRPWM_UNIT_2);\
														HRPWM_PWM_ChBOutputEnable(HRPWM_UNIT_2);\
														HRPWM_PWM_ChAOutputEnable(HRPWM_UNIT_3);\
														HRPWM_PWM_ChBOutputEnable(HRPWM_UNIT_3);\
														HRPWM_PWM_ChAOutputEnable(HRPWM_UNIT_4);\
														HRPWM_PWM_ChBOutputEnable(HRPWM_UNIT_4);\
														HRPWM_PWM_ChAOutputEnable(HRPWM_UNIT_5);\
														HRPWM_PWM_ChBOutputEnable(HRPWM_UNIT_5);\
                            HRPWM_PWM_ChAOutputEnable(HRPWM_UNIT_6);\
														HRPWM_PWM_ChBOutputEnable(HRPWM_UNIT_6)														



//===========================故障标志宏定义=====================

#define   OUTPUT_DOWN_ALARM_FLAG_0          	(flag &= ~0x01)   
#define		OUTPUT_DOWN_ALARM_FLAG_1 		        (flag |= 0x01)
#define		OUTPUT_DOWN_IS_ALARM         		    (flag & 0x01)

#define   LOCK_MODULE_0                  		  (flag &= ~0x02)  
#define		LOCK_MODULE_1		   	     		        (flag |= 0x02)
#define		LOCK_MODULE                	   		  (flag & 0x02)
						
//#define   OFFLINE_FLAG_0						          (flag &= ~0x04) 
//#define		OFFLINE_FLAG_1		                  (flag |= 0x04)
//#define		IS_OFFLINE_FAULT             		    (flag & 0x04)

#define     FAN_FAULT_FLAG_0        			(flag &= ~0x04)    
#define		  FAN_FAULT_FLAG_1		   			  (flag |= 0x04)
#define		  FAN_IS_FAULT             			(flag & 0x04)
#define		  FAN_IS_OK             			  !(flag &0x04)

#define   CUR_LIMIT_FLAG_0    			 	        (flag &= ~0x08)   
#define		CUR_LIMIT_FLAG_1 					          (flag |= 0x08)
#define		IS_CUR_LIMIT		      			        (flag & 0x08)

#define   UO_OVER_FLAG_0             			    (flag &= ~0x10)   
#define   UO_OVER_FLAG_1             			    (flag |= 0x10)
#define   UO_IS_OVER							            (flag & 0x10)

#define   UI_OVER_DOWN_ALARM_FLAG_0 								(flag &= ~0x20)   
#define   UI_OVER_DOWN_ALARM_FLAG_1 								(flag |= 0x20)
#define		IS_UI_OVER_DOWN_ALARM										(flag & 0x20)

#define   TMP_SHUT_FLAG_0											(flag &= ~0x40)   
#define   TMP_SHUT_FLAG_1											(flag |= 0x40)
#define   TMP_IS_TOO_HIGH_TO_SHUT							(flag & 0x40)

#define   Vout_OVER_ALARM_FLAG_0										(flag &= ~0x80)   
#define   Vout_OVER_ALARM_FLAG_1										(flag |= 0x80)
#define		Vout_IS_OVER_ALARM									(flag & 0x80)

#define   IS_SMALL_FAULT											(flag&0x00)  //(flag&0x40)// 
#define   IS_ALARM				  									(flag&0xAD)//

#define NOT_FAULT_ON_RELAY_CNT    Time_100ms//1111//
#define FAULT_ON_RELAY_CNT        33333//

#define  FAULT_SHUT_RL1_INS       	0XE1//0b11100001 故障指令，亮红灯不开继电器，高位与低位为1，确保通信正常
#define  FAULT_UNSHUT_RL1_INS     	0XA1//0b10100001 故障指令，亮红灯开继电器
#define  ALARM_INS                  0X91//0b10010001 告警指令，亮黄灯开机
#define  OK_INS                		  0X89//0b10001001 正常指令，亮绿灯开机
#define	 SLEEP_INS					        0XC5//0b11000101 休眠指令
#define	 Vbus_ADJUST_INS					  0XE3//0b11100011 前级母线电压校准指令

#define     IO_TXD            0
#define     TMP_TXD 	   	    1
#define     STATE_FAULT  	  	2
#define     UI  							3
#define 		U_OUT  	  		  	4
#define 		TEMP  						5
#define 		FAN  							6
#define 		UC   							7


#define   IO_LMT            		0
#define		FLOAT_SHUT	   			  1
#define		FLT	   		   		      2
#define		AVR			   			      3
#define 	UO_OV 	   	   	      4
#define		UO_LK		   	   		    5
#define		UI_OV	   		   		    6
#define 	UI_LK		  	  				7
#define 	CHARGE_LMT		   		  8
#define		TMP_PRTCT	   	   	    9
#define 	TRIM_SET			   		 10
#define   TYP_18_50            11

#define	STATE_SHUT_MODULE			0x0001
#define	STATE_ABNORMAL_SHUT		0x0002
#define	STATE_ACV_LACK				0x0004	
#define	STATE_ACV_OVER				0x0008
#define	STATE_DCV_LACK				0x0010
#define	STATE_DCV_OVER				0x0020
#define	STATE_CUR_LIMIT			  0x0040
#define	STATE_FAN_FAULT			  0x0080
#define	STATE_TMP_OVER				0x0100
#define	STATE_HARD_FAULT			0x0200	
#define	STATE_OVTEMPDROP_FAULT			0x0400
#define	STATE_VinVoutHARD_FAULT			0x0800//保护电路故障
#define	STATE_ACV_LACK_alarm				0x1000	
#define	STATE_ACV_OVER_alarm				0x2000
#define	STATE_FAN_SEC_FAULT			    0x4000
//关机位
#define	SHUT_MODULE						0x0003
#define	STATE_DC_SHORT				0x0006




#define DT_CNT_MAX 497
#define FB_ZERO  496


#define VIN_CNT_MAX  40//
#define VOT_CNT_MAX  160   //
#define SR_CNT_MAX   419
#define DEFAULT_VOS  1200
#define DEFAULT_IOS  525
#define MAX_COMM_VOS  3250
#define MIN_COMM_VOS  2150


#define	TEMP_35_Degree	2859
#define	TEMP_40_Degree	2703
#define	TEMP_45_Degree	2541
#define	TEMP_50_Degree	2379
#define	TEMP_55_Degree	2216
#define	TEMP_60_Degree	2054
#define	TEMP_65_Degree	1897
#define	TEMP_70_Degree	1746
#define	TEMP_75_Degree	1598
#define	TEMP_80_Degree	1464
#define	TEMP_85_Degree	1334
#define	TEMP_90_Degree	1217
#define	TEMP_95_Degree	1105
#define	TEMP_100_Degree 1004
#define	TEMP_105_Degree 912
#define	TEMP_110_Degree 827
#define	TEMP_115_Degree 751
#define	TEMP_120_Degree 681
#define	TEMP_125_Degree 618

#define	P_DOWN_TEMP		TEMP_85_Degree   

#define Prc_comp_0_4V   852
#define Prc_comp_0_2V   425
#define I_Step  2//调流步长   1A
#define V_Step  10//调压步长   10mV
//运放电压采样1V--61lsb
//10k+51k分压

#define VOS_REF_MAX			VOS_REF_13V
#define VOS_REF_0_4V    1635
#define VOS_REF_0_2V    817
#define VOS_REF_0_3V    1226
#define VOS_REF_1V      4089//
#define VOS_REF_2V      8178//
#define VOS_REF_6V      24535//
#define VOS_REF_12V     49070//
#define VOS_REF_13V     53159//



#define VOT_0V5     127
#define VOT_0V8     204
#define VOT_1V      255
#define VOT_1_2V    306
#define VOT_1V5     383
#define VOT_3V      766
#define VOT_4_5V    1149
#define VOT_5V      1277

#define VOT_10V     2555
#define VOT_12V     3066
#define VOT_14V     3577
#define VOT_15V     3832


#define	IOT_PI_MAX		(32L*FB_CNT_MAX)//(128L*TS_MAX)
#define	IOT_PI_MIN		(0)//0

#define	VOT_PI_MAX		(32L*FB_CNT_MAX)//(128L*TS_MAX)
#define	VOT_PI_MIN		(0)//0   0-128L*2982

#define	FAST_VOT_PI_MAX		(16L*FB_CNT_MAX)//(128L*TS_MAX)
#define	FAST_VOT_PI_MIN		(0)//0   0-128L*2982

#define I_LIMIT_DEF  I_LIMIT_26_25A
#define I_LIMIT_30A  1406
#define I_LIMIT_29A  1366
#define I_LIMIT_28A  1326
#define I_LIMIT_27A  1286
#define I_LIMIT_26_25A  1516//1256
#define I_LIMIT_26A  1245
#define I_LIMIT_25A  1205
#define I_LIMIT_24A  1165
#define I_LIMIT_23A  1125
#define I_LIMIT_22A  1085
#define I_LIMIT_21A  1045
#define I_LIMIT_20A  1005
#define I_LIMIT_19A  964
#define I_LIMIT_18A  924
#define I_LIMIT_17A  884
#define I_LIMIT_16A  844
#define I_LIMIT_15A  804
#define I_LIMIT_14A  764
#define I_LIMIT_13A  862//724
#define I_LIMIT_12A  683
#define I_LIMIT_11A  643
#define I_LIMIT_10A  603
#define I_LIMIT_9A   563
#define I_LIMIT_8A   523
#define I_LIMIT_7A   483
#define I_LIMIT_6A   443
#define I_LIMIT_5A   452//402
#define I_LIMIT_4A   402//362
#define I_LIMIT_3A   353//322
#define I_LIMIT_2A   305//282
#define I_LIMIT_1A   256//242


#define V_LIMIT_DEF  V_LIMIT_28
#define V_LIMIT_36   3436
#define V_LIMIT_35   3340
#define V_LIMIT_34   3245
#define V_LIMIT_33   3149
#define V_LIMIT_32   3055
#define V_LIMIT_31   2960
#define V_LIMIT_30   2864
#define V_LIMIT_29   2769
#define V_LIMIT_28   2673
#define V_LIMIT_27   2578
#define V_LIMIT_26   2482
#define V_LIMIT_25   2387
#define V_LIMIT_24   2291
#define V_LIMIT_23   2196
#define V_LIMIT_22   2101
#define V_LIMIT_21   2005
#define V_LIMIT_20   1910
#define V_LIMIT_19   1814
#define V_LIMIT_18   1719
#define V_LIMIT_05   50


#define IOT_10A      603
#define IOT_5A       402
#define IOT_4A       362
#define IOT_3A       322
#define IOT_2A       282

#define MAX_I_LIMIT   COMM_I_LIMIT_0_30
#define COMM_I_LIMIT_1_9    3800//190A
#define COMM_I_LIMIT_1_6    3200//160A
#define COMM_I_LIMIT_1_5    3000//150A
#define COMM_I_LIMIT_1_4    2800//140A
#define COMM_I_LIMIT_1_3    2600//130A
#define COMM_I_LIMIT_1_2    2400//120A
#define COMM_I_LIMIT_1_1    2200//110A
#define COMM_I_LIMIT_1_0    2000//100A
#define COMM_I_LIMIT_0_90   1800//90A
#define COMM_I_LIMIT_0_80   1600//80A
#define COMM_I_LIMIT_0_70   1400//70A
#define COMM_I_LIMIT_0_60   1200//60A
#define COMM_I_LIMIT_0_50   1000//50A
#define COMM_I_LIMIT_0_40   800 //40A
#define COMM_I_LIMIT_0_30   600 //30A
#define COMM_I_LIMIT_0_25   500 //25A
#define COMM_I_LIMIT_0_20   400 //20A
#define COMM_I_LIMIT_0_10   200 //10A

/*******定时时间**********/
#define    Time_0_3ms       1
#define    Time_0_9ms       3
#define    Time_2ms        7
#define    Time_10ms       33
#define    Time_20ms       66
#define    Time_25ms       83
#define    Time_30ms       99
#define    Time_36ms       120
#define    Time_50ms       167
#define    Time_90ms       300
#define    Time_100ms      333
#define    Time_110ms      367
#define    Time_200ms      666
#define    Time_210ms      700
#define    Time_400ms      1333
#define    Time_500ms       1667
#define    Time_800ms       2666
#define    Time_1s          3333
#define    Time_1_5s        5000
#define    Time_2s          6666
#define    Time_3s          9999
#define    Time_4s          13332
#define    Time_6s          19998
#define    Time_8s          26664
#define    Time_10s         33333
#define    Time_11s         36666
#define    Time_20s         66666
#define    Time_30s         99999
#define    Time_40s         133332
#define    Time_48s         159996
#define    Time_60s         200000
#define    Time_61s         203333
#define    Time_70s         233333
#define    Time_80s         266667
#define    Time_300s        999990
#define    Moudle_START_TIME		Time_3s		//3s启动延时

#define   OV_DISPLAY_FILTER_UP				Time_50ms//4000//
#define   OV_DISPLAY_FILTER_DOWN			Time_10ms//100//
#define	  OV_FILTER_MAX_CNT					Time_1_5s 
#define	  OV_FILTER_MAX_CNT_open		Time_10s  


//输出电压
#define		Uo_MAX               	2900
#define		Uo_UP_SHUT     				2550
#define		Uo_UP_START				   	2450
#define		Uo_UP_ALARM           2400
#define		Uo_UP_OK							2150
#define		Uo_JUDGE							2200	
#define		Uo_DOWN_OK						2150
#define		Uo_DOWN_ALARM         2000	
#define		Uo_DOWN_START					1550
#define		Uo_DOWN_SHUT					1400 
#define		Uo_MIN								33 

//此处电压为输入电压的有效值，与有效值的关系为1.1111倍
#define		DC_UI_MAX               5500 
#define		DC_UI_UP_SHUT     		  4000
#define		DC_UI_UP_START				  3700
#define		DC_UI_UP_ALARM          3650   
#define		DC_UI_UP_OK						  3600
#define		DC_UI_JUDGE						  2400
#define		DC_UI_DOWN_OK						1800
#define		DC_UI_DOWN_ALARM        1750  	
#define		DC_UI_DOWN_START				1750
#define		DC_UI_DOWN_SHUT					1650
#define		DC_UI_MIN							  1500//15V  

//========================地址检测相关参数=======================

#define   A0_ERR   100
#define   A0_11     3950 	//理论值3808
#define   A0_10     3685  //理论值3563
#define   A0_9     	3432   //理论值3302
#define   A0_8      3170   //理论值3038
#define   A0_7      2905  //理论值2772
#define   A0_6      2633  //理论值3226
#define   A0_5      3040  //理论值2854
#define   A0_4      2668   //理论值2482
#define   A0_3      2296   //理论值2110
#define   A0_2      1924   //理论值1737
#define   A0_1      1561   //理论值1384
#define   A0_0      1211    //理论值1037

#define   A1_ERR    200 
//#define   A1_7      3490
#define   A1_6      3090
#define   A1_5      2655
#define   A1_4      2308
#define   A1_3      1861
#define   A1_2      1452
#define   A1_1      980

//==========================温度相关参数=========================
#define  TMP_SHUT           250
#define  TMP_BOOT              300
#define  TMP_FILTER_MAX_CNT        Time_2s 
#define  TMP_DISPLAY_FILTER_UP   Time_1s 
#define  TMP_DISPLAY_FILTER_DOWN         Time_10ms
#define	 TMP_FAN_STOP						43
#define	 TMP_FAN_START          40

#ifndef BIT_SET
#define BIT_SET(var,bitno) ((var) |= ((u16)1<<(bitno)))	//置位
#endif

#ifndef BIT_CLR
#define BIT_CLR(var,bitno) ((var) &= ~((u16)1<<(bitno)))	//清0
#endif

#ifndef BIT_TEST
#define BIT_TEST(var,bitno) ((var >> bitno)& 0x01)			//位状态检测
#endif


#ifndef CMP_MAX
#define CMP_MAX(var_a,var_b) 	if(var_a > var_b) var_a = var_b
#endif

#ifndef CMP_MIN
#define CMP_MIN(var_a,var_b) 	if(var_a < var_b) var_a = var_b
#endif


#ifndef ABS
#define ABS(a)				((a)<0?(-a):(a))
#endif
#ifndef ABS1
#define ABS1(a,b)				((a)<(b)?(b-a):(a-b))
#endif

#endif /* DEF_H_ */


