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

sys/ztimer: add LPTIMER auto init #17654

Merged
merged 2 commits into from
Sep 15, 2022
Merged

Conversation

kfessel
Copy link
Contributor

@kfessel kfessel commented Feb 14, 2022

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

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);

Issues/PRs references

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

@kfessel kfessel requested a review from jue89 February 14, 2022 15:06
@github-actions github-actions bot added Area: sys Area: System Area: timers Area: timer subsystems labels Feb 14, 2022
@kfessel kfessel changed the title P ztimer lptimer sys/ztimer: add LPTIMER auto init Feb 14, 2022
@maribu
Copy link
Member

maribu commented Feb 15, 2022

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

@kfessel kfessel requested a review from benpicco June 28, 2022 09:04
@kfessel kfessel marked this pull request as ready for review June 28, 2022 09:08
@kfessel kfessel requested a review from bergzand as a code owner June 28, 2022 09:08
@benpicco
Copy link
Contributor

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?

@github-actions github-actions bot added Area: boards Area: Board ports Area: tests Area: tests and testing framework labels Jul 19, 2022
@kfessel
Copy link
Contributor Author

kfessel commented Jul 19, 2022

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)

sys/ztimer/init.c Outdated Show resolved Hide resolved
@github-actions github-actions bot added the Area: tools Area: Supplementary tools label Sep 14, 2022
@benpicco
Copy link
Contributor

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)

@@ -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"
Copy link
Contributor

Choose a reason for hiding this comment

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

This will break for all non-stm32 boards

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i will remove that -- maybe i should remove the whole example commit ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yea it's probably for the better

@kfessel
Copy link
Contributor Author

kfessel commented Sep 15, 2022

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);

@github-actions github-actions bot removed Area: tools Area: Supplementary tools Area: tests Area: tests and testing framework Area: boards Area: Board ports labels Sep 15, 2022
@benpicco benpicco added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Sep 15, 2022
@benpicco benpicco merged commit 1be4da5 into RIOT-OS:master Sep 15, 2022
@maribu maribu added this to the Release 2022.10 milestone Oct 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: sys Area: System Area: timers Area: timer subsystems CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ztimer with two instances of ztimer_periph_timer
3 participants