#include "main.h"

#define LED_GPIO_PIN        GPIO_PIN_13
#define LED_GPIO_PORT       CW_GPIOC
#define LEDON()             {GPIO_WritePin(LED_GPIO_PORT, LED_GPIO_PIN, GPIO_Pin_RESET);}
#define LEDOFF()            {GPIO_WritePin(LED_GPIO_PORT, LED_GPIO_PIN, GPIO_Pin_SET);}
#define LEDTOG()            {GPIO_TogglePin(LED_GPIO_PORT, LED_GPIO_PIN);}

#define BKIN_GPIO_PIN       GPIO_PIN_11
#define BKIN_GPIO_PORT      CW_GPIOA

#define DR_GPIO_PIN         GPIO_PIN_5
#define DR_GPIO_PORT        CW_GPIOB

#define EN_GPIO_PIN         GPIO_PIN_14
#define EN_GPIO_PORT        CW_GPIOC

#define VOUT_EN_GPIO_PIN    GPIO_PIN_6
#define VOUT_EN_GPIO_PORT   CW_GPIOF

#define LEDDIR_GPIO_PIN     GPIO_PIN_12
#define LEDDIR_GPIO_PORT    CW_GPIOA

#define LEDSTSTO_GPIO_PIN   GPIO_PIN_11
#define LEDSTSTO_GPIO_PORT  CW_GPIOB

#define PWM_AP_PORT         (CW_GPIOA)
#define PWM_AP_PIN          (GPIO_PIN_8)
#define PWM_AN_PORT         (CW_GPIOB)
#define PWM_AN_PIN          (GPIO_PIN_13)

#define PWM_BP_PORT         (CW_GPIOA)
#define PWM_BP_PIN          (GPIO_PIN_9)
#define PWM_BN_PORT         (CW_GPIOB)
#define PWM_BN_PIN          (GPIO_PIN_14)

#define PWM_CP_PORT         (CW_GPIOA)
#define PWM_CP_PIN          (GPIO_PIN_10)
#define PWM_CN_PORT         (CW_GPIOB)
#define PWM_CN_PIN          (GPIO_PIN_15)

#define PWM_AH_OFF GPIO_WritePin(PWM_AP_PORT, PWM_AP_PIN, GPIO_Pin_RESET)
#define PWM_BH_OFF GPIO_WritePin(PWM_BP_PORT, PWM_BP_PIN, GPIO_Pin_RESET)
#define PWM_CH_OFF GPIO_WritePin(PWM_CP_PORT, PWM_CP_PIN, GPIO_Pin_RESET)

#define PWM_AH_ON GPIO_WritePin(PWM_AP_PORT, PWM_AP_PIN, GPIO_Pin_SET)
#define PWM_BH_ON GPIO_WritePin(PWM_BP_PORT, PWM_BP_PIN, GPIO_Pin_SET)
#define PWM_CH_ON GPIO_WritePin(PWM_CP_PORT, PWM_CP_PIN, GPIO_Pin_SET)

#define PWM_AL_OFF GPIO_WritePin(PWM_AN_PORT, PWM_AN_PIN, GPIO_Pin_RESET)
#define PWM_BL_OFF GPIO_WritePin(PWM_BN_PORT, PWM_BN_PIN, GPIO_Pin_RESET)
#define PWM_CL_OFF GPIO_WritePin(PWM_CN_PORT, PWM_CN_PIN, GPIO_Pin_RESET)

#define PWM_AL_ON GPIO_WritePin(PWM_AN_PORT, PWM_AN_PIN, GPIO_Pin_SET)
#define PWM_BL_ON GPIO_WritePin(PWM_BN_PORT, PWM_BN_PIN, GPIO_Pin_SET)
#define PWM_CL_ON GPIO_WritePin(PWM_CN_PORT, PWM_CN_PIN, GPIO_Pin_SET)

/* 20 kHz PWM @ 64 MHz */
#define PWM_PERIOD 3199
#define OnepercentPWM ((PWM_PERIOD + 1) / 100)

#define STATESTARTCHECK 0
#define STATESTARTPID 1
#define STATERUNPID 2
#define STATESTOP 3
#define STATEERROR 5
#define STATEERROROVER 6

#define NMAXVD 2950
#define NMINVD 750

#define RS (float)0.005
#define AP 21
#define Ikvalue (float)1200 / (float)AP / RS
#define NumErr 2
#define RV1 1
#define RV2 20
#define Kvvalue (float)1.2 / RV1 * (RV1 + RV2)

extern unsigned int TimeCountCompuSpeed, TimeCountTemp, TimeCountkey;
extern unsigned int TimeCountRealSpd, TimeCountPID, TimeCountAvgSpd, TimeCountVI;

extern unsigned char MOTORSTATE;

extern const unsigned int PWM_EN_TAB[6];
extern const unsigned char STEP_TAB[2][6];

extern uint16_t SampleData[4];

extern unsigned char ErrorCode;
extern unsigned int Motor_Start_F;
extern unsigned int Cur_step;
extern unsigned char Dir;

extern unsigned int HALLcount, HALLcount1, HALLcountTemp;
extern unsigned int TargS1;
extern unsigned int RealS1;
extern unsigned int RealS;
extern unsigned int SetSpeed;
extern float KKN, KPWMN;

extern unsigned int OutPwmValue;

extern unsigned char ErrCleFlag;

extern unsigned int SpeedD;

extern unsigned int DIin;
extern unsigned int CanshuV;
extern unsigned int CanshuI;

extern unsigned int MPolePairs;
extern unsigned int MAXSPEED;
extern unsigned int CanshuTimeDelay;

extern unsigned int ISH;
extern unsigned int VSH;
extern unsigned int LSH;

extern unsigned int PWMMin;
extern unsigned int PWMMax;
extern unsigned int TImeUP;

extern unsigned int MINSPEED;
extern unsigned int CanshuSTT;
extern unsigned int SP, SI, SD;

extern unsigned int STEP_last;
extern unsigned int startflag, dirflag;
extern unsigned int LevelSet;
