
/**
 \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        Cpu0_main.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
 ******************************************************************************
 */

/* MISRA RULE 19.15 VIOLATION:  The following exclusion was added because of the
 *                              inclusion of multiple headers, which are the
 *                              same. This could result in redefinitions of
 *                              types causing out of boundary scenario's.
 *                              The violation may only be turned off for header
 *                              files of external units.
 *                              Since all header files are protected against
 *                              multiple inclusion, no redefinitions can occur.
 */
/*lint -save -e537 */
/*lint -restore */

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


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

/*
 ******************************************************************************
 * Variable declarations
 ******************************************************************************
 */
Ifx_VADC_RES conversionResult[5];
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       VADC Auto Scan example
 * \details     An example on how to use the VADC ILLDs in auto scan mode
 * \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_0;

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

    IfxCpu_enableInterrupts();

    /* create channel config */
    IfxVadc_Adc_ChannelConfig adcChannelConfig[5];
    IfxVadc_Adc_Channel adcChannel[5];

    int chnIx;
    for(chnIx=0; chnIx<5; ++chnIx) {
            IfxVadc_Adc_initChannelConfig(&adcChannelConfig[chnIx], &adcGroup);
            adcChannelConfig[chnIx].channelId = (IfxVadc_ChannelId)(chnIx);
            adcChannelConfig[chnIx].resultRegister = (IfxVadc_ChannelResult)(chnIx); /* use dedicated result register */

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

            /* add to autoscan */
            unsigned channels = (1 << adcChannelConfig[chnIx].channelId);
            unsigned mask = channels;
            boolean continuous = TRUE;
            IfxVadc_Adc_setScan(&adcGroup, channels, mask, continuous);
        }

    IfxVadc_Adc_startScan(&adcGroup);
    while(1)
    {

       /* check results */
       for(chnIx=0; chnIx<5; ++chnIx) {
           unsigned group = adcChannel[chnIx].group->groupId;
           unsigned channel = adcChannel[chnIx].channel;

           /* wait for valid result */
           do {
               conversionResult[chnIx] = IfxVadc_Adc_getResult(&adcChannel[chnIx]);
           } while( !conversionResult[chnIx].B.VF );
       }
    }

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


