Freertos là gì

  • FreeRTOS là gì?
    • Cài đặt thư viện FreeRTOS trên Arduino IDE
  • Cấu trúc chương trình
  • Code ví dụ

FreeRTOS là gì?

FreeRTOS là một hệ điều hành nhúng thời gian thực (Real Time Operating System) mã nguồn mở được phát triển bởi Real Time Engineers Ltd, sáng lập và sở hữu bởi Richard Barry. FreeRTOS được thiết kế phù hợp cho nhiều hệ nhúng nhỏ gọn vì nó chỉ triển khai rất ít các chức năng như: cơ chế quản lý bộ nhớ và tác vụ cơ bản, các hàm API quan trọng cho cơ chế đồng bộ. Nó không cung cấp sẵn các giao tiếp mạng, drivers, hay hệ thống quản lý tệp (file system) như những hệ điều hành nhúng cao cấp khác. Tuy vậy, FreeRTOS có nhiều ưu điểm, hỗ trợ nhiều kiến trúc vi điều khiển khác nhau, kích thước nhỏ gọn (4.3 Kbytes sau khi biên dịch trên Arduino), được viết bằng ngôn ngữ C và có thể sử dụng, phát triển với nhiều trình biên dịch C khác nhau (GCC, OpenWatcom, Keil, IAR, Eclipse, …), cho phép không giới hạn các tác vụ chạy đồng thời, không hạn chế quyền ưu tiên thực thi, khả năng khai thác phần cứng. Ngoài ra, nó cũng cho phép triển khai các cơ chế điều độ giữa các tiến trình như: queues, counting semaphore, mutexes.

Freertos là gì

Freertos là gì

Freertos là gì

Cài đặt thư viện FreeRTOS trên Arduino IDE

Để bắt đầu với FreeRTOS, chúng ta cần cài đặt thư viện của nó trên Arduino IDE đã:

Ở giao diện chính của Aruduino IDE, Sketch Include Library >  Manage Libraries. Search từ khóa FreeRTOS và cài đặt thư viện:

Freertos là gì

Cấu trúc chương trình

#include
// Khai báo nhiệm vụ:
void TaskBlink( 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)
while (!Serial)
; // wait for serial port to connect. Needed for native USB, on LEONARDO, MICRO, YUN, and other 32u4 based boards

// Thiết lập nhiệm vụ để chạy độc lậ
xTaskCreate
Task
, (const portCHAR *)”Task1″ // A name just for human
, 128 // Bộ nhớ RAM để cho tiến trình hoạt động >= 64byt
, NUL
, 2 // Mức độ ưu tiên
, NULL )

xTaskCreate
Task
, (const portCHAR *) “Task2
, 128 // Stack siz
, NUL
, 1 // Priorit
, NULL )

// Now the task scheduler, which takes over control of scheduling individual tasks, is automatically started

}
void loop()
{
// Chương trình chính đã được thực hiện trong các Task nên loop() trống
}
/*————————————————–*/
/*———————- Tasks ———————*/
/*————————————————–*/
void Task1(void *pvParameters) // This is a task.
{
(void) pvParameters
//Code được đặt ở đây sẽ chạy 1 lần giống void setup(
for (;;)

//Code được đặt trong đây sẽ tương đương với hàm void loop(

}
void Task2(void *pvParameters) // This is a task.
{
(void) pvParameters
for (;;)

}

CODE chạy FreeRTOS với LCD1602, cảm biến LM35 và cảm biến ánh sáng LDR: TẠI ĐÂY

Freertos là gì

Code ví dụ

#include 
 
// define three tasks for Blink & LDR & VR
void TaskBlink( void *pvParameters );
void TaskLDR( void *pvParameters );
void TaskVR( 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);
 
  // Now set up two tasks to run independently.
  xTaskCreate(
    TaskBlink
    ,  (const portCHAR *)"Blink"   // A name just for humans
    ,  128  // This stack size can be checked & adjusted by reading the Stack Highwater
    ,  NULL
    ,  3
    ,  NULL );
 
 
  xTaskCreate(
    TaskVR
    ,  (const portCHAR *) "VR"
    ,  128  // Stack size
    ,  NULL
    ,  2  // Priority
    ,  NULL );
 
  xTaskCreate(
    TaskLDR
    ,  (const portCHAR *) "LDR"
    ,  128  // Stack size
    ,  NULL
    ,  1  // Priority
    ,  NULL );
 
}
 
void loop()
{
  // Empty. Things are done in Tasks.
}
 
/*---------------------- Tasks ---------------------*/
 
void TaskBlink(void *pvParameters)  // This is a task.
{
  (void) pvParameters;
 
 
  // initialize digital LED_BUILTIN on pin 13 as an output.
  pinMode(12, OUTPUT);
 
  for (;;) // A Task shall never return or exit.
  {
    digitalWrite(12, LOW);   // turn the LED on
    Serial.println("LED 12 on");
    vTaskDelay( 1000 / portTICK_PERIOD_MS );
    digitalWrite(12, HIGH);    // turn the LED off
    Serial.println("LED 12 off");
    vTaskDelay( 1000 / portTICK_PERIOD_MS );
  }
}
 
void TaskLDR(void *pvParameters)  // This is a task.
{
  (void) pvParameters;
 
 
  for (;;)
  {
    // read the input on analog pin 1:
    int sensorValue = analogRead(A1);
    // print out the value you read:
    Serial.print("LDR value = ");
    Serial.println(sensorValue);
    vTaskDelay(20);  // one tick delay (300ms) in between reads for stability
  }
}
 
void TaskVR(void *pvParameters)  // This is a task.
{
  (void) pvParameters;
 
 
  for (;;)
  {
    // read the input on analog pin 0:
    int sensorValue = analogRead(A0);
    // print out the value you read:
    Serial.print("VR value = ");
    Serial.println(sensorValue);
    vTaskDelay(20);  // one tick delay (300ms) in between reads for stability
  }
}