Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cpu_load module from Zephyr and align it with existing cpu_load module #21098

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions applications/nrf_desktop/doc/cpu_meas.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ Configuration
*************

To enable this module, use the :ref:`CONFIG_DESKTOP_CPU_MEAS_ENABLE <config_desktop_app_options>` Kconfig option.
This option selects the :kconfig:option:`CONFIG_CPU_LOAD` option.
The :kconfig:option:`CONFIG_CPU_LOAD` option enables the :ref:`cpu_load` library that is used to perform the measurements.
This option selects the :kconfig:option:`CONFIG_NRF_CPU_LOAD` option, which enables the :ref:`cpu_load` library that is used to perform the measurements.

Set the time between subsequent CPU load measurements, in milliseconds, using the :ref:`CONFIG_DESKTOP_CPU_MEAS_PERIOD <config_desktop_app_options>` option.

Expand Down
2 changes: 1 addition & 1 deletion applications/nrf_desktop/src/modules/Kconfig.cpu_meas
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ menu "CPU load measurement"

config DESKTOP_CPU_MEAS_ENABLE
bool "Enable measuring CPU load"
select CPU_LOAD
select NRF_CPU_LOAD
help
The CPU load is sent periodically using dedicated application event.

Expand Down
2 changes: 1 addition & 1 deletion applications/nrf_desktop/src/modules/cpu_meas.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ static void init(void)
/* When this option is enabled, CPU load measurement is periodically
* resetted. Only cpu_meas module should reset the measurement.
*/
BUILD_ASSERT(!IS_ENABLED(CONFIG_CPU_LOAD_LOG_PERIODIC));
BUILD_ASSERT(!IS_ENABLED(CONFIG_NRF_CPU_LOAD_LOG_PERIODIC));

static bool initialized;

Expand Down
2 changes: 1 addition & 1 deletion doc/nrf/libraries/debug/cpu_load.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ To precisely measure the sleep period, the module requires the POWER peripheral
The events are connected to a TIMER peripheral using PPI/DPPI.

The sleep period is measured using the TIMER peripheral, which is clocked by default by the high frequency clock.
Alternatively, it can be clocked using low frequency clock (see :kconfig:option:`CONFIG_CPU_LOAD_ALIGNED_CLOCKS`).
Alternatively, it can be clocked using low frequency clock (see :kconfig:option:`CONFIG_NRF_CPU_LOAD_ALIGNED_CLOCKS`).
It is then compared against the system clock, which is clocked by the low frequency clock.
The accuracy of measurements depends on the accuracy of the given clock sources.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,9 @@ Common Application Framework
Debug libraries
---------------

|no_changes_yet_note|
* :ref:`cpu_load` library:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


* Added prefix ``NRF_`` to all Kconfig options (for example, :kconfig:option:`CONFIG_NRF_CPU_LOAD`) to avoid conflict with Zephyr Kconfig option with the same name.

DFU libraries
-------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ choice NRF_MODEM_LIB_TRACE_BACKEND

config NRF_MODEM_LIB_TRACE_BACKEND_CUSTOM
bool "Count modem traces and print to terminal"
depends on CPU_LOAD
depends on NRF_CPU_LOAD
depends on FPU
depends on NEWLIB_LIBC_FLOAT_PRINTF || PICOLIBC_IO_FLOAT
help
Expand Down
2 changes: 1 addition & 1 deletion samples/cellular/modem_trace_backend/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ CONFIG_NRF_MODEM_LIB_TRACE=y
CONFIG_NRF_MODEM_LIB_TRACE_BACKEND_CUSTOM=y
CONFIG_CUSTOM_BACKEND_PRINT_PERIOD_MSEC=500

CONFIG_CPU_LOAD=y
CONFIG_NRF_CPU_LOAD=y
CONFIG_FPU=y
CONFIG_PICOLIBC_IO_FLOAT=y
2 changes: 1 addition & 1 deletion subsys/debug/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#

add_subdirectory_ifdef(CONFIG_CPU_LOAD cpu_load)
add_subdirectory_ifdef(CONFIG_NRF_CPU_LOAD cpu_load)
add_subdirectory_ifdef(CONFIG_ETB_TRACE etb_trace)
add_subdirectory_ifdef(CONFIG_PPI_TRACE ppi_trace)
54 changes: 29 additions & 25 deletions subsys/debug/cpu_load/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,45 @@
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#

