From b40ab8f8339f2cd33abfa39c796234f31b3acd30 Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Fri, 28 Apr 2023 12:13:59 +0200 Subject: [PATCH] cpu/nrf5x_common: implement periph_timer_query_freqs --- cpu/nrf53/Kconfig | 1 + cpu/nrf5x_common/Kconfig | 1 + cpu/nrf5x_common/Makefile.features | 1 + cpu/nrf5x_common/include/periph_cpu_common.h | 10 ++++++++ cpu/nrf5x_common/periph/timer.c | 26 +++++++++++++++++++- cpu/nrf9160/Kconfig | 1 + 6 files changed, 39 insertions(+), 1 deletion(-) diff --git a/cpu/nrf53/Kconfig b/cpu/nrf53/Kconfig index be2cb9fee2dc..e86107a0529b 100644 --- a/cpu/nrf53/Kconfig +++ b/cpu/nrf53/Kconfig @@ -16,6 +16,7 @@ config CPU_FAM_NRF53 select HAS_PERIPH_GPIO select HAS_PERIPH_GPIO_IRQ select HAS_PERIPH_TIMER_PERIODIC + select HAS_PERIPH_TIMER_QUERY_FREQS select HAS_PERIPH_UART_MODECFG select HAS_PERIPH_WDT select HAS_PERIPH_WDT_CB diff --git a/cpu/nrf5x_common/Kconfig b/cpu/nrf5x_common/Kconfig index b6bf0b38eb8e..9a9a8d7736a4 100644 --- a/cpu/nrf5x_common/Kconfig +++ b/cpu/nrf5x_common/Kconfig @@ -20,6 +20,7 @@ depends on !CPU_FAM_NRF53 select HAS_PERIPH_HWRNG select HAS_PERIPH_TEMPERATURE select HAS_PERIPH_TIMER_PERIODIC + select HAS_PERIPH_TIMER_QUERY_FREQS select HAS_PERIPH_RTT_OVERFLOW select HAS_PERIPH_UART_MODECFG select HAS_PERIPH_WDT diff --git a/cpu/nrf5x_common/Makefile.features b/cpu/nrf5x_common/Makefile.features index 1b0330eaef56..0bfe5661f356 100644 --- a/cpu/nrf5x_common/Makefile.features +++ b/cpu/nrf5x_common/Makefile.features @@ -5,6 +5,7 @@ FEATURES_PROVIDED += periph_flashpage_in_address_space FEATURES_PROVIDED += periph_flashpage_pagewise FEATURES_PROVIDED += periph_gpio periph_gpio_irq FEATURES_PROVIDED += periph_timer_periodic +FEATURES_PROVIDED += periph_timer_query_freqs FEATURES_PROVIDED += periph_uart_modecfg FEATURES_PROVIDED += periph_wdt periph_wdt_cb diff --git a/cpu/nrf5x_common/include/periph_cpu_common.h b/cpu/nrf5x_common/include/periph_cpu_common.h index 19cbc5a98a09..c6ab6d51ccae 100644 --- a/cpu/nrf5x_common/include/periph_cpu_common.h +++ b/cpu/nrf5x_common/include/periph_cpu_common.h @@ -257,6 +257,16 @@ typedef struct { */ #define PERIPH_TIMER_PROVIDES_SET 1 +/** + * @brief Maximum number of channels + * + * @note NRF_TIMER1 and NRF_TIMER2 only have 4 hardware channels (and 3 of + * of them are available to the application, as one has to be used + * to implement timer_read()). Use @ref timer_query_channel_numof to + * check the actual number of supported channels for a given timer. + */ +#define TIMER_CHANNEL_NUMOF 5 + #ifndef DOXYGEN /** * @brief Override SPI mode values diff --git a/cpu/nrf5x_common/periph/timer.c b/cpu/nrf5x_common/periph/timer.c index cefe07ddafe3..556ead9b63be 100644 --- a/cpu/nrf5x_common/periph/timer.c +++ b/cpu/nrf5x_common/periph/timer.c @@ -45,6 +45,30 @@ static inline NRF_TIMER_Type *dev(tim_t tim) return timer_config[tim].dev; } +uword_t timer_query_freqs_numof(tim_t dev) +{ + assert(dev < TIMER_NUMOF); + (void)dev; + return 10; +} + +uword_t timer_query_channel_numof(tim_t dev) +{ + assert(dev < TIMER_NUMOF); + return timer_config[dev].channels; +} + +uint32_t timer_query_freqs(tim_t dev, uword_t index) +{ + assert(dev < TIMER_NUMOF); + (void)dev; + if (index >= 10) { + return 0; + } + + return F_TIMER >> index; +} + int timer_init(tim_t tim, uint32_t freq, timer_cb_t cb, void *arg) { /* make sure the given timer is valid */ @@ -75,7 +99,7 @@ int timer_init(tim_t tim, uint32_t freq, timer_cb_t cb, void *arg) dev(tim)->PRESCALER = i; break; } - cando /= 2; + cando >>= 1; } if (i == 10) { return -1; diff --git a/cpu/nrf9160/Kconfig b/cpu/nrf9160/Kconfig index 1f791cde6bee..e90ad52ea331 100644 --- a/cpu/nrf9160/Kconfig +++ b/cpu/nrf9160/Kconfig @@ -17,6 +17,7 @@ config CPU_FAM_NRF9160 select HAS_PERIPH_GPIO_LL_IRQ select HAS_PERIPH_GPIO_LL_IRQ_UNMASK select HAS_PERIPH_TIMER_PERIODIC + select HAS_PERIPH_TIMER_QUERY_FREQS select HAS_PERIPH_UART_MODECFG select HAS_PERIPH_SPI_GPIO_MODE select HAS_PERIPH_WDT