#ifndef _BOARD_MC_V1_H__
#define _BOARD_MC_V1_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 200.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 SCHED_TIMER TIMER5
#define SCHED_TIMER_RCU RCU_TIMER5
#define SCHED_TIMER_IRQ TIMER5_IRQn
#define SCHED_TIMER_IRQHandler TIMER5_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 TIMER0
#define PWM_MODE TIMER_OC_MODE_PWM0
#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

#ifdef GD32F30X_CL
#define PWM_BRK_IRQ TIMER0_BRK_TIMER8_IRQn
#define PWM_UP_IRQ  TIMER0_UP_TIMER9_IRQn
#define PWM_UP_IRQHandler   TIMER0_UP_TIMER9_IRQHandler
#define PWM_BRK_IRQHandler  TIMER0_BRK_TIMER8_IRQHandler

#else
#define PWM_BRK_IRQ TIMER0_BRK_IRQn
#define PWM_UP_IRQ  TIMER0_UP_IRQn
#define PWM_UP_IRQHandler   TIMER0_UP_IRQHandler
#define PWM_BRK_IRQHandler  TIMER0_BRK_IRQHandler
#endif


#define HALL_SENSOR_CEOF 0.32F

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

#define V_PHASE_I_CHAN  ADC_CHANNEL_5
#define W_PHASE_I_CHAN  ADC_CHANNEL_6

#define V_PHASE_ADC_GROUP 	GPIOA
#define V_PHASE_ADC_PIN 	GPIO_PIN_5
#define V_PHASE_ADC_RCU 	RCU_GPIOA
#define V_PHASE_ADC_MODE 	GPIO_MODE_AIN

#define W_PHASE_ADC_GROUP 	GPIOA
#define W_PHASE_ADC_PIN 	GPIO_PIN_6
#define W_PHASE_ADC_RCU 	RCU_GPIOA
#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_CHANNEL_3  //adc012
#define VBUS_V_ADC_GROUP 	GPIOA
#define VBUS_V_ADC_PIN 		GPIO_PIN_3
#define VBUS_V_ADC_RCU 		RCU_GPIOA
#define VBUS_V_ADC_MODE 	GPIO_MODE_AIN
#define VBUS_VOL_CEOF 		(ADC_REFERENCE_VOLTAGE*41/ADC_FULL_MAX)

#define ACC_V_CHAN 		ADC_CHANNEL_2    //adc012
#define ACC_V_ADC_GROUP 	GPIOA
#define ACC_V_ADC_PIN 		GPIO_PIN_2
#define ACC_V_ADC_RCU 		RCU_GPIOA
#define ACC_V_ADC_MODE 	GPIO_MODE_AIN
#define ACC_VOL_CEOF 		(ADC_REFERENCE_VOLTAGE*41/ADC_FULL_MAX)

#define VBUS_I_CHAN 		ADC_CHANNEL_4
#define VBUS_I_ADC_GROUP 	GPIOA
#define VBUS_I_ADC_PIN 		GPIO_PIN_4
#define VBUS_I_ADC_RCU 		RCU_GPIOA
#define VBUS_I_ADC_MODE 	GPIO_MODE_AIN
#define VBUS_I_CEOF         (HALL_SENSOR_CEOF)


/* MOS 温度采集 */
#define MOS_TEMP_ADC_CHAN    ADC_CHANNEL_14
#define MOS_TEMP_ADC_GROUP 	 GPIOC
#define MOS_TEMP_ADC_PIN 	 GPIO_PIN_4
#define MOS_TEMP_ADC_RCU 	 RCU_GPIOC
#define MOS_TEMP_ADC_MODE 	 GPIO_MODE_AIN

#define MOS_TEMP1_ADC_CHAN   ADC_CHANNEL_15
#define MOS_TEMP1_ADC_GROUP  GPIOC
#define MOS_TEMP1_ADC_PIN 	 GPIO_PIN_5
#define MOS_TEMP1_ADC_RCU 	 RCU_GPIOC
#define MOS_TEMP1_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_CHANNEL_0 //adc012
#define MOTOR_TEMP_ADC_GROUP 	GPIOA
#define MOTOR_TEMP_ADC_PIN 	GPIO_PIN_0
#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_CHANNEL_1 //转把信号 adc012
#define THROTTLE_V_ADC_GROUP 	GPIOA
#define THROTTLE_V_ADC_PIN 		GPIO_PIN_1
#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)

/* UVW三相对地电压采集 */
#define U_VOL_ADC_CHAN     ADC_CHANNEL_9
#define U_VOL_ADC_GROUP 	GPIOB
#define U_VOL_ADC_PIN 	GPIO_PIN_1
#define U_VOL_ADC_RCU 	RCU_GPIOB
#define U_VOL_ADC_MODE 	GPIO_MODE_AIN

#define V_VOL_ADC_CHAN     ADC_CHANNEL_8
#define V_VOL_ADC_GROUP 	GPIOB
#define V_VOL_ADC_PIN 	GPIO_PIN_0
#define V_VOL_ADC_RCU 	RCU_GPIOB
#define V_VOL_ADC_MODE 	GPIO_MODE_AIN

#define W_VOL_ADC_CHAN     ADC_CHANNEL_7
#define W_VOL_ADC_GROUP 	GPIOA
#define W_VOL_ADC_PIN 	GPIO_PIN_7
#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)

/* 刹车手把输入 */
#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_3
#define GPIO_BRAKE_EXIT_SRC_GROUP GPIO_PORT_SOURCE_GPIOB
#define GPIO_BRAKE_EXIT_SRC_PIN GPIO_PIN_SOURCE_3

