CPU load monitor for STM32F4xx

I’ve been searching for a simple CPU load monitor on STM32F4xx device. Today, I’ve managed to get one very, very primitive CPU load monitor for these devices.

Check library here.

STM32F4xx have built-in DWT (Data Watchpoint and Trace unit) which has several timers there. 2 of them are cycle counter and sleep counter. The first counts number of CPU cycles and the second counts number of cycles where CPU was sleeping.

It would be very easy to get CPU load, in case that cycle counter and sleep counter were the same. Then, equation for CPU load would be:

CPU_LOAD [%] = (cycle_counter / (cycle_counter + sleep_counter)) * 100%

And that’s all in a case that you CPU is going to sleep mode every time it does not work on any task. If this is not true then this CPU load method will not work.

As I mentioned above, equation would match in case that timers are the same, but the problem is, that cycle counter 32-bits register and sleep counter is only 8-bits timer. So this timer will overlap at least 100times before you will be able to read anything useful from it. So, with proper method you can still measure sleep time using normal cycle counter.

We have next problem. If we read cycle counter before WFI instruction and after WFI we read register again, do substract, we will also measure a time we were inside interrupt routine which wake up CPU. The workaround of this problem is, that you do these steps:

  • Disable all interrupts
  • Read cycle counter and store it to variable
  • Go to sleep mode using __WFI() or __WFE()
  • Read cycle counter again and do substract ob previous read before sleep
  • Enable interrupts, program will jump to interrupt handler which woke up CPU

Note: Without wake up source, you will not be able to measure anything.


To run example below, I used Nucleo-F401RE board with 84MHz CPU speed:

  • CPU checks inside main if button is pressed
  • If it is pressed, then it does some useless looping to load some CPU
  • It toggles LED
  • It goes to sleep mode
  • Systick is a timer which wakes up timer in periods of 1ms
  • Each second, result is printed via USART

Note: I had some problems with all my boards where ST-Link is installed that after programming, DWT timer did not work. And then I got some really strange results because I had division with zero. In this case, remove power from board and put again. Then it will work.

Driving this examples I got these results:

  • When button was not pressed, CPU load was 0.18%. Everything what CPU needs to do is to checks if button is pressed, toggle led and write some data with printf function
  • When button was pressed, CPU load was 51.37%. This time, CPU did some useless counting which makes him very busy in relation to when button was not pressed.


Running example above, you will need these libraries:


Owner of this site. Also electronic enthusiasts, web developer, 3D printer fan, handball player and more. Big fan of STM32F4 devices. In anticipation of the new Discovery board for STM32F7 lines.

You may also like...

Read before commenting!

Before you make a new comment, make sure you agree with things listed below:

  • - Read post to make sure if it is already posted what you are asking for,
  • - Make sure you have the latest version of libraries used in your project,
  • - Make a clean and grammatically correct written message,
  • - Report as many details as possible, including what have you done so far,
  • - Do NOT post any code here. Use Pastebin,
  • - Do NOT post any error codes here. Use Pastebin,
  • - Specify STM32Fxxx family and used Discovery/EVAL/Nucleo or custom made board,
  • - Make sure your clock is set correct for PLL,
  • - If you are using my HAL drivers, please check this post how to start.
Comment will be deleted on breaking these rules without notification!