Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs

nRF52840 CPU Usage Accuracy and Explanation

Hello,

I'm a beginner using the nRF52840 DK, sdk v16.0, and Keil on Windows. I'm currently working on the provided pwr_mgmt example. My first goal was to display the CPU Usage percentage as a float number, which I accomplished by editing the nrf_pwr_mgmt.c file (inside the pwr_mgmt_cpu_usage_monitor_update function), such that:

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
__STATIC_INLINE void pwr_mgmt_cpu_usage_monitor_update(void)
{
uint32_t delta;
uint32_t ticks;
//uint8_t cpu_usage; <- ORIGINAL
float cpu_usage; // <- ME
ticks = app_timer_cnt_get();
delta = app_timer_cnt_diff_compute(ticks, m_ticks_last);
cpu_usage = (float)(delta - m_ticks_sleeping) / (delta) * 100; // <- ME
//NRF_LOG_INFO("CPU Usage: %u%%", cpu_usage); <- ORIGINAL
NRF_LOG_INFO("CPU Usage: " NRF_LOG_FLOAT_MARKER "%%", NRF_LOG_FLOAT(cpu_usage)); // <- ME
if (m_max_cpu_usage < cpu_usage)
{
m_max_cpu_usage = cpu_usage;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

I was wondering if this was an accurate way of doing so?

Additionally, I edited the main.c file , such that I included my own function to test the CPU Usage:

Fullscreen
1
2
3
4
5
6
7
8
9
10
// ME
static void ME_addition() {
int i;
for (i = 0; i < 1000000; i++){
uint32_t x = i;
uint32_t y = i+1;
uint64_t z = x + y;
}
return;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

I call this inside the main() function and receive ~12.335% CPU Usage. For simple addition, I expected the CPU usage to be somewhat lower for the chip...so, I'm wondering if this is accurate or if I'm potentially missing something in my thinking?

In summary:

1.) What's the best way to display the CPU usage as a percentage with 3 decimal places?

2.) Why am I seeing ~12% usage for addition arithmetic?

Thanks.