#ifndef _BOARD_MC_V3_H__
#define _BOARD_MC_V3_H__
#if defined (GD32F30X_HD) || defined (GD32F30X_XD) || defined (GD32F30X_CL)
#include "gd32f30x.h"
#elif defined GD32E10x
#include "gd32e10x.h"
#endif

#define CONFIG_MOS_MAX_VOL 145.0F
#define CONFIG_MAX_DC_VOL 110.0F
#define CONFIG_RATED_DC_VOL (96.0f)   /* 母线最大电压 V*/
#define CONFIG_MIN_DC_VOL   (36.0f)

#define CONFIG_MAX_VBUS_CURRENT 120.0f
#define CONFIG_MAX_MOT_RPM      9000.0f
#define CONFIG_MAX_PHASE_CURR   500.0F
#define CONFIG_MAX_PHASE_VOL    (CONFIG_MOS_MAX_VOL - 20.0F)
#define CONFIG_MAX_TORQUE       CONFIG_MAX_PHASE_CURR
#define CONFIG_MAX_LOCK_TORQUE  20

//#define CONFIG_BEEP 
#define CONFIG_STALL_MAX_CURRENT 100.0f //最大堵转相电流电流
#define CONFIG_STALL_MAX_TIME    3000   //ms, 超过最大堵转电流持续时间，判断堵转
#define CONFIG_UNDER_VOL_RPM     1000
#define CONFIG_UNDER_VOL_PHASE_CURR 100.0F
#define CONFIG_UNDER_VOL_DC_CURR 15.0F

#define CONFIG_CURR_LP_WC (600.0F)

#define CONFIG_CURR_LP_CEOF (CONFIG_CURR_LP_WC*2*3.14F/(float)FOC_PWM_FS)

#define CONFIG_96V_MODE_VOL (60.0F)

#define CONFIG_SMO_OBSERVER 1
#define CONFIG_SPEED_LADRC  1
//#define CONFIG_FORCE_96V_MODE 1

#define SCHED_TIMER TMR5
#define SCHED_TIMER_RCU CRM_TMR5_PERIPH_CLOCK
#define SCHED_TIMER_IRQ TMR5_GLOBAL_IRQn
#define SCHED_TIMER_IRQHandler TMR5_GLOBAL_IRQHandler

#define PWM_DEAD_TIME_NS 400u
#define HW_DEAD_TIME_NS  200u
#define HW_RISE_TIME_NS  500u
#define HW_NOISE_TIME_NS 300u

#define TDead NS_2_TCLK(HW_DEAD_TIME_NS + PWM_DEAD_TIME_NS)/* ����ʱ�� */ 
#define TRise NS_2_TCLK(HW_RISE_TIME_NS)/* MOS ����ʱ��*/
#define TNoise NS_2_TCLK(HW_NOISE_TIME_NS)/* MOS��������Ŀ�������ʱ�� */
#define TADC  ((uint16_t)((ADC_TRIG_CONV_LATENCY_CYCLES + ADC_SAMPLING_CYCLES) *2 * TIM_CLOCK_MHz) / ADC_CLOCK_MHz + 1u)/* ADC ����ʱ�� */
#define TSampleMIN (TDead + TRise + TADC) //采样需要的总时间
#define TSampleBefore (TDead + TRise) //采样开始前需要等待的时间

#define ADC_REFERENCE_VOLTAGE  (3.3F)
#define ADC_FULL_MAX          (4095.0F)

/* MOS驱动 */
#define MOS_PWM_TIMER 		TMR1
#define PWM_MODE 		TMR_OUTPUT_CONTROL_PWM_MODE_A
#define PWM_CRM_CLK     CRM_TMR1_PERIPH_CLOCK
#define PWM_U_P_GROUP 	GPIOA
#define PWM_U_P_PIN 	GPIO_PINS_8
#define PWM_U_P_RCU 	CRM_GPIOA_PERIPH_CLOCK
#define PWM_U_P_MODE 	GPIO_MODE_MUX

