/**************************************************************************//**
  * \file      Analog_Signals.c
  * \brief     Analog signal processing
  * \attention
  *
  * This file is automatically generated by analog signals configuration tool.
  * Date         : 2024/3/31 13:14:40
  * Cfg Tool Ver : 1.1.0
  * Engineer     : ShiHao
  * (c) Heilongjiang TYW electronics co., LTD
  *
******************************************************************************/

/* Includes -----------------------------------------------------------------*/
#include "Analog_Signals.h"

/* Private typedef ----------------------------------------------------------*/
typedef struct
{
    uint8_t   u8Ch;
    uint8_t   u8RefType;
    uint8_t   u8CalMode;
    uint16_t  u16Reference;
    uint16_t  u16CalData;
    uint16_t  u16Resolution;
    ADC_Circuit_Calc_Func  pfProcFunc;
}ADC_Ch_Cfg_st_t;

typedef enum ASigConvStat
{
    ADC_STAT_IDLE = 0,
    ADC_STAT_INIT,
    ADC_STAT_WAIT,
    ADC_STAT_CONV,
    ADC_STAT_PROC,
    ADC_STAT_ERR,
}ADC_Stat_en_t;

typedef struct
{
    ADC_Stat_en_t  enStatus;
    uint8_t        u8CurrentCh;
    uint8_t        u8DebounceCnt;
    uint8_t        u8Timer;
}ADC_Ctrl_st_t;

typedef struct
{
    uint8_t  u8Valid;
    uint8_t  u8Rsvd;
    uint16_t u16Value;
}ADC_Data_st_t;

/* Private macro -------------------------------------------------------------*/
#define   ADC_DEFAULT_REF_VOLTAGE           (5000U)
#define   ADC_CONV_CH_NUMBER                (ADC_TOTAL_CH_NUMBER)

#define   ADC_SAMPLE_INTERVAL               (2U)
#define   ADC_MAX_SAMPLE_WAIT               (10U)
#define   ADC_PWR_UP_DEBOUNCE               (10U)
#define   ADC_CONV_WAIT                     (50000U)

#define   ADC_REF_TYPE_V_NONE               (0x00U)
#define   ADC_REF_TYPE_V_VREF               (0x01U)
#define   ADC_REF_TYPE_V_CH                 (0x02U)
#define   ADC_REF_TYPE_V_FIXED              (0x03U)
#define   ADC_REF_TYPE_I_FIXED              (0x13U)

/* Private variables --------------------------------------------------------*/
volatile uint16_t u16ADCRefVoltage;
ADC_Ctrl_st_t     stADCCtrl;
uint16_t          u16ADCSample[ADC_CONV_CH_NUMBER];
ADC_Data_st_t     stADCData[ADC_SIGNAL_CH_NUMBER];

const uint8_t u8ADCChList[ADC_CONV_CH_NUMBER] =
{
     2U,  3U,  7U,  5U, 
};

const ADC_Ch_Cfg_st_t stADCChCfg[ADC_SIGNAL_CH_NUMBER] =
{
    {  0U,   0U,   0U,     0U,   700U,     1U,  ADC_Voltage_Calc_Circuit102,},
    {  1U,   0U,   0U,     0U,     0U,     1U,  ADC_Voltage_Calc_Circuit102,},
    {  2U,   0U,   0U,     0U,     0U,     1U,  ADC_Voltage_Calc_Circuit101,},
    {  3U,   2U,   0U,     2U,    18U,     1U,      ADC_Res_Calc_Circuit101,},
};

const ADC_Res_List_st_t stADCResList[ADC_SIGNAL_CH_NUMBER] =
{
    {    3300000U,   1000000U,         0U,         0U,},
    {    3300000U,   1000000U,         0U,         0U,},
    {          0U,         0U,         0U,         0U,},
    {          0U,         0U,       2000U,       300U,},
};

/* Private function prototypes ----------------------------------------------*/
/* Private functions --------------------------------------------------------*/

