1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-12-16 10:03:50 +01:00
Marian Buschsieweke 5b600d3a8e
drivers/periph_timer: add timer_get_closest_freq
Add an API to search for the frequency supported by a timer that
is closest to the given target frequency.

This is in fact non-trivial to get right, as pre-scaler registers can
be 16 bit or even 32 bit in size, making a naive loop over all possible
pre-scalers too expensive (computationally).

Co-authored-by: mguetschow <mikolai.guetschow@tu-dresden.de>
2025-04-02 16:21:07 +02:00

91 lines
2.6 KiB
C

/*
* Copyright (C) 2017 Freie Universität Berlin
*
* 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 drivers_periph_timer
* @{
*
* @file
* @brief Shared peripheral timer code
*
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
*
* @}
*/
#include "periph/timer.h"
#include "irq.h"
#ifndef PERIPH_TIMER_PROVIDES_SET
int timer_set(tim_t dev, int channel, unsigned int timeout)
{
unsigned int state = irq_disable();
int res = timer_set_absolute(dev, channel, timer_read(dev) + timeout);
irq_restore(state);
return res;
}
#endif
#ifdef MODULE_PERIPH_TIMER_QUERY_FREQS
__attribute__((weak))
uword_t timer_query_channel_numof(tim_t dev)
{
(void)dev;
return TIMER_CHANNEL_NUMOF;
}
uint32_t timer_get_closest_freq(tim_t dev, uint32_t target)
{
uint32_t freq;
uword_t idx_max = timer_query_freqs_numof(dev) - 1;
/* use binary search to find one of the three possibilities:
* 1. If an exact match is possible: The exact match is found
* 2. Otherwise the closest frequency is found, which might be either
* a) the highest frequency below the target, or
* b) the lowest frequency above the target
*/
uword_t idx_lower = 0;
uword_t idx_upper = idx_max;
while (idx_lower != idx_upper) {
uword_t idx_mid = (idx_lower + idx_upper) >> 1;
freq = timer_query_freqs(dev, idx_mid);
if (freq > target) {
idx_lower = idx_mid + 1;
}
else {
idx_upper = idx_mid;
}
}
freq = timer_query_freqs(dev, idx_lower);
if ((freq < target) && (idx_lower > 0)) {
/* binary search yielded the closest frequency below the target. But
* maybe the closest frequency above the target is actually better. */
uint32_t diff = target - freq;
uint32_t alternative = timer_query_freqs(dev, idx_lower - 1);
if (target + diff > alternative) {
/* the alternative is better */
return alternative;
}
}
else if ((freq > target) && (idx_lower < idx_max)) {
/* Got a frequency above the target, maybe the one below would be
* a closer match */
uint32_t diff = freq - target;
uint32_t alternative = timer_query_freqs(dev, idx_lower + 1);
if (target < alternative + diff) {
return alternative;
}
}
/* either we got an exact match, or the other candidate was no closer */
return freq;
}
#endif