software:firmware
MakAir Firmware
cpu_load.cpp
Go to the documentation of this file.
1 
8 #pragma once
9 
10 // INCLUDES ===================================================================
11 
12 // Associated header
13 #include "../includes/cpu_load.h"
14 
15 #include "Arduino.h"
16 
17 // INITIALISATION =============================================================
18 
19 // idleCyclesCount will be incremented in the loop, and read in the systick IRQ
20 // If the systick IRQ set idleCyclesCount to 0, there is no guarantee it returns to 0,
21 // because chances are high to be in the middle of the increment operation, which is not atomic.
22 volatile uint32_t idleCyclesCount = 0;
23 
24 // Computed CPU load in percent
25 volatile uint8_t cpuLoadPercent = 0;
26 
27 // As there is no Free RTOS here, we can use is for CPU load estimator
29 
31 
32 // When nothing is enabled, the increment in the loop is done xx /second
33 // To measure it, remove all the code in setup except Serial initialization,
34 // and uncomment Serial output below
35 #define CPU_MAX_LOOP_PER_SECOND 8327007u
36 
37 // FUNCTIONS ==================================================================
38 
39 uint8_t readCpuLoadPercent(void) { return cpuLoadPercent; }
40 
41 void cpuLoadCallback(void) {
43  if (cpuLoadTimeCount == 0u) {
45  // Overflow won't be a problem, same type
46  uint32_t delta = idleCyclesCount - cpuLoadLatestCycleCount;
48  if (delta > CPU_MAX_LOOP_PER_SECOND) {
50  }
51  cpuLoadPercent = static_cast<uint8_t>(100u - (100u * delta) / CPU_MAX_LOOP_PER_SECOND);
52 
53  // Uncomment to tune CPU_MAX_LOOP_PER_SECOND value
54  // Serial.println(delta);
55  }
56 }
57 
58 extern "C" {
59 // This is the highest priority 1 ms callback
60 // cppcheck-suppress unusedFunction
62 }
uint32_t cpuLoadLatestCycleCount
Definition: cpu_load.cpp:30
#define CPU_MAX_LOOP_PER_SECOND
Definition: cpu_load.cpp:35
volatile uint32_t idleCyclesCount
Access to idleCyclesCount needed by the COUNT_IDLE_CYCLE macro.
Definition: cpu_load.cpp:22
uint8_t readCpuLoadPercent(void)
Get the value of the CPU load.
Definition: cpu_load.cpp:39
void osSystickHandler()
Definition: cpu_load.cpp:61
volatile uint8_t cpuLoadPercent
Definition: cpu_load.cpp:25
void cpuLoadCallback(void)
Definition: cpu_load.cpp:41
uint16_t cpuLoadTimeCount
Definition: cpu_load.cpp:28
#define TIME_CPU_CYCLE_RESET
Duration in ms after which the CPU cycle count is reset and the result is stored.
Definition: cpu_load.h:17