herzchen für Combie.
Semaphore scheint mal ein Weg gewesen zu sein.
// ESP32 - fun with RTOS
// Serial print protected by semaphore (based on an idea of https://forum.arduino.cc/t/freertos-zusammenspiel-von-tasks/641707/21 )
SemaphoreHandle_t xSemaphore = NULL;
void printInfo(char c, int core)
{
Serial.printf("%c runs on core %d\n", c, core);
}
void taskA(void * parameter)
{
(void)parameter;
for (;;)
{
if ( xSemaphoreTake( xSemaphore, ( TickType_t ) 10 ) == pdTRUE )
{
// If not available, wait 10 ticks and try again
// If task has it, do some exclusive stuff here;
printInfo('A', xPortGetCoreID());
Serial.println('A');
// After finishing, release the semaphore
xSemaphoreGive( xSemaphore ); // Now free or "Give" the resource to other tasks which might want it too.
}
vTaskDelay(300);
}
}
void taskB(void * parameter)
{
(void)parameter;
for (;;)
{
if ( xSemaphoreTake( xSemaphore, ( TickType_t ) 10 ) == pdTRUE )
{
// If not available, wait 10 ticks and try again
// If task has it, do some exclusive stuff here;
printInfo('B', xPortGetCoreID());
Serial.println('B');
// After finishing, release the semaphore
xSemaphoreGive( xSemaphore ); // Now free or "Give" the resource back to other tasks which might want it too.
}
vTaskDelay(1200);
}
}
void taskC(void * parameter)
{
(void)parameter;
for (;;)
{
if ( xSemaphoreTake( xSemaphore, ( TickType_t ) 10 ) == pdTRUE )
{
// If not available, wait 10 ticks and try again
// If task has it, do some exclusive stuff here;
printInfo('C', xPortGetCoreID());
Serial.println("C fixed 1");
// After finishing, release the semaphore
xSemaphoreGive( xSemaphore ); // Now free or "Give" the resource back to other tasks which might want it too.
}
vTaskDelay(1300);
}
}
void taskD(void * parameter)
{
(void)parameter;
for (;;)
{
if ( xSemaphoreTake( xSemaphore, ( TickType_t ) 10 ) == pdTRUE )
{
// If not available, wait 10 ticks and try again
// If task has it, do some exclusive stuff here;
printInfo('D', xPortGetCoreID());
Serial.println("D fixed 0");
// After finishing, release the semaphore
xSemaphoreGive( xSemaphore ); // Now free or "Give" the resource back to other tasks which might want it too.
}
vTaskDelay(1400);
}
}
void setup() {
Serial.begin(115200);
// create semaphore
if ( xSemaphore == NULL ) // Ensure that the Semaphore has not already been created.
{
xSemaphore = xSemaphoreCreateMutex(); // A mutex semaphore to manage the resource
if ( ( xSemaphore ) != NULL )
xSemaphoreGive( ( xSemaphore ) ); // Make the resource available for exclusive use, by "Giving" the Semaphore.
}
xTaskCreatePinnedToCore(
taskA // Pointer to the task entry function. Tasks must be implemented to never return (i.e. continuous loop), or should be terminated using vTaskDelete function.
, "TaskA" // A descriptive name for the task. This is mainly used to facilitate debugging. Max length defined by configMAX_TASK_NAME_LEN - default is 16
, 2048 // The size of the task stack specified as the number of bytes. This stack size can be checked & adjusted by reading the Stack Highwater
, NULL // Pointer that will be used as the parameter for the task being created.
, 2 // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
, NULL // Used to pass back a handle by which the created task can be referenced.
, tskNO_AFFINITY); // If the value is tskNO_AFFINITY, the created task is not pinned to any CPU, and the scheduler can run it on any core available.
xTaskCreatePinnedToCore(
taskB
, "TaskB" // A name just for humans
, 2048 // This stack size can be checked & adjusted by reading the Stack Highwater
, NULL
, 2 // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
, NULL
, tskNO_AFFINITY);
xTaskCreatePinnedToCore(
taskC
, "TaskC" // A name just for humans
, 2048 // This stack size can be checked & adjusted by reading the Stack Highwater
, NULL
, 9 // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
, NULL
, 1); // core fix zugewiesen
xTaskCreatePinnedToCore(
taskD
, "TaskD" // A name just for humans
, 2048 // This stack size can be checked & adjusted by reading the Stack Highwater
, NULL
, 9 // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
, NULL
, 0); // core fix zugewiesen
}
void loop() {
}