/* USER CODE BEGIN 1 */
/*
 * HAL_adc.c
 *
 *  Created on: 11. 3. 2018
 *      Author: Dušan Benda
 */

#include "HAL_adc.h"

#define VREFINT_CAL_ADDR    0x1FFFF7BA


uint32_t ADC1_result[5];
uint32_t ADC2_result[4];
uint32_t ADC1_value_result[5];
uint32_t ADC2_value_result[4];

extern  ADC_HandleTypeDef hadc1;
extern  ADC_HandleTypeDef hadc2;


uint16_t HAL_return_ADC_calibration_value(void)
{
	return (*((uint16_t*)VREFINT_CAL_ADDR));
}


void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
	HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_8);
	/*
	if(hadc->Instance == ADC1)
	{
		ADC1_value_result[0] = ADC1_result[0]; // feedback 12 V
		ADC1_value_result[1] = ADC1_result[1]; // feedback 3.3 V
		ADC1_value_result[2] = ADC1_result[2]; // internal temperature
		ADC1_value_result[3] = ADC1_result[3]; // calibration value for ADC
		ADC1_value_result[4] = ADC1_result[4]; // output temperature of LT3992
	}
	if(hadc->Instance == ADC2)
	{
		ADC2_value_result[0] = ADC2_result[0]; // I input
		ADC2_value_result[1] = ADC2_result[1]; // I output
		ADC2_value_result[2] = ADC2_result[2]; // U input
		ADC2_value_result[3] = ADC2_result[3]; // U output
	}
	*/
}

void HAL_ADC_Init_DMA(void)
{
	HAL_ADC_Start_DMA(&hadc1,(uint32_t*)ADC1_result,5);
	HAL_ADC_Start_DMA(&hadc2,(uint32_t*)ADC2_result,4);
}

void HAL_new_ADC_samples(void)
{
	//HAL_ADC_Init_DMA();
	//HAL_ADC_Start_IT(&hadc1);
	HAL_ADC_Start_IT(&hadc2);
}

uint16_t HAL_recalculate_ADC_12V_mV(void)
{
	return(ADC1_value_result[0]*3.178); // (resistor divider, ADC range) * 1.03 constant
}

uint16_t HAL_recalculate_ADC_3_3V_mV(void)
{
	return(ADC1_value_result[1]*0.862); // (resistor divider, ADC range) * 1.03 constant
}

uint16_t HAL_recalculate_ADC_temp_LT3992(void)
{
	return(ADC1_value_result[4]*0.0728); // 10 mV/degree
}

/* USER CODE END 1 */
