Loading...
Searching...
No Matches
busy_wait.h
1/*
2 * Copyright (C) 2024 ML!PA Consulting GmbH
3 *
4 * This file is subject to the terms and conditions of the GNU Lesser
5 * General Public License v2.1. See the file LICENSE in the top level
6 * directory for more details.
7 */
8
9#pragma once
10
22
23#include <stdint.h>
24#include "periph_conf.h"
25#include "time_units.h"
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
37#ifndef CPU_CYCLES_PER_LOOP
38#define CPU_CYCLES_PER_LOOP 3
39#endif
40
49static inline void busy_wait(unsigned loops)
50{
51 while (loops) {
52 /* This empty inline assembly should be enough to convince the
53 * compiler that the loop cannot be optimized out. Tested with
54 * GCC 12.2 and clang 16.0.0 successfully. */
55 __asm__ __volatile__ (
56 ""
57 : /* no outputs */
58 : /* no inputs */
59 : /* no clobbers */
60 );
61 loops--;
62 }
63}
64
76static inline void busy_wait_us(unsigned usec)
77{
78 unsigned loops = ((uint64_t)usec * CLOCK_CORECLOCK)
80 busy_wait(loops);
81}
82
83#ifdef __cplusplus
84}
85#endif
86
#define CLOCK_CORECLOCK
Clock configuration.
Definition periph_cpu.h:31
#define CPU_CYCLES_PER_LOOP
CPU cycles per busy wait loop.
Definition cpu.h:62
static void busy_wait_us(unsigned usec)
Spin for a number of microseconds.
Definition busy_wait.h:76
static void busy_wait(unsigned loops)
Spin for a number of cycles.
Definition busy_wait.h:49
#define US_PER_SEC
The number of microseconds per second.
Definition time_units.h:84
Utility header providing time unit defines.