Analog_Signals.c 14.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
/**************************************************************************//**
  * \file      Analog_Signals.c
  * \brief     Analog signal processing
  * \attention
  *
  * This file is automatically generated by analog signals configuration tool.
  * Date         : 2023/9/23 14:09:24
  * Cfg Tool Ver : 1.1.0
  * Engineer     : LQY
  * (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,  4U,  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] =
{
    {     330000U,    100000U,         0U,         0U,},
    {     330000U,    100000U,         0U,         0U,},
    {          0U,    100000U,         0U,         0U,},
    {          0U,    180000U,      1550U,       300U,},
};

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

void Analog_Signal_Conv_Init(void)
{
    uint8_t i;

    ADC_Stop_Conversion();

    ADC_Init();

    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;

    u8ADCSequenceRunningFlag = 0U;
    u8ADCChannelCount = 0U;
}

void Analog_Signal_Conv_Stop(void)
{
    uint8_t i;

    ADC_Stop();

    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    : 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 (ADC_Get_Conversion_Status() == 0U)
                                {
                                    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;
                                        ADC_Stop_Conversion();
                                    }
                                }
                                break;

        case ADC_STAT_CONV    : if (ADC_Get_Conversion_Status() == 0U)
                                {
                                    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;
                                        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)
                                {
                                    ADC_Start_Conversion();
                                    stADCCtrl.u8CurrentCh = 0U;
                                    stADCCtrl.enStatus    = ADC_STAT_CONV;
                                }
                                break;

        case ADC_STAT_ERR     : if (ADC_Get_Conversion_Status() == 0U)
                                {
                                    stADCCtrl.enStatus = ADC_STAT_INIT;
                                }
                                else
                                {
                                    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 (ADC_Get_Conversion_Status() == 0U)
        {
            ADC_Start_Conversion();
        }

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

        if (u8ConvResult == 0U)
        {
            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;
}