Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

9.

#include <Arduino_FreeRTOS.h>
#include <semphr.h>
SemaphoreHandle_t xCountingSemaphore;

void setup()
{
Serial.begin(9600); // Enable serial communication library.
pinMode(LED_BUILTIN, OUTPUT);

// Create task for Arduino led


xTaskCreate(Task1, // Task function
"Ledon", // Task name
128, // Stack size
NULL,
0 ,// Priority
NULL );
xTaskCreate(Task2, // Task function
"Ledoff", // Task name
128, // Stack size
NULL,
0, // Priority
NULL );
xCountingSemaphore = xSemaphoreCreateCounting(1,1);
xSemaphoreGive(xCountingSemaphore);

}
void loop() {}

void Task1(void *pvParameters)


{
(void) pvParameters;

for (;;)
{
xSemaphoreTake(xCountingSemaphore, portMAX_DELAY);
Serial.println("Inside Task1 and Serial monitor Resource Taken");
digitalWrite(LED_BUILTIN, HIGH);
xSemaphoreGive(xCountingSemaphore);
vTaskDelay(1);
}
}

void Task2(void *pvParameters)


{
(void) pvParameters;
for (;;)
{
xSemaphoreTake(xCountingSemaphore, portMAX_DELAY);
Serial.println("Inside Task2 and Serial monitor Resource Taken");
digitalWrite(LED_BUILTIN, LOW);
xSemaphoreGive(xCountingSemaphore);
vTaskDelay(1);

}
}

9.1 lỗi

#include <Arduino_FreeRTOS.h>

// Use only core 1 for demo purposes


#if CONFIG_FREERTOS_UNICORE
static const BaseType_t app_cpu = 0;
#else
static const BaseType_t app_cpu = 1;
#endif

// Task: Perform some mundane task


void testTask(void *parameter) {
while (1) {
int a = 1;
int b[100];

for (int i = 0; i < 100; i++) {


b[i] = a + 1;
}
Serial.println(b[0]);

Serial.print("High water mark (words): ");


Serial.println(uxTaskGetStackHighWaterMark(NULL));

Serial.print("Heap before malloc (bytes): ");


Serial.println(xPortGetFreeHeapSize());
int ptr = (int)pvPortMalloc(1024 * sizeof(int));

for (int i = 0; i < 1024; i++) {


ptr[i] = 3;
}

Serial.print("Heap after malloc (bytes): ");


Serial.println(xPortGetFreeHeapSize());

vPortFree(ptr);

vTaskDelay(100 / portTICK_PERIOD_MS);
}
}

void setup() {
//Configure Serial
Serial.begin(115200);

//Wait a moment to start (we don't miss Serial output)


vTaskDelay(1000 / portTICK_PERIOD_MS);
Serial.println();
Serial.println("---FreeRTOS Memory Demo---");

// Start the only other task


xTaskCreatePinnedToCore(testTask,
"Test Task",
1500, NULL,
1,
NULL,
app_cpu);

// Delete "setup and loop" task


vTaskDelete(NULL);
}

void loop() {
// Put code here
}

You might also like