/**
 * @file       analogMeasurement.c
 *
 * @copyright  Nehemis SARL reserves all rights even in the event of industrial
 *             property rights. We reserve all rights of disposal such as
 *             copying and passing on to third parties.
 *
 * @brief      Source code for ADC data reading module.
 */

/******************************************************************************
 * Include Header Files
 ******************************************************************************/
#include "analogMeasurement.h"
#include "hal_adc.h"

/******************************************************************************
 * Macro Constant Declarations
 ******************************************************************************/
#define ADC_BUFFER_SIZE  9u

/******************************************************************************
 * Module Global Variable Declarations
 ******************************************************************************/
static uint16 adcBuffer_u32[ADC_BUFFER_SIZE];

/******************************************************************************
 * Private Function Declarations
 ******************************************************************************/
static void AnalogMeasurementInitBuffer(void);

/******************************************************************************
 * Function Definitions
 ******************************************************************************/
void AnalogMeasurementInit(void)
{
	uint32 error_u32 = NMS_ERR_DEFAULT;

	AnalogMeasurementInitBuffer();

	HalAdcCalibrate(MAP_HAL_ADC_1);
	error_u32 = HalAdcStartDMA(MAP_HAL_ADC_1, (uint32 *)adcBuffer_u32, ADC_BUFFER_SIZE);

	if (error_u32 != NMS_ERR_NONE)
	{
		/* Handle the error (DMA start failed) */
	}
}

void AnalogMesaurementRun(void)
{
	HalAdcStartDMA(MAP_HAL_ADC_1, (uint32 *)adcBuffer_u32, sizeof(adcBuffer_u32));
}


void AnalogMeasurementReadData(uint32 channel_u32, uint32 *adcValue_pu32)
{

    if (adcValue_pu32 == NULL)
    {
    	return;  /* Prevent dereferencing NULL pointer */
    }
    switch (channel_u32)
    {
    	case PS1_ADC_CHANNEL: *adcValue_pu32 = adcBuffer_u32[0]; break;
    	case PS2_ADC_CHANNEL: *adcValue_pu32 = adcBuffer_u32[1]; break;
    	case PS3_ADC_CHANNEL: *adcValue_pu32 = adcBuffer_u32[2]; break;
    	case PS4_ADC_CHANNEL: *adcValue_pu32 = adcBuffer_u32[3]; break;

    	case PMP_ADC_CHANNEL: *adcValue_pu32 = adcBuffer_u32[4]; break;

    	case FM1_ADC_CHANNEL: *adcValue_pu32 = adcBuffer_u32[5]; break;
    	case FM2_ADC_CHANNEL: *adcValue_pu32 = adcBuffer_u32[6]; break;
    	case FM3_ADC_CHANNEL: *adcValue_pu32 = adcBuffer_u32[7]; break;
    	case FM4_ADC_CHANNEL: *adcValue_pu32 = adcBuffer_u32[8]; break;
    	default: *adcValue_pu32 = 0uL; break;
    }
}

/******************************************************************************
 * Private Function Definitions
 ******************************************************************************/
/* @brief 	Initialize buffer with default values*/
static void AnalogMeasurementInitBuffer(void)
{
    for (uint8 i_u8 = 0u; i_u8 < ADC_BUFFER_SIZE; i_u8++)
    {
        adcBuffer_u32[i_u8] = 0uL;
    }
}
