#ifndef _HAL_H__
#define _HAL_H__
#include "stm32f3xx_hal.h"

#define Start_Stop_Pin GPIO_PIN_13
#define Start_Stop_GPIO_Port GPIOC

#define PWM_UH_Pin GPIO_PIN_8
#define PWM_UH_GPIO_Port GPIOA
#define PWM_VH_Pin GPIO_PIN_9
#define PWM_VH_GPIO_Port GPIOA
#define PWM_WH_Pin GPIO_PIN_10
#define PWM_WH_GPIO_Port GPIOA


#define PWM_EN_U_Pin GPIO_PIN_10
#define PWM_EN_U_GPIO_Port GPIOC
#define PWM_EN_V_Pin GPIO_PIN_11
#define PWM_EN_V_GPIO_Port GPIOC
#define PWM_EN_W_Pin GPIO_PIN_12
#define PWM_EN_W_GPIO_Port GPIOC


#define CURR_AMPL_W_Pin GPIO_PIN_0
#define CURR_AMPL_W_GPIO_Port GPIOC
#define CURR_AMPL_V_Pin GPIO_PIN_1
#define CURR_AMPL_V_GPIO_Port GPIOC
#define TEMPERATURE_Pin GPIO_PIN_2
#define TEMPERATURE_GPIO_Port GPIOC
#define CURR_AMPL_U_Pin GPIO_PIN_0
#define CURR_AMPL_U_GPIO_Port GPIOA
#define BUS_VOLTAGE_Pin GPIO_PIN_1
#define BUS_VOLTAGE_GPIO_Port GPIOA
#define GAS_SENSOR_Pin GPIO_PIN_1
#define GAS_SENSOR_GPIO_Port GPIOB
#define M1_OCP_Pin GPIO_PIN_11
#define M1_OCP_GPIO_Port GPIOA

#define HALL_1_PIN GPIO_PIN_15
#define HALL_1_GROUP GPIOA
#define HALL_2_PIN GPIO_PIN_3
#define HALL_2_GROUP GPIOB
#define HALL_3_PIN GPIO_PIN_10
#define HALL_3_GROUP GPIOB

#define UART_TX_Pin GPIO_PIN_2
#define UART_TX_GPIO_Port GPIOA
#define UART_RX_Pin GPIO_PIN_3
#define UART_RX_GPIO_Port GPIOA

#define CURRENT_U_ADC_CHANNAL 1
#define CURRENT_V_ADC_CHANNAL 7
#define CURRENT_W_ADC_CHANNAL 6
#define TEMP_SENSOR_ADC_CHANNEL 8
#define VBUS_SENSOR_ADC_CHANNEL 2

#define SPEED_SENSOR_ADC_CHANNEL 12

#define ADC_REFERENCE_VOLTAGE  3.30f


#define READ_HALL1() (HAL_GPIO_ReadPin(HALL_1_GROUP, HALL_1_PIN) == GPIO_PIN_SET ?1:0)
#define READ_HALL2() (HAL_GPIO_ReadPin(HALL_2_GROUP, HALL_2_PIN) == GPIO_PIN_SET ?1:0)
#define READ_HALL3() (HAL_GPIO_ReadPin(HALL_3_GROUP, HALL_3_PIN) == GPIO_PIN_SET ?1:0)

#define SYSTEM_CLOCK (72000000L)
#define TIM_CLOCK (SYSTEM_CLOCK * 2) /*SystemClock_Config��TIM1��clk��sys PLL �������̶�2����PLLƵ��*/
#define TIM_CLOCK_MHz (144)
#define ADC_CLOCK (SYSTEM_CLOCK)
#define ADC_CLOCK_MHz (72)
#define NS_PER_TCLK (7) /* (1/144000000 * 1000000000) */
#define NS_2_TCLK(ns) ((ns/NS_PER_TCLK) + 1) //ns תΪpwmʹ�õ��Ǹ�TIM��clk count
#define FOC_PWM_FS (30 * 1000)
#define FOC_PWM_period (TIM_CLOCK/FOC_PWM_FS)
#define FOC_PWM_Half_Period (FOC_PWM_period/2)
#define MAX_VBUS (12.f) //12v

#define ADC_TRIG_CONV_LATENCY_CYCLES 3.5f
#define ADC_SAMPLING_CYCLES 7.5f


#define HW_DEAD_TIME_NS  800
#define HW_RISE_TIME_NS  50
#define HW_NOISE_TIME_NS 50
#define TDead NS_2_TCLK(HW_DEAD_TIME_NS/2)/* ����ʱ�� */ 
#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 START_RAMP_DURATION (100)//ms
#define SPEED_SAMPLE_INVAL (100) //ת�Ѳɼ��ļ��,ms
#define SPEED_RAMP_DURATION SPEED_SAMPLE_INVAL //�����ٶȵ�б��ʱ�䣬�ٶ�ƽ���������½�

#define MAX_VBUS_VOLTAGE 12.0f
#define MAX_CURRENT      50.0F
void Error_Handler(void);
void SystemClock_Config(void);
void HAL_GPIO_init(void);
void HAL_NVIC_Init(void);
void HAL_EXIT_Enable(void);

#endif /* _HAL_H__ */
