If you use the audio library, it provides CPU usage info.
https://www.pjrc.com/teensy/td_libs_AudioProcessorUsage.html
If you wish to build this for other code, you can measure elapsed time by reading the ARM DWT cycle counter at the beginning and end of doing some work and then compute how many cycles were taken.
An alternate approach to directly measuring is to find the place in your program where you expect it to run when no other work is happening. You can add code there to perform some work like incrementing a number and then periodically check how much it has increased. Or you can output a signal on a pin using digitalToggle(pin) and watch it with an oscilloscope or logic analyzer to estimate how much of the time it's rapidly toggling versus "stuck" high or low while other parts of your program run.
// RAM FREE
#define LOG 0
#define LIN 1
#define SCALE LIN
static IntervalTimer cpu;
uint32_t Idle_Counter;
uint8_t CPU_Utilization_Info_Read_To_Compute;
uint32_t Prev_Idle_Counter;
uint32_t Idle_Counts;
uint32_t Calculate_Idle_Counts (void);
extern unsigned long _heap_start;
extern unsigned long _heap_end;
extern char *__brkval;
extern "C" char* sbrk(int incr);
int freeRam() {
// char top;
// return &top - reinterpret_cast<char*>(sbrk(0));
return (char *)&_heap_end - __brkval;
}
void setup() {
delay(1000);
// initialize the serial communication at max speed:
Serial.begin(250000);
Serial.println("started");
cpu.begin(Update_Task_Ready_Flags, 1000);
}
uint32_t Read_Idle_Counts(void) {
return Idle_Counts;
}
uint32_t Calculate_CPU_Utilization (uint32_t temp_counts) {
return 100 - ((100 * temp_counts) / 13600000);
}
uint32_t Calculate_Idle_Counts (void) {
Idle_Counts = Idle_Counter - Prev_Idle_Counter;
Prev_Idle_Counter = Idle_Counter;
return Idle_Counts;
}
bool One_MS_Task_Ready;
bool Ten_MS_Task_Ready;
bool One_Hundred_MS_Task_Ready;
bool One_S_Task_Ready;
inline void One_MS_Task(void) {
for (int i = 0; i < random(1000, 6000); i++) {
Serial.print(".");
} Serial.println();
delay(100);
}
inline void Ten_MS_Task(void) {
// uncomment line below to put in some cpu load :)
// delay(8);
}
inline void One_Hundred_MS_Task(void) {}
inline void One_S_Task(void) {
// Serial.print(F("One_S_Task: "));
uint32_t idleCounts = Calculate_Idle_Counts();
#if SCALE == (LOG)
uint8_t percent1 = (Calculate_CPU_Utilization(idleCounts)*2)/2;
// Serial.print(F(" percent1: ")); Serial.print(percent1);
uint8_t percent2 = (Calculate_CPU_Utilization(idleCounts)*2)/2;
// Serial.print(F(" percent2: ")); Serial.print(percent2);
uint8_t percent = (percent1*percent2)/100;
// Serial.print(F(" percent: ")); Serial.print(percent);
#elif SCALE == (LIN)
uint8_t percent = Calculate_CPU_Utilization(idleCounts);
// Serial.print(F(" percent: ")); Serial.print(percent);
#endif
// output percent to serial monitor
Serial.print(F("CPU usage: "));
Serial.print(100 - percent);
Serial.println(F("%"));
Serial.print(F("Free RAM: "));
Serial.println(freeRam());
}
inline void Run_Tasks(void) {
if(One_MS_Task_Ready) {
One_MS_Task_Ready = 0;
One_MS_Task();
}
if(Ten_MS_Task_Ready) {
Ten_MS_Task_Ready = 0;
Ten_MS_Task();
}
if(One_Hundred_MS_Task_Ready) {
One_Hundred_MS_Task_Ready = 0;
One_Hundred_MS_Task();
}
if(One_S_Task_Ready) {
One_S_Task_Ready = 0;
One_S_Task();
}
}
void loop() {
Idle_Counter++;
Run_Tasks();
delay(1000);
}
/* WARNING this function called from ISR */
void Update_Task_Ready_Flags(void) {
static uint32_t counter;
One_MS_Task_Ready = 1;
counter++;
if((counter%10) == 0) Ten_MS_Task_Ready = 1;
if((counter%100) == 0) One_Hundred_MS_Task_Ready = 1;
if(counter == 1000) {
One_S_Task_Ready = 1;
counter = 0;
}
}
Maybe that message from @Ñeñe_Kinich is from ChatGPT or a similar AI bot?
Thanks Ñeñe_Kinich!
#include <ArduinoJson.h>
DynamicJsonDocument doc(370000); // MAX: RAM2(Free)
JsonArray myArray = doc.to<JsonArray>(); // volatile unsigned
int ELEMENT_COUNT_MAX = 0; // 10/100/1000/5000/10000
int timeStart = 0;
int cycles = 0;
extern unsigned long _heap_end;
extern char *__brkval;
extern "C" char* sbrk(int incr);
IntervalTimer bufferCanSendObj;
int bufferCanSendSpeed = 1000;
int long counterSendingCan = 0;
String last_CANID_from_serial = "7E9";
void setup() {
bufferCanSendObj.begin(sendMsgsBufferCan, bufferCanSendSpeed); // run every 1 millisecond
createMsgsTest();
}
void loop() {
if (timeStart == 0 && myArray.size()>0) timeStart = micros(), Serial.print(" | Start:");
if (myArray.size() == 0 && timeStart != 0) {
int _timeEnd = micros();
int timeEnd = _timeEnd - timeStart;
Serial.print("End"); // Serial.print(_timeEnd);
// Serial.print(" | cycles: "); Serial.print(cycles);
Serial.print(" | "); Serial.print(timeEnd); Serial.print(" micros");
// Serial.print(" | memoryUsage Obj: "); Serial.print(myArray.memoryUsage()); // 10 on AVR
Serial.print(" | memoryUsage Doc: "); Serial.print(doc.memoryUsage()); // 16 on AVR
Serial.print(" | memoryUsage Free: "); Serial.println(freeRam());
delay(10);
createMsgsTest();
}
}
void sendMsgsBufferCan() {
if (timeStart > 0 && myArray.size()>0) {
// Serial.println("proccesing index 0");
myArray.remove(0);
}
}
void readMsgs() {
cycles++;
clearMsgs();
/*
* NO USAR == ERROR
* for (int i = 0; i < myArray.size(); i++) {
* // String msg = myArray[i];
* myArray.erase(myArray.begin()+i); // myArray.begin()
* }
*/
}
void clearMsgs() {
if (myArray.size() > 0) {
// Metro serialMetro = Metro(250); // Instantiate an instance
// if (serialMetro.check() == 1)
myArray.remove(0), clearMsgs();
}
// Serial.print(".");
}
void createMsgsTest() {
// 10/100/1000/5000/10000
if (ELEMENT_COUNT_MAX == 0 || ELEMENT_COUNT_MAX == 10000) ELEMENT_COUNT_MAX = 10;
else if (ELEMENT_COUNT_MAX == 10) ELEMENT_COUNT_MAX = 100;
else if (ELEMENT_COUNT_MAX == 100) ELEMENT_COUNT_MAX = 1000;
else if (ELEMENT_COUNT_MAX == 1000) ELEMENT_COUNT_MAX = 5000;
else if (ELEMENT_COUNT_MAX == 5000) ELEMENT_COUNT_MAX = 10000;
else ELEMENT_COUNT_MAX = 0;
doc.clear();
myArray = doc.to<JsonArray>();
Serial.print("Loading... ");
timeStart = 0;
cycles = 0;
for (int i = 0; i < ELEMENT_COUNT_MAX; i++) {
char Byte0[15]; sprintf(Byte0, "%06X", int(i+1));
myArray.add("7E180123456789" + String(Byte0));
}
Serial.print("Total Msgs: "); Serial.print(myArray.size());
delay(3000);
}
float freeRam() {
return (char *)&_heap_end - __brkval;
}