@@ -202,7 +202,7 @@ static int test_timer(unsigned num, uint32_t timer_freq)
202202 return 0 ;
203203 }
204204
205- printf (" OK (no spurious IRQs)\n" );
205+ printf (" [OK] (no spurious IRQs)\n" );
206206
207207 return 1 ;
208208}
@@ -249,8 +249,47 @@ static void print_supported_frequencies(tim_t dev)
249249 " %u: %" PRIu32 "\n" ,
250250 (unsigned )(end - 1 ), timer_query_freqs (dev , end - 1 ));
251251 }
252+ }
253+
254+ static void test_querying (tim_t dev )
255+ {
256+ uword_t last = query_freq_numof (dev ) - 1 ;
257+
258+ puts ("Testing timer_get_closest_freq()..." );
259+ for (uword_t i = 0 ; i <= MIN (last , 255 ); i ++ ) {
260+ uint32_t closest ;
261+ uint32_t freq = timer_query_freqs (dev , i );
262+
263+ /* Searching for a supported freq should yield the supported freq: */
264+ expect (freq == timer_get_closest_freq (dev , freq ));
265+
266+ /* Assuming that no other frequency close to `freq` are supported,
267+ * we would assume that asking for `freq - 1` and for `freq + 1` would
268+ * also return `freq`. There are some corner cases, though. Let's look
269+ * at asking for `freq - 1` here first:
270+ *
271+ * - `freq - 1` is actually also supported. In that case
272+ * `timer_get_closest_freq(dev, freq - 1)` should actually return
273+ * `freq - 1`
274+ * - `freq - 2` is also supported (but not `freq - 1`). In this case
275+ * returning either `freq - 2` or `freq` would be valid for
276+ * `timer_get_closest_freq(dev, freq - 1)`.
277+ *
278+ * Therefore, we just except `freq - 2`, `freq - 1`, and `freq` as
279+ * results for `timer_get_closest_freq(dev, freq - 1)`. */
280+ closest = timer_get_closest_freq (dev , freq - 1 );
281+ expect ((freq >= closest ) && closest >= freq - 2 );
282+
283+ /* Now same with `freq + 1` as target. Due to the same corner cases,
284+ * we accept `freq`, `freq + 1`, and `freq + 2` here. */
285+ closest = timer_get_closest_freq (dev , freq + 1 );
286+ expect ((freq <= closest ) && closest <= freq + 2 );
287+ }
252288
253- expect (timer_query_freqs (dev , end ) == 0 );
289+ puts ("[OK]\n"
290+ "Testing timer_query_freqs() for out of bound index..." );
291+ expect (timer_query_freqs (dev , last + 1 ) == 0 );
292+ puts ("[OK]" );
254293}
255294
256295int main (void )
@@ -265,6 +304,12 @@ int main(void)
265304 printf ("\nTIMER %u\n"
266305 "=======\n\n" , i );
267306 print_supported_frequencies (TIMER_DEV (i ));
307+
308+ /* test querying of frequencies, but only if supported by the driver */
309+ if (IS_USED (MODULE_PERIPH_TIMER_QUERY_FREQS )) {
310+ test_querying (TIMER_DEV (i ));
311+ }
312+
268313 uword_t end = query_freq_numof (TIMER_DEV (i ));
269314
270315 /* Test only up to three frequencies and only the fastest once.
0 commit comments