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

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

#include "systemcontrol.h"
#include "systemdisplay.h"

//! \brief display the faults with blinking LED, time base = 5ms
void SYS_displayFault(void)
{
    // DPM - toggle status LED
    if(systemVars.faultSysView.all == 0)
    {
        if((motorVars[MTR_1].motorState == MOTOR_CTRL_RUN) ||
                (motorVars[MTR_2].motorState == MOTOR_CTRL_RUN))
        {
            // fast blink 0.2s/0.2s
            systemVars.blinkTimesSetLEDS = LED_SYS_NORM_RUN_TIMES;
            systemVars.waitTimeSetLEDS = LED_RUN_WAIT_TIME;             // 0.2s
            systemVars.delayTimeSetLEDS = LED_RUN_DELAY_TIME;           // 0.2s
        }
        else        // Both motors are stopping
        {
            // slow blink 0.5s/0.5s
            systemVars.blinkTimesSetLEDS = LED_SYS_NORM_STOP_TIMES;
            systemVars.waitTimeSetLEDS = LED_STOP_WAIT_TIME;            // 0.5s
            systemVars.delayTimeSetLEDS = LED_STOP_DELAY_TIME;          // 0.5s
        }
    }
    else
    {
        if(systemVars.faultSysView.bit.pfcOverCurrent == 1)
        {
            systemVars.blinkTimesSetLEDS = LED_PFC_OC_FAULT_TIMES;
        }
        else if(systemVars.faultSysView.bit.motor1OverCurrent == 1)
        {
            systemVars.blinkTimesSetLEDS = LED_MTR1_OC_FAULT_TIMES;
        }
        else if(systemVars.faultSysView.bit.motor1RunFailed == 1)
        {
            systemVars.blinkTimesSetLEDS = LED_MTR1_RUN_FAULT_TIMES;
        }
        else if(systemVars.faultSysView.bit.motor2OverCurrent == 1)
        {
            systemVars.blinkTimesSetLEDS = LED_MTR2_OC_FAULT_TIMES;
        }
        else if(systemVars.faultSysView.bit.motor2RunFailed == 1)
        {
            systemVars.blinkTimesSetLEDS = LED_MTR2_RUN_FAULT_TIMES;
        }
        else if(systemVars.faultSysView.bit.sysVoltage == 1)
        {
            systemVars.blinkTimesSetLEDS = LED_SYS_VOLTAGE_FAULT_TIMES;
        }
        else if(systemVars.faultSysView.bit.moduleOverTemp == 1)
        {
            systemVars.blinkTimesSetLEDS = LED_MODULE_OT_FAULT_TIMES;
        }
        else if(systemVars.faultSysView.bit.sysComLoss == 1)
        {
            systemVars.blinkTimesSetLEDS = LED_SYS_COMM_LOSS_TIMES;
        }
        else if(systemVars.faultSysView.bit.SysPipeOverTemp == 1)
        {
            systemVars.blinkTimesSetLEDS = LED_SYS_PIPEHEAT_OT_TIMES;
        }
        else if(systemVars.faultSysView.bit.SysCompOverTemp == 1)
        {
            systemVars.blinkTimesSetLEDS = LED_SYS_COMPRESSOR_OT_TIMES;
        }

        systemVars.waitTimeSetLEDS   = LED_FAULT_WAIT_TIME;      // 0.4s
        systemVars.delayTimeSetLEDS  = LED_FAULT_DELAY_TIME;     // 2.0s
    }

    if(systemVars.blinkTimesCntLEDS >= systemVars.blinkTimesSetLEDS)
    {
        if(systemVars.waitTimeCntLEDS >= systemVars.delayTimeSetLEDS)
        {
            systemVars.blinkStatusLEDS = LED_BLINKING_LIGHT;
            systemVars.blinkTimesCntLEDS = 0;
            systemVars.waitTimeCntLEDS = 0;
        }
        else
        {
            systemVars.waitTimeCntLEDS++;
            systemVars.blinkStatusLEDS = LED_BLINKING_DARK;
        }
    }
    else if(systemVars.waitTimeCntLEDS >= systemVars.waitTimeSetLEDS)
    {
        systemVars.waitTimeCntLEDS = 0;

        if(systemVars.blinkStatusLEDS == LED_BLINKING_DARK)
        {
            systemVars.blinkStatusLEDS = LED_BLINKING_LIGHT;
        }
        else
        {
            systemVars.blinkTimesCntLEDS++;
            systemVars.blinkStatusLEDS = LED_BLINKING_DARK;
        }
    }
    else
    {
        systemVars.waitTimeCntLEDS++;
    }

#if !defined(DAC128S_ENABLE) && (defined(DMCPFC_REV3P1) || defined(DMCPFC_REV3P2))
    if(systemVars.blinkStatusLEDS == LED_BLINKING_DARK)     // Off
    {
        HAL_turnOffLED(HAL_GPIO_LEDS);
    }
    else                                    // On
    {
        HAL_turnOnLED(HAL_GPIO_LEDS);
    }
#elif defined(BSXL8323RH_REVB)
    if(systemVars.blinkStatusLEDS == LED_BLINKING_DARK)     // Off
    {
        HAL_turnOffLED(HAL_GPIO_LEDS);
    }
    else                                    // On
    {
        HAL_turnOnLED(HAL_GPIO_LEDS);
    }
#else   // DAC128S_ENABLE & DMCPFC_REV3P1 & DMCPFC_REV3P2
    // No Actions
#endif  // DAC128S_ENABLE & DMCPFC_REV3P1 & DMCPFC_REV3P2

    // Clear Fault for display
    if(systemVars.faultViewDelayTimeCnt > 0)
    {
        systemVars.faultViewDelayTimeCnt--;
    }
    else if(systemVars.faultSysUse.all == 0)
    {
        systemVars.faultSysView.all = 0;
    }

    // Clear Fault for communication
    if(systemVars.faultComDelayTimeCnt > 0)
    {
        systemVars.faultComDelayTimeCnt--;
    }
    else if(systemVars.faultSysUse.all == 0)
    {
        systemVars.faultSysCom.all = 0;
    }

    return;
}