#define PWM_U_N_GROUP 	GPIOB
#define PWM_U_N_PIN 	GPIO_PINS_13
#define PWM_U_N_RCU 	CRM_GPIOB_PERIPH_CLOCK
#define PWM_U_N_MODE 	GPIO_MODE_MUX

#define PWM_V_P_GROUP 	GPIOA
#define PWM_V_P_PIN 	GPIO_PINS_9
#define PWM_V_P_RCU 	CRM_GPIOA_PERIPH_CLOCK
#define PWM_V_P_MODE 	GPIO_MODE_MUX

#define PWM_V_N_GROUP 	GPIOB
#define PWM_V_N_PIN 	GPIO_PINS_14
#define PWM_V_N_RCU 	CRM_GPIOB_PERIPH_CLOCK
#define PWM_V_N_MODE 	GPIO_MODE_MUX

#define PWM_W_P_GROUP 	GPIOA
#define PWM_W_P_PIN 	GPIO_PINS_10
#define PWM_W_P_RCU 	CRM_GPIOA_PERIPH_CLOCK
#define PWM_W_P_MODE 	GPIO_MODE_MUX

#define PWM_W_N_GROUP 	GPIOB
#define PWM_W_N_PIN 	GPIO_PINS_15
#define PWM_W_N_RCU 	CRM_GPIOB_PERIPH_CLOCK
#define PWM_W_N_MODE 	GPIO_MODE_MUX

#define PWM_BRAKE_GROUP GPIOB
#define PWM_BRAKE_PIN 	GPIO_PINS_12
#define PWM_BRAKE_RCU 	CRM_GPIOB_PERIPH_CLOCK
#define PWM_BRAKE_MODE 	GPIO_MODE_INPUT

#define HALL_SENSOR_CEOF 0.32F

/* 高边电流传感器采样 */
#define HIGH_SIDE_CURRENT_SENSOR

#define V_PHASE_I_CHAN  			ADC_CHANNEL_14
#define W_PHASE_I_CHAN  			ADC_CHANNEL_10

#define V_PHASE_ADC_GROUP 			GPIOC
#define V_PHASE_ADC_PIN 			GPIO_PINS_4
#define V_PHASE_ADC_RCU 			CRM_GPIOC_PERIPH_CLOCK
#define V_PHASE_ADC_MODE 			GPIO_MODE_ANALOG

#define W_PHASE_ADC_GROUP 			GPIOC
#define W_PHASE_ADC_PIN 			GPIO_PINS_0
#define W_PHASE_ADC_RCU 			CRM_GPIOC_PERIPH_CLOCK
#define W_PHASE_ADC_MODE 			GPIO_MODE_ANALOG

#define ADC_TO_CURR_ceof1  			(HALL_SENSOR_CEOF)
#define ADC_TO_CURR_ceof2 			(HALL_SENSOR_CEOF)

#define CONFIG_PWM_UV_SWAP 		   	1

//#define CONFIG_HW_MUTISAMPLE ADC_OVERSAMPLING_RATIO_MUL8
//#define CONFIG_HW_MUTISAMPLE_SHIFT ADC_OVERSAMPLING_SHIFT_3B
//#define CONFIG_SW_MUTISAMPLE 1

/* 母线电压采集 */
#define VBUS_V_CHAN 				ADC_CHANNEL_12  //adc012
#define VBUS_V_ADC_GROUP 			GPIOC
#define VBUS_V_ADC_PIN 				GPIO_PINS_2
#define VBUS_V_ADC_RCU 				CRM_GPIOC_PERIPH_CLOCK
#define VBUS_V_ADC_MODE 			GPIO_MODE_ANALOG
#define VBUS_VOL_CEOF 				(ADC_REFERENCE_VOLTAGE*41/ADC_FULL_MAX)

