#include <Arduino_FreeRTOS.h>
#include <semphr.h> // add the FreeRTOS functions for Semaphores (or Flags).
SemaphoreHandle_t xBinarySemaphore;
void vPeriodicTask(void *pvParameters); // It is not necessary if we use actual Interupt, not simulation
void vHandlerTask(void *pvParameters);
void vExampleInterruptHandler(void);
#define interruptPin 2
//#define mainINTERRUPT_NUMBER 2
int portTick_period_ms;
void setup() {
Serial.begin(9600);
xBinarySemaphore = xSemaphoreCreateBinary();
//Pin mode Status just for Simulation.
//If there is real pulse, then it is not necessary
pinMode(interruptPin, OUTPUT);
attachInterrupt(digitalPinToInterrupt(interruptPin), vExampleInterruptHandler, RISING);
if (xBinarySemaphore != NULL) //If return Value not NULL from "xSemaphoreCreateBinary()" function
{
xTaskCreate(vHandlerTask, "Handler", 128, NULL, 3, NULL);
xTaskCreate(vPeriodicTask, "Periodic", 128, NULL, 1, NULL);
vTaskStartScheduler();
}
for (;;)
;
}
void loop() {
;
}
void vPeriodicTask(void *pvParameters) {
const TickType_t xDelay3000ms = pdMS_TO_TICKS(3000);
for (;;) {
vTaskDelay(xDelay3000ms);
Serial.print("Periodic task - About to generate an interrupt.\r\n");
//vPortGenerateSimulatedInterrupt(mainINTERRUPT_NUMBER);
//Simulate
digitalWrite(interruptPin, LOW);
digitalWrite(interruptPin, HIGH);
Serial.print("Periodic task - Interrupt generated.\r\n\r\n\r\n");
}
}
void vExampleInterruptHandler(void)
{
BaseType_t pxHigherPriorityTaskWoken = pdFALSE;
//Input Command here as per emergency task
Serial.print("Emergency Task - Pump Stop due to Over Filling. \r\n");
//Activated High Priority Task
xSemaphoreGiveFromISR(xBinarySemaphore, &pxHigherPriorityTaskWoken);
//Checking whether High Pririty Task is Active
if(pxHigherPriorityTaskWoken == pdTRUE){
portYIELD_FROM_ISR();
}
xSemaphoreGiveFromISR(xBinarySemaphore, NULL);
}
void vHandlerTask(void *pvParameters)
{
xSemaphoreTake(xBinarySemaphore, 0);
for(;;){
xSemaphoreTake(xBinarySemaphore, portMAX_DELAY);
/*
while(...){
delay();
}
*/
Serial.println("Handler task - Processing event.\r\n");
}
}