Let's start now to make use of the power of AURIX™: at least start to use 2 (out of the 3 available) cores!
Source Code available in GitHub here.
[..]
#include "./Libraries/Arduino/Arduino.h"
//****************************************************************************
// ARDUINO SKETCH
//****************************************************************************
// set-up function for CPU0
void cpu0_setup() {
}
// set-up function for CPU1
void cpu1_setup() {
// I intentionally create a delay between the two CPU execution
delay(500);
}
// loop function for CPU0
void cpu0_loop() {
// Create a small delay to permit to observe the LED toggling
delay(750);
// Now toggle the LED status
toggleLED();
}
// loop function for CPU1
void cpu1_loop() {
// Create a small delay to permit to observe the LED toggling
delay(750);
// Now toggle the LED status
toggleLED2();
}
//****************************************************************************
// END OF FILE
//****************************************************************************
#include "./Libraries/Arduino/Arduino.h"
//****************************************************************************
// ARDUINO SKETCH
//****************************************************************************
// set-up function for CPU0
void cpu0_setup() {
}
// set-up function for CPU1
void cpu1_setup() {
// I intentionally create a delay between the two CPU execution
delay(500);
}
// loop function for CPU0
void cpu0_loop() {
// Create a small delay to permit to observe the LED toggling
delay(750);
// Now toggle the LED status
toggleLED();
}
// loop function for CPU1
void cpu1_loop() {
// Create a small delay to permit to observe the LED toggling
delay(750);
// Now toggle the LED status
toggleLED2();
}
//****************************************************************************
// END OF FILE
//****************************************************************************
Where the cpu0_init()/cpu0_loop() and cpu1_init()/cpu1_loop() are called by CPU0_Main.c and CPU1_Main.c.
Multicore execution synchronization
As intentionally generated in the previous example, the two cores are not synchronized and therefore the two led are blinking in an asynchronous manner. I like now to show how to bring them into synchronization by using a simple semaphore. Here my semaphore.h support module implementation:
[..]
/******************************************************************************
** FUNCTION PROTOTYPES **
******************************************************************************/
#define semaphore_init(s) \
s = GREEN;
#define semaphore_take(s) \
s = RED;
#define semaphore_give(s) \
s = GREEN;
#define semaphore_wait(s) \
while (s == RED) { \
__asm__ ("nop"); \
__asm__ ("nop"); \
__asm__ ("nop"); \
__asm__ ("nop"); \
}
[..]
Such will be used as - to bring back cpu1 execution alignment with cpu0:
** FUNCTION PROTOTYPES **
******************************************************************************/
#define semaphore_init(s) \
s = GREEN;
#define semaphore_take(s) \
s = RED;
#define semaphore_give(s) \
s = GREEN;
#define semaphore_wait(s) \
while (s == RED) { \
__asm__ ("nop"); \
__asm__ ("nop"); \
__asm__ ("nop"); \
__asm__ ("nop"); \
}
[..]
#include "./Libraries/Arduino/Arduino.h"
//****************************************************************************
// ARDUINO SKETCH
//****************************************************************************
static semaphore_t s_cpuSync;
// set-up function for CPU0
void cpu0_setup() {
// init semaphore to support CPU0 / CPU1 syncronization
semaphore_init(s_cpuSync);
// Take the semaphore and wait till cpu1 give it back
semaphore_take(s_cpuSync);
semaphore_wait(s_cpuSync);
}
// set-up function for CPU1
void cpu1_setup() {
// delay to intentionally de-syncronize the two CPU executions
delay(500);
//let cpu0 execution to continue
semaphore_give(s_cpuSync);
// ... now cpu0 and cpu1 are syncronized and led blinking with be in sync ..
}
// set-up function for CPU2
void cpu2_setup() {
}
// loop function for CPU0
void cpu0_loop() {
// Create a small delay to permit to observe the LED toggling
delay(750);
// Now toggle the LED status
toggleLED1();
}
// loop function for CPU1
void cpu1_loop() {
// Create a small delay to permit to observe the LED toggling
delay(750);
// Now toggle the LED status
toggleLED2();
}
// loop function for CPU2
void cpu2_loop() {
}
//****************************************************************************
// END OF FILE
//****************************************************************************
Note: the above code is not optimized for the AURIX architecture, since to syncronize cpu core execution it would be more appropriate to use the iLLD functions, such as IfxCpu_waitEvent() - but take the above sketch as basic semaphores example.
Now the two led are toggling in sync, if we remove the semaphore_wait(s_cpuSync) from cpu0_setup() we will have the blinking delay effect - try it!
//****************************************************************************
// ARDUINO SKETCH
//****************************************************************************
static semaphore_t s_cpuSync;
// set-up function for CPU0
void cpu0_setup() {
// init semaphore to support CPU0 / CPU1 syncronization
semaphore_init(s_cpuSync);
// Take the semaphore and wait till cpu1 give it back
semaphore_take(s_cpuSync);
semaphore_wait(s_cpuSync);
}
// set-up function for CPU1
void cpu1_setup() {
// delay to intentionally de-syncronize the two CPU executions
delay(500);
//let cpu0 execution to continue
semaphore_give(s_cpuSync);
// ... now cpu0 and cpu1 are syncronized and led blinking with be in sync ..
}
// set-up function for CPU2
void cpu2_setup() {
}
// loop function for CPU0
void cpu0_loop() {
// Create a small delay to permit to observe the LED toggling
delay(750);
// Now toggle the LED status
toggleLED1();
}
// loop function for CPU1
void cpu1_loop() {
// Create a small delay to permit to observe the LED toggling
delay(750);
// Now toggle the LED status
toggleLED2();
}
// loop function for CPU2
void cpu2_loop() {
}
//****************************************************************************
// END OF FILE
//****************************************************************************
Source Code available in GitHub here.
Comments
Post a Comment