//! \brief processing PFC and motor fault, time base = 1ms
void SYS_processFault(void)
{
    if(((motorVars[MTR_1].faultMtrUse.all & M_HARDWARE_BITS) != 0) ||
       ((motorVars[MTR_2].faultMtrUse.all & M_HARDWARE_BITS) != 0))
    {
        systemVars.faultSysUse.bit.hardware = 1;
    }
    else
    {
        systemVars.faultSysUse.bit.hardware = 0;
    }

    if((motorVars[MTR_1].faultMtrUse.all & M_OVERCURRENT_BITS) != 0)
    {
        systemVars.faultSysUse.bit.motor1OverCurrent = 1;
    }
    else
    {
        systemVars.faultSysUse.bit.motor1OverCurrent = 0;
    }

    if((motorVars[MTR_1].faultMtrUse.all & M_RUN_FAILED_BITS) != 0)
    {
        systemVars.faultSysUse.bit.motor1RunFailed = 1;
    }
    else
    {
        systemVars.faultSysUse.bit.motor1RunFailed = 0;
    }

    if((motorVars[MTR_2].faultMtrUse.all & M_OVERCURRENT_BITS) != 0)
    {
        systemVars.faultSysUse.bit.motor2OverCurrent = 1;
    }
    else
    {
        systemVars.faultSysUse.bit.motor2OverCurrent = 0;
    }

    if((motorVars[MTR_2].faultMtrUse.all & M_RUN_FAILED_BITS) != 0)
    {
        systemVars.faultSysUse.bit.motor2RunFailed = 1;
    }
    else
    {
        systemVars.faultSysUse.bit.motor2RunFailed = 0;
    }

    if((pfcVars.faultPFCUse.all & P_OVER_CURRENT_BITS) != 0)
    {
        systemVars.faultSysUse.bit.pfcOverCurrent = 1;
    }
    else
    {
        systemVars.faultSysUse.bit.pfcOverCurrent = 0;
    }

    if((pfcVars.faultPFCUse.all & P_OVER_VOLTAGE_BITS) != 0)
    {
        systemVars.faultSysUse.bit.sysVoltage = 1;
    }
    else
    {
        systemVars.faultSysUse.bit.sysVoltage = 0;
    }

    if(motorVars[MTR_1].faultMtrUse.bit.moduleOverTemp != 0)
    {
        systemVars.faultSysUse.bit.moduleOverTemp = 1;
    }
    else
    {
        systemVars.faultSysUse.bit.moduleOverTemp = 0;
    }

    systemVars.faultSysView.all |= systemVars.faultSysUse.all;
    systemVars.faultSysCom.all |= systemVars.faultSysUse.all;

    if(systemVars.faultSysUse.all != 0)
    {
        systemVars.faultViewDelayTimeCnt = FAULT_VIEW_WAIT_TIME;
        systemVars.faultComDelayTimeCnt = FAULT_COM_WAIT_TIME;
    }

    return;
}
//
//-- end of this file ----------------------------------------------------------
//
