From f0c36720d4bd09c5a8405e894558e780d4d1d972 Mon Sep 17 00:00:00 2001 From: Juergen Fitschen Date: Wed, 6 May 2020 13:55:01 +0200 Subject: [PATCH 1/2] tests/ztimer_pm_layered: initial commit --- tests/ztimer_pm_layered/Makefile | 6 ++ tests/ztimer_pm_layered/README.md | 8 +++ tests/ztimer_pm_layered/main.c | 98 +++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+) create mode 100644 tests/ztimer_pm_layered/Makefile create mode 100644 tests/ztimer_pm_layered/README.md create mode 100644 tests/ztimer_pm_layered/main.c diff --git a/tests/ztimer_pm_layered/Makefile b/tests/ztimer_pm_layered/Makefile new file mode 100644 index 000000000000..0dcd3a1a4791 --- /dev/null +++ b/tests/ztimer_pm_layered/Makefile @@ -0,0 +1,6 @@ +include ../Makefile.tests_common + +USEMODULE += pm_layered +USEMODULE += ztimer ztimer_msec ztimer_usec + +include $(RIOTBASE)/Makefile.include diff --git a/tests/ztimer_pm_layered/README.md b/tests/ztimer_pm_layered/README.md new file mode 100644 index 000000000000..e433a162b1fe --- /dev/null +++ b/tests/ztimer_pm_layered/README.md @@ -0,0 +1,8 @@ +# Introduction + +This test application runs an endless loop of ztimer_sleep() based on the clocks +ZTIMER_USEC and ZTIMER_MSEC alternating. + +If the board running this application has configured +CONFIG_ZTIMER_USEC_REQUIRED_PM_MODE, it should consume less power when sleeping +on the ZTIMER_MSEC clock. diff --git a/tests/ztimer_pm_layered/main.c b/tests/ztimer_pm_layered/main.c new file mode 100644 index 000000000000..bf408cc015ef --- /dev/null +++ b/tests/ztimer_pm_layered/main.c @@ -0,0 +1,98 @@ +/* + * Copyright (C) 2020 Juergen Fitschen + * 2020 SSV Software Systems GmbH + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup test + * @{ + * + * @file + * @brief ztimer test application for interacting with pm_layered + * + * @author 2020 SSV Software Systems GmbH + * + * @} + */ + +#include +#include +#include +#include + +#include "board.h" +#include "ztimer.h" +#include "timex.h" +#include "pm_layered.h" + +#define SLEEP_SEC (5LU) +#define SLEEP_MSEC (SLEEP_SEC * MS_PER_SEC) +#define SLEEP_USEC (SLEEP_SEC * MS_PER_SEC * MS_PER_SEC) + +static void dump_time(void) +{ + static uint32_t past_msec = 0; + static uint32_t past_usec = 0; + + uint32_t now_msec = ztimer_now(ZTIMER_MSEC); + uint32_t now_usec = ztimer_now(ZTIMER_USEC); + + printf("Elapsed time - ZTIMER_MSEC: %4" PRIu32 "ms, ZTIMER_USEC: %4" PRIu32 "ms\n", + (now_msec - past_msec), (now_usec - past_usec) / US_PER_MS); + + past_msec = now_msec; + past_usec = now_usec; +} + +int main(void) +{ + printf("This test application lets the CPU sleep for %" PRIu32 " seconds using the clocks \n" + "ZTIMER_MSEC and ZTIMER_USEC alternating.\n", SLEEP_SEC); + +#if (!defined(CONFIG_ZTIMER_MSEC_REQUIRED_PM_MODE) || \ + CONFIG_ZTIMER_MSEC_REQUIRED_PM_MODE == ZTIMER_CLOCK_NO_REQUIRED_PM_MODE) && \ + (!defined(CONFIG_ZTIMER_USEC_REQUIRED_PM_MODE) || \ + CONFIG_ZTIMER_USEC_REQUIRED_PM_MODE == ZTIMER_CLOCK_NO_REQUIRED_PM_MODE) + printf("WARNING: Neither ZTIMER_MSEC nor ZTIMER_USEC is interacting with pm_layered!\n" + "Don't expect any power saving.\n"); +#else + printf("\nUnblocking all pm modes related to ztimer clocks ...\n"); +#endif + +#if defined(CONFIG_ZTIMER_MSEC_REQUIRED_PM_MODE) && \ + CONFIG_ZTIMER_MSEC_REQUIRED_PM_MODE != ZTIMER_CLOCK_NO_REQUIRED_PM_MODE + while (pm_get_blocker().val_u8[CONFIG_ZTIMER_MSEC_REQUIRED_PM_MODE]) { + printf("pm_unblock(CONFIG_ZTIMER_MSEC_REQUIRED_PM_MODE)\n"); + pm_unblock(CONFIG_ZTIMER_MSEC_REQUIRED_PM_MODE); + } +#endif + +#if defined(CONFIG_ZTIMER_USEC_REQUIRED_PM_MODE) && \ + CONFIG_ZTIMER_USEC_REQUIRED_PM_MODE != ZTIMER_CLOCK_NO_REQUIRED_PM_MODE + /* If ztimer_msec is based on ztimer_usec, it already blocked + * CONFIG_ZTIMER_USEC_REQUIRED_PM_MODE once during auto_init. + * Don't remove that ... */ + uint8_t usec_pm_mode = (ZTIMER_MSEC_BASE == ZTIMER_USEC_BASE) ? 1 : 0; + while (pm_get_blocker().val_u8[CONFIG_ZTIMER_USEC_REQUIRED_PM_MODE] > usec_pm_mode) { + printf("pm_unblock(CONFIG_ZTIMER_USEC_REQUIRED_PM_MODE)\n"); + pm_unblock(CONFIG_ZTIMER_USEC_REQUIRED_PM_MODE); + } +#endif + + printf("\nEntering the sleepy loop ...\n"); + + while (1) { + dump_time(); + printf("ztimer_sleep(ZTIMER_MSEC, %" PRIu32 ")\n", SLEEP_MSEC); + ztimer_sleep(ZTIMER_MSEC, SLEEP_MSEC); + dump_time(); + printf("ztimer_sleep(ZTIMER_USEC, %" PRIu32 ")\n", SLEEP_USEC); + ztimer_sleep(ZTIMER_USEC, SLEEP_USEC); + } + + return 0; +} From 96ac811e9da0580d1ac3cfd66583ad1815de9f1a Mon Sep 17 00:00:00 2001 From: Juergen Fitschen Date: Wed, 6 May 2020 13:55:38 +0200 Subject: [PATCH 2/2] boards/samr30-xpro: added ztimer configuration --- boards/samr30-xpro/include/board.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/boards/samr30-xpro/include/board.h b/boards/samr30-xpro/include/board.h index a0711ff63025..f122071daed2 100644 --- a/boards/samr30-xpro/include/board.h +++ b/boards/samr30-xpro/include/board.h @@ -92,6 +92,15 @@ enum { #endif /** @} */ +/** + * @name ztimer configuration + * @{ + */ +#define CONFIG_ZTIMER_USEC_ADJUST_SET (19) +#define CONFIG_ZTIMER_USEC_ADJUST_SLEEP (23) +#define CONFIG_ZTIMER_USEC_REQUIRED_PM_MODE (1) +/** @} */ + /** * @brief Set antenna switch */