#ifndef _BOARD_MC_V3_H__
#define _BOARD_MC_V3_H__

#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 TIM5
#define SCHED_TIMER_RCU RCU_TIMER5
#define SCHED_TIMER_IRQ TIM5_IRQn
#define SCHED_TIMER_IRQHandler TIM5_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 		TIM1
#define PWM_TIM_CLK     RCU_TIMER1
#define PWM_MODE 		TIM_OCMODE_PWM1
#define PWM_U_P_GROUP 	GPIOA
#define PWM_U_P_PIN 	GPIO_PIN_8
#define PWM_U_P_RCU 	RCU_GPIOA
#define PWM_U_P_MODE 	GPIO_MODE_AF_PP

#define PWM_U_N_GROUP 	GPIOB
#define PWM_U_N_PIN 	GPIO_PIN_13
#define PWM_U_N_RCU 	RCU_GPIOB
#define PWM_U_N_MODE 	GPIO_MODE_AF_PP

#define PWM_V_P_GROUP 	GPIOA
#define PWM_V_P_PIN 	GPIO_PIN_9
#define PWM_V_P_RCU 	RCU_GPIOA
#define PWM_V_P_MODE 	GPIO_MODE_AF_PP

#define PWM_V_N_GROUP 	GPIOB
#define PWM_V_N_PIN 	GPIO_PIN_14
#define PWM_V_N_RCU 	RCU_GPIOB
#define PWM_V_N_MODE 	GPIO_MODE_AF_PP

#define PWM_W_P_GROUP 	GPIOA
#define PWM_W_P_PIN 	GPIO_PIN_10
#define PWM_W_P_RCU 	RCU_GPIOA
#define PWM_W_P_MODE 	GPIO_MODE_AF_PP

#define PWM_W_N_GROUP 	GPIOB
#define PWM_W_N_PIN 	GPIO_PIN_15
#define PWM_W_N_RCU 	RCU_GPIOB
#define PWM_W_N_MODE 	GPIO_MODE_AF_PP

#define PWM_BRAKE_GROUP 	GPIOB
#define PWM_BRAKE_PIN 	GPIO_PIN_12
#define PWM_BRAKE_RCU 	RCU_GPIOB
#define PWM_BRAKE_MODE 	GPIO_MODE_IN_FLOATING


#define PWM_BRK_IRQ TIM1_BRK_IRQn
#define PWM_UP_IRQ  TIM1_UP_IRQn
#define PWM_UP_IRQHandler   TIM1_UP_IRQHandler
#define PWM_BRK_IRQHandler  TIM1_BRK_IRQHandler



#define HALL_SENSOR_CEOF 0.32F

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

#define V_PHASE_I_CHAN  ADC_CH_6   //adc12
#define W_PHASE_I_CHAN  ADC_CH_5   //adc2

#define V_PHASE_ADC_GROUP 	GPIOC
#define V_PHASE_ADC_PIN 	GPIO_PIN_4
#define V_PHASE_ADC_RCU 	RCU_GPIOC
#define V_PHASE_ADC_MODE 	GPIO_MODE_AIN

#define W_PHASE_ADC_GROUP 	GPIOC
#define W_PHASE_ADC_PIN 	GPIO_PIN_0
#define W_PHASE_ADC_RCU 	RCU_GPIOC
#define W_PHASE_ADC_MODE 	GPIO_MODE_AIN

#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_CH_8  //adc12
#define VBUS_V_ADC_GROUP 	GPIOC
#define VBUS_V_ADC_PIN 		GPIO_PIN_2
#define VBUS_V_ADC_RCU 		RCU_GPIOC
#define VBUS_V_ADC_MODE 	GPIO_MODE_AIN
#define VBUS_VOL_CEOF 		(ADC_REFERENCE_VOLTAGE*41/ADC_FULL_MAX)

