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:

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

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.

Parents Reply
  • I'm calling ME_addition() inside the main() function's while loop (at line 30). I'll include a code snippet below:

    int main(void)
    {
        APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
        NRF_LOG_DEFAULT_BACKENDS_INIT();
    
        NRF_LOG_INFO("Power Management example");
    
        lfclk_config();
    
    		uint32_t err_code = app_timer_init(); // drv_rtc.c:108 "RTC: initialized."
        APP_ERROR_CHECK(err_code);
    
    #if NRF_PWR_MGMT_CONFIG_USE_SCHEDULER
        APP_SCHED_INIT(APP_SCHED_MAX_EVENT_SIZE, APP_SCHED_QUEUE_SIZE);
    #endif // NRF_PWR_MGMT_CONFIG_USE_SCHEDULER
    	
        bsp_configuration();
    
    		ret_code_t ret_code = nrf_pwr_mgmt_init(); // nrf_pwr_mgmt.c:326 "Init"
        APP_ERROR_CHECK(ret_code);
    
        while (true)
        {
    			
    #if NRF_PWR_MGMT_CONFIG_USE_SCHEDULER
            app_sched_execute();
    #endif // NRF_PWR_MGMT_CONFIG_USE_SCHEDULER
    
    			
    		ME_addition();
    			
            if (NRF_LOG_PROCESS() == false)
            {
                nrf_pwr_mgmt_run();
            }
        }
    }

    Thanks

Children
Related