/* 前刹 */
#define GPIO_BRAKE1_IN_GROUP 	GPIOD
#define GPIO_BRAKE1_IN_PIN 	GPIO_PIN_2
#define GPIO_BRAKE1_IN_RCU 	RCU_GPIOD
#define GPIO_BRAKE1_IN_MODE 	GPIO_MODE_IN_FLOATING
#define GPIO_BRAKE1_IRQ  EXTI2_IRQn
#define GPIO_BRAKE1_EXTI EXTI_2
#define GPIO_BRAKE1_EXIT_SRC_GROUP GPIO_PORT_SOURCE_GPIOD
#define GPIO_BRAKE1_EXIT_SRC_PIN GPIO_PIN_SOURCE_2
#define GPIO_BREAK_MODE GPIO_LOW_BRK_MODE      

/* 锁电机线,  使用查询模式 */
#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_9
#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 TIMER7
#define FAN_PWM_CHAN  TIMER_CH_2
#define FAN_TIMER_RCU  RCU_TIMER7

/* 风扇1检测 */
#define GPIO_FAN1_IN_GROUP 	GPIOC
#define GPIO_FAN1_IN_PIN 	GPIO_PIN_10
#define GPIO_FAN1_IN_RCU 	RCU_GPIOC
#define GPIO_FAN1_IN_MODE 	GPIO_MODE_IN_FLOATING
#define GPIO_FAN1_IRQ  EXTI10_15_IRQn
#define GPIO_FAN1_EXTI EXTI_10
#define GPIO_FAN1_EXIT_SRC_GROUP GPIO_PORT_SOURCE_GPIOC
#define GPIO_FAN1_EXIT_SRC_PIN GPIO_PIN_SOURCE_10

/* 风扇2检测 */
#define GPIO_FAN2_IN_GROUP 	GPIOC
#define GPIO_FAN2_IN_PIN 	GPIO_PIN_11
#define GPIO_FAN2_IN_RCU 	RCU_GPIOC
#define GPIO_FAN2_IN_MODE 	GPIO_MODE_IN_FLOATING
#define GPIO_FAN2_IRQ  EXTI10_15_IRQn
#define GPIO_FAN2_EXTI EXTI_11
#define GPIO_FAN2_EXIT_SRC_GROUP GPIO_PORT_SOURCE_GPIOC
#define GPIO_FAN2_EXIT_SRC_PIN GPIO_PIN_SOURCE_11

/* CAN 定义 */
#define CAN_TX_GROUP GPIOA
#define CAN_TX_PIN   GPIO_PIN_12
#define CAN_RX_GROUP GPIOA
#define CAN_RX_PIN   GPIO_PIN_11
#define CAN_PIN_RCU  RCU_GPIOA
#ifdef GD32F30X_CL
#define CAN_REMAP    GPIO_CAN0_PARTIAL_REMAP
#define CAN_IRQ0     CAN0_RX0_IRQn
#define CAN_RX0_IRQHandler  CAN0_RX0_IRQHandler
#else
#define CAN_REMAP    GPIO_CAN_PARTIAL_REMAP
#define CAN_IRQ0     USBD_LP_CAN0_RX0_IRQn
#define CAN_RX0_IRQHandler  USBD_LP_CAN0_RX0_IRQHandler
#endif

/* 是否用编码器 */
#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_TIMER2_PARTIAL_REMAP

#define ENC_PWM_GROUP GPIOA
#define ENC_PWM_PIN GPIO_PIN_15
#define ENC_PWM_RCU RCU_GPIOA
#define ENC_PWM_MODE GPIO_MODE_IN_FLOATING
#define TIMER1_PA15_REMAP GPIO_TIMER1_PARTIAL_REMAP0

#define ENC_I_GROUP GPIOB     /*测量编码器的ABI的I信号，360度同步一次*/
#define ENC_I_PIN GPIO_PIN_8
#define ENC_I_RCU RCU_GPIOB
#define ENC_I_MODE GPIO_MODE_IPU
#define ENC_I_IRQ  EXTI5_9_IRQn
#define ENC_I_EXTI EXTI_8
#define ENC_I_EXIT_SRC_GROUP GPIO_PORT_SOURCE_GPIOB
#define ENC_I_EXIT_SRC_PIN GPIO_PIN_SOURCE_8

#define ENC_TIMER TIMER2  /* 测量编码器的ABI信号的AB信号 */
#define ENC_TIMER_RCU RCU_TIMER2
#define ENC_TIMER_IRQ TIMER2_IRQn
#define ENC_TIMER_IRQHandler TIMER2_IRQHandler

#define ENC_PWM_TIMER TIMER1    /* 测量绝对编码器PWM输出的占空比，获取转子angle*/
#define ENC_PWM_TIMER_RCU RCU_TIMER1
#define ENC_PWM_TIMER_IRQ TIMER1_IRQn
#define ENC_PWM_TIMER_CHAN  TIMER_CH_0
#define ENC_PWM_TIMER_IRQ_CH TIMER_INT_CH0
#define ENC_PWM_TIMER_INT_FLG TIMER_INT_FLAG_CH0
#define ENC_PWM_IRQHandler TIMER1_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

#if (CONFIG_MOT_TYPE==MOTOR_BLUESHARK_A1)
#define CONFIG_FORCE_96V_MODE 1
#endif

//#define CONFIG_DQ_STEP_RESPONSE

#endif /*_BOARD_MC_V1_H__ */

