#if CONFIG_FREERTOS_UNICORE
static const BaseType_t cpu_app = 0;
#else
static const BaseType_t cpu_app = 1;
#endif
// Task
void startTask (void *parameter) {
while (1) {
int a = 1;
int b[100]; // int (4 bytes)
for (int x=0;x<100;x++) {
b[x] = a + 1;
}
Serial.println(b[0]);
// print out remaining stack memory (words)
Serial.print("High water mark (words): ");
Serial.println(uxTaskGetStackHighWaterMark(NULL));
// print out number of free heap memory bytes before malloc
Serial.print("Heap before malloc (bytes): ");
Serial.println(xPortGetFreeHeapSize());
int *ptr = (int*)pvPortMalloc(1024 * sizeof(int));
// do something with memory so it's not optimized out by the compiler
if (ptr == NULL) {
Serial.println("Not enough heap.");
} else {
for (int x=0;x<1024;x++) {
ptr[x] = 3;
}
}
// print out number of free heap memory bytes after malloc
Serial.print("Heap after malloc (bytes): ");
Serial.println(xPortGetFreeHeapSize());
vPortFree(ptr);
vTaskDelay(100 / portTICK_PERIOD_MS);
}
}
void setup () {
Serial.begin(115200);
vTaskDelay(1000 / portTICK_PERIOD_MS);
Serial.println("\n---FreeRTOS Memory Demo---");
// start permanent loop task
xTaskCreatePinnedToCore(startTask, "task0", 1400, NULL, 1, NULL, cpu_app);
// delete "setup and loop"
vTaskDelete(NULL);
}
void loop () {
}