
/**
 \verbatim
 ******************************************************************************
 * Copyright (c) Infineon Technologies AG, all rights reserved.
 ******************************************************************************
 * This source code and any compilation or derivative thereof is the sole
 * property of Infineon Technologies AG and is provided pursuant to a
 * Software License  Agreement. This code is the proprietary information of
 * Infineon Technologies AG and is confidential in nature. Its use and
 * dissemination by any other party other than Infineon Technologies AG
 * is strictly limited by the confidential information provisions of the
 * Agreement referenced above.
 ******************************************************************************
 \endverbatim
 ******************************************************************************
 * \file        .c
 * \ingroup     XXX - Xxxxxx
 * \brief       XXXX.
 * \details
 * \author
 * \date        \$LastChangedDate:: 2009-05-14 #$
 * \version     \$Revision: 1383 $
 * \attention   -
 ******************************************************************************
 */

/**
 ******************************************************************************
 * \defgroup    XXX - Xxxxx
 * \brief       XXXXXX
 * \details     -
 * \attention   -
 ******************************************************************************
 */


 /*
 ******************************************************************************
 * Standard include files
 ******************************************************************************
 */
#include "Ifx_Types.h"
#include "IfxCpu_Intrinsics.h"
#include "IfxScuWdt.h"
#include "Ifx_reg.h"
#include <Port/Io/IfxPort_Io.h>
#include "IfxCpu_Irq.h"
#include <Qspi/SpiMaster/IfxQspi_SpiMaster.h>
#include <Asclin/Asc/IfxAsclin_Asc.h>
#include <Vadc/Adc/IfxVadc_Adc.h>

#include "Test_Time.h"

/*
 ******************************************************************************
 * Project include files
 ******************************************************************************
 */

/*
 ******************************************************************************
 * Types
 ******************************************************************************
 */


/*
 ******************************************************************************
 * Macro defines
 ******************************************************************************
 */

/*
 ******************************************************************************
 * Variable declarations
 ******************************************************************************
 */

/* The module initialisation can be done in the same function:  */
/* create configuration  */

Ifx_VADC_RES conversionResult;
uint32 actual;
uint8 error = 0;
/*
 ******************************************************************************
 * Routine prototypes
 ******************************************************************************
 */


/*
 ******************************************************************************
 * Routine implementation
 ******************************************************************************
 */
/* VADC handle  */
IfxVadc_Adc vadc;

/* create configuration  */
IfxVadc_Adc_Config adcConfig;

/* create group config  */
IfxVadc_Adc_GroupConfig adcGroupConfig;

/**
 ******************************************************************************
 * \brief       ADC Queued Example
 * \details     An example on how to use the VADC ILLD module
 * \param[in]   -
 * \param[out]  -
 * \return      Void
 * \pre         None.
 * \post        -
 * \attention   -
 ******************************************************************************
 */


int core0_main (void)
{

	int chnIx;
	IfxVadc_Adc_Config adcConfig;

    /*
     * !!WATCHDOG0 AND SAFETY WATCHDOG ARE DISABLED HERE!!
     * Enable the watchdog in the demo if it is required and also service the watchdog periodically
     */
    IfxScuWdt_disableCpuWatchdog (IfxScuWdt_getCpuWatchdogPassword ());
    IfxScuWdt_disableSafetyWatchdog (IfxScuWdt_getSafetyWatchdogPassword ());

	IfxVadc_Adc_initModuleConfig(&adcConfig, &MODULE_VADC);// initialize module

	IfxVadc_Adc vadc;

	/* declared globally  */
	IfxVadc_Adc_initModule(&vadc, &adcConfig);

	/* create group config  */
	IfxVadc_Adc_GroupConfig adcGroupConfig;
	IfxVadc_Adc_initGroupConfig(&adcGroupConfig, &vadc);

	/* change group (default is GroupId_0, change to GroupId_3)  */
	adcGroupConfig.groupId = IfxVadc_GroupId_3;			/* IMPORTANT: usually we use the same group as master!  */
	adcGroupConfig.master = adcGroupConfig.groupId;     /* enable all arbiter request sources                   */
	adcGroupConfig.arbiter.requestSlotBackgroundScanEnabled = TRUE; /* enable Background scan  */

	/* enable all gates in "always" mode (no edge detection) */
	adcGroupConfig.queueRequest.triggerConfig.gatingMode          = IfxVadc_GatingMode_always;
	adcGroupConfig.scanRequest.triggerConfig.gatingMode           = IfxVadc_GatingMode_always;
	adcGroupConfig.backgroundScanRequest.triggerConfig.gatingMode = IfxVadc_GatingMode_always;

	/* initialize the group */
	IfxVadc_Adc_Group adcGroup;
	IfxVadc_Adc_initGroup(&adcGroup, &adcGroupConfig);


    /* create channel config */
    IfxVadc_Adc_ChannelConfig adcChannelConfig[2];
    IfxVadc_Adc_Channel adcChannel[2];
    for(chnIx=0; chnIx<2; ++chnIx)
    {
    	IfxVadc_Adc_initChannelConfig(&adcChannelConfig[chnIx], &adcGroup);
    	adcChannelConfig[chnIx].channelId = (IfxVadc_ChannelId)(chnIx + 5);
    	adcChannelConfig[chnIx].resultRegister = (IfxVadc_ChannelResult)(5 + chnIx); /* use register #5 and 6 for results  */
    	adcChannelConfig[chnIx].backgroundChannel = TRUE;

    	/* initialize the channel */
    	IfxVadc_Adc_initChannel(&adcChannel[chnIx], &adcChannelConfig[chnIx]);       /* add to background scan  */

    	unsigned channels = (1 << adcChannelConfig[chnIx].channelId);
    	unsigned mask = channels;

    	IfxVadc_Adc_setBackgroundScan(&vadc, &adcGroup, channels, mask);
    }   /* start autoscan */

    boolean continuous = TRUE;
    IfxVadc_Adc_startBackgroundScan(&vadc, continuous);   // check results
    for(chnIx=0; chnIx<2; ++chnIx)
    {
    	unsigned group = adcChannel[chnIx].group->groupId;
    	unsigned channel = adcChannel[chnIx].channel;

    	/* wait for valid result */

    	Ifx_VADC_RES conversionResult;
    	do
    	{
    		conversionResult = IfxVadc_Adc_getResult(&adcChannel[chnIx]);
    	} 	while( !conversionResult.B.VF );

    	/* check with expected value  */
    	/* ... */

    }


    return (0);
}