menuconfig CPU_LOAD
menuconfig NRF_CPU_LOAD
bool "Enable CPU load measurement"
select NRFX_PPI if HAS_HW_NRF_PPI
select NRFX_DPPI if HAS_HW_NRF_DPPIC
select NRFX_GPPI
#select NRFX_PPI if HAS_HW_NRF_PPI
#select NRFX_DPPI if HAS_HW_NRF_DPPIC
depends on !SOC_SERIES_NRF51X #Lack of required HW events
depends on !SOC_SERIES_NRF54HX #Lack of required HW events
depends on !RISCV #Lack of required HW events
help
Enable the CPU load measurement instrumentation. This tool is using
one TIMER peripheral and PPI to perform accurate CPU load measurement.
POWER peripheral events with one TIMER peripheral and PPI to perform
accurate CPU load measurement.

if CPU_LOAD
if NRF_CPU_LOAD

module = CPU_LOAD
module = NRF_CPU_LOAD
module-str = CPU load measurement
source "${ZEPHYR_BASE}/subsys/logging/Kconfig.template.log_config"

config CPU_LOAD_CMDS
config NRF_CPU_LOAD_CMDS
bool "Enable shell commands"
depends on SHELL
default y

if LOG

config CPU_LOAD_LOG_PERIODIC
config NRF_CPU_LOAD_LOG_PERIODIC
bool "Periodically log current CPU load"
help
INFO level must be enabled to get the log.

config CPU_LOAD_LOG_INTERVAL
config NRF_CPU_LOAD_LOG_INTERVAL
int "Logging interval for CPU load [ms]"
depends on CPU_LOAD_LOG_PERIODIC
depends on NRF_CPU_LOAD_LOG_PERIODIC
default 2000

endif # LOG

config CPU_LOAD_ALIGNED_CLOCKS
config NRF_CPU_LOAD_ALIGNED_CLOCKS
bool "Enable aligned clock sources"
help
After enabling this option, the sleep period measurement
Expand All @@ -50,7 +54,7 @@ config CPU_LOAD_ALIGNED_CLOCKS
Enabling this option allows going to low power idle mode
because the high frequency clock is not used by this module.

config CPU_LOAD_USE_SHARED_DPPI_CHANNELS
config NRF_CPU_LOAD_USE_SHARED_DPPI_CHANNELS
bool "Use shared DPPI channels"
depends on HAS_HW_NRF_DPPIC
help
Expand All @@ -64,37 +68,37 @@ config CPU_LOAD_USE_SHARED_DPPI_CHANNELS

choice
prompt "Timer instance"
default CPU_LOAD_TIMER_2
default NRF_CPU_LOAD_TIMER_2

config CPU_LOAD_TIMER_0
config NRF_CPU_LOAD_TIMER_0
depends on $(dt_nodelabel_has_compat,timer0,$(DT_COMPAT_NORDIC_NRF_TIMER))
bool "Timer 0"
select NRFX_TIMER0
config CPU_LOAD_TIMER_1
config NRF_CPU_LOAD_TIMER_1
depends on $(dt_nodelabel_has_compat,timer1,$(DT_COMPAT_NORDIC_NRF_TIMER))
bool "Timer 1"
select NRFX_TIMER1
config CPU_LOAD_TIMER_2
config NRF_CPU_LOAD_TIMER_2
depends on $(dt_nodelabel_has_compat,timer2,$(DT_COMPAT_NORDIC_NRF_TIMER))
bool "Timer 2"
select NRFX_TIMER2
config CPU_LOAD_TIMER_3
config NRF_CPU_LOAD_TIMER_3
depends on $(dt_nodelabel_has_compat,timer3,$(DT_COMPAT_NORDIC_NRF_TIMER))
bool "Timer 3"
select NRFX_TIMER3
config CPU_LOAD_TIMER_4
config NRF_CPU_LOAD_TIMER_4
depends on $(dt_nodelabel_has_compat,timer4,$(DT_COMPAT_NORDIC_NRF_TIMER))
bool "Timer 4"
select NRFX_TIMER4

endchoice

config CPU_LOAD_TIMER_INSTANCE
config NRF_CPU_LOAD_TIMER_INSTANCE
int
default 0 if CPU_LOAD_TIMER_0
default 1 if CPU_LOAD_TIMER_1
default 2 if CPU_LOAD_TIMER_2
default 3 if CPU_LOAD_TIMER_3
default 4 if CPU_LOAD_TIMER_4
default 0 if NRF_CPU_LOAD_TIMER_0
default 1 if NRF_CPU_LOAD_TIMER_1
default 2 if NRF_CPU_LOAD_TIMER_2
default 3 if NRF_CPU_LOAD_TIMER_3
default 4 if NRF_CPU_LOAD_TIMER_4

