Skip to content

Commit

Permalink
drivers/periph_timer: add periph_timer_query_freq
Browse files Browse the repository at this point in the history
Allow accessing supported timer frequencies with a dedicated API.
This API needs to be implemented per platform and is available with
the feature periph_timer_query_freq.
  • Loading branch information
maribu committed Dec 5, 2023
1 parent 848f3d1 commit fe20117
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 0 deletions.
63 changes: 63 additions & 0 deletions drivers/include/periph/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <limits.h>
#include <stdint.h>

#include "architecture.h"
#include "periph_cpu.h"
#include "periph_conf.h"

Expand Down Expand Up @@ -232,6 +233,68 @@ void timer_start(tim_t dev);
*/
void timer_stop(tim_t dev);

/**
* @brief Get the number of different frequencies supported by the given
* timer
*
* If calling @ref timer_query_freq_numof for the same timer with an index
* smaller this number, it hence MUST return a frequency (and not zero).
*
* @details This function is marked with attribute pure to tell the compiler
* that this function has no side affects and will return the same
* value when called with the same parameter. (E.g. to not call this
* function in every loop iteration when iterating over all
* supported frequencies.)
*/
__attribute__((pure))
uword_t timer_query_freq_numof(tim_t dev);

/**
* @brief Get the number of timer channels for the given timer
*
* @details This function is marked with attribute pure to tell the compiler
* that this function has no side affects and will return the same
* value when called with the same timer as parameter.
* @details There is a weak default implementation that returns the value of
* `TIMER_CHANNEL_NUMOF`. For some MCUs the number of supported
* channels depends on @p dev - those are expected to provide there
* own implementation of this function.
*/
__attribute__((pure))
uword_t timer_query_channel_numof(tim_t dev);

/**
* @brief Iterate over supported frequencies
*
* @param dev Timer to get the next supported frequency of
* @param index Index of the frequency to get
* @return The @p index highest frequency supported by the timer
* @retval 0 @p index is too high
*
* @note Add `FEATURES_REQUIRED += periph_timer_query_freq` to your `Makefile`.
*
* When called with a value of 0 for @p index, the highest supported frequency
* is returned. For a value 1 the second highest is returned, and so on. For
* values out of range, 0 is returned. A program hence can iterate over all
* supported frequencies using:
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.c}
* uint32_t freq:
* for (uword_t i; (freq = timer_query_freq(dev, i)); i++) {
* work_with_frequency(freq);
* }
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* Or alternatively:
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.c}
* for (uword_t i; i < timer_query_freq_numof(dev); i++) {
* work_with_frequency(timer_query_freq(dev, i));
* }
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
uint32_t timer_query_freq(tim_t dev, uword_t index);

#ifdef __cplusplus
}
#endif
Expand Down
4 changes: 4 additions & 0 deletions drivers/periph_common/Kconfig.timer
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ config MODULE_PERIPH_INIT_TIMER_PERIODIC
depends on MODULE_PERIPH_TIMER_PERIODIC
default y if MODULE_PERIPH_INIT

config MODULE_PERIPH_TIMER_QUERY_FREQ
bool "Support for querying supported timer frequencies"
depends on HAS_PERIPH_TIMER_QUERY_FREQ

endif # MODULE_PERIPH_TIMER

endif # TEST_KCONFIG
Expand Down
9 changes: 9 additions & 0 deletions drivers/periph_common/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,12 @@ int timer_set(tim_t dev, int channel, unsigned int timeout)
return res;
}
#endif

#ifdef MODULE_PERIPH_TIMER_QUERY_FREQ
__attribute__((weak))
uword_t timer_query_channel_numof(tim_t dev)
{
(void)dev;
return TIMER_CHANNEL_NUMOF;
}
#endif
5 changes: 5 additions & 0 deletions kconfigs/Kconfig.features
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,11 @@ config HAS_PERIPH_TIMER_PERIODIC
Indicates that the Timer peripheral provides the periodic timeout
functionality.

config HAS_PERIPH_TIMER_QUERY_FREQ
bool
help
Indicates that the driver of the timer supports iterating over supported frequencies.

config HAS_PERIPH_UART
bool
help
Expand Down
1 change: 1 addition & 0 deletions makefiles/features_modules.inc.mk
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ PERIPH_IGNORE_MODULES := \
periph_rtt_hw_rtc \
periph_rtt_hw_sys \
periph_spi_on_qspi \
periph_timer_query_freq \
periph_uart_collision \
periph_uart_rxstart_irq \
periph_wdog \
Expand Down

0 comments on commit fe20117

Please sign in to comment.