#define ACC_V_CHAN 					ADC_CHANNEL_11    //adc012
#define ACC_V_ADC_GROUP 			GPIOC
#define ACC_V_ADC_PIN 				GPIO_PINS_1
#define ACC_V_ADC_RCU 				CRM_GPIOC_PERIPH_CLOCK
#define ACC_V_ADC_MODE 				GPIO_MODE_ANALOG
#define ACC_VOL_CEOF 				(ADC_REFERENCE_VOLTAGE*41/ADC_FULL_MAX)

#define VBUS_I_CHAN 				ADC_CHANNEL_0 //adc012
#define VBUS_I_ADC_GROUP 			GPIOA
#define VBUS_I_ADC_PIN 				GPIO_PINS_0
#define VBUS_I_ADC_RCU 				CRM_GPIOA_PERIPH_CLOCK
#define VBUS_I_ADC_MODE 			GPIO_MODE_ANALOG
#define VBUS_I_CEOF         		(HALL_SENSOR_CEOF)
#define VBUS_I_POSITIVE     		1

/* MOS 温度采集 */
#define MOS_TEMP_ADC_CHAN    		ADC_CHANNEL_8
#define MOS_TEMP_ADC_GROUP 	 		GPIOB
#define MOS_TEMP_ADC_PIN 	 		GPIO_PINS_0
#define MOS_TEMP_ADC_RCU 	 		CRM_GPIOB_PERIPH_CLOCK
#define MOS_TEMP_ADC_MODE 	 		GPIO_MODE_ANALOG
#define MOS_TEMP_R(adc) 			((adc)/ADC_FULL_MAX / ((1.0f - (adc)/ADC_FULL_MAX)/(10.0f*1000.0f)))

/* 电机温度采集 */
#define MOTOR_TEMP_ADC_CHAN     	ADC_CHANNEL_5
#define MOTOR_TEMP_ADC_GROUP 		GPIOA
#define MOTOR_TEMP_ADC_PIN 			GPIO_PINS_5
#define MOTOR_TEMP_ADC_RCU 			CRM_GPIOA_PERIPH_CLOCK
#define MOTOR_TEMP_ADC_MODE 		GPIO_MODE_ANALOG
#define MOTOR_TEMP_R(adc) 			((adc)/ADC_FULL_MAX / ((1.0f - (adc)/ADC_FULL_MAX)/2000.0f))

/* 是否有母线电流采集 */
//#define NO_SAMPLE_IDC //如果硬件没有采集母线电流，定义一下

/* 转把信号电压采集 */
#define THROTTLE_CHAN           	ADC_CHANNEL_4
#define THROTTLE_V_ADC_GROUP 		GPIOA
#define THROTTLE_V_ADC_PIN 			GPIO_PINS_4
#define THROTTLE_V_ADC_RCU 			CRM_GPIOA_PERIPH_CLOCK
#define THROTTLE_V_ADC_MODE 		GPIO_MODE_ANALOG
#define THROTTLE_VOL_CEOF 			(ADC_REFERENCE_VOLTAGE*(15.1f/10.0f)/ADC_FULL_MAX)

/* 第二路转把信号电压采集 */
#define THROTTLE2_CHAN           	ADC_CHANNEL_7
#define THROTTLE2_V_ADC_GROUP 		GPIOA
#define THROTTLE2_V_ADC_PIN 		GPIO_PINS_7
#define THROTTLE2_V_ADC_RCU 		CRM_GPIOA_PERIPH_CLOCK
#define THROTTLE2_V_ADC_MODE 		GPIO_MODE_ANALOG

/* 转把供电5V电压采集 */
#define THROTTLE_5V_CHAN            ADC_CHANNEL_6
#define THROTTLE_5V_ADC_GROUP 		GPIOA
#define THROTTLE_5V_ADC_PIN 		GPIO_PINS_6
#define THROTTLE_5V_ADC_RCU 		CRM_GPIOA_PERIPH_CLOCK
#define THROTTLE_5V_ADC_MODE 		GPIO_MODE_ANALOG