#define ACC_V_CHAN 			ADC_CH_7    //adc12
#define ACC_V_ADC_GROUP 	GPIOC
#define ACC_V_ADC_PIN 		GPIO_PIN_1
#define ACC_V_ADC_RCU 		RCU_GPIOC
#define ACC_V_ADC_MODE 		GPIO_MODE_AIN
#define ACC_VOL_CEOF 		(ADC_REFERENCE_VOLTAGE*41/ADC_FULL_MAX)

#define VBUS_I_CHAN 		ADC_CH_1 //adc1
#define VBUS_I_ADC_GROUP 	GPIOA
#define VBUS_I_ADC_PIN 		GPIO_PIN_0
#define VBUS_I_ADC_RCU 		RCU_GPIOA
#define VBUS_I_ADC_MODE 	GPIO_MODE_AIN
#define VBUS_I_CEOF         (HALL_SENSOR_CEOF)
#define VBUS_I_POSITIVE     1

/* MOS 温度采集 */
#define MOS_TEMP_ADC_CHAN    ADC_CH_3  //adc2
#define MOS_TEMP_ADC_GROUP 	 GPIOB
#define MOS_TEMP_ADC_PIN 	 GPIO_PIN_1
#define MOS_TEMP_ADC_RCU 	 RCU_GPIOB
#define MOS_TEMP_ADC_MODE 	 GPIO_MODE_AIN
#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_CH_2 //adc2
#define MOTOR_TEMP_ADC_GROUP 	GPIOA
#define MOTOR_TEMP_ADC_PIN 	GPIO_PIN_5
#define MOTOR_TEMP_ADC_RCU 	RCU_GPIOA
#define MOTOR_TEMP_ADC_MODE 	GPIO_MODE_AIN
#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_CH_1 //adc2
#define THROTTLE_V_ADC_GROUP 	GPIOA
#define THROTTLE_V_ADC_PIN 		GPIO_PIN_4
#define THROTTLE_V_ADC_RCU 		RCU_GPIOA
#define THROTTLE_V_ADC_MODE 	GPIO_MODE_AIN
#define THROTTLE_VOL_CEOF 		(ADC_REFERENCE_VOLTAGE*(15.1f/10.0f)/ADC_FULL_MAX)

/* 第二路转把信号电压采集 */
#define THROTTLE2_CHAN           	ADC_CH_4 //adc2
#define THROTTLE2_V_ADC_GROUP 		GPIOA
#define THROTTLE2_V_ADC_PIN 		GPIO_PIN_7
#define THROTTLE2_V_ADC_RCU 		RCU_GPIOA
#define THROTTLE2_V_ADC_MODE 		GPIO_MODE_AIN

/* 转把供电5V电压采集 */
#define THROTTLE_5V_CHAN           ADC_CH_3 //adc1
#define THROTTLE_5V_ADC_GROUP 	GPIOA
#define THROTTLE_5V_ADC_PIN 		GPIO_PIN_6
#define THROTTLE_5V_ADC_RCU 		RCU_GPIOA
#define THROTTLE_5V_ADC_MODE 	GPIO_MODE_AIN

#if 0
/* 第二路供电5V电压采集 */
#define THROTTLE2_5V_CHAN           	ADC_CH_9
#define THROTTLE2_5V_ADC_GROUP 		GPIOB
#define THROTTLE2_5V_ADC_PIN 		GPIO_PIN_1
#define THROTTLE2_5V_ADC_RCU 		RCU_GPIOB
#define THROTTLE2_5V_ADC_MODE 		GPIO_MODE_AIN
#endif

/* UVW三相对地电压采集 */
#define U_VOL_ADC_CHAN     ADC_CH_12 //adc2
#define U_VOL_ADC_GROUP 	GPIOC
#define U_VOL_ADC_PIN 	GPIO_PIN_5
#define U_VOL_ADC_RCU 	RCU_GPIOC
#define U_VOL_ADC_MODE 	GPIO_MODE_AIN

