/*
 * Vars.h
 *
 *  Created on: Jan 21, 2019
 *      Author: 212315224
 */

#ifdef LOCATE_VARS
   #define LOCATION
#else
   #define LOCATION extern
#endif

#define TWO_PI           6.2831853

LOCATION float           Tsmp;
LOCATION float           Tadc;
LOCATION float           SwitchingFrequency;
LOCATION float           PeriodCounterFloat;
LOCATION unsigned int    PeriodCounter;
LOCATION unsigned int    AdcCounter;
LOCATION unsigned int    DeadTimeCounts;
LOCATION int             PeriodCounterMax;
LOCATION int             PeriodCounterMin;
LOCATION unsigned int    BackgroundCounter;
LOCATION unsigned int    BackgroundCounts;
LOCATION unsigned int    AdcIntCounter;
LOCATION volatile unsigned int*   pAdcBuffer;

LOCATION volatile unsigned int BackgroundRunFlag;

LOCATION union FaultCodeUnion  FaultCode;
LOCATION union DiscreteIOUnion DiscreteIO;

LOCATION unsigned int    FpgaVersion;
LOCATION unsigned int    SoftwareVersion;

LOCATION StateTypedef      State;
LOCATION StateTypedef      StatePrev;
LOCATION SubStatesTypedef  SubState;
LOCATION ModeTypedef       Mode;
LOCATION ModeTypedef       ModeCmd;
LOCATION SubStatesTypedef  SubMode;

LOCATION float             Iind1;
LOCATION float             Iind2;
LOCATION float             Iload;
LOCATION float             Vdc;
LOCATION float             VdcOut;
LOCATION float             BodeExc;

LOCATION float             InvVdc;
LOCATION float             IindRef;
LOCATION float             IindSum;
LOCATION float             IindDiff;

LOCATION struct VLPI_Data  VoltReg;
LOCATION struct VLPI_Data  CurrSumReg;
LOCATION struct VLPI_Data  CurrDiffReg;

LOCATION struct Dfilt1     Iload_HPF;
LOCATION struct Dfilt1     CL_ZP1;

LOCATION struct AdcGainStruct     Gain;

LOCATION float           FFv;
LOCATION float           VoutDiffCmd;
LOCATION float           VoutSumCmd;
LOCATION float           Duty1Cmd;
LOCATION float           Duty2Cmd;
LOCATION unsigned int    Duty1CMPACounts;
LOCATION unsigned int    Duty2CMPACounts;
LOCATION unsigned int    BodeSwitch;
LOCATION float           BodeX;
LOCATION float           BodeY;
LOCATION float           BodeDistMag;

LOCATION unsigned int    RunConverter;
LOCATION unsigned int    nStopStartPrev;
LOCATION unsigned int    MasterReset;

LOCATION int    Ind1OvercurrentCounter;
LOCATION int    Ind1OvercurrentCounts;
LOCATION int    Ind2OvercurrentCounter;
LOCATION int    Ind2OvercurrentCounts;
LOCATION int    VdcOvervoltageCounter;
LOCATION int    VdcOvervoltageCounts;
LOCATION int    VdcOutUndervoltageCounter;
LOCATION int    VdcOutUndervoltageCounts;

LOCATION unsigned int    GuiCmdCounter;
LOCATION unsigned int    GuiCmdTimeoutCount;

LOCATION unsigned int    LedCounter;
LOCATION unsigned int    LedCounts;
LOCATION unsigned int    RunTimeCounter;

LOCATION unsigned int    DacValue[3];

LOCATION float           GuiGainBitPerUnit[5][4];

LOCATION float Frequency;
LOCATION float Theta;
LOCATION float SinTheta;
LOCATION float CosTheta;

LOCATION float Var[4];

LOCATION unsigned long RunTime;

#if defined(LOCATE_VARS)

const struct Cal_struct Cal =
{
      200.0e6,         // float CoreClockFreq; 0
      100.5e6,         // float TimerClockFreq; 2
       20.0e3,         // float SwitchingFreq; 4
       40.0e3,         // float ControlFreq; 6
      100.0,           // float SlowTaskFreq 8
      500.0e-9,        // float DeadTime 10
      800.0,           // float VdcRef; 12
      214.2857142,     // float InductorCurrentRange 14
      840.3361345,     // float LoadCurrentRange 16
      512,             // float VdcInRange 18
      512,             // float VdcOutRange; 20
        4.0,           // float OversamplingRate 22
      290.0,           // float VdcHi; Above VdcHi full current FF is used 24
      280.0,           // float VdcLow; Bellow VdcLow no current feed forward is used 26
        0.95,          // float VoltFF; 28
        0.9,           // float CurrentFF; 30
       40.0,           // float HPF_fc; 32
        0.95,          // float CurrSumULGain; 34
        0.05,          // float CurrSumLLGain; 36
        1.0,           // float LedPeriod; 38
        5.0,           // float CanTimeout; 40

      200.0,           // float IndOvercurrent; 42
       30.0e-6,        // float IndOvercurrentTimeout; 3 cycles of 10us 44
      420.0,           // float DcOvervoltage; 44
       50.0e-6,        // float DcOvervoltageTimeout; 5 cycles of 10us 46
      180.0,           // float DcOutUndervoltage; 48
       50.0e-6,        // float DcOutUndervoltageTimeout; 5 cycles of 10us 50

   {  -10.0e3,         // struct ZP1Struct CurrLoopZP : float Z 52
       -4.0e3},        //                               float P 54

   {    1.2,           // struct VLPI_Def VoltRegDef: Kp 56
       18.0,           //                             Ki 58
      600.0,           //                             UL 60
     -600.0},          //                             LL 62

   {    0.3,           // struct PI_Def CurrSumRegDef: Kp 64
     6150.0},          //                              Ki 66

   {    0.015,         // struct VLPI_Def CurrDiffRegDef: Kp 68
       18.33,          //                                 Ki 70
       10.0,           //                                 UL 72
      -10.0},          //                                 LL 74

   //    0        1        2        3
   //  15:0     31:16    47:32    63:48
    {{1000.0,  1000.0,  1000.0,  1000.0},    // GuiGain [0][0..3]
     {1000.0,  1000.0,  1000.0,  1000.0},    // GuiGain [1][0..3]
     {1000.0,  1000.0,  1000.0,  1000.0},    // GuiGain [2][0..3]
     {1000.0,  1000.0,  1000.0,  1000.0},    // GuiGain [3][0..3]
     {32767.0, 1000.0,  1000.0,  1000.0}}    // GuiGain [4][0..3]
};

#pragma DATA_SECTION(AdcBuffer,"ramgs15");
volatile unsigned int  AdcBuffer[4 * 6]; // allocated for 4x oversampling of 6 variables

#else

extern const struct Cal_struct      Cal;
extern volatile unsigned int  AdcBuffer[];

#endif /* LOCATE_VARS */

