
/**
 \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
 ******************************************************************************
 */
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)
{
    /*
     * !!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);
    IfxVadc_Adc_initModule(&vadc, &adcConfig);
    IfxVadc_Adc_initGroupConfig(&adcGroupConfig, &vadc);


    /* change group (default is GroupId_0, change to GroupId_3) */
    adcGroupConfig.groupId = IfxVadc_GroupId_4;
    /* IMPORTANT: usually we use the same group as master! */
    adcGroupConfig.master = adcGroupConfig.groupId;
    /* enable all arbiter request sources */
    adcGroupConfig.arbiter.requestSlotQueueEnabled          = TRUE; /* enable Queue mode       */
    adcGroupConfig.arbiter.requestSlotScanEnabled           = TRUE; /* enable Scan mode        */
    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;

    adcGroupConfig.groupId = IfxVadc_GroupId_4;
    adcGroupConfig.master = adcGroupConfig.groupId;
    adcGroupConfig.backgroundScanRequest.autoBackgroundScanEnabled = TRUE;
    adcGroupConfig.backgroundScanRequest.triggerConfig.gatingMode  = IfxVadc_GatingMode_always;
    adcGroupConfig.arbiter.requestSlotBackgroundScanEnabled        = TRUE;

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

    /* IMPORTANT: for deterministic results we have to disable the queue gate
     while filling the queue, otherwise results could be output in the wrong order */
    unsigned savedGate = adcGroup.module.vadc->G[adcGroup.groupId].QMR0.B.ENGT;
    adcGroup.module.vadc->G[adcGroup.groupId].QMR0.B.ENGT = 0;

    /* create channel config */
    IfxVadc_Adc_ChannelConfig adcChannelConfig;
    IfxVadc_Adc_Channel adcChannel;

    IfxVadc_Adc_initChannelConfig(&adcChannelConfig, &adcGroup);
    adcChannelConfig.channelId = IfxVadc_ChannelId_7;
    adcChannelConfig.resultRegister = IfxVadc_ChannelResult_1; /* use result register #1 for all channels */

    /* initialize the channel */
    IfxVadc_Adc_initChannel(&adcChannel, &adcChannelConfig);

    /* Add channel to queue with refill enabled */
    IfxVadc_Adc_addToQueue(&adcChannel, IFXVADC_QUEUE_REFILL);

    /* restore previous gate config */
    adcGroup.module.vadc->G[adcGroup.groupId].QMR0.B.ENGT = savedGate;

    /* start the Queue */
    IfxVadc_Adc_startQueue(&adcGroup); /* just for the case that somebody copy&pastes the code - the queue has already been started in previous test  */

    /* get 10 results for all 3 channels and store in temporary buffer
       (the usage of a buffer is required, since the print statements used by the checks take more time than the conversions)
    */
    while(1)
    {
    	do {
    	    conversionResult = IfxVadc_Adc_getResult(&adcChannel);
    	    actual = conversionResult.B.RESULT;
    	    if(actual> 5000)
    	    {
    	    	error = 0x01;
    	    }
    	} while( !conversionResult.B.VF );
    }
    IfxVadc_Adc_clearQueue(&adcGroup);

    while(1)
	{
    	;
	}
    return (0);
}


