#ifndef __MYPROJECT_H_
#define __MYPROJECT_H_

#include <FU68xx_5.h>
#include <FU68xx_5_Type.h>

#include <Customer.h>
#include <Parameter.h>
#include <AddFunction.h>
#include <Protect.h>
#include <ProtectSet.h>
#include <VoltageCompensation.h>


// math.h包含算术运算的数学函数
#include <math.h>                       
#include <Definition.h>
#include <Parameter.h>

typedef enum
{
    mcReady     = 0,
    mcInit      = 1,
    mcCharge    = 2,
    mcTailWind  = 3,
    mcPosiCheck = 4,
    mcAlign     = 5,
    mcStart     = 6,
    mcRun       = 7,
    mcStop      = 8,
    mcFault     = 9,
    mcBrake     = 10
}MotStaType;

typedef union
{
    uint8 SetMode;                                     // 整个配置模式使能位
    struct
    {
        uint8 AlignSetFlag     :1;                     // 预定位配置标志位
    } SetFlag;
}MotStaM;


extern MotStaType mcState;
extern MotStaM    McStaSet;


void CMP0_Init(void);
void CMP3_Init(void);
void CMP3_Interrupt_Init(void);
void CMP5_Init(void);


void Timer2_Init(void);
void Timer3_Init(void);
void Timer4_Init(void);
void SPI_Init(void);

void WatchDogConfig(uint32 value, uint8 Status);


unsigned short CRC_Check(unsigned char start_sector , unsigned char offset_sector);



typedef struct
{
  int16   IuOffset;       //Iu的偏置电压
  int32   IuOffsetSum;    //Iu的偏置电压总和
  int16   IvOffset;       //Iv的偏置电压
  int32   IvOffsetSum;    //Iv的偏置电压总和
  int16   Iw_busOffset;   //Iw或Ibus的偏置电压
  int32   Iw_busOffsetSum;//Iw或Ibus的偏置电压总和
  int16   OffsetCount;    //偏置电压采集计数
  int8    OffsetFlag;     //偏置电压结束标志位
	uint8_t CalibFlag; // 偏置电压配置标志位
}CurrentOffset;

extern CurrentOffset xdata mcCurOffset;
extern void Get_Current_Offset(void);

extern void FOC_Init(void);
extern void Motor_Charge(void);
extern void Motor_Open(void);
extern void Motor_Align(void);



#if (FiledWeakenCompEnable==1)
typedef struct
{
    uint16    FieldWeakenActualUS_Q15;    
    uint16    FieldWeakenActualDcbus_Q15;
    int16     FieldWeakenIsTheta;
	  int16     mcIqref;                       
    int16     mcIdref;                        
		   
}FieldWeakeningTypeDef;

extern FieldWeakeningTypeDef xdata mcFieldWeaken;	
extern void GetFieldWeakenUsAndUdc_Q15(int16 Ud, int16 Uq , uint16 Dcbus);
extern void GetFieldWeakenIdRefAndIqRef_Q15(int16 Is);
extern void GetFieldWeakenIsTheta_Q15(void);
extern int16 mcFieldWeakenPI(int16 feedbackvalue, int16 referencevalue);
extern void  FiledWeakenControl(int16 Ud, int16 Uq , uint16 Dcbusk,int16 Is);
#endif

typedef enum
{
    BIG_ENDIAN    = 0,
    LITTLE_ENDIAN = ENDIAN
}eType_DMA_Endian;

extern void SetEndian_DMA(eType_DMA_Endian eEndian);

#endif