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


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:

 __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;

        m_ticks_last        = ticks;
        m_ticks_sleeping    = 0;

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:

// 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;

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, 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?