void Analog_Signal_Conv_Init(void)
{
    uint8_t i;
    uint32_t u32Timer_Init = 0;

    while(RTE_ADC_Get_Conversion_Status())
    {
        u32Timer_Init++;
        if (u32Timer_Init < ADC_CONV_WAIT)
        {
            RTE_ADC_Stop_Conversion();
        }
        else
        {
            u32Timer_Init = 0;
            break;
        }
    }

    RTE_ADC_Init(0,2);
    RTE_ADC_Init(0,3);
    RTE_ADC_Init(0,5);
    RTE_ADC_Init(0,7);

    for (i = 0U; i < ADC_SIGNAL_CH_NUMBER; i++)
    {
        stADCData[i].u8Valid  = 0U;
        stADCData[i].u16Value = 0U;
    }

    stADCCtrl.enStatus        = ADC_STAT_INIT;
    stADCCtrl.u8CurrentCh     = 0U;
    stADCCtrl.u8DebounceCnt   = ADC_PWR_UP_DEBOUNCE / ADC_SAMPLE_INTERVAL;
    stADCCtrl.u8Timer         = 0U;
    u16ADCRefVoltage          = ADC_DEFAULT_REF_VOLTAGE;

}

void Analog_Signal_Conv_Stop(void)
{
    uint8_t i;

    RTE_ADC_DeInit();

    for (i = 0U; i < ADC_SIGNAL_CH_NUMBER; i++)
    {
        stADCData[i].u8Valid = 0U;
    }

    stADCCtrl.enStatus       = ADC_STAT_IDLE;
    stADCCtrl.u8DebounceCnt  = ADC_PWR_UP_DEBOUNCE / ADC_SAMPLE_INTERVAL;
    u16ADCRefVoltage = ADC_DEFAULT_REF_VOLTAGE;
}

