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
50static inline void busy_wait(unsigned loops)
51{
52 while (loops) {
53 /* The empty volatile inline assembly prevents the compiler from
54 * optimizing out the loop. Declaring `loops` as read-write operand
55 * hides its value from the compiler, so the number of iterations
56 * is unknown at compile time and the loop cannot be unrolled. */
57 __asm__ __volatile__ (
58 ""
59 : "+r" (loops) /* no actual output */
60 : /* no inputs */
61 : /* no clobbers */
62 );
63 loops--;
64 }
65}
66
78static inline void busy_wait_us(unsigned usec)
79{
80 unsigned loops = ((uint64_t)usec * CLOCK_CORECLOCK)
82 busy_wait(loops);
83}
84
85#ifdef __cplusplus
86}
87#endif
88
#define CLOCK_CORECLOCK
Clock configuration.
Definition periph_cpu.h:29
#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:78
static void busy_wait(unsigned loops)
Spin for a number of cycles.
Definition busy_wait.h:50
#define US_PER_SEC
The number of microseconds per second.
Definition time_units.h:81
Utility header providing time unit defines.