aboutsummaryrefslogtreecommitdiffstats
path: root/firmware/tests/fake/util/delay.h
blob: 5484862266c4b55c88edd97639943eab647196e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#ifndef FAKE_DELAY_H_
#define FAKE_DELAY_H_

#include <time.h>
#include <errno.h>
#include <string.h>
#include <math.h>
#include <stdio.h>


static inline void _usleep__(uint64_t us)
{
	int err;
	struct timespec tv;

	tv.tv_sec = 0;
	while (us >= 1000000u) {
		tv.tv_sec++;
		us -= 1000000u;
	}
	tv.tv_nsec = (int64_t)us * 1000;
	do {
		err = nanosleep(&tv, &tv);
	} while (err && errno == EINTR);
	if (err)
		perror("nanosleep()");
}

static inline void _delay_ms(double ms)
{
	_usleep__((uint64_t)ceil(ms * 1000.0));
}

static inline void _delay_us(double us)
{
	_usleep__((uint64_t)ceil(us));
}


#endif /* FAKE_DELAY_H_ */
bues.ch cgit interface