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

#define CONFIG_RATED_DC_VOL (16)   /* 母线最大电压 V*/


#define SCHED_TIMER TIMER5
#define SCHED_TIMER_RCU RCU_TIMER5
#define SCHED_TIMER_IRQ TIMER5_IRQn
#define SCHED_TIMER_IRQHandler TIMER5_IRQHandler

/* mos 参数,电流采集需要 */
#define MOSDEV_DLY      550U
#define MOSDRV_DT_NS     400U
#define HW_DEAD_TIME_NS  (MOSDEV_DLY+MOSDRV_DT_NS)
#define HW_RISE_TIME_NS  500u
#define HW_NOISE_TIME_NS 500u

#define TDead NS_2_TCLK(HW_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) * TIM_CLOCK_MHz) / ADC_CLOCK_MHz + 1u)/* ADC ����ʱ�� */
#define TSampleMIN (TDead + TRise + TADC) //采样需要的总时间
#define TSampleBefore (TDead + TRise) //采样开始前需要等待的时间

#define ADC_REFERENCE_VOLTAGE  (3.3F)

/* 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 SHUNT_NUM 			THREE_SHUNTS_SAMPLE
#define U_PHASE_I_CHAN  	ADC_CHANNEL_1
#define V_PHASE_I_CHAN  	ADC_CHANNEL_12
#define W_PHASE_I_CHAN  	ADC_CHANNEL_13

#define U_PHASE_ADC_GROUP 	GPIOA
#define U_PHASE_ADC_PIN 	GPIO_PIN_1
#define U_PHASE_ADC_RCU 	RCU_GPIOA
#define U_PHASE_ADC_MODE 	GPIO_MODE_AIN

#define V_PHASE_ADC_GROUP 	GPIOC
#define V_PHASE_ADC_PIN 	GPIO_PIN_2
#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_3
#define W_PHASE_ADC_RCU 	RCU_GPIOC
#define W_PHASE_ADC_MODE 	GPIO_MODE_AIN

/* 温度，母线，油门等采集*/
#define MOTOR_TEMP_CHAN 	ADC_CHANNEL_11

#define VBUS_V_CHAN 		ADC_CHANNEL_0
#define VBUS_V_ADC_GROUP 	GPIOA
#define VBUS_V_ADC_PIN 		GPIO_PIN_0
#define VBUS_V_ADC_RCU 		RCU_GPIOA
#define VBUS_V_ADC_MODE 	GPIO_MODE_AIN

#define THROTTLE_CHAN   		ADC_CHANNEL_10
#define THROTTLE_V_ADC_GROUP 	GPIOC
#define THROTTLE_V_ADC_PIN 		GPIO_PIN_0
#define THROTTLE_V_ADC_RCU 		RCU_GPIOC
#define THROTTLE_V_ADC_MODE 	GPIO_MODE_AIN

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

/* ADC 的电压系数 */
#define ADC_TO_CURR_ceof (0.008f)
#define VBUS_VOL_CEOF (ADC_REFERENCE_VOLTAGE*16/4096.0f)
#define THROTTLE_VOL_CEOF (ADC_REFERENCE_VOLTAGE/4096.0f * 2.0f)

/* 是否用编码器 */
#define USE_ENCODER_ABI
#define ENCODER_CC_INVERT //编码器方向和电机反向

/* 编码器 */
#define ENC_A_GROUP GPIOA
#define ENC_A_PIN GPIO_PIN_6
#define ENC_A_RCU RCU_GPIOA
#define ENC_A_MODE GPIO_MODE_IN_FLOATING

#define ENC_B_GROUP GPIOA
#define ENC_B_PIN GPIO_PIN_7
#define ENC_B_RCU RCU_GPIOA
#define ENC_B_MODE GPIO_MODE_IN_FLOATING

#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 GPIOB     /*测量编码器的ABI的I信号，360度同步一次*/
#define ENC_I_PIN GPIO_PIN_0
#define ENC_I_RCU RCU_GPIOB
#define ENC_I_MODE GPIO_MODE_IPU
#define ENC_I_IRQ  EXTI0_IRQn
#define ENC_I_EXTI EXTI_0
#define ENC_I_EXIT_SRC_GROUP GPIO_PORT_SOURCE_GPIOB
#define ENC_I_EXIT_SRC_PIN GPIO_PIN_SOURCE_0

#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 TIMER3    /* 测量绝对编码器PWM输出的占空比，获取转子angle*/
#define ENC_PWM_TIMER_RCU RCU_TIMER3
#define ENC_PWM_TIMER_IRQ TIMER3_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 TIMER3_IRQHandler

#define ENC_MAX_interpolation 1.0f
#define ENC_FILTER_NR          4

/* 编码器参数      */
#define ENC_MAX_RES  4096
/*min. 490 Hz, max 603 Hz*/
#define ENC_PWM_Max_P (1.0f/490.0f)
#define ENC_PWM_Min_P (1.0f/603.0f)

#define ENC_PWM_MAX_RES 4119.0F
#define ENC_PWM_INIT_WIDTH 12.0F //PWM 起始宽度
#define ENC_PWM_ERROR_WIDTH 4.0f //PWM 指示错误的宽度
#define ENC_PWM_END_WIDTH   0.0F
#define ENC_PWM_Error_P (ENC_PWM_INIT_WIDTH/ENC_PWM_MAX_RES)
#define ENC_PWM_MIN_duty ((ENC_PWM_INIT_WIDTH + ENC_PWM_ERROR_WIDTH + ENC_PWM_END_WIDTH)/ENC_PWM_MAX_RES)

#define ENC_Duty_2_Pluse_Nr(duty) (duty * ENC_PWM_MAX_RES - (ENC_PWM_INIT_WIDTH + ENC_PWM_ERROR_WIDTH + ENC_PWM_END_WIDTH)) //通过占空比计算有几个脉冲
#define ENC_Pluse_Nr_2_angle(Nr) (360.0f/(float)ENC_MAX_RES * (Nr))

#define ENC_Duty(d, t) ((d)/(t))

#define MOTOR_3505

#endif /*_BOARD_GD32DEMO_H__ */