#define V_VOL_ADC_CHAN     ADC_CH_2 //adc1
#define V_VOL_ADC_GROUP 	GPIOA
#define V_VOL_ADC_PIN 	GPIO_PIN_1
#define V_VOL_ADC_RCU 	RCU_GPIOA
#define V_VOL_ADC_MODE 	GPIO_MODE_AIN

#define W_VOL_ADC_CHAN     ADC_CH_11 //adc12
#define W_VOL_ADC_GROUP 	GPIOA
#define W_VOL_ADC_PIN 	GPIO_PIN_2
#define W_VOL_ADC_RCU 	RCU_GPIOA
#define W_VOL_ADC_MODE 	GPIO_MODE_AIN
#define UVW_VOL_CEOF (ADC_REFERENCE_VOLTAGE*(41.0f)/ADC_FULL_MAX)

/* 模拟5v电压采集 */
#define DC5V_ADC_CHAN     ADC_CH_4 //adc1
#define DC5V_ADC_GROUP 	GPIOA
#define DC5V_ADC_PIN 	GPIO_PIN_3
#define DC5V_ADC_RCU 	RCU_GPIOA
#define DC5V_ADC_MODE 	GPIO_MODE_AIN

/* 0v电压采集，主要是用来给上一次的采集放电 */
#define ZERO_ADC_CHAN     ADC_CH_9 //adc12
#define ZERO_ADC_GROUP 	GPIOC
#define ZERO_ADC_PIN 	GPIO_PIN_3
#define ZERO_ADC_RCU 	RCU_GPIOC
#define ZERO_ADC_MODE 	GPIO_MODE_AIN

/* 刹车手把输入 */
#define GPIO_BREAK_MODE GPIO_LOW_BRK_MODE
#define GPIO_BRAKE_IN_GROUP 	GPIOB
#define GPIO_BRAKE_IN_PIN 	GPIO_PIN_3
#define GPIO_BRAKE_IN_RCU 	RCU_GPIOB
#define GPIO_BRAKE_IN_MODE 	GPIO_MODE_IN_FLOATING
#define GPIO_BRAKE_IRQ  EXTI3_IRQn
#define GPIO_BRAKE_EXTI EXTI_LINE3
#define GPIO_BRAKE_EXIT_SRC_GROUP GPIOB_PORT_SOURCE
#define GPIO_BRAKE_EXIT_SRC_PIN GPIO_PIN_SOURCE3
#define GPIO_BRAKE_PIN_REMAP GPIO_RMP_SW_JTAG_SW_ENABLE

/* 锁电机线,  使用查询模式 */
#define GPIO_MLOCK_IN_GROUP GPIOC
#define GPIO_MLOCK_IN_PIN GPIO_PIN_13
#define GPIO_MLOCK_IN_RCU RCU_GPIOC
#define GPIO_MLOCK_IN_MODE 	GPIO_MODE_IN_FLOATING

/* 触发U相检测 */
#define GPIO_UDEC_OUT_GROUP 	GPIOB
#define GPIO_UDEC_OUT_PIN 	GPIO_PIN_7
#define GPIO_UDEC_OUT_RCU 	RCU_GPIOB
#define GPIO_UDEC_OUT_MODE 	GPIO_MODE_OUT_PP

/* 风扇 PWM */
#define GPIO_FAN_OUT_GROUP 	GPIOC
#define GPIO_FAN_OUT_PIN 	GPIO_PIN_8
#define GPIO_FAN_OUT_RCU 	RCU_GPIOC
#define GPIO_FAN_OUT_MODE 	GPIO_MODE_AF_PP
#define FAN_PWM_TIMER TIM8
#define FAN_PWM_CHAN  TIM_CH_3
#define FAN_TIMER_RCU  RCU_TIMER8

