Добавлена поддержка FreeRTOS

This commit was merged in pull request #16.
This commit is contained in:
klassents committed 2025-03-28 06:37:50 +03:00
1 parent 1aaf8c59ae
commit 43c97f7b56
49 files changed
+35167 -1

No files matched your search

@@ -0,0 +1,120 @@
#include <Arduino_FreeRTOS.h>
#include <semphr.h> // add the FreeRTOS functions for Semaphores (or Flags).
// Declare a mutex Semaphore Handle which we will use to manage the Serial Port.
// It will be used to ensure only one Task is accessing this resource at any time.
SemaphoreHandle_t xSerialSemaphore;
// define two Tasks for DigitalRead & AnalogRead
void TaskDigitalRead( void *pvParameters );
void TaskAnalogRead( void *pvParameters );
// the setup function runs once when you press reset or power the board
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// Semaphores are useful to stop a Task proceeding, where it should be paused to wait,
// because it is sharing a resource, such as the Serial port.
// Semaphores should only be used whilst the scheduler is running, but we can set it up here.
if ( xSerialSemaphore == NULL ) // Check to confirm that the Serial Semaphore has not already been created.
{
xSerialSemaphore = xSemaphoreCreateMutex(); // Create a mutex semaphore we will use to manage the Serial Port
if ( ( xSerialSemaphore ) != NULL )
xSemaphoreGive( ( xSerialSemaphore ) ); // Make the Serial Port available for use, by "Giving" the Semaphore.
}
// Now set up two Tasks to run independently.
xTaskCreate(
TaskDigitalRead,
"DigitalRead", // A name just for humans
128, // This stack size can be checked & adjusted by reading the Stack Highwater
NULL, // Parameters for the task
2, // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
NULL); // Task Handle
xTaskCreate(
TaskAnalogRead,
"AnalogRead", // A name just for humans
128, // Stack size
NULL, // Parameters for the task
1, // Priority
NULL); // Task Handle
// Now the Task scheduler, which takes over control of scheduling individual Tasks, is automatically started.
}
void loop()
{
// Empty. Things are done in Tasks.
}
/*--------------------------------------------------*/
/*---------------------- Tasks ---------------------*/
/*--------------------------------------------------*/
void TaskDigitalRead( void *pvParameters __attribute__((unused)) ) // This is a Task.
{
/*
DigitalReadSerial
Reads a digital input on pin pushButton, prints the result to the serial monitor
This example code is in the public domain.
*/
#ifdef BTN_BUILTIN
uint8_t pushButton = BTN_BUILTIN;
#elif (defined(ARDUINO_ELSOMIK))
uint8_t pushButton = P0_0;
#else
uint8_t pushButton = 2;
#endif
// make the pushbutton's pin an input:
pinMode(pushButton, INPUT);
for (;;) // A Task shall never return or exit.
{
// read the input pin:
int buttonState = digitalRead(pushButton);
// See if we can obtain or "Take" the Serial Semaphore.
// If the semaphore is not available, wait 5 ticks of the Scheduler to see if it becomes free.
if ( xSemaphoreTake( xSerialSemaphore, ( TickType_t ) 5 ) == pdTRUE )
{
// We were able to obtain or "Take" the semaphore and can now access the shared resource.
// We want to have the Serial Port for us alone, as it takes some time to print,
// so we don't want it getting stolen during the middle of a conversion.
// print out the state of the button:
Serial.println(buttonState);
xSemaphoreGive( xSerialSemaphore ); // Now free or "Give" the Serial Port for others.
}
vTaskDelay(1); // one tick delay (10ms) in between reads for stability
}
}
void TaskAnalogRead( void *pvParameters __attribute__((unused)) ) // This is a Task.
{
for (;;)
{
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// See if we can obtain or "Take" the Serial Semaphore.
// If the semaphore is not available, wait 5 ticks of the Scheduler to see if it becomes free.
if ( xSemaphoreTake( xSerialSemaphore, ( TickType_t ) 5 ) == pdTRUE )
{
// We were able to obtain or "Take" the semaphore and can now access the shared resource.
// We want to have the Serial Port for us alone, as it takes some time to print,
// so we don't want it getting stolen during the middle of a conversion.
// print out the value you read:
Serial.println(sensorValue);
xSemaphoreGive( xSerialSemaphore ); // Now free or "Give" the Serial Port for others.
}
vTaskDelay(1); // one tick delay (10ms) in between reads for stability
}
}
@@ -0,0 +1,162 @@
/*
* Example of a basic FreeRTOS queue
* https://www.freertos.org/Embedded-RTOS-Queues.html
*/
// Include Arduino FreeRTOS library
#include <Arduino_FreeRTOS.h>
// Include queue support
#include <queue.h>
// Define a Array
int pinReadArray[4]={0,0,0,0};
#ifdef LED_BUILTIN
uint8_t blink_pin = LED_BUILTIN;
#elif defined(ARDUINO_ELSOMIK)
uint8_t blink_pin = P0_0;
#else
uint8_t blink_pin = 2;
#endif
//Function Declaration
void TaskBlink(void *pvParameters);
void TaskAnalogReadPin0(void *pvParameters);
void TaskAnalogReadPin1(void *pvParameters);
void TaskSerial(void *pvParameters);
/*
* Declaring a global variable of type QueueHandle_t
*
*/
QueueHandle_t arrayQueue;
void setup() {
/**
* Create a queue.
* https://www.freertos.org/a00116.html
*/
arrayQueue=xQueueCreate(10, // Queue length
sizeof(int)); // Queue item size
if(arrayQueue!=NULL){
// Create task that consumes the queue if it was created.
xTaskCreate(TaskSerial, // Task function
"PrintSerial", // Task name
128, // Stack size
NULL,
2, // Priority
NULL);
// Create task that publish data in the queue if it was created.
xTaskCreate(TaskAnalogReadPin0, // Task function
"AnalogRead1", // Task name
128, // Stack size
NULL,
1, // Priority
NULL);
// Create other task that publish data in the queue if it was created.
xTaskCreate(TaskAnalogReadPin1, // Task function
"AnalogRead2", // Task name
128, // Stack size
NULL,
1, // Priority
NULL);
xTaskCreate(TaskBlink, // Task function
"Blink", // Task name
128, // Stack size
NULL,
0, // Priority
NULL);
}
}
void loop() {}
/**
* Analog read task for Pin A0
* Reads an analog input on pin 0 and send the readed value through the queue.
* See Blink_AnalogRead example.
*/
void TaskAnalogReadPin0(void *pvParameters){
(void) pvParameters;
for (;;){
pinReadArray[0]=0;
pinReadArray[1]=analogRead(A0);
/**
* Post an item on a queue.
* https://www.freertos.org/a00117.html
*/
xQueueSend(arrayQueue,&pinReadArray,portMAX_DELAY);
// One tick delay (10ms) in between reads for stability
vTaskDelay(1);
}
}
/**
* Analog read task for Pin A1
* Reads an analog input on pin 1 and send the readed value through the queue.
* See Blink_AnalogRead example.
*/
void TaskAnalogReadPin1(void *pvParameters){
(void) pvParameters;
for (;;){
pinReadArray[2]=1;
pinReadArray[3]=analogRead(A1);
/**
* Post an item on a queue.
* https://www.freertos.org/a00117.html
*/
xQueueSend(arrayQueue,&pinReadArray,portMAX_DELAY);
// One tick delay (10ms) in between reads for stability
vTaskDelay(1);
}
}
/**
* Serial task.
* Prints the received items from the queue to the serial monitor.
*/
void TaskSerial(void *pvParameters){
(void) pvParameters;
// Init Arduino serial
Serial.begin(9600);
for (;;){
if(xQueueReceive(arrayQueue,&pinReadArray,portMAX_DELAY) == pdPASS ){
Serial.print("PIN:");
Serial.println(pinReadArray[0]);
Serial.print("value:");
Serial.println(pinReadArray[1]);
Serial.print("PIN:");
Serial.println(pinReadArray[2]);
Serial.print("value:");
Serial.println(pinReadArray[3]);
vTaskDelay(500/portTICK_PERIOD_MS);
}
}
}
/*
* Blink task.
* See Blink_AnalogRead example.
*/
void TaskBlink(void *pvParameters){
(void) pvParameters;
pinMode(blink_pin,OUTPUT);
digitalWrite(blink_pin,LOW);
for (;;){
digitalWrite(blink_pin,HIGH);
vTaskDelay(250/portTICK_PERIOD_MS);
digitalWrite(blink_pin,LOW);
vTaskDelay(250/portTICK_PERIOD_MS);
}
}
@@ -0,0 +1,95 @@
#include <Arduino_FreeRTOS.h>
// define two tasks for Blink & AnalogRead
void TaskBlink( void *pvParameters );
void TaskAnalogRead( void *pvParameters );
#ifdef LED_BUILTIN
uint8_t blink_pin = LED_BUILTIN;
#elif defined(ARDUINO_ELSOMIK)
uint8_t blink_pin = P0_0;
#else
uint8_t blink_pin = 2;
#endif
// the setup function runs once when you press reset or power the board
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// Now set up two tasks to run independently.
xTaskCreate(
TaskBlink,
"Blink", // A name just for humans
128, // 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);
xTaskCreate(
TaskAnalogRead,
"AnalogRead",
128, // Stack size
NULL,
1, // Priority
NULL);
// Now the task scheduler, which takes over control of scheduling individual tasks, is automatically started.
}
void loop()
{
// Empty. Things are done in Tasks.
}
/*--------------------------------------------------*/
/*---------------------- Tasks ---------------------*/
/*--------------------------------------------------*/
void TaskBlink(void *pvParameters) // This is a task.
{
(void) pvParameters;
/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
Most boards have an on-board LED you can control. You can use it via LED_BUILTIN macro.
If there is no led on board, you can set different pin to blink_pin variable.
*/
// initialize blink_pin as an output.
pinMode(blink_pin, OUTPUT);
for (;;) // A Task shall never return or exit.
{
digitalWrite(blink_pin, HIGH); // turn the pin on (HIGH is the voltage level)
vTaskDelay( 1000 / portTICK_PERIOD_MS ); // wait for one second
digitalWrite(blink_pin, LOW); // turn the pin off by making the voltage LOW
vTaskDelay( 1000 / portTICK_PERIOD_MS ); // wait for one second
}
}
void TaskAnalogRead(void *pvParameters) // This is a task.
{
(void) pvParameters;
/*
AnalogReadSerial
Reads an analog input on pin 0, prints the result to the serial monitor.
Graphical representation is available using serial plotter (Tools > Serial Plotter menu)
Attach the center pin of a potentiometer to pin A0, and the outside pins to +3V3 and ground.
This example code is in the public domain.
*/
for (;;)
{
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// print out the value you read:
Serial.println(sensorValue);
vTaskDelay(250 / portTICK_PERIOD_MS);
}
}
@@ -0,0 +1,135 @@
/*
* Example of a basic FreeRTOS queue
* https://www.freertos.org/Embedded-RTOS-Queues.html
*/
// Include Arduino FreeRTOS library
#include <Arduino_FreeRTOS.h>
// Include queue support
#include <queue.h>
/*
* Declaring a global variable of type QueueHandle_t
*
*/
QueueHandle_t integerQueue;
#ifdef LED_BUILTIN
uint8_t blink_pin = LED_BUILTIN;
#elif defined(ARDUINO_ELSOMIK)
uint8_t blink_pin = P0_0;
#else
uint8_t blink_pin = 2;
#endif
void setup() {
/**
* Create a queue.
* https://www.freertos.org/a00116.html
*/
integerQueue = xQueueCreate(10, // Queue length
sizeof(int) // Queue item size
);
if (integerQueue != NULL) {
// Create task that consumes the queue if it was created.
xTaskCreate(TaskSerial, // Task function
"Serial", // A name just for humans
128, // 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);
// Create task that publish data in the queue if it was created.
xTaskCreate(TaskAnalogRead, // Task function
"AnalogRead", // Task name
128, // Stack size
NULL,
1, // Priority
NULL);
}
xTaskCreate(TaskBlink, // Task function
"Blink", // Task name
128, // Stack size
NULL,
0, // Priority
NULL );
}
void loop() {}
/**
* Analog read task
* Reads an analog input on pin 0 and send the readed value through the queue.
* See Blink_AnalogRead example.
*/
void TaskAnalogRead(void *pvParameters)
{
(void) pvParameters;
for (;;)
{
// Read the input on analog pin 0:
int sensorValue = analogRead(A0);
/**
* Post an item on a queue.
* https://www.freertos.org/a00117.html
*/
xQueueSend(integerQueue, &sensorValue, portMAX_DELAY);
// One tick delay (10ms) in between reads for stability
vTaskDelay(1);
}
}
/**
* Serial task.
* Prints the received items from the queue to the serial monitor.
*/
void TaskSerial(void * pvParameters) {
(void) pvParameters;
// Init Arduino serial
Serial.begin(9600);
int valueFromQueue = 0;
for (;;)
{
/**
* Read an item from a queue.
* https://www.freertos.org/a00118.html
*/
if (xQueueReceive(integerQueue, &valueFromQueue, portMAX_DELAY) == pdPASS) {
Serial.println(valueFromQueue);
}
}
}
/*
* Blink task.
* See Blink_AnalogRead example.
*/
void TaskBlink(void *pvParameters)
{
(void) pvParameters;
pinMode(blink_pin, OUTPUT);
for (;;)
{
digitalWrite(blink_pin, HIGH);
vTaskDelay( 250 / portTICK_PERIOD_MS );
digitalWrite(blink_pin, LOW);
vTaskDelay( 250 / portTICK_PERIOD_MS );
}
}
@@ -0,0 +1,90 @@
/*
* Example of a Arduino interruption and RTOS Binary Semaphore
* https://www.freertos.org/Embedded-RTOS-Binary-Semaphores.html
*/
// Include Arduino FreeRTOS library
#include <Arduino_FreeRTOS.h>
// Include semaphore supoport
#include <semphr.h>
/*
* Declaring a global variable of type SemaphoreHandle_t
*
*/
SemaphoreHandle_t interruptSemaphore;
// set interrupt pin
#ifdef BTN_BUILTIN
uint8_t int_pin = BTN_BUILTIN;
#elif defined(ARDUINO_ELSOMIK)
uint8_t int_pin = P0_8;
#else
uint8_t int_pin = 2;
#endif
// set led pin
#ifdef LED_BUILTIN
uint8_t blink_pin = LED_BUILTIN;
#elif defined(ARDUINO_ELSOMIK)
uint8_t blink_pin = P0_0;
#else
uint8_t blink_pin = 3;
#endif
void setup() {
// Create task for Arduino led
xTaskCreate(TaskLed, // Task function
"Led", // Task name
128, // Stack size
NULL,
0, // Priority
NULL );
/**
* Create a binary semaphore.
* https://www.freertos.org/xSemaphoreCreateBinary.html
*/
interruptSemaphore = xSemaphoreCreateBinary();
if (interruptSemaphore != NULL) {
// Attach interrupt for Arduino digital pin
attachInterrupt(digitalPinToInterrupt(int_pin), interruptHandler, RISING);
}
}
void loop() {}
void interruptHandler() {
/**
* Give semaphore in the interrupt handler
* https://www.freertos.org/a00124.html
*/
xSemaphoreGiveFromISR(interruptSemaphore, NULL);
}
/*
* Led task.
*/
void TaskLed(void *pvParameters)
{
(void) pvParameters;
pinMode(blink_pin, OUTPUT);
for (;;) {
/**
* Take the semaphore.
* https://www.freertos.org/a00122.html
*/
if (xSemaphoreTake(interruptSemaphore, portMAX_DELAY) == pdPASS) {
digitalWrite(blink_pin, !digitalRead(blink_pin));
}
vTaskDelay(10);
}
}
@@ -0,0 +1,80 @@
/*
Example of a FreeRTOS mutex
https://www.freertos.org/Real-time-embedded-RTOS-mutexes.html
*/
// Include Arduino FreeRTOS library
#include <Arduino_FreeRTOS.h>
// Include mutex support
#include <semphr.h>
/*
Declaring a global variable of type SemaphoreHandle_t
*/
SemaphoreHandle_t mutex;
int globalCount = 0;
int task1Delay = 1250;
int task2Delay = 1000;
void setup() {
Serial.begin(9600);
/**
Create a mutex.
https://www.freertos.org/CreateMutex.html
*/
mutex = xSemaphoreCreateMutex();
if (mutex != NULL) {
Serial.println("Mutex created");
}
/**
Create tasks
*/
xTaskCreate(TaskMutex, // Task function
"Task1", // Task name for humans
128,
&task1Delay, // Task parameter
1, // Task priority
NULL);
xTaskCreate(TaskMutex, "Task2", 128, &task2Delay, 1, NULL);
}
void loop() {}
void TaskMutex(void *pvParameters)
{
TickType_t delayTime = *((TickType_t*)pvParameters); // Use task parameters to define delay
for (;;)
{
/**
Take mutex
https://www.freertos.org/a00122.html
*/
if (xSemaphoreTake(mutex, 10) == pdTRUE)
{
Serial.print(pcTaskGetName(NULL)); // Get task name
Serial.print(", Count read value: ");
Serial.print(globalCount);
globalCount++;
Serial.print(", Updated value: ");
Serial.print(globalCount);
Serial.println();
/**
Give mutex
https://www.freertos.org/a00123.html
*/
xSemaphoreGive(mutex);
}
vTaskDelay(delayTime / portTICK_PERIOD_MS);
}
}
@@ -0,0 +1,61 @@
/**
Example of a Arduino interruption and RTOS Task Notification.
https://www.freertos.org/RTOS_Task_Notification_As_Binary_Semaphore.html
*/
// Include Arduino FreeRTOS library
#include <Arduino_FreeRTOS.h>
/**
Declaring a global TaskHandle for the led task.
*/
TaskHandle_t taskNotificationHandler;
// set interrupt pin
#ifdef BTN_BUILTIN
uint8_t int_pin = BTN_BUILTIN;
#elif defined(ARDUINO_ELSOMIK)
uint8_t int_pin = P0_8;
#else
uint8_t int_pin = 2;
#endif
void setup() {
// Create task for FreeRTOS notification
xTaskCreate(TaskNotification, // Task function
"Notification", // Task name
128, // Stack size
NULL,
3, // Priority
&taskNotificationHandler); // TaskHandle
}
void loop() {
}
/*
Notification task.
*/
void TaskNotification(void *pvParameters)
{
(void) pvParameters;
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(int_pin), digitalPinInterruptHandler, RISING);
for (;;) {
if (ulTaskNotifyTake(pdTRUE, portMAX_DELAY)) {
Serial.println("Notification received");
}
}
}
void digitalPinInterruptHandler() {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
vTaskNotifyGiveFromISR(taskNotificationHandler, &xHigherPriorityTaskWoken);
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
@@ -0,0 +1,149 @@
/*
* Example of a basic FreeRTOS queue
* https://www.freertos.org/Embedded-RTOS-Queues.html
*/
// Include Arduino FreeRTOS library
#include <Arduino_FreeRTOS.h>
// Include queue support
#include <queue.h>
// Define a Structure Array
struct Arduino{
int pin[2];
int ReadValue[2];
};
// set led pin
#ifdef LED_BUILTIN
uint8_t blink_pin = LED_BUILTIN;
#elif defined(ARDUINO_ELSOMIK)
uint8_t blink_pin = P0_0;
#else
uint8_t blink_pin = 2;
#endif
//Function Declaration
void Blink(void *pvParameters);
void POT(void *pvParameters);
void TaskSerial(void *pvParameters);
/*
* Declaring a global variable of type QueueHandle_t
*
*/
QueueHandle_t structArrayQueue;
void setup() {
/**
* Create a queue.
* https://www.freertos.org/a00116.html
*/
structArrayQueue=xQueueCreate(10, // Queue length
sizeof(struct Arduino)); // Queue item size
if(structArrayQueue!=NULL){
xTaskCreate(TaskBlink, // Task function
"Blink", // Task name
128, // Stack size
NULL,
0, // Priority
NULL);
// Create other task that publish data in the queue if it was created.
xTaskCreate(POT, // Task function
"AnalogRead", // Task name
128, // Stack size
NULL,
2, // Priority
NULL);
// Create task that consumes the queue if it was created.
xTaskCreate(TaskSerial, // Task function
"PrintSerial",// A name just for humans
128, // This stack size can be checked & adjusted by reading the Stack Highwater
NULL,
1, // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
NULL);
}
}
void loop() {}
/*
* Blink task.
* See Blink_AnalogRead example.
*/
void TaskBlink(void *pvParameters){
(void) pvParameters;
pinMode(blink_pin,OUTPUT);
for(;;)
{
digitalWrite(blink_pin,HIGH);
vTaskDelay(250/portTICK_PERIOD_MS);
digitalWrite(blink_pin,LOW);
vTaskDelay(250/portTICK_PERIOD_MS);
}
}
/**
* Analog read task for Pin A0 and A1
* Reads an analog input on pin 0 and pin 1
* Send the readed value through the queue.
* See Blink_AnalogRead example.
*/
void POT(void *pvParameters){
(void) pvParameters;
pinMode(A0,INPUT);
pinMode(A1,INPUT);
for (;;){
// Read the input on analog pin 0:
struct Arduino currentVariable;
currentVariable.pin[0]=0;
currentVariable.pin[1]=1;
currentVariable.ReadValue[0]=analogRead(A0);
currentVariable.ReadValue[1]=analogRead(A1);
/**
* Post an item on a queue.
* https://www.freertos.org/a00117.html
*/
xQueueSend(structArrayQueue,&currentVariable,portMAX_DELAY);
// One tick delay (10ms) in between reads for stability
vTaskDelay(1);
}
}
/**
* Serial task.
* Prints the received items from the queue to the serial monitor.
*/
void TaskSerial(void *pvParameters){
(void) pvParameters;
// Init Arduino serial
Serial.begin(9600);
for (;;){
struct Arduino currentVariable;
/**
* Read an item from a queue.
* https://www.freertos.org/a00118.html
*/
if(xQueueReceive(structArrayQueue,&currentVariable,portMAX_DELAY) == pdPASS ){
for(int i=0; i<2; i++){
Serial.print("PIN:");
Serial.println(currentVariable.pin[i]);
Serial.print("value:");
Serial.println(currentVariable.ReadValue[i]);
}
}
vTaskDelay(500/portTICK_PERIOD_MS);
}
}
@@ -0,0 +1,183 @@
/*
* Example of a basic FreeRTOS queue
* https://www.freertos.org/Embedded-RTOS-Queues.html
*/
// Include Arduino FreeRTOS library
#include <Arduino_FreeRTOS.h>
// Include queue support
#include <queue.h>
// Define a struct
struct pinRead {
int pin;
int value;
};
/*
* Declaring a global variable of type QueueHandle_t
*
*/
QueueHandle_t structQueue;
// set led pin
#ifdef LED_BUILTIN
uint8_t blink_pin = LED_BUILTIN;
#elif defined(ARDUINO_ELSOMIK)
uint8_t blink_pin = P0_0;
#else
uint8_t blink_pin = 2;
#endif
void setup() {
/**
* Create a queue.
* https://www.freertos.org/a00116.html
*/
structQueue = xQueueCreate(10, // Queue length
sizeof(struct pinRead) // Queue item size
);
if (structQueue != NULL) {
// Create task that consumes the queue if it was created.
xTaskCreate(TaskSerial, // Task function
"Serial", // A name just for humans
128, // 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);
// Create task that publish data in the queue if it was created.
xTaskCreate(TaskAnalogReadPin0, // Task function
"AnalogReadPin0", // Task name
128, // Stack size
NULL,
1, // Priority
NULL);
// Create other task that publish data in the queue if it was created.
xTaskCreate(TaskAnalogReadPin1, // Task function
"AnalogReadPin1", // Task name
128, // Stack size
NULL,
1, // Priority
NULL);
}
xTaskCreate(TaskBlink, // Task function
"Blink", // Task name
128, // Stack size
NULL,
0, // Priority
NULL );
}
void loop() {}
/**
* Analog read task for Pin A0
* Reads an analog input on pin 0 and send the readed value through the queue.
* See Blink_AnalogRead example.
*/
void TaskAnalogReadPin0(void *pvParameters)
{
(void) pvParameters;
for (;;)
{
// Read the input on analog pin 0:
struct pinRead currentPinRead;
currentPinRead.pin = 0;
currentPinRead.value = analogRead(A0);
/**
* Post an item on a queue.
* https://www.freertos.org/a00117.html
*/
xQueueSend(structQueue, &currentPinRead, portMAX_DELAY);
// One tick delay (10ms) in between reads for stability
vTaskDelay(1);
}
}
/**
* Analog read task for Pin A1
* Reads an analog input on pin 1 and send the readed value through the queue.
* See Blink_AnalogRead example.
*/
void TaskAnalogReadPin1(void *pvParameters)
{
(void) pvParameters;
for (;;)
{
// Read the input on analog pin 1:
struct pinRead currentPinRead;
currentPinRead.pin = 1;
currentPinRead.value = analogRead(A1);
/**
* Post an item on a queue.
* https://www.freertos.org/a00117.html
*/
xQueueSend(structQueue, &currentPinRead, portMAX_DELAY);
// One tick delay (10ms) in between reads for stability
vTaskDelay(1);
}
}
/**
* Serial task.
* Prints the received items from the queue to the serial monitor.
*/
void TaskSerial(void * pvParameters) {
(void) pvParameters;
// Init Arduino serial
Serial.begin(9600);
for (;;)
{
struct pinRead currentPinRead;
/**
* Read an item from a queue.
* https://www.freertos.org/a00118.html
*/
if (xQueueReceive(structQueue, &currentPinRead, portMAX_DELAY) == pdPASS) {
Serial.print("Pin: ");
Serial.print(currentPinRead.pin);
Serial.print(" Value: ");
Serial.println(currentPinRead.value);
}
}
}
/*
* Blink task.
* See Blink_AnalogRead example.
*/
void TaskBlink(void *pvParameters)
{
(void) pvParameters;
pinMode(blink_pin, OUTPUT);
for (;;)
{
digitalWrite(blink_pin, HIGH);
vTaskDelay( 250 / portTICK_PERIOD_MS );
digitalWrite(blink_pin, LOW);
vTaskDelay( 250 / portTICK_PERIOD_MS );
}
}
@@ -0,0 +1,89 @@
#include <Arduino_FreeRTOS.h>
//define task handles
TaskHandle_t TaskBlink_Handler;
TaskHandle_t TaskSerial_Handler;
// define two tasks for Blink & Serial
void TaskBlink( void *pvParameters );
void TaskSerial(void* pvParameters);
// set led pin
#ifdef LED_BUILTIN
uint8_t blink_pin = LED_BUILTIN;
#elif defined(ARDUINO_ELSOMIK)
uint8_t blink_pin = P0_0;
#else
uint8_t blink_pin = 2;
#endif
// the setup function runs once when you press reset or power the board
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// Now set up two tasks to run independently.
xTaskCreate(
TaskBlink,
"Blink", // A name just for humans
128, // This stack size can be checked & adjusted by reading the Stack Highwater
NULL, // Parameters passed to the task function
2, // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
&TaskBlink_Handler); // Task handle
xTaskCreate(
TaskSerial,
"Serial",
128, // Stack size
NULL, // Parameters passed to the task function
1, // Priority
&TaskSerial_Handler); // Task handle
}
void loop()
{
// Empty. Things are done in Tasks.
}
/*--------------------------------------------------*/
/*---------------------- Tasks ---------------------*/
/*--------------------------------------------------*/
void TaskSerial(void* pvParameters){
/*
Serial
Send "s" or "r" through the serial port to control the suspend and resume of the LED light task.
This example code is in the public domain.
*/
(void) pvParameters;
for (;;) // A Task shall never return or exit.
{
while(Serial.available()>0){
switch(Serial.read()){
case 's':
vTaskSuspend(TaskBlink_Handler);
Serial.println("Suspend!");
break;
case 'r':
vTaskResume(TaskBlink_Handler);
Serial.println("Resume!");
break;
}
vTaskDelay(1);
}
}
}
void TaskBlink(void *pvParameters) // This is a task.
{
(void) pvParameters;
pinMode(blink_pin, OUTPUT);
for (;;) // A Task shall never return or exit.
{
digitalWrite(blink_pin, HIGH); // turn the pin on (HIGH is the voltage level)
vTaskDelay(1000 / portTICK_PERIOD_MS); // wait for one second
digitalWrite(blink_pin, LOW); // turn the pin off by making the voltage LOW
vTaskDelay(1000 / portTICK_PERIOD_MS); // wait for one second
}
}
@@ -0,0 +1,155 @@
/*
* Example of FreeRTOS task utilities
* https://www.freertos.org/a00021.html
*/
// Include Arduino FreeRTOS library
#include <Arduino_FreeRTOS.h>
/**
* Task handlers
* https://www.freertos.org/a00019.html#xTaskHandle
*/
TaskHandle_t taskBlinkHandle;
TaskHandle_t taskDeletedHandle;
TaskHandle_t taskBlockedHandle;
// set led pin
#ifdef LED_BUILTIN
uint8_t blink_pin = LED_BUILTIN;
#elif defined(ARDUINO_ELSOMIK)
uint8_t blink_pin = P0_0;
#else
uint8_t blink_pin = 2;
#endif
void setup() {
/**
* Task creation
*/
xTaskCreate(TaskBlink, // Task function
"Blink", // Task name
128, // Stack size
NULL,
0, // Priority
&taskBlinkHandle); // Task handler
xTaskCreate(TaskSerial,
"Serial",
128,
NULL,
2,
NULL);
xTaskCreate(TaskDeleted,
"Deleted",
64,
NULL,
1,
&taskDeletedHandle);
xTaskCreate(TaskBlocked,
"Blocked",
64,
NULL,
1,
&taskBlockedHandle);
}
void loop() {}
/**
* Example of utilities usage
*/
void TaskSerial(void *pvParameters)
{
(void) pvParameters;
Serial.begin(9600);
for (;;)
{
Serial.println("======== Tasks status ========");
Serial.print("Tick count: ");
Serial.print(xTaskGetTickCount());
Serial.print(", Task count: ");
Serial.print(uxTaskGetNumberOfTasks());
Serial.println();
Serial.println();
// Serial task status
Serial.print("- TASK ");
Serial.print(pcTaskGetName(NULL)); // Get task name without handler https://www.freertos.org/a00021.html#pcTaskGetName
Serial.print(", High Watermark: ");
Serial.print(uxTaskGetStackHighWaterMark(NULL)); // https://www.freertos.org/uxTaskGetStackHighWaterMark.html
TaskHandle_t taskSerialHandle = xTaskGetCurrentTaskHandle(); // Get current task handle. https://www.freertos.org/a00021.html#xTaskGetCurrentTaskHandle
Serial.println();
Serial.print("- TASK ");
Serial.print(pcTaskGetName(taskBlinkHandle)); // Get task name with handler
Serial.print(", High Watermark: ");
Serial.print(uxTaskGetStackHighWaterMark(taskBlinkHandle));
Serial.println();
Serial.print("- TASK ");
Serial.print(pcTaskGetName(taskDeletedHandle));
Serial.print(", High Watermark: ");
Serial.print(uxTaskGetStackHighWaterMark(taskDeletedHandle));
Serial.println();
Serial.print("- TASK ");
Serial.print(pcTaskGetName(taskBlockedHandle));
Serial.print(", High Watermark: ");
Serial.print(uxTaskGetStackHighWaterMark(taskBlockedHandle));
Serial.println();
Serial.println();
vTaskDelay( 5000 / portTICK_PERIOD_MS );
}
}
/**
* Blocked tasks when run
*/
void TaskBlocked(void *pvParameters) {
(void) pvParameters;
for (;;)
{
vTaskDelay( 900000 / portTICK_PERIOD_MS );
}
}
/**
* Deleted tasks when run
*/
void TaskDeleted(void *pvParameters) {
(void) pvParameters;
vTaskDelete(NULL);
}
/*
* Blink task.
* See Blink_AnalogRead example.
*/
void TaskBlink(void *pvParameters)
{
(void) pvParameters;
pinMode(blink_pin, OUTPUT);
for (;;)
{
digitalWrite(blink_pin, HIGH);
vTaskDelay( 250 / portTICK_PERIOD_MS );
digitalWrite(blink_pin, LOW);
vTaskDelay( 250 / portTICK_PERIOD_MS );
}
}