Loading...
Searching...
No Matches
busy_wait.h
1/*
2 * SPDX-FileCopyrightText: 2024 ML!PA Consulting GmbH
3 * SPDX-License-Identifier: LGPL-2.1-only
4 */
5
6#pragma once
7
19
20#include <stdint.h>
21#include "periph_conf.h"
22#include "time_units.h"
23
24#ifdef __cplusplus
25extern "C" {
26#endif
27
34#ifndef CPU_CYCLES_PER_LOOP
35#define CPU_CYCLES_PER_LOOP 3
36#endif
37
46static inline void busy_wait(unsigned loops)
47{
48 while (loops) {
49 /* This empty inline assembly should be enough to convince the
50 * compiler that the loop cannot be optimized out. Tested with
51 * GCC 12.2 and clang 16.0.0 successfully. */
52 __asm__ __volatile__ (
53 ""
54 : /* no outputs */
55 : /* no inputs */
56 : /* no clobbers */
57 );
58 loops--;
59 }
60}
61
73static inline void busy_wait_us(unsigned usec)
74{
75 unsigned loops = ((uint64_t)usec * CLOCK_CORECLOCK)
77 busy_wait(loops);
78}
79
80#ifdef __cplusplus
81}
82#endif
83
#define CLOCK_CORECLOCK
Clock configuration.
Definition periph_cpu.h:28
#define CPU_CYCLES_PER_LOOP
CPU cycles per busy wait loop.
Definition cpu.h:59
static void busy_wait_us(unsigned usec)
Spin for a number of microseconds.
Definition busy_wait.h:73
static void busy_wait(unsigned loops)
Spin for a number of cycles.
Definition busy_wait.h:46
#define US_PER_SEC
The number of microseconds per second.
Definition time_units.h:81
Utility header providing time unit defines.