//###########################################################################
//
// FILE:    threshold_find.c
//
// TITLE:
//
// NOTES:
//  1)
//###########################################################################
//
//  Ver  | dd mmm yyyy | Who        | Description of changes
// ======|=============|============|========================================
//  00     05 04 2015    HPCS
//
//  Texas Instruments, Inc
//  Copyright Texas Instruments 2017. All rights reserved.
//###########################################################################
#include "system_defines.h"
#include "cyclone_device.h"
#include "variables.h"
#include "function_definitions.h"
#include "software_interrupts.h"
#include "cyclone_defines.h"
#include "pmbus_common.h"
#include "pmbus_topology.h"
#include "pmbus_constants.h"

void system_voltage_treshold_finding_routine(void)
{
    dflash_variables.variables.Min_Vbus_dynamic = 100000000U;
    dflash_variables.variables.Min_Vbus_static = 100000000U;
    dflash_variables.variables.Max_Vbus_dynamic = 0U;
    dflash_variables.variables.Max_Vbus_static = 0U;

    dflash_variables.variables.Max_Vbus_EADC_DAC =
            FeCtrl1Regs.EADCDAC.bit.DAC_VALUE;
    if (dflash_variables.variables.Max_Vbus_static
            < AdcRegs.ADCAVGRESULT[0].all)
    {
        dflash_variables.variables.Max_Vbus_static =
                AdcRegs.ADCAVGRESULT[0].all;
        dflash_variables.variables.Min_Vbus_static =
                AdcRegs.ADCAVGRESULT[0].all; //Temporary value.
    }
    while (1)
    {
        count++;
        if (dflash_variables.variables.Min_Vbus_dynamic
                > AdcRegs.ADCRESULT[0].all)
        {
            dflash_variables.variables.Min_Vbus_dynamic =
                    AdcRegs.ADCRESULT[0].all;
        }
        if (dflash_variables.variables.Max_Vbus_dynamic
                < AdcRegs.ADCRESULT[0].all)
        {
            dflash_variables.variables.Max_Vbus_dynamic =
                    AdcRegs.ADCRESULT[0].all;
        }
        if (wait_for_steady_state(AdcRegs.ADCAVGRESULT[0].all))
        {
            if (dflash_variables.variables.Min_Vbus_static
                    > (AdcRegs.ADCAVGRESULT[0].all + 2U))
            {
                dflash_variables.variables.Min_Vbus_static =
                        AdcRegs.ADCAVGRESULT[0].all;
                dflash_variables.variables.Vbus_error_diff =
                        FeCtrl1Regs.EADCVALUE.bit.ERROR_VALUE;
                while (1)
                {
                    pmbus_handler();
                    count++;
                    if (FeCtrl1Regs.EADCVALUE.bit.ERROR_VALUE > 2)
                    {
                        FeCtrl1Regs.EADCDAC.bit.DAC_VALUE--;
                    }
                    if (FeCtrl1Regs.EADCVALUE.bit.ERROR_VALUE < -2)
                    {
                        FeCtrl1Regs.EADCDAC.bit.DAC_VALUE++;
                    }
                    if (FeCtrl1Regs.EADCVALUE.bit.ERROR_VALUE == 0)
                    {
                        dflash_variables.variables.Min_Vbus_EADC_DAC =
                                FeCtrl1Regs.EADCDAC.bit.DAC_VALUE;
                        dflash_variables.variables.Vbus_EADC_error_threshold =
                                0;
                        break;
                    }
                    if (count == 10000000)
                    {
                        break;
                    }
                }
                while (1)
                {
                    count++;
                    pmbus_handler();
                    if (FeCtrl1Regs.EADCVALUE.bit.ERROR_VALUE
                            > dflash_variables.variables.Vbus_EADC_error_threshold)
                    {
                        dflash_variables.variables.Vbus_EADC_error_threshold =
                                FeCtrl1Regs.EADCVALUE.bit.ERROR_VALUE;
                    }
                    if (FeCtrl1Regs.EADCVALUE.bit.ERROR_VALUE
                            < -(dflash_variables.variables.Vbus_error_diff))
                    {
                        break;
                    }
                    if (count == 10000000)
                    {
                        break;
                    }
                }
            }

        }
        if ((dflash_variables.variables.Max_Vbus_static
                > dflash_variables.variables.Min_Vbus_static)
                && (dflash_variables.variables.Max_Vbus_dynamic
                        > dflash_variables.variables.Max_Vbus_static)
                && (dflash_variables.variables.Min_Vbus_dynamic
                        < dflash_variables.variables.Min_Vbus_static)
                && (store != 1000U))
        {
            count = 10000000 - 1000;
            dflash_variables.variables.integration_calibrated = 0xBBBB;
            reduced_voltage_loop_DAC_value =
                    dflash_variables.variables.Min_Vbus_EADC_DAC;
            if (voltage_loop_DAC_value < reduced_voltage_loop_DAC_value)
            {
                reduced_voltage_loop_DAC_value = voltage_loop_DAC_value;
            }
            store = 1000U;
        }
        if (count == 10000000)
        {
            store = 3U;
            count = 0;
            dflash_variables.variables.Vbus_EADC_error_threshold += 2;
            FaultMuxRegs.DCOMPCTRL0.bit.THRESH =
                    dflash_variables.variables.Vbus_EADC_error_threshold;
            dflash_variables.variables.Vbus_error_diff_margin =
                    (dflash_variables.variables.Vbus_error_diff
                            - dflash_variables.variables.Vbus_EADC_error_threshold);
            if (dflash_variables.variables.Vbus_error_diff_margin < 10)
            {
                dflash_variables.variables.Vbus_error_diff_margin = 10;
            }
            FaultMuxRegs.DCOMPCTRL0.bit.INT_EN = 1U;
            break;

        }
    }
}

