The code below demonstrates how to establish a simple blocking delay using one of the timers, specifically TIM2 with an 8-bit offset. It is necessary to monitor the TIM count until it reaches 39062, which corresponds to a timeout of 100ms. An 8-bit offset signifies that fSTM is divided by 256.
#include "Ifx_Types.h"
#include "IfxCpu.h"
#include "IfxScuWdt.h"
#include "IfxPort.h"
#include "IfxStm.h"
IFX_ALIGN(4) IfxCpu_syncEvent g_cpuSyncEvent = 0;
void core0_main(void)
{
volatile uint32 Ticks;
IfxCpu_enableInterrupts();
/* !!WATCHDOG0 AND SAFETY WATCHDOG ARE DISABLED HERE!!
* Enable the watchdogs and service them periodically if it is required */
IfxScuWdt_disableCpuWatchdog( IfxScuWdt_getCpuWatchdogPassword() );
IfxScuWdt_disableSafetyWatchdog( IfxScuWdt_getSafetyWatchdogPassword() );
/* Wait for CPU sync event */
IfxCpu_emitEvent( &g_cpuSyncEvent );
IfxCpu_waitEvent( &g_cpuSyncEvent, 1 );
/*configure the pin */
IfxPort_setPinMode( &MODULE_P00, 5, IfxPort_Mode_outputPushPullGeneral );
IfxPort_setPinPadDriver( &MODULE_P00, 5, IfxPort_PadDriver_cmosAutomotiveSpeed1 );
/* get the count form timer TIM2 which has anf offset of 8 bits, if fSTM = 100MHz,
then its resolution is 10ns, for the TIM2 the resolution is 10ns * 256 = 2.56us*/
Ticks = IfxStm_getOffset8Timer( &MODULE_STM0 );
while(1)
{
/*Flip the led on port 00 pin 5*/
IfxPort_togglePin( &MODULE_P00, 5 );
/*wait until timer TIM2 count 39062 steps the equivalent to 100ms, we need
to count from the previous time we get the TIM2 count value minus the current count
until its differnece is 39062 */
while( ( IfxStm_getOffset8Timer( &MODULE_STM0 ) - Ticks ) < 39062 );
/*get a new count*/
Ticks = IfxStm_getOffset8Timer( &MODULE_STM0 );
}
}