In this program two core access the same GPIO port to write in different sections of the same port, a mutex is been used in order to avoid each core access at the same time the GPIO port 00, the ILLD comes with a special function that act as a Mutex using a global variable, this global variable is been access by a special swap instruction internally by the library.
Corre 0
#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;
IFX_ALIGN(4) IfxCpu_mutexLock resourceLock;
void core0_main( void )
{
uint64 Timeout_500ms;
uint64 SeedTick;
uint8 state = 0;
uint16 leds = 0x0003;
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() );
/*configure the pin from 5 to 12 on port 00 as output pushpull and cmos speed of 1 */
IfxPort_setGroupModeOutput( &MODULE_P00, 5, 0xff, IfxPort_Mode_outputPushPullGeneral, IfxPort_OutputIdx_general );
IfxPort_setGroupPadDriver( &MODULE_P00, 5, 0xff, IfxPort_PadDriver_cmosAutomotiveSpeed1 );
/*set the initial value to zero, leds turned off*/
IfxPort_setGroupState( &MODULE_P00, 5, 0xff, 0x00 );
/* Wait for CPU sync event */
IfxCpu_emitEvent( &g_cpuSyncEvent );
IfxCpu_waitEvent( &g_cpuSyncEvent, 10u );
/*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)
{
switch(state)
{
case 0:
/* 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 );
state = 1;
}
break;
case 1:
/*try to acquire the mutex and remain in this state until then*/
if( IfxCpu_acquireMutex( &resourceLock ) == TRUE )
{
/*once the mutex is ours, write the new value in the less significant four bytes
* of the PORT00 starting at pin 5 */
leds = 0x000f & ~leds;
IfxPort_setGroupState( &MODULE_P00, 5, 0x000f, leds );
/*release the mutex for some other core*/
IfxCpu_releaseMutex( &resourceLock );
state = 0;
}
break;
}
}
}
Core 1
#include "Ifx_Types.h"
#include "IfxCpu.h"
#include "IfxScuWdt.h"
#include "IfxPort_Io.h"
#include "IfxStm.h"
extern IfxCpu_syncEvent g_cpuSyncEvent;
extern IfxCpu_mutexLock resourceLock;
void core1_main(void)
{
uint64 Timeout_500ms;
uint64 SeedTick;
uint8 state = 0;
uint16 leds = 0x0030;
IfxCpu_enableInterrupts();
/* !!WATCHDOG1 IS DISABLED HERE!!
* Enable the watchdog and service it periodically if it is required
*/
IfxScuWdt_disableCpuWatchdog(IfxScuWdt_getCpuWatchdogPassword());
/* Wait for CPU sync event, we need to wait until ports have been initialized */
IfxCpu_emitEvent(&g_cpuSyncEvent);
IfxCpu_waitEvent(&g_cpuSyncEvent, 10);
/*get the number of ticks corresponding to 500ms*/
Timeout_500ms = (uint64)IfxStm_getTicksFromMilliseconds( &MODULE_STM1, 500 );
/*get the timer STM1 count for the first time*/
SeedTick = IfxStm_get( &MODULE_STM1 );
while(1)
{
switch(state)
{
case 0:
/* query if the timer count already is equal or bigger than the last count*/
if( ( IfxStm_get( &MODULE_STM1 ) - SeedTick ) >= Timeout_500ms )
{
/*get the timer STM1 count for again*/
SeedTick = IfxStm_get( &MODULE_STM1 );
state = 1;
}
break;
case 1:
/*try to acquire the mutex and remain in this state until then*/
if( IfxCpu_acquireMutex( &resourceLock ) == TRUE )
{
/*once the mutex is ours, write the new value in the most significant four bytes
* of the PORT00 starting at pin 5 */
leds = 0x00f0 & ~leds;
IfxPort_setGroupState( &MODULE_P00, 5, 0x00f0, leds );
/*release the mutex for some other core*/
IfxCpu_releaseMutex( &resourceLock );
state = 0;
}
break;
}
}
}