//#############################################################################
// $Copyright:
// Copyright (C) 2017-2023 Texas Instruments Incorporated - http://www.ti.com/
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
//   Redistributions of source code must retain the above copyright
//   notice, this list of conditions and the following disclaimer.
//
//   Redistributions in binary form must reproduce the above copyright
//   notice, this list of conditions and the following disclaimer in the
//   documentation and/or other materials provided with the
//   distribution.
//
//   Neither the name of Texas Instruments Incorporated nor the names of
//   its contributors may be used to endorse or promote products derived
//   from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// $
//#############################################################################


//! \file   /solutions/tidm_02010_dmpfc/common/include/sys_main.h
//! \brief  header file to be included
//!

#ifndef SYS_MAIN_H
#define SYS_MAIN_H


//*****************************************************************************
//
// If building with a C++ compiler, make all of the definitions in this header
// have a C binding.
//
//*****************************************************************************
#ifdef __cplusplus
extern "C"
{
#endif   // extern "C"

//*****************************************************************************
//
//! \defgroup SYS_MAIN SYS_MAIN
//! @{
//
//*****************************************************************************

// modules
#ifdef __TMS320C28XX_CLA__
#include "libraries/math/include/CLAmath.h"
#else
#include "libraries/math/include/math.h"
#include <math.h>
#endif

#include "user.h"
#include "hal.h"

#include "motor_common.h"
#include "motor1_drive.h"
#include "motor2_drive.h"
#include "pfc_ctrl.h"

#if defined(SFRA_ENABLE)
#include "sfra_settings.h"
#endif  // SFRA_ENABLE

#include "datalogIF.h"


#if defined(DAC128S_ENABLE)
#if defined(DAC80504_SEL)
#include "dac80504.h"
#else   // DAC128S805
#include "dac128s085.h"
#endif  // DAC128S805
#endif  // DAC128S_ENABLE

//
//! \brief Enumeration for the system run state
//
typedef enum
{
    SYS_RUN_IDLE = 0,       //!<
    SYS_RUN_PFC  = 1,       //!<
    SYS_RUN_FAN  = 2,       //!<
    SYS_RUN_COMP = 3,       //!<
    SYS_RUN_ALL  = 4        //!<
} SYS_RUN_State_e;

//
//! \brief typedefs for the fault
//
typedef struct _FAULT_SYS_BITS_
{             // bits  description
    uint16_t hardware:1;            // 0  Hardware failed
    uint16_t reserve1:1;            // 1  Reserve
    uint16_t reserve2:1;            // 2  Reserve
    uint16_t reserve3:1;            // 3  Reserve

    uint16_t motor1OverCurrent:1;   // 4  Motor 1 over current
    uint16_t motor1RunFailed:1;     // 5  Motor 1 run failed
    uint16_t motor2OverCurrent:1;   // 6  Motor 2 over current
    uint16_t motor2RunFailed:1;     // 7  Motor 2 run failed

    uint16_t pfcOverCurrent:1;      // 8  PFC Over Current
    uint16_t sysVoltage:1;          // 9  System over/under voltage
    uint16_t moduleOverTemp:1;      // 10  module over temperature
    uint16_t reserve11:1;           // 11 Reserve

    uint16_t sysComLoss:1;          // 12 System communication loss
    uint16_t SysPipeOverTemp:1;     // 13 System heater pipe over temperature
    uint16_t SysCompOverTemp:1;     // 14 System compressor over temperature
    uint16_t reserve15:1;           // 15 Reserve
} FAULT_SYS_BITS;

typedef union _FAULT_SYS_t
{
    uint16_t        all;
    FAULT_SYS_BITS  bit;
}FAULT_SYS_t;

//------------------------------------------------------------------------
typedef struct _SYSTEM_Vars_t_
{
    uint32_t timerCnt_1min;

    float32_t pfcVdcbusStart_V;
    float32_t pfcVdcbusTarget_V;

    float32_t startSpeedComp_Hz;
    float32_t startSpeedFan_Hz;

    float32_t speedComp_Hz;
    float32_t speedFan_Hz;

    float32_t accelerationComp_Hzps;
    float32_t accelerationFan_Hzps;

    uint16_t sysDelayTimeCnt;

    uint16_t startFanDelayTimeSet;
    uint16_t startCompDelayTimeSet;
    uint16_t stopFanDelayTimeSet;

    uint16_t timerBase_1ms;
    uint16_t timerCnt_5ms;
    uint16_t timerCnt_1s;

    uint16_t waitTimeCntLEDS;       //!< LEDS blink time counter
    uint16_t waitTimeSetLEDS;       //!< LEDS blink wait time settings
    uint16_t delayTimeSetLEDS;      //!< LEDS blink cycle delay time settings
    uint16_t blinkStatusLEDS;       //!< LEDs blink status
    uint16_t blinkTimesSetLEDS;     //!< LEDS blinking times settings
    uint16_t blinkTimesCntLEDS;     //!< LEDS blinking times counter
    uint16_t faultViewDelayTimeCnt;
    uint16_t faultComDelayTimeCnt;

    uint16_t powerRelayWaitTime_ms;
    uint16_t powerRelayWaitTime_ms1;
    FAULT_SYS_t faultSysUse;
    FAULT_SYS_t faultSysView;
    FAULT_SYS_t faultSysCom;

    Board_Kit_e boardKit;
    EST_Type_e  estType_M1;
    EST_Type_e  estType_M2;
    CURRENTSEN_Type_e   currentSenseType_M1;
    CURRENTSEN_Type_e   currentSenseType_M2;
    SYS_RUN_State_e sysRunState;

    bool flagEnableSystem;
    bool flagEnableRun;
    bool flagEnableSynCtrl;
    bool flagEnableExtCmd;
    bool flagEnableUrgencyStop;
}SYSTEM_Vars_t;

extern volatile SYSTEM_Vars_t systemVars;

#if defined(EPWMDAC_MODE)
extern HAL_PWMDACData_t pwmDACData;
#endif  // EPWMDAC_MODE


#if defined(DAC128S_ENABLE)
#if defined(DAC80504_SEL)
extern DAC80504_Handle  dac128sHandle;
extern DAC80504_Obj     dac128s;
#else   // DAC128S805
extern DAC128S_Handle   dac128sHandle;
extern DAC128S_Obj      dac128s;
#endif  // DAC128S805
#endif  // DAC128S_ENABLE


//*****************************************************************************
//
// Close the Doxygen group.
//! @}
//
//*****************************************************************************

//*****************************************************************************
//
// Mark the end of the C bindings section for C++ compilers.
//
//*****************************************************************************
#ifdef __cplusplus
}
#endif // extern "C"

#endif // end of SYS_MAIN_H definition