void Analog_Signal_Conv_Service(void)
{
    uint8_t  u8Valid;
    uint16_t u16Voltage;
    uint16_t u16Reference;
    uint16_t u16Result;

    switch (stADCCtrl.enStatus)
    {
        case ADC_STAT_IDLE    : break;

        case ADC_STAT_INIT    : RTE_ADC_Start_Conversion();
                                stADCCtrl.u8Timer     = 0U;
                                stADCCtrl.u8CurrentCh = 0U;

                                if (stADCCtrl.u8DebounceCnt == 0U)
                                {
                                    stADCCtrl.enStatus = ADC_STAT_CONV;
                                }
                                else
                                {
                                    stADCCtrl.enStatus = ADC_STAT_WAIT;
                                }
                                break;

        case ADC_STAT_WAIT    : if (stADCCtrl.u8DebounceCnt)
                                {
                                    stADCCtrl.u8DebounceCnt--;
                                }

                                if (RTE_ADC_Get_Conversion_Status() == 0U)
                                {
                                    RTE_ADC_Start_Conversion();
                                    stADCCtrl.u8Timer = 0U;

                                    if (stADCCtrl.u8DebounceCnt == 0U)
                                    {
                                        stADCCtrl.enStatus = ADC_STAT_CONV;
                                    }
                                }
                                else
                                {
                                    stADCCtrl.u8Timer++;
                                    if (stADCCtrl.u8Timer >= ADC_MAX_SAMPLE_WAIT / ADC_SAMPLE_INTERVAL)
                                    {
                                        stADCCtrl.enStatus = ADC_STAT_ERR;
                                        RTE_ADC_Stop_Conversion();
                                    }
                                }
                                break;

        case ADC_STAT_CONV    : if (RTE_ADC_Get_Conversion_Status() == 0U)
                                {
                                    RTE_ADC_Get_Conversion_Result(u16ADCSample, ADC_CONV_CH_NUMBER);

                                    stADCCtrl.u8Timer      = 0U;
                                    stADCCtrl.u8CurrentCh  = 0U;
                                    u16ADCRefVoltage       = ADC_DEFAULT_REF_VOLTAGE;

                                    stADCCtrl.enStatus     = ADC_STAT_PROC;
                                }
                                else
                                {
                                    stADCCtrl.u8Timer++;
                                    if (stADCCtrl.u8Timer >= ADC_MAX_SAMPLE_WAIT / ADC_SAMPLE_INTERVAL)
                                    {
                                        stADCCtrl.enStatus = ADC_STAT_ERR;
                                        RTE_ADC_Stop_Conversion();
                                    }
                                }
                                break;

        case ADC_STAT_PROC    : u16Voltage = ADC_Input_Voltage_Calc(u16ADCSample[stADCChCfg[stADCCtrl.u8CurrentCh].u8Ch], ADC_RESOLUTION, u16ADCRefVoltage);
                                u8Valid    = 1U;

                                if (stADCChCfg[stADCCtrl.u8CurrentCh].u8RefType == ADC_REF_TYPE_V_NONE)
                                {
                                    u16Reference = 0U;
                                }
                                else if (stADCChCfg[stADCCtrl.u8CurrentCh].u8RefType == ADC_REF_TYPE_V_VREF)
                                {
                                    u16Reference = u16ADCRefVoltage;
                                }
                                else if (stADCChCfg[stADCCtrl.u8CurrentCh].u8RefType == ADC_REF_TYPE_V_CH)
                                {
                                    u16Reference = ADC_Read_Signal((uint8_t)stADCChCfg[stADCCtrl.u8CurrentCh].u16Reference);
                                    u8Valid      = ADC_Read_Signal_Valid((uint8_t)stADCChCfg[stADCCtrl.u8CurrentCh].u16Reference);
                                }
                                else if ((stADCChCfg[stADCCtrl.u8CurrentCh].u8RefType == ADC_REF_TYPE_V_FIXED) || \
                                         (stADCChCfg[stADCCtrl.u8CurrentCh].u8RefType == ADC_REF_TYPE_I_FIXED))
                                {
                                    u16Reference = stADCChCfg[stADCCtrl.u8CurrentCh].u16Reference;
                                }
                                else
                                {
                                    u8Valid = 0U;
                                }
                                
                                if (u8Valid)
                                {
                                    u16Result = stADCChCfg[stADCCtrl.u8CurrentCh].pfProcFunc(u16Voltage,
                                                                                             u16Reference,
                                                                                             stADCChCfg[stADCCtrl.u8CurrentCh].u16Resolution,
                                                                                             &stADCResList[stADCCtrl.u8CurrentCh]);

                                    u16Result = ADC_Data_Calibrate(u16Result,
                                                                   stADCChCfg[stADCCtrl.u8CurrentCh].u8CalMode,
                                                                   stADCChCfg[stADCCtrl.u8CurrentCh].u16CalData);

                                    stADCData[stADCCtrl.u8CurrentCh].u16Value = u16Result;
                                    stADCData[stADCCtrl.u8CurrentCh].u8Valid  = 1U;
                                }
                                else
                                {
                                    stADCData[stADCCtrl.u8CurrentCh].u16Value = 0U;
                                    stADCData[stADCCtrl.u8CurrentCh].u8Valid  = 0U;
                                }

                                stADCCtrl.u8CurrentCh++;
                                if (stADCCtrl.u8CurrentCh >= ADC_SIGNAL_CH_NUMBER)
                                {
                                    RTE_ADC_Start_Conversion();
                                    stADCCtrl.u8CurrentCh = 0U;
                                    stADCCtrl.enStatus    = ADC_STAT_CONV;
                                }
                                break;

        case ADC_STAT_ERR     : if (RTE_ADC_Get_Conversion_Status() == 0U)
                                {
                                    stADCCtrl.enStatus = ADC_STAT_INIT;
                                }
                                else
                                {
                                    RTE_ADC_Stop_Conversion();
                                }
                                break;

        default               : Analog_Signal_Conv_Init();
                                break;
    }
}