/* 第二路供电5V电压采集 */
#define THROTTLE2_5V_CHAN           ADC_CHANNEL_9
#define THROTTLE2_5V_ADC_GROUP 		GPIOB
#define THROTTLE2_5V_ADC_PIN 		GPIO_PINS_1
#define THROTTLE2_5V_ADC_RCU 		CRM_GPIOB_PERIPH_CLOCK
#define THROTTLE2_5V_ADC_MODE 		GPIO_MODE_ANALOG


/* UVW三相对地电压采集 */
#define U_VOL_ADC_CHAN     			ADC_CHANNEL_15
#define U_VOL_ADC_GROUP 			GPIOC
#define U_VOL_ADC_PIN 				GPIO_PINS_5
#define U_VOL_ADC_RCU 				CRM_GPIOC_PERIPH_CLOCK
#define U_VOL_ADC_MODE 				GPIO_MODE_ANALOG

#define V_VOL_ADC_CHAN     			ADC_CHANNEL_1 //adc012
#define V_VOL_ADC_GROUP 			GPIOA
#define V_VOL_ADC_PIN 				GPIO_PINS_1
#define V_VOL_ADC_RCU 				CRM_GPIOA_PERIPH_CLOCK
#define V_VOL_ADC_MODE 				GPIO_MODE_ANALOG

#define W_VOL_ADC_CHAN     			ADC_CHANNEL_2 //adc012
#define W_VOL_ADC_GROUP 			GPIOA
#define W_VOL_ADC_PIN 				GPIO_PINS_2
#define W_VOL_ADC_RCU 				CRM_GPIOA_PERIPH_CLOCK
#define W_VOL_ADC_MODE 				GPIO_MODE_ANALOG
#define UVW_VOL_CEOF 				(ADC_REFERENCE_VOLTAGE*(41.0f)/ADC_FULL_MAX)

/* 模拟5v电压采集 */
#define DC5V_ADC_CHAN     			ADC_CHANNEL_3 //adc012
#define DC5V_ADC_GROUP 				GPIOA
#define DC5V_ADC_PIN 				GPIO_PINS_3
#define DC5V_ADC_RCU 				CRM_GPIOA_PERIPH_CLOCK
#define DC5V_ADC_MODE 				GPIO_MODE_ANALOG

/* 0v电压采集，主要是用来给上一次的采集放电 */
#define ZERO_ADC_CHAN    			ADC_CHANNEL_13 //adc012
#define ZERO_ADC_GROUP 				GPIOC
#define ZERO_ADC_PIN 				GPIO_PINS_3
#define ZERO_ADC_RCU 				CRM_GPIOC_PERIPH_CLOCK
#define ZERO_ADC_MODE 				GPIO_MODE_ANALOG

/* 刹车手把输入 */
#define GPIO_BREAK_MODE 			GPIO_LOW_BRK_MODE
#define GPIO_BRAKE_IN_GROUP 		GPIOB
#define GPIO_BRAKE_IN_PIN 			GPIO_PINS_3
#define GPIO_BRAKE_IN_RCU 			CRM_GPIOC_PERIPH_CLOCK
#define GPIO_BRAKE_IN_MODE 			GPIO_MODE_INPUT
#define GPIO_BRAKE_IRQ  			EXINT3_IRQn
#define GPIO_BRAKE_EXTI 			EXINT_LINE_3
#define GPIO_BRAKE_EXIT_SRC_GROUP	GPIO_PORT_SOURCE_GPIOB
#define GPIO_BRAKE_EXIT_SRC_PIN 	GPIO_PINS_SOURCE3
#define GPIO_BRAKE_PIN_REMAP 		SWJTAG_CONF_010

/* 锁电机线,  使用查询模式 */
#define GPIO_MLOCK_IN_GROUP 		GPIOC
#define GPIO_MLOCK_IN_PIN 			GPIO_PINS_13
#define GPIO_MLOCK_IN_RCU 			CRM_GPIOC_PERIPH_CLOCK
#define GPIO_MLOCK_IN_MODE 			GPIO_MODE_INPUT

