I have gone through https://www.freertos.org/a00122.html I am getting error 'mutex' was not declared in this scope. I think I am missing some files that needs to includes
/* Include FreeRTOS APIs and defines */
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/timers.h"
#include <mutex>
SemaphoreHandle_t xSemaphore = NULL;
const int RED = 4;
void setup()
{
Serial.begin(115200);
assert(mutex);
pinMode(RED, OUTPUT);
xTaskCreatePinnedToCore(Task1, "Task1", 1000, NULL, 1, NULL, 0);
xTaskCreatePinnedToCore(Task2, "Task2", 1000, NULL, 1, NULL, 0);
}
void Task1( void * parameter )
{
for(;;)
{
xSemaphore = xSemaphoreCreateMutex();
digitalWrite( RED, HIGH);
vTaskDelay( 400 );
digitalWrite( RED, LOW);
vTaskDelay( 400 );
xSemaphoreGive(mutex);
}
}
void Task2( void * parameter )
{
for(;;)
{
if( xSemaphore != NULL )
{
/* See if we can obtain the semaphore. If the semaphore is not
available wait 10 ticks to see if it becomes free. */
if( xSemaphoreTake( xSemaphore, ( TickType_t ) 10 ) == pdTRUE )
{
/* We were able to obtain the semaphore and can now access the
shared resource. */
digitalWrite(RED, HIGH);
vTaskDelay( 500 );
digitalWrite(RED, LOW);
vTaskDelay( 500 );
/* We have finished accessing the shared resource. Release the
semaphore. */
xSemaphoreGive( xSemaphore );
}
else
{
/* We could not obtain the semaphore and can therefore not access
the shared resource safely. */
}
}
}
}
void loop() {}
/* Include FreeRTOS APIs and defines */
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/timers.h"
SemaphoreHandle_t xSemaphore = NULL;
const int RED = 4;
void setup()
{
Serial.begin(115200);
xSemaphore = xSemaphoreCreateMutex();
pinMode(RED, OUTPUT);
xTaskCreatePinnedToCore(Task1, "Task1", 1000, NULL, 1, NULL, 0);
xTaskCreatePinnedToCore(Task2, "Task2", 1000, NULL, 1, NULL, 0);
}
void Task1( void * parameter )
{
for(;;)
{
digitalWrite( RED, HIGH);
vTaskDelay( 200 );
digitalWrite( RED, LOW);
vTaskDelay( 200 );
xSemaphoreGive( xSemaphore);
}
}
void Task2( void * parameter )
{
for(;;)
{
if( xSemaphore != NULL )
{
/* See if we can obtain the semaphore. If the semaphore is not
available wait 10 ticks to see if it becomes free. */
if( xSemaphoreTake( xSemaphore, ( TickType_t ) 10 ) == pdTRUE )
{
/* We were able to obtain the semaphore and can now access the
shared resource. */
digitalWrite(RED, HIGH);
vTaskDelay( 800 );
digitalWrite(RED, LOW);
vTaskDelay( 800 );
/* We have finished accessing the shared resource. Release the
semaphore. */
xSemaphoreGive( xSemaphore );
}
else
{
/* We could not obtain the semaphore and can therefore not access
the shared resource safely. */
}
}
}
}
void loop() {}