// -----------------------------------------------------------
/* Código para Arduino Mega 2560
Criação de três tarefas, FreeRTOS
jams, SEM/EESC/USP, 2025 */
#include <Arduino_FreeRTOS.h>
#define TRUE 1
// -----------------------------------------------------------
// Variáveis globais
const int delayTask1 = 2000;
const int delayTask2 = 2000;
const int delayTask3 = 2000;
// -----------------------------------------------------------
// Prototipos das funcoes
void TaskBlink1( void *pvParameters );
void TaskBlink2( void *pvParameters );
void Taskprint( void *pvParameters );
// -----------------------------------------------------------
// Configuração periféricos
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// create tasks:
/*BaseType_t xTaskCreate( TaskFunction_t pvTaskCode,
const char * const pcName,
const configSTACK_DEPTH_TYPE uxStackDepth,
void *pvParameters,
UBaseType_t uxPriority,
TaskHandle_t *pxCreatedTask
);
*/
xTaskCreate(
TaskBlink1, // Ponteiro para a tarefa
"task1", // Nome da tarefa no SO
128, // Quantidade de palavras (do tipo size_t) para alocar a tarefa
NULL, // Parâmetros da função
1, // Prioridade da função
NULL ); // Parâmetro utilizado para passar manipuladores
xTaskCreate(
TaskBlink2,
"task2",
128,
NULL,
2,
NULL );
xTaskCreate(
Taskprint,
"task3",
128,
NULL,
3,
NULL );
// call RTOS scheduler
vTaskStartScheduler();
}
// -----------------------------------------------------------
// Loop infinito (não é executado)
void loop(){
// code shouldn't reach this point
}
// -----------------------------------------------------------
// Tarefa 01
void TaskBlink1( void *pvParameters ){
// Configuração GPIO etc.:
pinMode( 13, OUTPUT );
// Loop infinito da Task1:
while( TRUE ){
Serial.println( "Task1" );
digitalWrite( 13, HIGH );
vTaskDelay( delayTask1 / portTICK_PERIOD_MS );
digitalWrite( 13, LOW );
vTaskDelay( delayTask1 / portTICK_PERIOD_MS );
}
}
// -----------------------------------------------------------
// Tarefa 02
void TaskBlink2( void *pvParameters ){
// Configuração GPIO etc.:
pinMode( 7, OUTPUT );
// Loop infinito da Task2:
while( TRUE ){
Serial.println( "Task2" );
digitalWrite( 7, HIGH );
vTaskDelay( delayTask2 / portTICK_PERIOD_MS );
digitalWrite( 7, LOW );
vTaskDelay( delayTask2 / portTICK_PERIOD_MS );
}
}
// -----------------------------------------------------------
// Tarefa 03
void Taskprint( void *pvParameters ){
// Configuração GPIO etc.:
int counter = 0;
// Loop infinito da Task3:
while( TRUE ){
counter++;
Serial.println( "Task3" );
// Serial.println( counter );
vTaskDelay( delayTask3 / portTICK_PERIOD_MS );
}
}