#include <Arduino_FreeRTOS.h>
#include "semphr.h"
SemaphoreHandle_t serial_mutex;
void setup() {
Serial.begin(9600);
serial_mutex = xSemaphoreCreateMutex();
xTaskCreate(task1, "print task", 128, NULL, 2, NULL);
xTaskCreate(task2, "print task", 128, NULL, 2, NULL);
}
void loop() {
//this code is empty
}
//how to use the taskYIELD(); --> it does the context switching
void task1(void *pvParameters){
// taskENTER_CRITICAL(); //suspends all task and interrupts
vTaskSuspendAll(); //suspends all task but not with the interrupts
Serial.println("Something Important!");
// taskEXIT_CRITICAL(); //resumes the task
xTaskResumeAll(); //resumes all the task
while(1){
if(xSemaphoreTake(serial_mutex, portMAX_DELAY) == pdTRUE){
Serial.println("hello from task1");
xSemaphoreGive(serial_mutex);
taskYIELD(); //this is to context switch
vTaskDelay(1000/portTICK_PERIOD_MS);
}
}
}
void task2(void *pvParameters){
while(1){
if(xSemaphoreTake(serial_mutex, portMAX_DELAY) == pdTRUE){
Serial.println("Hello from task2");
xSemaphoreGive(serial_mutex);
taskYIELD(); //this is to contex switch
vTaskDelay(1000/portTICK_PERIOD_MS);
}
}
}