HAL Library 19- CPU LOAD monitor for STM32Fxxx

CPU load monitor library is simple library, which counts number of sleeping cycles using WFI() instruction compared to working cycles in one second.

Load variable is updated every 1 second to determinate how much your processor works. For that case, you have to always put your system to sleep mode when you don’t need it.

It uses DWT counter in Cortex M4/M7 to measure cycle counters!


Read more about new HAL libraries


  • Measures CPU load. Load depends between sleep time and work time of CPU
  • CPU load is updated for every System core clock cycles. In other words, that is each 1 second
  • DWT counter used for measurements
  • Sleep mode which is measured can be wait for interrupt or wait for event


  • HAL
  • TM
    • STM32Fxxx HAL
    • defines.h

Functions and enumerations


Project is available on Github, download library below.

TM STM32 Libraries

STM32 libraries based on STM32Fxxx HAL drivers.


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!
  • Pingback: All STM32 HAL libraries - STM32F4 Discovery()

  • raviteja

    Hi tilz0R,

    I am trying to get CPU load/utilization on STM32F767 Nucleo 144 board, I am using your CPU Load implementation (tm_stm32_cpu_load, tm_stm32_general files alone)and HAL library generated from STM32 cube.

    I have remapped the Disco led to LD2, Button to User Button 2 and Usart6 to Usart1 using HAL from STM32 Cube.
    I am able to flash and run the code but the CPU load always comes around 88% and when the button is pressed, it goes to 99 %, please tell me what could be going wrong ??

    W: 1263737; S: 209832; Load: 85.760292 (Without button press)
    W: 80240036; S: 30744; Load: 99.961693 (Button pressed)


    • Let me explain a little bit. CPU load is very strong word and has in fact only sense if you are using operating system with inter-thread communications where threads wait each other for tasks/events/syncs. CPU load then measures the difference how much time CPU is in user tasks versus how much time is in IDLE task (doing while loop). If there is no OS, then we can do some “test” by calculating how much time CPU has to sleep before next interrupt wakes CPU again.

      This lib is more experimental. In NON-OS mode, CPU Load is always 100% except if in low-power mode.

      • Sahbi Lahmar

        hello Majerle , I’m using an stm32f429I discovery and i want to connect it with an esp8266 wifi module after that i ‘ll connect it to a realtime firebase can you help me with librarys or advance ,i also need to developp a gui

  • Alex

    I wonder why you could not simply do this in your main loop:

    for (;;)
    DWT->CYCCNT = 0;
    cpuLoadPercent = (DWT->CYCCNT * (uint32_t)100) / SystemCoreClock;

    Or am I just measuring how many ms it takes for the CPU to complete one pass through the loop?

    • Like I explained. To measure cpi load, your cpu must have IDLE option mode. Without rtos, you cannot have this option and one solution is to use sleep mode to fake idle time and then interrupt will wakeup mcu once necessary.