Skip to content

Commit 182ca4b

Browse files
committed
Worked on logging, made task setup safer
1 parent 4ca4e3e commit 182ca4b

1 file changed

Lines changed: 46 additions & 8 deletions

File tree

src/main.cpp

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,15 @@ void setup() {
202202
// Set up tasks
203203
static bool tasksSetup = false;
204204
if (!tasksSetup) {
205+
ESP_LOGI(TAG, "Setting up tasks...");
205206
for (TaskInfo &taskDesc : load.mainTaskDescriptions) {
206207
if (taskDesc.stackSize_bytes % sizeof(uint_fast8_t) != 0) {
207208
ESP_LOGW(TAG, "Stack size not word aligned");
208209
}
210+
if (taskDesc.function == nullptr) {
211+
ESP_LOGE(TAG, "Caught null task description!");
212+
continue;
213+
}
209214
// Syntax: xTaskCreate(Task function, Name of the task (for
210215
// debugging), Stack size (in words, not bytes), Task input
211216
// parameter, Priority of the task, Task handle)
@@ -215,7 +220,7 @@ void setup() {
215220
if (result != pdPASS) {
216221
ESP_LOGE(TAG, "Failed to create task %s", taskDesc.name);
217222
} else {
218-
ESP_LOGV(
223+
ESP_LOGD(
219224
TAG,
220225
"Created task %s with priority %u and stack size %u bytes",
221226
taskDesc.name, taskDesc.priority, taskDesc.stackSize_bytes);
@@ -461,8 +466,7 @@ constexpr uint32_t LOG_ITEM_INTERVAL_MS =
461466

462467
ESP_LOGD(TAG, "Logging Data:");
463468

464-
ESP_LOGI(TAG, "Num tasks reported by FreeRTOS: %u",
465-
uxTaskGetNumberOfTasks());
469+
ESP_LOGI(TAG, "FreeRTOS Tasks: %u", uxTaskGetNumberOfTasks());
466470

467471
constexpr uint_fast8_t REC_BYTES_PER_TASK = 40;
468472
constexpr uint_fast8_t NUM_ESP_TASKS =
@@ -477,13 +481,25 @@ constexpr uint32_t LOG_ITEM_INTERVAL_MS =
477481
"Number of tasks (%d) exceeds expected max (%d), skipping to "
478482
"prevent memory corruption",
479483
uxTaskGetNumberOfTasks(), NUM_MAIN_TASKS + NUM_ESP_TASKS);
484+
delay(LOG_ITEM_INTERVAL_MS);
480485
} else {
486+
// } else if (statsBuffer[0] == '\0') {
487+
// Refresh stats buffer
488+
481489
// TODO: Not recommended in production
482490
vTaskGetRunTimeStats(statsBuffer);
483491
statsBuffer[STATS_BUFFER_SIZE - 1] =
484492
'\0'; // hard cap, avoid over-read
485493
// uxTaskGetSystemState();
494+
size_t usedBytes = strnlen(statsBuffer, STATS_BUFFER_SIZE);
495+
ESP_LOGD(TAG, "Task Buffer Used (%): %d",
496+
usedBytes * 100 / sizeof(statsBuffer));
497+
ESP_LOGD(TAG, "Stats Buffer Used: %d bytes", usedBytes);
498+
ESP_LOGD(TAG, "Stats Buffer Free: %d bytes",
499+
sizeof(statsBuffer) - usedBytes);
500+
ESP_LOGD(TAG, "Stats Buffer Size: %d", sizeof(statsBuffer));
486501
ESP_LOGI(TAG, "Task Run Time Stats:\n%s", statsBuffer);
502+
Serial.flush();
487503
delay(LOG_ITEM_INTERVAL_MS);
488504

489505
for (TaskInfo &taskDesc : load.mainTaskDescriptions) {
@@ -493,11 +509,33 @@ constexpr uint32_t LOG_ITEM_INTERVAL_MS =
493509
taskDesc.stackSize_bytes - taskDesc.minFreeStack_Bytes,
494510
taskDesc.minFreeStack_Bytes);
495511
}
512+
Serial.flush();
513+
delay(LOG_ITEM_INTERVAL_MS);
496514
}
497-
delay(LOG_ITEM_INTERVAL_MS);
515+
// else {
516+
// // Print buffer
517+
// char *saveptr = nullptr;
518+
// char *line = strtok_r(statsBuffer, "\n", &saveptr);
519+
// delay(LOG_ITEM_INTERVAL_MS);
520+
// ESP_LOGI(TAG, "Task Info: %s:", line);
521+
// while (line != nullptr) {
522+
// line = strtok_r(nullptr, "\n", &saveptr);
523+
// ESP_LOGI(TAG, "Task Info: %s:", line);
524+
// }
525+
// delay(LOG_ITEM_INTERVAL_MS);
526+
527+
// for (TaskInfo &taskDesc : load.mainTaskDescriptions) {
528+
// taskDesc.minFreeStack_Bytes =
529+
// uxTaskGetStackHighWaterMark(taskDesc.pxHandle);
530+
// ESP_LOGI(TAG, "T: %s, U: %u, F: %u", taskDesc.name,
531+
// taskDesc.stackSize_bytes -
532+
// taskDesc.minFreeStack_Bytes,
533+
// taskDesc.minFreeStack_Bytes);
534+
// }
535+
// delay(LOG_ITEM_INTERVAL_MS);
536+
// }
498537

499-
ESP_LOGI(TAG, "Minimum free heap: %u bytes",
500-
esp_get_minimum_free_heap_size());
538+
ESP_LOGI(TAG, "Min, free heap: %u b", esp_get_minimum_free_heap_size());
501539
delay(LOG_ITEM_INTERVAL_MS);
502540

503541
// Enable temperature sensor
@@ -510,9 +548,9 @@ constexpr uint32_t LOG_ITEM_INTERVAL_MS =
510548
constexpr int32_t MAX_EXT_TEMP = 105;
511549
constexpr int32_t MIN_EXT_TEMP = -40;
512550
if (tempTrunc_C > MAX_EXT_TEMP || tempTrunc_C < MIN_EXT_TEMP) {
513-
ESP_LOGE(TAG, "Temperature out of bounds: %d dC", tempTrunc_C);
551+
ESP_LOGE(TAG, "Temp. out of bounds: %d dC", tempTrunc_C);
514552
} else {
515-
ESP_LOGI(TAG, "Temperature: %d dC", tempTrunc_C);
553+
ESP_LOGI(TAG, "Temp.: %d dC", tempTrunc_C);
516554
}
517555
// Disable the temperature sensor if it is not needed and save the power
518556
ESP_ERROR_CHECK(temperature_sensor_disable(tempSensHandle));

0 commit comments

Comments
 (0)