/* 触发U相检测 */
#define GPIO_UDEC_OUT_GROUP 		GPIOB
#define GPIO_UDEC_OUT_PIN 			GPIO_PINS_7
#define GPIO_UDEC_OUT_RCU 			CRM_GPIOB_PERIPH_CLOCK
#define GPIO_UDEC_OUT_MODE 			GPIO_MODE_OUTPUT

/* 风扇 PWM */
#define GPIO_FAN_OUT_GROUP 			GPIOC
#define GPIO_FAN_OUT_PIN 			GPIO_PINS_8
#define GPIO_FAN_OUT_RCU 			CRM_GPIOC_PERIPH_CLOCK
#define GPIO_FAN_OUT_MODE 			GPIO_MODE_MUX
#define FAN_PWM_TIMER 				TMR8
#define FAN_PWM_CHAN  				TMR_SELECT_CHANNEL_3
#define FAN_TIMER_RCU  				CRM_TMR8_PERIPH_CLOCK

/* 风扇1检测 */
#define GPIO_FAN1_IN_GROUP 			GPIOC
#define GPIO_FAN1_IN_PIN 			GPIO_PINS_11
#define GPIO_FAN1_IN_RCU 			CRM_GPIOC_PERIPH_CLOCK
#define GPIO_FAN1_IN_MODE 			GPIO_MODE_INPUT
#define GPIO_FAN1_IRQ  				EXINT15_10_IRQn
#define GPIO_FAN1_EXTI 				EXINT_LINE_11
#define GPIO_FAN1_EXIT_SRC_GROUP 	GPIO_PORT_SOURCE_GPIOC
#define GPIO_FAN1_EXIT_SRC_PIN 		GPIO_PINS_SOURCE11

/* LED 灯控制 */
#define GPIO_LED_OUT_GROUP 			GPIOC
#define GPIO_LED_OUT_PIN 			GPIO_PINS_14
#define GPIO_LED_OUT_RCU 			CRM_GPIOC_PERIPH_CLOCK
#define GPIO_LED_OUT_MODE 			GPIO_MODE_OUTPUT

/* 刹车灯控制，能量回收的时候需要电量刹车灯 */
#define GPIO_BRAKE_LIGHT_OUT_GROUP 	GPIOD
#define GPIO_BRAKE_LIGHT_OUT_PIN 	GPIO_PINS_2
#define GPIO_BRAKE_LIGHT_OUT_RCU 	CRM_GPIOD_PERIPH_CLOCK
#define GPIO_BRAKE_LIGHT_OUT_MODE 	GPIO_MODE_OUTPUT

/* CAN 定义 */
#define CAN_TX_GROUP GPIOB
#define CAN_TX_PIN   GPIO_PINS_9
#define CAN_RX_GROUP GPIOB
#define CAN_RX_PIN   GPIO_PINS_8
#define CAN_PIN_RCU  CRM_GPIOB_PERIPH_CLOCK
#define CAN_REMAP    CAN_MUX_10
/* 是否用编码器 */
#define USE_ENCODER_ABI
#define ENCODER_TYPE ENCODER_MT

/* 编码器 */
#define ENC_A_GROUP 	GPIOB
#define ENC_A_PIN 		GPIO_PINS_4
#define ENC_A_RCU 		CRM_GPIOB_PERIPH_CLOCK
#define ENC_A_MODE 		GPIO_MODE_INPUT

#define ENC_B_GROUP 	GPIOB
#define ENC_B_PIN 		GPIO_PINS_5
#define ENC_B_RCU 		CRM_GPIOB_PERIPH_CLOCK
#define ENC_B_MODE 		GPIO_MODE_INPUT
#define TIMER2_PB4_PB5_REMAP TMR3_MUX_10

#define ENC_PWM_GROUP 	GPIOA
#define ENC_PWM_PIN 	GPIO_PINS_15
#define ENC_PWM_RCU 	CRM_GPIOA_PERIPH_CLOCK
#define ENC_PWM_MODE	GPIO_MODE_MUX
#define TIMER1_PA15_REMAP TMR2_MUX_01