endif # CPU_LOAD
endif # NRF_CPU_LOAD
50 changes: 17 additions & 33 deletions subsys/debug/cpu_load/cpu_load.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,14 @@
*/
#include <debug/cpu_load.h>
#include <zephyr/shell/shell.h>
#ifdef DPPI_PRESENT
#include <nrfx_dppi.h>
#else
#include <nrfx_ppi.h>
#endif
#include <helpers/nrfx_gppi.h>
#include <nrfx_timer.h>
#include <hal/nrf_rtc.h>
#include <hal/nrf_power.h>
#include <debug/ppi_trace.h>
#include <zephyr/logging/log.h>

LOG_MODULE_REGISTER(cpu_load, CONFIG_CPU_LOAD_LOG_LEVEL);
LOG_MODULE_REGISTER(cpu_load, CONFIG_NRF_CPU_LOAD_LOG_LEVEL);

/* Convert event address to associated publish register */
#define PUBLISH_ADDR(evt) (volatile uint32_t *)(evt + 0x80)
Expand All @@ -26,71 +21,60 @@ LOG_MODULE_REGISTER(cpu_load, CONFIG_CPU_LOAD_LOG_LEVEL);
#define CH_INVALID 0xFF

/* Define to please compiler when periodic logging is disabled. */
#ifdef CONFIG_CPU_LOAD_LOG_INTERVAL
#define CPU_LOAD_LOG_INTERVAL CONFIG_CPU_LOAD_LOG_INTERVAL
#ifdef CONFIG_NRF_CPU_LOAD_LOG_INTERVAL
#define CPU_LOAD_LOG_INTERVAL CONFIG_NRF_CPU_LOAD_LOG_INTERVAL
#else
#define CPU_LOAD_LOG_INTERVAL 0
#endif

static nrfx_timer_t timer = NRFX_TIMER_INSTANCE(CONFIG_CPU_LOAD_TIMER_INSTANCE);
static nrfx_timer_t timer = NRFX_TIMER_INSTANCE(CONFIG_NRF_CPU_LOAD_TIMER_INSTANCE);
static bool ready;
static struct k_work_delayable cpu_load_log;
static uint32_t cycle_ref;
static uint32_t shared_ch_mask;

