Migrate from https://cmakerhk.wordpress.com/2019/03/16/freertos-on-arduino/
https://github.com/feilipu/Arduino_FreeRTOS_Library
#include
#include // add the FreeRTOS functions for Semaphores (or Flags).
//===create task===
void TaskBlink( void *pvParameters );
void TaskAnalogRead( void *pvParameters );
void TaskDigitalRead( void *pvParameters );
void setup(){
//===create task event
xTaskCreate(
TaskBlink
, (const portCHAR *)”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 );
}
void loop(){}
//====everything is in task
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 +5V 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(1); // one tick delay (15ms) in between reads for stability
}
}
===========
#include // 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 only one Task is accessing this resource at any time.
SemaphoreHandle_t xSerialSemaphore;
void setup(){
// 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.
}
}
so… to check
// 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.
xSemaphoreTake( xSerialSemaphore, ( TickType_t ) 5 );
–> will return pdTRUE/pdFALSE
xSemaphoreGive( xSerialSemaphore ); // Now free or “Give” the Serial Port for others.
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.
}
//==end of the example of the arduino freeRTOS
Flow of doing that:
– Task function declaration
– create task in the setup
– write the task function
Generally in FreeRTOS
int main(void){
prvSetupHardwar(); //perform hardware setup
//create task here
//======end=====
vTaskStartScheduler(); /start the created tasked
while(1);
return 0;
}
the naming of the FreeRTOS
c: char, s: short(int16_t), l: long(int32_t), x: BaseType_t
uc: unsigned char(uint8_t), pc: pointer2char
pdTRUE, pdFALSE: 1,0
pdPASS, pdFAIL: 1,0
For Task Management
void