Skip to content

Commit b44f89c

Browse files
authored
Merge pull request #225 from LeeLeahy2/idle-count
Add CPU idle count tasks
2 parents a4bd781 + 74ce610 commit b44f89c

File tree

5 files changed

+107
-1
lines changed

5 files changed

+107
-1
lines changed

Firmware/RTK_Surveyor/Begin.ino

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,3 +787,22 @@ void beginLBand()
787787

788788
online.lband = true;
789789
}
790+
791+
void beginIdleTasks()
792+
{
793+
char taskName[32];
794+
795+
for (int index = 0; index < MAX_CPU_CORES; index++)
796+
{
797+
sprintf(taskName, "IdleTask%d", index);
798+
if (idleTaskHandle[index] == NULL)
799+
xTaskCreatePinnedToCore(
800+
idleTask,
801+
taskName, //Just for humans
802+
2000, //Stack Size
803+
NULL, //Task input parameter
804+
0, // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest
805+
&idleTaskHandle[index], //Task handle
806+
index); //Core where task should run, 0=core, 1=Arduino
807+
}
808+
}

Firmware/RTK_Surveyor/RTK_Surveyor.ino

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ const int FIRMWARE_VERSION_MINOR = 3;
4141

4242
#include "settings.h"
4343

44+
#define MAX_CPU_CORES 2
45+
#define IDLE_COUNT_PER_SECOND 196289
46+
#define IDLE_TIME_DISPLAY_SECONDS 5
47+
#define MAX_IDLE_TIME_COUNT (IDLE_TIME_DISPLAY_SECONDS * IDLE_COUNT_PER_SECOND)
48+
4449
//Hardware connections
4550
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
4651
//These pins are set in beginBoard()
@@ -417,6 +422,10 @@ unsigned long systemTestDisplayTime = 0; //Timestamp for swapping the graphic du
417422
uint8_t systemTestDisplayNumber = 0; //Tracks which test screen we're looking at
418423
unsigned long rtcWaitTime = 0; //At poweron, we give the RTC a few seconds to update during PointPerfect Key checking
419424

425+
uint32_t cpuIdleCount[MAX_CPU_CORES];
426+
TaskHandle_t idleTaskHandle[MAX_CPU_CORES];
427+
uint32_t cpuLastIdleDisplayTime;
428+
420429
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
421430

422431
void setup()
@@ -426,6 +435,11 @@ void setup()
426435
Wire.begin(); //Start I2C on core 1
427436
//Wire.setClock(400000);
428437

438+
//Initialize the CPU idle time counts
439+
for (int index = 0; index < MAX_CPU_CORES; index++)
440+
cpuIdleCount[index] = 0;
441+
cpuLastIdleDisplayTime = millis();
442+
429443
beginDisplay(); //Start display first to be able to display any errors
430444

431445
beginGNSS(); //Connect to GNSS to get module type
@@ -463,10 +477,15 @@ void setup()
463477
log_d("Boot time: %d", millis());
464478

465479
danceLEDs(); //Turn on LEDs like a car dashboard
480+
481+
beginIdleTasks();
466482
}
467483

468484
void loop()
469485
{
486+
uint32_t delayTime;
487+
488+
470489
if (online.gnss == true)
471490
{
472491
i2cGNSS.checkUblox(); //Regularly poll to get latest data and any RTCM
@@ -501,7 +520,40 @@ void loop()
501520
//Convert current system time to minutes. This is used in F9PSerialReadTask()/updateLogs() to see if we are within max log window.
502521
systemTime_minutes = millis() / 1000L / 60;
503522

504-
delay(10); //A small delay prevents panic if no other I2C or functions are called
523+
//Display the CPU idle time
524+
if (settings.enablePrintIdleTime)
525+
printIdleTimes();
526+
527+
//A small delay prevents panic if no other I2C or functions are called
528+
delay(10);
529+
}
530+
531+
//Print the CPU idle times
532+
void printIdleTimes()
533+
{
534+
uint32_t idleCount[MAX_CPU_CORES];
535+
int index;
536+
537+
//Determine if it is time to print the CPU idle times
538+
if ((millis() - cpuLastIdleDisplayTime) >= (IDLE_TIME_DISPLAY_SECONDS * 1000))
539+
{
540+
//Get the idle times
541+
cpuLastIdleDisplayTime = millis();
542+
for (index = 0; index < MAX_CPU_CORES; index++)
543+
{
544+
idleCount[index] = cpuIdleCount[index];
545+
cpuIdleCount[index] = 0;
546+
}
547+
548+
//Display the idle times
549+
for (int index = 0; index < MAX_CPU_CORES; index++)
550+
Serial.printf("CPU %d idle time: %d%% (%d/%d)\r\n", index,
551+
idleCount[index] * 100 / MAX_IDLE_TIME_COUNT,
552+
idleCount[index], MAX_IDLE_TIME_COUNT);
553+
554+
//Print the task count
555+
Serial.printf("%d Tasks\r\n", uxTaskGetNumberOfTasks());
556+
}
505557
}
506558

507559
//Create or close files as needed (startup or as user changes settings)

Firmware/RTK_Surveyor/Tasks.ino

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,3 +438,30 @@ void ButtonCheckTask(void *e)
438438
taskYIELD();
439439
}
440440
}
441+
442+
void idleTask(void *e)
443+
{
444+
uint32_t lastStackPrintTime;
445+
446+
while (1)
447+
{
448+
if (settings.enablePrintIdleTime)
449+
{
450+
//Increment a count during the idle time
451+
cpuIdleCount[xPortGetCoreID()]++;
452+
453+
//Let other same priority tasks run
454+
yield();
455+
}
456+
else
457+
delay(1000);
458+
459+
//Display the high water mark if requested
460+
if ((settings.enableTaskReports == true) && ((millis() - lastStackPrintTime) >= 1000))
461+
{
462+
lastStackPrintTime = millis();
463+
Serial.printf("idleTask %d High watermark: %d\n\r",
464+
xPortGetCoreID(), uxTaskGetStackHighWaterMark(NULL));
465+
}
466+
}
467+
}

Firmware/RTK_Surveyor/menuSystem.ino

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,9 @@ void menuDebug()
301301
Serial.print(F("16) Periodically print position: "));
302302
Serial.printf("%s\r\n", settings.enablePrintPosition ? "Enabled" : "Disabled");
303303

304+
Serial.print(F("17) Periodically print CPU idle time: "));
305+
Serial.printf("%s\r\n", settings.enablePrintIdleTime ? "Enabled" : "Disabled");
306+
304307
Serial.println(F("t) Enter Test Screen"));
305308

306309
Serial.println(F("e) Erase LittleFS"));
@@ -429,6 +432,10 @@ void menuDebug()
429432
{
430433
settings.enablePrintPosition ^= 1;
431434
}
435+
else if (incoming == 17)
436+
{
437+
settings.enablePrintIdleTime ^= 1;
438+
}
432439
else
433440
printUnknown(incoming);
434441
}

Firmware/RTK_Surveyor/settings.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,7 @@ typedef struct {
431431
bool enablePrintNtripServerState = false;
432432
bool enablePrintNtripServerRtcm = false;
433433
bool enablePrintPosition = false;
434+
bool enablePrintIdleTime = false;
434435
} Settings;
435436
Settings settings;
436437

0 commit comments

Comments
 (0)