/*
 * Struct.h
 *
 *  Created on: Aug 18, 2022
 *      Author: 210055498
 */

#ifndef SOURCE_STRUCT_H_
#define SOURCE_STRUCT_H_

typedef enum ModeEnum {
   STANDBY_MODE = 0,
   RUN_MODE,
   FAULT_MODE
} ModeTypedef;

typedef enum StateEnum {
   STANDBY_STATE = 0,
   RUN_STATE,
   FAULT_STATE
} StateTypedef;

typedef enum SubStatesEnum {
   ENTRY = 0,
   IN,
   EXIT
} SubStatesTypedef;

struct AdcGainStruct {
   float      Iind;
   float      Vout;
   float      Iload;
   float      Vdc;
   float      BodeExc;
   float      VdcOut;
};

union FaultCodeUnion {
   struct {                                    // Bit
      unsigned long   OvercurrentIndA   :  1;  //   0
      unsigned long   OvercurrentIndB   :  1;  //   1
      unsigned long   OvercurrentIndC   :  1;  //   2
      unsigned long   DcOvervoltage     :  1;  //   3
      unsigned long   DcOutUndervoltage :  1;  //   4
      unsigned long   DesatA            :  1;  //   5
      unsigned long   DesatB            :  1;  //   6
      unsigned long   DesatC            :  1;  //   7
      unsigned long   CAN_Comm          :  1;  //   9
      unsigned long   reserved          : 22;  //   9 .. 31
   } Bit;
   struct {
      unsigned int    I0;
      unsigned int    I1;
   } Int;
   unsigned long      All;
};

union DiscreteIOUnion {
   struct {                              //  Bit
      unsigned int   Bit0          :  1; //    0
      unsigned int   Bit1          :  1; //    1
      unsigned int   Bit2          :  1; //    2
      unsigned int   Bit3          :  1; //    3
      unsigned int   reserved      : 12; //    4 .. 15
   } Bit;
   unsigned int      All;
};

struct VLPI_Def {
   float Kp;
   float Ki;
   float UL;
   float LL;
};

struct PI_Def {
   float Kp;
   float Ki;
};

struct Cal_struct {
   float             CoreClockFreq;            // core clock frequency in Hz
   float             TimerClockFreq;           // timer clock frequency in Hz
   float             SwitchingFreq;            // switching frequency in Hz
   float             ControlFreq;              // controller running frequency in Hz
   float             BackgroundFreq;           // background task frequency in Hz
   float             DeadTime;                 // PWM dead time
   float             VdcRef;                   // Dc link reference voltage
   float             InductorCurrentRange;
   float             VoltageOutputRange;
   float             LoadCurrentRange;
   float             VdcRange;
   float             OversamplingRate;         // Oversampling rate
   float             VdcHi;                    // Above VdcHi full current FF is used
   float             VdcLow;                   // Below VdcLow no current feed forward is used
   float             VoltFF;                   // Voltage loop FF gain
   float             CurrentFF;                // Current loop FF gain
   float             CurrSumULGain;            // UL of sum current loop is CurrSumULGain * Vdc
   float             CurrSumLLGain;            // LL of sum current loop is CurrSumLLGain * Vdc
   float             LedPeriod;                // Blinking LED period
   float             CanTimeout;               // Can Timeout shutdowns converter

   float             IndOvercurrent;           // Inductor overcurrent threshold
   float             IndOvercurrentTimeout;    // Inductor overcurrent timeout
   float             DcOvervoltage;            // Dc link overvoltage threshold
   float             DcOvervoltageTimeout;     // Dc link overvoltage timeout
   float             DcOutUndervoltage;        // Dc output undervoltage threshold
   float             DcOutUndervoltageTimeout; // Dc output undervoltage timeout

   struct PI_Def     CurrZRegDef;

   float             GuiGain[5][4];
};

#endif /* SOURCE_STRUCT_H_ */
