//#############################################################################
// $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/source/systemcontrol.c
//! \brief  This project is used to control two motor and pfc with one MCU
//!         Support for dualmtrpfc board with F28002x/F28003x/F280013x
//!

//
// include the related header files
//
#include "sys_main.h"

#include "systemcontrol.h"


//! \brief setup PFC, motor_1(Compressor) and motor_2(Fan). Time base = 1ms
void SYS_updateCtrlState(void)
{
    if(systemVars.flagEnableSynCtrl ==true)
    {
        if(systemVars.sysDelayTimeCnt == 0)
        {
            if(systemVars.flagEnableRun == true)
            {
                switch(systemVars.sysRunState)
                {
                   case SYS_RUN_IDLE:
                       systemVars.sysRunState = SYS_RUN_PFC;
                       systemVars.sysDelayTimeCnt =
                               systemVars.startFanDelayTimeSet;

                       pfcVars.VdcSet_V = systemVars.pfcVdcbusTarget_V;
                       pfcVars.flagEnablePFC = true;
                       break;
                   case SYS_RUN_PFC:
                       if(pfcVars.flagStatePFC == true)
                       {
                           systemVars.sysRunState = SYS_RUN_FAN;

                           systemVars.sysDelayTimeCnt =
                                   systemVars.startCompDelayTimeSet;

                           motorVars[MTR_2].speedRef_Hz = systemVars.speedFan_Hz;
                           motorVars[MTR_2].flagEnableRunAndIdentify = true;
                       }
                       break;
                   case SYS_RUN_FAN:
                       if(motorVars[MTR_2].motorState == MOTOR_CTRL_RUN)
                       {
                           systemVars.sysRunState = SYS_RUN_COMP;

                           motorVars[MTR_1].speedRef_Hz =
                                   systemVars.speedComp_Hz;

                           motorVars[MTR_1].flagEnableRunAndIdentify = true;
                       }
                       break;
                   case SYS_RUN_COMP:
                       if(motorVars[MTR_1].motorState == MOTOR_CTRL_RUN)
                       {
                           systemVars.sysRunState = SYS_RUN_ALL;
                       }
                       break;
                   case SYS_RUN_ALL:
                       pfcVars.VdcSet_V = systemVars.pfcVdcbusTarget_V;

                       motorVars[MTR_1].speedRef_Hz = systemVars.speedComp_Hz;
                       motorVars[MTR_2].speedRef_Hz = systemVars.speedFan_Hz;

                       motorVars[MTR_2].accelerationMax_Hzps =
                               systemVars.accelerationFan_Hzps;

                       motorVars[MTR_1].accelerationMax_Hzps =
                               systemVars.accelerationComp_Hzps;
                       break;
                }
            }
            else
            {
                switch(systemVars.sysRunState)
                {
                   case SYS_RUN_IDLE:
                       break;
                   case SYS_RUN_PFC:
                       systemVars.sysRunState = SYS_RUN_IDLE;
                       pfcVars.VdcSet_V = 0.0f;
                       motorVars[MTR_1].speedRef_Hz = 0.0f;
                       motorVars[MTR_2].speedRef_Hz = 0.0f;
                       break;
                   case SYS_RUN_FAN:
                       systemVars.sysRunState = SYS_RUN_PFC;
                       pfcVars.flagEnablePFC = false;
                       break;
                   case SYS_RUN_COMP:
                       systemVars.sysRunState = SYS_RUN_FAN;
                       motorVars[MTR_2].flagEnableRunAndIdentify = false;
                       break;
                   case SYS_RUN_ALL:
                       systemVars.sysDelayTimeCnt = systemVars.stopFanDelayTimeSet;
                       systemVars.sysRunState = SYS_RUN_COMP;
                       motorVars[MTR_1].flagEnableRunAndIdentify = false;
                       break;
                }
            }
        }
    }
    else
    {
        systemVars.sysRunState = SYS_RUN_IDLE;
    }

    if(systemVars.flagEnableUrgencyStop == true)
    {
        systemVars.flagEnableUrgencyStop = false;
        pfcVars.flagEnablePFC = false;
        motorVars[MTR_1].flagEnableRunAndIdentify = false;
        motorVars[MTR_2].flagEnableRunAndIdentify = false;
    }

    return;
}

//
//-- end of this file ----------------------------------------------------------
//
