01 Basic Example of FreeRTOS with Arduino

 FreeRTOS: 

FreeRTOS is a real-time operating system kernel for embedded device. It is distributed under the MIR license. In this tutorial, we will start with FreeRTOS. FreeRTOS is a class of RTOS for embedded devices which is small enough to be run on 8/16 bit microcontrollers. It is completely open source. If we know some basic concepts of FreeRTOS then it is very easy to use because FreeRTOS is well documented APIs which can be directly used in the code without knowing the backend part of the coding. It can also be run on Arduino Uno board. We have to just download the FreeRTOS library  and install then start implementing the code using APIs.

Install FreeRTOS Library:

1. Open Arduino IDE and go to Sketch -> Include Library -> Manage Libraries. Search for FreeRTOS and install the library as shown below.


Schematic Diagram:

This is schematic diagram, I am using one Arduino Nano, two LEDs and two 220 Ohms resistor. You can see in schematic diagram Anode(Positive) pin of LED is connected  7th and 8th digital pin and resistor is connected in series between Led's Anode pin and Arduino digital pin. Cathode pin of Leds are common of both Leds and connected to GND pin of Arduino Nano.

Let us Code:

first of all  we have to include FreeRTOS library. then define Pin, Here i am using 7th digital pin of Arduino nano as a redLed and 8th digital pin as a greenLed. 
 #include <Arduino_FreeRTOS.h>  
#define redLed 7
#define greenLed 8
This is void setup  function it will run only one time. Here we have to create task in void setup function.

BaseType_t xTaskCreate( TaskFunction_t pvTaskCode, const char * const pcName, configSTACK_DEPTH_TYPE usStackDepth, void *pvParameters, UBaseType_t uxPriority, TaskHandle_t *pxCreatedTask );
pvTaskCode:  It is simply a pointer to the function that implements the task (Just the name of the function).
pcName:  It is a descriptive name for the task. This is not used by FreeRTOS. It is included purely for debugging purpose. It can also be used to obtain the task handle.
usStackDepth:  Each task has its own unique stack that is allocated by the kernel to the task when the task is created. The value specifies the number of words  the stack can hold, not the number of bytes. For example, if the stack is 32-bits wide and usStackDepth is passed in as 100, then 400 bytes of stack space willl be allocated (100 *4 bytes) in RAM.
uxPriority:  It is priority of the task (0 is  the lowest priority).
pxCreatedTask:  Used to pass a handle to the created task out of the xTaskCreated() function. It is a optional and can be set to NULL.
 void setup() {  
xTaskCreate(redLedTask, "red Led Task", 128, NULL, 1, NULL);
xTaskCreate(greenLedTask, "green Led Task", 128, NULL, 1, NULL);
}
This the "greenLedTask"  task function which is called in void setup function. In this task Green Led will  blink with 200ms delay. Its priority is 1 means high.
  void greenLedTask(void *pvParameters){  
pinMode(greenLed, OUTPUT);
while(1){
digitalWrite(greenLed, HIGH);
delay(200);
digitalWrite(greenLed, LOW);
delay(200);
}
}
This is "redLedTask" task function. It is called in void setup function and its priority is high. In this task Red Led will blink with 500ms.
 void redLedTask(void *pvParameters){  
pinMode(redLed, OUTPUT);
while(1){
digitalWrite(redLed, HIGH);
delay(500);
digitalWrite(redLed, LOW);
delay(500);
}
}
This is the final code you can download from here 

Code
 #include <Arduino_FreeRTOS.h>  
#define redLed 7
#define greenLed 8
void setup() {
xTaskCreate(redLedTask, "red Led Task", 128, NULL, 1, NULL);
xTaskCreate(greenLedTask, "green Led Task", 128, NULL, 1, NULL);
}
void loop() {}
void redLedTask(void *pvParameters){
pinMode(redLed, OUTPUT);
while(1){
digitalWrite(redLed, HIGH);
delay(500);
digitalWrite(redLed, LOW);
delay(500);
}
}
void greenLedTask(void *pvParameters){
pinMode(greenLed, OUTPUT);
while(1){
digitalWrite(greenLed, HIGH);
delay(200);
digitalWrite(greenLed, LOW);
delay(200);
}
}

Click here to watch video for more details

Thanks for visiting💖 🙏
Please Donate to help me and afford new equipment & components to make more videos and blogs.
Please Donate us

No comments:

Powered by Blogger.