/*
 * adc.c
 *
 *  Created on: 2020. 8. 3.
 *      Author: parkyj
 */
#include "main.h"
#include "adc.h"
#include "NessLab.h"


extern ADC_HandleTypeDef hadc1;
extern DMA_HandleTypeDef hdma_adc1;
extern Nesslab_Prot Currstatus;


volatile uint16_t ADC1valuearray[ADC1_CNT][ADC_AVERAGECNT];
volatile uint16_t ADC1value[ADC1_CNT];
uint16_t adc1cnt = 0 ;

void ADC_Initialize(){
	while(!(HAL_ADCEx_Calibration_Start(&hadc1)==HAL_OK));
	HAL_ADC_Start_DMA(&hadc1, (uint16_t*)ADC1value,(uint32_t) 4);
}
void ADC_Check(){
    Currstatus.DownLink_Forward_Det_H
        = ((ADC1value[0] & 0xFF00) >> 8);
    Currstatus.DownLink_Forward_Det_L
        = ((ADC1value[0] & 0x00FF) );

    Currstatus.DownLink_Reverse_Det_H
        = ((ADC1value[0] & 0xFF00) >> 8);
    Currstatus.DownLink_Reverse_Det_L
        = ((ADC1value[0] & 0x00FF) );

    Currstatus.DC_Level_Det_H
        = ((ADC1value[0] & 0xFF00) >> 8);
    Currstatus.DC_Level_Det_L
        = ((ADC1value[0] & 0x00FF) );

    Currstatus.Temp_Monitor
        = ((ADC1value[0] & 0xFF00) >> 8);
    Currstatus.Temp_Monitor
        = ((ADC1value[0] & 0x00FF) );        


}
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
    if(hadc->Instance == hadc1.Instance)
    {
        if(adc1cnt < ADC_AVERAGECNT){
            for(int i = 0; i < 4; i++){
                ADC1valuearray[i][adc1cnt] = ADC1value[i];
            }
            adc1cnt++;
        }
    }
}