#define ENC_I_GROUP 			GPIOB     /*测量编码器的ABI的I信号，360度同步一次*/
#define ENC_I_PIN 				GPIO_PINS_6
#define ENC_I_RCU 				CRM_GPIOB_PERIPH_CLOCK
#define ENC_I_MODE 				GPIO_MODE_INPUT
#define ENC_I_IRQ  				EXINT9_5_IRQn
#define ENC_I_EXTI 				EXINT_LINE_6
#define ENC_I_EXIT_SRC_GROUP 	GPIO_PORT_SOURCE_GPIOB
#define ENC_I_EXIT_SRC_PIN 		GPIO_PINS_SOURCE6

#define ENC_TIMER 				TMR3  /* 测量编码器的ABI信号的AB信号 */
#define ENC_TIMER_RCU 			CRM_TMR3_PERIPH_CLOCK
#define ENC_TIMER_IRQ 			TMR3_GLOBAL_IRQn
#define ENC_TIMER_IRQHandler 	TMR3_GLOBAL_IRQHandler

#define ENC_PWM_TIMER 			TMR2    /* 测量绝对编码器PWM输出的占空比，获取转子angle*/
#define ENC_PWM_TIMER_RCU 		CRM_TMR2_PERIPH_CLOCK
#define ENC_PWM_TIMER_IRQ 		TMR2_GLOBAL_IRQn
#define ENC_PWM_TIMER_CHAN  	TMR_SELECT_CHANNEL_1
#define ENC_PWM_TIMER_IRQ_CH 	TMR_C1_INT
#define ENC_PWM_TIMER_INT_FLG 	TMR_C1_FLAG
#define ENC_PWM_IRQHandler 		TMR2_GLOBAL_IRQHandler

#define ENC_MAX_interpolation 1.0F

#define ENC_FILTER_NR          15
#ifdef CONFIG_PWM_UV_SWAP
#define ENCODER_CC_INVERT 1
#endif
/* 编码器参数      */
#define ENC_MAX_RES  4096.0f
#define ENC_Duty_2_Pluse_Nr(duty) (duty * ENC_MAX_RES) //通过占空比计算有几个脉冲
#define ENC_Pluse_Nr_2_angle(Nr) (360.0f/(float)ENC_MAX_RES * (Nr))
#define ENC_PWM_Min_P 0.0f//(1.0f/(131.0f + 1.0f))
#define ENC_PWM_Max_P  1.0f

#if ENCODER_TYPE==ENCODER_MPS
#define ENC_Duty(d, t) ((1.0f/128.0f) * (130.0f * (d)/(t) - 1.0f))
#elif ENCODER_TYPE==ENCODER_MT
/*min. 994 hz*/
#define ENC_PWM_MAX_RES    4119.0F
#define ENC_PWM_INIT_WIDTH 16.0F //PWM 起始宽度
#define ENC_PWM_END_WIDTH   8.0F
//#define ENC_PWM_Min_P      (ENC_PWM_INIT_WIDTH/(ENC_PWM_MAX_RES + 1.0f))
//#define ENC_PWM_Max_P      ((ENC_PWM_MAX_RES-ENC_PWM_END_WIDTH)/(ENC_PWM_MAX_RES - 1.0f))
#define PWM_Duty(d, t) ((d)/(t))
#define ENC_Duty(d, t) ((PWM_Duty(d, t)*ENC_PWM_MAX_RES - ENC_PWM_INIT_WIDTH)/(ENC_PWM_MAX_RES - ENC_PWM_END_WIDTH - ENC_PWM_INIT_WIDTH))
#else
#error "Postion sensor ERROR"

#endif
#define DEBUG_PORT_UART2

#define CONFIG_MOT_TYPE MOTOR_BLUESHARK_ZD_100

//#define CONFIG_DQ_STEP_RESPONSE

#endif /*_BOARD_MC_V3_H__ */


