Library 60- CPU load monitor for STM32F4xx devices
Yesterday, I’ve made a post about monitoring CPU load depending on CPU work and it’s sleep time using DWT counter. This post can be found here.
This post is now a library, set together from post about CPU load.
Library
Features
- 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
Dependencies
- CMSIS
- STM32F4xx
- STM32F4xx PWR
- STM32F4xx
- TM
- defines.h
- TM LOWPOWER
- TM GENERAL
- defines.h
Functions and enumerations
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
/** * @defgroup TM_CPULOAD_Typedefs * @brief Library Typedefs * @{ */ /** * @brief CPU LOAD structure */ typedef struct { uint32_t WCNT; /*!< Number of working cycles in one period */ uint32_t SCNT; /*!< Number of sleeping cycles in one period */ float Load; /*!< CPU load percentage */ uint8_t Updated; /*!< Set to 1 when new CPU load is calculated */ } TM_CPULOAD_t; /** * @} */ /** * @defgroup TM_CPULOAD_Functions * @brief Library Functions * @{ */ /** * @brief Initializes DWT functionality for sleep counter and prepares everything for CPU LOAD measurement * @param *CPU_Load: Pointer to empty @ref TM_CPULOAD_t structure * @retval Returns DWT counter status: * - 0: DWT counter has not started, measurement will fail * - > 0: DWT counter has started, everything OK */ uint8_t TM_CPULOAD_Init(TM_CPULOAD_t* CPU_Load); /** * @brief Goes to low power mode and measures sleeping time and working time using DWT cycle counter * @note You need wake up source, otherwise you might stay forever inside low power mode * @param *CPU_Load: Pointer to @ref TM_CPULOAD_t structure * @param PowerMode: Select power mode you want to use for measure CPU load. Valid parameters are: * - TM_LOWPOWERMODE_SleepUntilInterrupt: Go to sleep mode with __WFI() instruction * - TM_LOWPOWERMODE_SleepUntilEvent: Go to sleep mode with __WFE() instruction * @retval CPU load value updated status. * - 0: CPU load value is not updated, still old results * - > 0: CPU load value is updated */ uint8_t TM_CPULOAD_GoToSleepMode(TM_CPULOAD_t* CPU_Load, TM_LOWPOWERMODE_t PowerMode); /** * @} */ |
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
/** * Keil project for CPU load monitor * * Before you start, select your target, on the right of the "Load" button * * @author Tilen Majerle * @email tilen@majerle.eu * @website http://stm32f4-discovery.net * @ide Keil uVision 5 * @conf PLL parameters are set in "Options for Target" -> "C/C++" -> "Defines" * @packs STM32F4xx Keil packs version 2.4.0 or greater required * @stdperiph STM32F4xx Standard peripheral drivers version 1.5.0 or greater required */ /* Include core modules */ #include "stm32f4xx.h" /* Include my libraries here */ #include "defines.h" #include "tm_stm32f4_delay.h" #include "tm_stm32f4_disco.h" #include "tm_stm32f4_cpu_load.h" #include "tm_stm32f4_usart.h" #include "stdio.h" int main(void) { __IO uint32_t i; /* CPU load structure */ TM_CPULOAD_t CPU_LOAD; /* Init CPU load monitor */ TM_CPULOAD_Init(&CPU_LOAD); /* Initialize system */ SystemInit(); /* Initialize delay */ TM_DELAY_Init(); /* Initialize leds on board */ TM_DISCO_LedInit(); /* Init button */ TM_DISCO_ButtonInit(); /* Init USART2, TX: PA2, RX: PA3, 921600 bauds */ TM_USART_Init(USART2, TM_USART_PinsPack_1, 921600); while (1) { /* Check if CPU LOAD variable is updated */ if (CPU_LOAD.Updated) { /* Print to user */ printf("W: %u; S: %u; Load: %5.2f\n", CPU_LOAD.WCNT, CPU_LOAD.SCNT, CPU_LOAD.Load); } /* Toggle leds */ TM_DISCO_LedToggle(LED_ALL); /* If button pressed, do some useless counting */ if (TM_DISCO_ButtonPressed()) { /* Count something to waste some time before entering to sleep mode */ i = 0; while (i++ < 0x1FFF); } /* Go low power mode, sleep mode until interrupt, measure CPU load */ TM_CPULOAD_GoToSleepMode(&CPU_LOAD, TM_LOWPOWERMODE_SleepUntilInterrupt); } } /* Handle printf functionality */ int fputc(int ch, FILE* fil) { /* Send character over USART */ TM_USART_Putc(USART2, ch); /* Return character */ return ch; } |
Project is available on Github, download library below.
Recent comments