#define IS_CH_SHARED(ch) \
(IS_ENABLED(CONFIG_CPU_LOAD_USE_SHARED_DPPI_CHANNELS) && \
(IS_ENABLED(CONFIG_NRF_CPU_LOAD_USE_SHARED_DPPI_CHANNELS) && \
(BIT(ch) & shared_ch_mask))


/** @brief Allocate (D)PPI channel. */
static nrfx_err_t ppi_alloc(uint8_t *ch, uint32_t evt)
{
nrfx_err_t err;
#ifdef DPPI_PRESENT
nrfx_dppi_t dppi = NRFX_DPPI_INSTANCE(0);

if (*PUBLISH_ADDR(evt) != 0) {
if (!IS_ENABLED(CONFIG_CPU_LOAD_USE_SHARED_DPPI_CHANNELS)) {
if (!IS_ENABLED(CONFIG_NRF_CPU_LOAD_USE_SHARED_DPPI_CHANNELS)) {
return NRFX_ERROR_BUSY;
}
/* Use mask of one of subscribe registers in the system,
* assuming that all subscribe registers has the same mask for
* channel id.
*/
*ch = *PUBLISH_ADDR(evt) & DPPIC_SUBSCRIBE_CHG_EN_CHIDX_Msk;
err = NRFX_SUCCESS;
shared_ch_mask |= BIT(*ch);
} else {
err = nrfx_dppi_channel_alloc(&dppi, ch);
return NRFX_SUCCESS;
}
#else
err = nrfx_ppi_channel_alloc((nrf_ppi_channel_t *)ch);
#endif
return err;
return nrfx_gppi_channel_alloc(ch);
}

static nrfx_err_t ppi_free(uint8_t ch)
{
#ifdef DPPI_PRESENT
nrfx_dppi_t dppi = NRFX_DPPI_INSTANCE(0);

if (!IS_ENABLED(CONFIG_CPU_LOAD_USE_SHARED_DPPI_CHANNELS)
|| ((BIT(ch) & shared_ch_mask) == 0)) {
return nrfx_dppi_channel_free(&dppi, ch);
} else {
if (IS_ENABLED(CONFIG_NRF_CPU_LOAD_USE_SHARED_DPPI_CHANNELS)
&& ((BIT(ch) & shared_ch_mask) == 0)) {
shared_ch_mask &= ~BIT(ch);
return NRFX_SUCCESS;
}
#else
return nrfx_ppi_channel_free((nrf_ppi_channel_t)ch);
#endif
return nrfx_gppi_channel_free(ch);
}

static void ppi_cleanup(uint8_t ch_tick, uint8_t ch_sleep, uint8_t ch_wakeup)
{
nrfx_err_t err = NRFX_SUCCESS;

if (IS_ENABLED(CONFIG_CPU_LOAD_ALIGNED_CLOCKS)) {
if (IS_ENABLED(CONFIG_NRF_CPU_LOAD_ALIGNED_CLOCKS)) {
err = ppi_free(ch_tick);
}

Expand Down Expand Up @@ -147,7 +131,7 @@ int cpu_load_init(void)
config.frequency = NRFX_MHZ_TO_HZ(1);
config.bit_width = NRF_TIMER_BIT_WIDTH_32;

if (IS_ENABLED(CONFIG_CPU_LOAD_ALIGNED_CLOCKS)) {
if (IS_ENABLED(CONFIG_NRF_CPU_LOAD_ALIGNED_CLOCKS)) {
/* It's assumed that RTC1 is driving system clock. */
config.mode = NRF_TIMER_MODE_COUNTER;
err = ppi_alloc(&ch_tick,
Expand Down Expand Up @@ -203,7 +187,7 @@ int cpu_load_init(void)

cpu_load_reset();

if (IS_ENABLED(CONFIG_CPU_LOAD_LOG_PERIODIC)) {
if (IS_ENABLED(CONFIG_NRF_CPU_LOAD_LOG_PERIODIC)) {
ret = cpu_load_log_init();
if (ret >= 0) {
ret = 0;
Expand All @@ -223,7 +207,7 @@ void cpu_load_reset(void)

static uint32_t sleep_ticks_to_us(uint32_t ticks)
{
return IS_ENABLED(CONFIG_CPU_LOAD_ALIGNED_CLOCKS) ?
return IS_ENABLED(CONFIG_NRF_CPU_LOAD_ALIGNED_CLOCKS) ?
(uint32_t)(((uint64_t)ticks * 1000000) / sys_clock_hw_cycles_per_sec()) :
ticks;
}
Expand Down Expand Up @@ -299,5 +283,5 @@ SHELL_STATIC_SUBCMD_SET_CREATE(sub_cmd_cpu_load,
SHELL_SUBCMD_SET_END
);

SHELL_COND_CMD_ARG_REGISTER(CONFIG_CPU_LOAD_CMDS, cpu_load, &sub_cmd_cpu_load,
SHELL_COND_CMD_ARG_REGISTER(CONFIG_NRF_CPU_LOAD_CMDS, cpu_load, &sub_cmd_cpu_load,
"CPU load", cmd_cpu_load_get, 1, 1);
4 changes: 2 additions & 2 deletions tests/subsys/debug/cpu_load/prj.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CONFIG_ZTEST=y
CONFIG_CPU_LOAD=y
CONFIG_NRF_CPU_LOAD=y
CONFIG_NRFX_TIMER1=y
CONFIG_CPU_LOAD_USE_SHARED_DPPI_CHANNELS=y
CONFIG_NRF_CPU_LOAD_USE_SHARED_DPPI_CHANNELS=y
2 changes: 1 addition & 1 deletion tests/subsys/debug/cpu_load/src/test_cpu_load.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ ZTEST(cpu_load, test_cpu_load)
nrfx_gppi_channel_endpoints_setup(ch, evt, tsk);
nrfx_gppi_channels_enable(BIT(ch));

if (!IS_ENABLED(CONFIG_CPU_LOAD_USE_SHARED_DPPI_CHANNELS)) {
if (!IS_ENABLED(CONFIG_NRF_CPU_LOAD_USE_SHARED_DPPI_CHANNELS)) {
err = cpu_load_init();
zassert_equal(err, -ENODEV, "Unexpected err:%d", err);

Expand Down
2 changes: 1 addition & 1 deletion tests/subsys/debug/cpu_load/testcase.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ tests:
- sysbuild
- ci_tests_subsys_debug
extra_configs:
- CONFIG_CPU_LOAD_USE_SHARED_DPPI_CHANNELS=y
- CONFIG_NRF_CPU_LOAD_USE_SHARED_DPPI_CHANNELS=y
2 changes: 1 addition & 1 deletion west.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ manifest:
# https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/zephyr/guides/modules.html
- name: zephyr
repo-path: sdk-zephyr
revision: c9113a87822e123bd287568c957d92b35502e5d1
revision: pull/2657/head
import:
# In addition to the zephyr repository itself, NCS also
# imports the contents of zephyr/west.yml at the above
Expand Down
Loading