this is just a theoretical idea as I haven't done it yet, but PSoC6 has an energy profiler built in. These are a bunch of 32bit timers that can count events and duration of events. Two of those events are the active time of each core.
- count the cores active cycles
- put the core into sleep mode during the idle task
- let another count the constant 1 duration
- both timers have to use the same clock source
you should now get the cpu load by dividing both values.
Attached is a code snipped that does calculate the CPU usage based on the Energy Profiler.
- WDT is generating an interrupt every 1s to wakeup from sleep.
- cpu load is generated with a cyDelay function.
- Brakepoint in line 83 is needed to read out the variables. I did not add a UART to the code.
cpu_free and cpu_load are both calculated as 10th percentile values.
In the example there is an error involved in the result, as the WDT and MCU (CyDelay) use different clock sources and the ILO can have an error of +-10%.
This should not be an issue when used in your RTOS!