RIOT icon indicating copy to clipboard operation
RIOT copied to clipboard

sys/ztimer: add LPTIMER auto init

Open kfessel opened this issue 3 years ago • 3 comments

Contribution description

adds LPTIMER to ztimer auto init

Testing procedure

CONFIG_ZTIMER_LPTIMER_DEV
CONFIG_ZTIMER_LPTIMER_FREQ
CONFIG_ZTIMER_LPTIMER_WIDTH
CONFIG_ZTIMER_LPTIMER_BLOCK_PM_MODE

have to be defined and ztimer_periph_lptimer be used msec and sec prefer lptimer over rtt

Issues/PRs references

includes #16342 fixes #17611 this may help with using rtt64 by PR #18120

kfessel avatar Feb 14 '22 15:02 kfessel

Maybe @jue89 could review this, as he has the hardware and also is interested in a use case for this.

maribu avatar Feb 15 '22 09:02 maribu

So say I have

--- a/boards/same54-xpro/include/periph_conf.h
+++ b/boards/same54-xpro/include/periph_conf.h
@@ -92,6 +92,15 @@ static const tc32_conf_t timer_config[] = {
         .gclk_id        = TC2_GCLK_ID,
         .gclk_src       = SAM0_GCLK_TIMER,
         .flags          = TC_CTRLA_MODE_COUNT32,
+    },
+    {   /* Timer 2, low power */
+        .dev            = TC4,
+        .irq            = TC4_IRQn,
+        .mclk           = &MCLK->APBCMASK.reg,
+        .mclk_mask      = MCLK_APBCMASK_TC4 | MCLK_APBCMASK_TC5,
+        .gclk_id        = TC4_GCLK_ID,
+        .gclk_src       = SAM0_GCLK_32KHZ,
+        .flags          = TC_CTRLA_MODE_COUNT32,
     }
 };
 
@@ -103,6 +112,10 @@ static const tc32_conf_t timer_config[] = {
 #define TIMER_1_CHANNELS    2
 #define TIMER_1_ISR         isr_tc2
 
+/* Timer 2 configuration */
+#define TIMER_2_CHANNELS    2
+#define TIMER_2_ISR         isr_tc4
+
 #define TIMER_NUMOF         ARRAY_SIZE(timer_config)
 /** @} */

how would I go about using this?

benpicco avatar Jul 18 '22 21:07 benpicco

maybe the example commit helps reviewing this I ran the test BOARD=nucleo-f767zi make flash term

I am not sure how to incorporate the extra timer configuration with our boards (stm32 had just one perih_timer prepared for use -not sure how common that is) If you use your own board just add a timer configuration (as benpico did)

i should have used timer5 (TIM3 has only 16bits)

kfessel avatar Jul 19 '22 15:07 kfessel

I can confirm that this works on same54-xpro too, unfortunately I don't see the power savings I had hoped for.

Please squash (& rebase to solve the merge conflict)

benpicco avatar Sep 14 '22 16:09 benpicco

I the details a diff can be found adding a second timer for stm32 (may not work on all devices) and using that in the ztimer_xsec example since this PR is about adding the lowpower-timer (lptimer) support to ztimer this was removed from it.

diff --git a/boards/common/stm32/include/cfg_timer_tim2.h b/boards/common/stm32/include/cfg_timer_tim2.h
index d7f6008c6f..c9c7126b30 100644
--- a/boards/common/stm32/include/cfg_timer_tim2.h
+++ b/boards/common/stm32/include/cfg_timer_tim2.h
@@ -47,10 +47,18 @@ static const timer_conf_t timer_config[] = {
 #endif
         .bus      = APB1,
         .irqn     = TIM2_IRQn
-    }
+    },
+    {
+        .dev      = TIM5,
+        .max      = 0xffffffff,
+        .rcc_mask = RCC_APB1ENR_TIM5EN,
+        .bus      = APB1,
+        .irqn     = TIM5_IRQn
+    },
 };
 
 #define TIMER_0_ISR         isr_tim2
+#define TIMER_1_ISR         isr_tim5
 
 #define TIMER_NUMOF         ARRAY_SIZE(timer_config)
 /** @} */
diff --git a/dist/tools/doccheck/exclude_patterns b/dist/tools/doccheck/exclude_patterns
index 362c11d1c3..8c1457d007 100644
--- a/dist/tools/doccheck/exclude_patterns
+++ b/dist/tools/doccheck/exclude_patterns
@@ -13119,3 +13119,4 @@ boards/esp32\-ttgo\-t\-beam/doc\.txt:[0-9]+: warning: unable to resolve referenc
 boards/esp32\-wemos\-lolin\-d32\-pro/doc\.txt:[0-9]+: warning: explicit link request to 'esp32_wemos_lolin_d32_pro_optional_hardware' could not be resolved
 drivers/include/xbee\.h:[0-9]+: warning: found documented return type for xbee_setup that does not return anything
 boards/nrf52840dongle/doc\.txt:[0-9]+: warning: unable to resolve reference to 'nrf52840dongle_flash' for \\ref command
+boards/common/stm32/include/cfg_timer_tim2\.h:[0-9]+: warning: Member TIMER_1_ISR \(macro definition\) of file cfg_timer_tim2\.h is not documented\.
diff --git a/tests/ztimer_xsec/Makefile b/tests/ztimer_xsec/Makefile
index 8709ee2154..f34aa56d08 100644
--- a/tests/ztimer_xsec/Makefile
+++ b/tests/ztimer_xsec/Makefile
@@ -4,6 +4,9 @@ USEMODULE += ztimer
 USEMODULE += ztimer_usec
 USEMODULE += ztimer_msec
 USEMODULE += ztimer_sec
+USEMODULE += ztimer_periph_lptimer
+
+CFLAGS += "-DCONFIG_ZTIMER_LPTIMER_DEV=TIMER_DEV(1)" "-DCONFIG_ZTIMER_LPTIMER_FREQ=1000LU" "-DCONFIG_ZTIMER_LPTIMER_WIDTH=32" "-DCONFIG_ZTIMER_LPTIMER_BLOCK_PM_MODE=ZTIMER_CLOCK_NO_REQUIRED_PM_MODE"
 
 # microbit qemu lacks rtt
 TEST_ON_CI_BLACKLIST += microbit
diff --git a/tests/ztimer_xsec/main.c b/tests/ztimer_xsec/main.c
index 993edfa344..4dc0d69d7b 100644
--- a/tests/ztimer_xsec/main.c
+++ b/tests/ztimer_xsec/main.c
@@ -56,7 +56,7 @@ int main(void)
 {
     puts("starting ztimers");
     /* start a timer on each high level ztimer*/
-    ztimer_set(ZTIMER_SEC, &sec_tim, 1);
+    ztimer_set(ZTIMER_SEC, &sec_tim, 3);
     ztimer_set(ZTIMER_MSEC, &msec_tim, 200);
     ztimer_set(ZTIMER_USEC, &usec_tim, 100 * US_PER_MS);

kfessel avatar Sep 15 '22 10:09 kfessel