uint16_t ADC_Read_Signal(uint8_t u8ADCCh)
{
    uint16_t u16Value;

    if (u8ADCCh < ADC_SIGNAL_CH_NUMBER)
    {
        u16Value = stADCData[u8ADCCh].u16Value;
    }
    else
    {
        u16Value = 0U;
    }

    return u16Value;
}

uint8_t  ADC_Read_Signal_Valid(uint8_t u8ADCCh)
{
    uint8_t u8Valid;

    if (u8ADCCh < ADC_SIGNAL_CH_NUMBER)
    {
        u8Valid = stADCData[u8ADCCh].u8Valid;
    }
    else
    {
        u8Valid = 0U;
    }

    return u8Valid;
}

uint16_t ADC_Conv_Single_Channel(uint8_t u8ADCCh)
{
    uint8_t  u8RefCh;
    uint8_t  u8Valid;
    uint8_t  u8ConvResult;
    uint16_t u16Voltage;
    uint16_t u16Reference;
    uint16_t u16Result;
    uint32_t u32Timer;

    u16Result = 0U;
    if (u8ADCCh < ADC_SIGNAL_CH_NUMBER)
    {
        if (RTE_ADC_Get_Conversion_Status() == 0U)
        {
            RTE_ADC_Start_Conversion();
        }

        u32Timer = 0U;
        do
        {
            u32Timer++;
            u8ConvResult = RTE_ADC_Get_Conversion_Status();
        }while ((u8ConvResult != 0U) && (u32Timer < ADC_CONV_WAIT));

        if (u8ConvResult == 0U)
        {
            RTE_ADC_Get_Conversion_Result(u16ADCSample, ADC_CONV_CH_NUMBER);
            u16ADCRefVoltage = ADC_DEFAULT_REF_VOLTAGE;

            u16Voltage = ADC_Input_Voltage_Calc(u16ADCSample[stADCChCfg[u8ADCCh].u8Ch], ADC_RESOLUTION, u16ADCRefVoltage);
            u8Valid    = 1U;

            if (stADCChCfg[u8ADCCh].u8RefType == ADC_REF_TYPE_V_NONE)
            {
                u16Reference = 0U;
            }
            else if (stADCChCfg[u8ADCCh].u8RefType == ADC_REF_TYPE_V_VREF)
            {
                u16Reference = u16ADCRefVoltage;
            }
            else if (stADCChCfg[u8ADCCh].u8RefType == ADC_REF_TYPE_V_CH)
            {
                u8RefCh = (uint8_t)stADCChCfg[u8ADCCh].u16Reference;

                u16Reference = ADC_Input_Voltage_Calc(u16ADCSample[stADCChCfg[u8RefCh].u8Ch], ADC_RESOLUTION, u16ADCRefVoltage);

                u16Reference = stADCChCfg[u8RefCh].pfProcFunc(u16Reference,
                                                              0U,
                                                              stADCChCfg[u8RefCh].u16Resolution,
                                                              &stADCResList[u8RefCh]);

                u16Reference = ADC_Data_Calibrate(u16Reference,
                                                  stADCChCfg[u8RefCh].u8CalMode,
                                                  stADCChCfg[u8RefCh].u16CalData);
            }
            else if ((stADCChCfg[u8ADCCh].u8RefType == ADC_REF_TYPE_V_FIXED) || \
                     (stADCChCfg[u8ADCCh].u8RefType == ADC_REF_TYPE_I_FIXED))
            {
                u16Reference = stADCChCfg[u8ADCCh].u16Reference;
            }
            else
            {
                u8Valid = 0U;
            }

            if (u8Valid)
            {
                u16Result = stADCChCfg[u8ADCCh].pfProcFunc(u16Voltage,
                                                           u16Reference,
                                                           stADCChCfg[u8ADCCh].u16Resolution,
                                                           &stADCResList[u8ADCCh]);

                u16Result = ADC_Data_Calibrate(u16Result,
                                               stADCChCfg[u8ADCCh].u8CalMode,
                                               stADCChCfg[u8ADCCh].u16CalData);
            }
        }
    }

    return u16Result;
}