/* 风扇1检测 */
#define GPIO_FAN1_IN_GROUP 	GPIOC
#define GPIO_FAN1_IN_PIN 	GPIO_PIN_11
#define GPIO_FAN1_IN_RCU 	RCU_GPIOC
#define GPIO_FAN1_IN_MODE 	GPIO_MODE_IN_FLOATING
#define GPIO_FAN1_IRQ  EXTI15_10_IRQn
#define GPIO_FAN1_EXTI EXTI_LINE11
#define GPIO_FAN1_EXIT_SRC_GROUP GPIOC_PORT_SOURCE
#define GPIO_FAN1_EXIT_SRC_PIN GPIO_PIN_SOURCE11

/* LED 灯控制 */
#define GPIO_LED_OUT_GROUP 	GPIOC
#define GPIO_LED_OUT_PIN 	GPIO_PIN_14
#define GPIO_LED_OUT_RCU 	RCU_GPIOC
#define GPIO_LED_OUT_MODE 	GPIO_MODE_OUT_PP

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

/* CAN 定义 */
#define CAN_TX_GROUP GPIOB
#define CAN_TX_PIN   GPIO_PIN_9
#define CAN_RX_GROUP GPIOB
#define CAN_RX_PIN   GPIO_PIN_8
#define CAN_PIN_RCU  RCU_GPIOB
#define CAN_REMAP    GPIO_RMP2_CAN1
#define CAN_IRQ0     USB_LP_CAN1_RX0_IRQn
#define CAN_RX0_IRQHandler  USB_LP_CAN1_RX0_IRQHandler
#define CAN_RX1_IRQHandler  CAN1_RX1_IRQHandler

/* 是否用编码器 */
#define USE_ENCODER_ABI
#define ENCODER_TYPE ENCODER_MT

/* 编码器 */
#define ENC_A_GROUP GPIOB
#define ENC_A_PIN GPIO_PIN_4
#define ENC_A_RCU RCU_GPIOB
#define ENC_A_MODE GPIO_MODE_IN_FLOATING

#define ENC_B_GROUP GPIOB
#define ENC_B_PIN GPIO_PIN_5
#define ENC_B_RCU RCU_GPIOB
#define ENC_B_MODE GPIO_MODE_IN_FLOATING

#define TIMER2_PB4_PB5_REMAP GPIO_PART1_RMP_TIM3

#define ENC_PWM_GROUP GPIOB
#define ENC_PWM_PIN GPIO_PIN_6
#define ENC_PWM_RCU RCU_GPIOB
#define ENC_PWM_MODE GPIO_MODE_IN_FLOATING

#define ENC_I_GROUP GPIOA     /*测量编码器的ABI的I信号，360度同步一次*/
#define ENC_I_PIN GPIO_PIN_15
#define ENC_I_RCU RCU_GPIOA
#define ENC_I_MODE GPIO_MODE_IPU
#define ENC_I_IRQ  EXTI15_10_IRQn
#define ENC_I_EXTI EXTI_LINE15
#define ENC_I_EXIT_SRC_GROUP GPIOB_PORT_SOURCE
#define ENC_I_EXIT_SRC_PIN GPIO_PIN_SOURCE15

#define ENC_TIMER TIM3  /* 测量编码器的ABI信号的AB信号 */
#define ENC_TIMER_RCU RCU_TIMER3
#define ENC_TIMER_IRQ TIM3_IRQn
#define ENC_TIMER_IRQHandler TIM3_IRQHandler

#define ENC_PWM_TIMER TIM4    /* 测量绝对编码器PWM输出的占空比，获取转子angle*/
#define ENC_PWM_TIMER_RCU RCU_TIMER4
#define ENC_PWM_TIMER_IRQ TIM4_IRQn
#define ENC_PWM_TIMER_CHAN  TIM_CH_1
#define ENC_PWM_TIMER_IRQ_CH TIM_INT_CC1
#define ENC_PWM_TIMER_INT_FLG TIM_INT_CC1
#define ENC_PWM_IRQHandler TIM4_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_A1

//#define CONFIG_DQ_STEP_RESPONSE

#endif /*_BOARD_MC_V3_H__ */


