drivers: clock_control: nrf: Add clock status shell command

Added shell command to clock driver to get clock status.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
This commit is contained in:
Krzysztof Chruscinski 2020-04-01 11:27:46 +02:00 committed by Carles Cufí
commit 04e770b20c
2 changed files with 65 additions and 1 deletions

View file

@ -21,6 +21,11 @@ menuconfig CLOCK_CONTROL_NRF
if CLOCK_CONTROL_NRF if CLOCK_CONTROL_NRF
config CLOCK_CONTROL_NRF_SHELL
bool "Shell commands"
depends on SHELL
default y if SHELL
choice CLOCK_CONTROL_NRF_SOURCE choice CLOCK_CONTROL_NRF_SOURCE
prompt "32KHz clock source" prompt "32KHz clock source"
default CLOCK_CONTROL_NRF_K32SRC_XTAL default CLOCK_CONTROL_NRF_K32SRC_XTAL

View file

@ -12,6 +12,7 @@
#include "nrf_clock_calibration.h" #include "nrf_clock_calibration.h"
#include <logging/log.h> #include <logging/log.h>
#include <hal/nrf_power.h> #include <hal/nrf_power.h>
#include <shell/shell.h>
LOG_MODULE_REGISTER(clock_control, CONFIG_CLOCK_CONTROL_LOG_LEVEL); LOG_MODULE_REGISTER(clock_control, CONFIG_CLOCK_CONTROL_LOG_LEVEL);
@ -75,6 +76,8 @@ struct nrf_clock_control_config {
}; };
static atomic_t hfclk_users; static atomic_t hfclk_users;
static uint64_t hf_start_tstamp;
static uint64_t hf_stop_tstamp;
/* Return true if given event has enabled interrupt and is triggered. Event /* Return true if given event has enabled interrupt and is triggered. Event
* is cleared. * is cleared.
@ -134,7 +137,7 @@ DEVICE_DECLARE(clock_nrf);
struct onoff_manager *z_nrf_clock_control_get_onoff(clock_control_subsys_t sys) struct onoff_manager *z_nrf_clock_control_get_onoff(clock_control_subsys_t sys)
{ {
return get_onoff_manager(DEVICE_GET(clock_nrf), return get_onoff_manager(DEVICE_GET(clock_nrf),
(enum clock_control_nrf_type)sys); (enum clock_control_nrf_type)sys);
} }
static enum clock_control_status get_status(struct device *dev, static enum clock_control_status get_status(struct device *dev,
@ -241,11 +244,19 @@ static void lfclk_stop(void)
static void hfclk_start(void) static void hfclk_start(void)
{ {
if (IS_ENABLED(CONFIG_CLOCK_CONTROL_NRF_SHELL)) {
hf_start_tstamp = k_uptime_get();
}
nrf_clock_task_trigger(NRF_CLOCK, NRF_CLOCK_TASK_HFCLKSTART); nrf_clock_task_trigger(NRF_CLOCK, NRF_CLOCK_TASK_HFCLKSTART);
} }
static void hfclk_stop(void) static void hfclk_stop(void)
{ {
if (IS_ENABLED(CONFIG_CLOCK_CONTROL_NRF_SHELL)) {
hf_stop_tstamp = k_uptime_get();
}
nrf_clock_event_clear(NRF_CLOCK, NRF_CLOCK_EVENT_HFCLKSTARTED); nrf_clock_event_clear(NRF_CLOCK, NRF_CLOCK_EVENT_HFCLKSTARTED);
nrf_clock_task_trigger(NRF_CLOCK, NRF_CLOCK_TASK_HFCLKSTOP); nrf_clock_task_trigger(NRF_CLOCK, NRF_CLOCK_TASK_HFCLKSTOP);
} }
@ -657,3 +668,51 @@ void nrf5_power_usb_power_int_enable(bool enable)
} }
} }
#endif #endif
static int cmd_status(const struct shell *shell, size_t argc, char **argv)
{
nrf_clock_hfclk_t hfclk_src;
bool hf_status;
bool lf_status =
nrf_clock_is_running(NRF_CLOCK, NRF_CLOCK_DOMAIN_LFCLK, NULL);
struct onoff_manager *hf_mgr =
get_onoff_manager(DEVICE_GET(clock_nrf),
CLOCK_CONTROL_NRF_TYPE_HFCLK);
struct onoff_manager *lf_mgr =
get_onoff_manager(DEVICE_GET(clock_nrf),
CLOCK_CONTROL_NRF_TYPE_LFCLK);
uint32_t abs_start, abs_stop;
int key = irq_lock();
uint64_t now = k_uptime_get();
(void)nrf_clock_is_running(NRF_CLOCK, NRF_CLOCK_DOMAIN_HFCLK,
(void *)&hfclk_src);
hf_status = (hfclk_src == NRF_CLOCK_HFCLK_HIGH_ACCURACY);
abs_start = hf_start_tstamp;
abs_stop = hf_stop_tstamp;
irq_unlock(key);
shell_print(shell, "HF clock:");
shell_print(shell, "\t- %srunning (users: %u)",
hf_status ? "" : "not ", hf_mgr->refs);
shell_print(shell, "\t- last start: %u ms (%u ms ago)",
(uint32_t)abs_start, (uint32_t)(now - abs_start));
shell_print(shell, "\t- last stop: %u ms (%u ms ago)",
(uint32_t)abs_stop, (uint32_t)(now - abs_stop));
shell_print(shell, "LF clock:");
shell_print(shell, "\t- %srunning (users: %u)",
lf_status ? "" : "not ", lf_mgr->refs);
return 0;
}
SHELL_STATIC_SUBCMD_SET_CREATE(subcmds,
SHELL_CMD_ARG(status, NULL, "Status", cmd_status, 1, 0),
SHELL_SUBCMD_SET_END
);
SHELL_COND_CMD_REGISTER(CONFIG_CLOCK_CONTROL_NRF_SHELL,
nrf_clock_control, &subcmds,
"Clock control commmands",
cmd_status);