Using timer STM count to established periods of times without required “delays”. Delay like functions are not desire in embedded programing due to its waste of Mcu resources, In this example you will see the best and more efficient way to set periods of times instead of delays, from this point and on avoid the usage of delay in all your code
#include "Ifx_Types.h"
#include "IfxCpu.h"
#include "IfxScuWdt.h"
#include "IfxPort_Io.h"
#include "IfxStm.h"
IFX_ALIGN(4) IfxCpu_syncEvent g_cpuSyncEvent = 0;
void core0_main(void)
{
uint64 Timeout_500ms;
uint64 SeedTick;
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 number of ticks corresponding to 500ms*/
Timeout_500ms = (uint64)IfxStm_getTicksFromMilliseconds( &MODULE_STM0, 500 );
/*get the timer STM0 count for the first time*/
SeedTick = IfxStm_get( &MODULE_STM0 );
while(1)
{
/* query if the timer count already is equal or bigger than the last count*/
if( ( IfxStm_get( &MODULE_STM0 ) - SeedTick ) >= Timeout_500ms )
{
/*get the timer STM0 count for again*/
SeedTick = IfxStm_get( &MODULE_STM0 );
/*Flip the led on port 00 pin 5*/
IfxPort_togglePin( &MODULE_P00, 5 );
}
}
}