 /*
 * 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 ;

/*
 *
 * ADC 0 :DL TX
 * ADC 1 :DL RX
 * ADC 2 :TEMP
 * */

void ADC_Value_Get(){
	uint16_t CalcRet = 0 ;

	/*DL TX Calc*/
	Currstatus.DownLink_Forward_Det_H = ((ADC1value[0] & 0xFF00) >> 8);
	Currstatus.DownLink_Forward_Det_L = (ADC1value[0] & 0x00FF);

	/*DL RX Calc*/
	Currstatus.DownLink_Reverse_Det_H = ((ADC1value[1] & 0xFF00) >> 8);
	Currstatus.DownLink_Reverse_Det_L = (ADC1value[1] & 0x00FF);

	/*Temp Calc*/
	Currstatus.Temp_Monitor = ((ADC1value[1] & 0xFF00) >> 8);


}
void ADC_Initialize(){
	while(!(HAL_ADCEx_Calibration_Start(&hadc1)==HAL_OK));
	HAL_ADC_Start_DMA(&hadc1, (uint16_t*)ADC1value,(uint32_t) 3);
}

void ADC_Check(){
    float tempval = 0;
    for(int i = 0 ; i < ADC1_CNT; i++){
        tempval = (ADC1value[i] * (3.3 / 4095));
//        tempval *= 1000;
    }
//    printf("ADC1value[%d] : %f \r\n",i,tempval);
//    printf("ADC1value[%d] : %d \r\n",i,ADC1value[i] );
#if 1 // PYJ.2020.08.26_BEGIN -- 
    Currstatus.DownLink_Forward_Det_H
        = (((uint16_t)tempval & 0xFF00) >> 8);
    Currstatus.DownLink_Forward_Det_L
        = (((uint16_t)tempval & 0x00FF) );
#endif // PYJ.2020.08.26_END -- 
    adc1cnt = 0;
}
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
    if(hadc->Instance == hadc1.Instance)
    {
//        if(adc1cnt < ADC_AVERAGECNT){
//            for(int i = 0; i < ADC1_CNT; i++){
//                ADC1valuearray[i][adc1cnt] = ADC1value[i];
//            }
//            adc1cnt++;
//        }
    }
}
