aboutsummaryrefslogtreecommitdiff
path: root/tmk_core/common/wait.h
diff options
context:
space:
mode:
Diffstat (limited to 'tmk_core/common/wait.h')
-rw-r--r--tmk_core/common/wait.h52
1 files changed, 52 insertions, 0 deletions
diff --git a/tmk_core/common/wait.h b/tmk_core/common/wait.h
index 89128e9da..0b3fd755a 100644
--- a/tmk_core/common/wait.h
+++ b/tmk_core/common/wait.h
@@ -6,10 +6,62 @@
6extern "C" { 6extern "C" {
7#endif 7#endif
8 8
9#if defined(__ARMEL__) || defined(__ARMEB__)
10# ifndef __OPTIMIZE__
11# pragma message "Compiler optimizations disabled; wait_cpuclock() won't work as designed"
12# endif
13
14# define wait_cpuclock(x) wait_cpuclock_allnop(x)
15
16# define CLOCK_DELAY_NOP8 "nop\n\t nop\n\t nop\n\t nop\n\t nop\n\t nop\n\t nop\n\t nop\n\t"
17
18__attribute__((always_inline))
19static inline void wait_cpuclock_allnop(unsigned int n) { /* n: 1..135 */
20 /* The argument n must be a constant expression.
21 * That way, compiler optimization will remove unnecessary code. */
22 if (n < 1) { return; }
23 if (n > 8) {
24 unsigned int n8 = n/8;
25 n = n - n8*8;
26 switch (n8) {
27 case 16: asm volatile (CLOCK_DELAY_NOP8::: "memory");
28 case 15: asm volatile (CLOCK_DELAY_NOP8::: "memory");
29 case 14: asm volatile (CLOCK_DELAY_NOP8::: "memory");
30 case 13: asm volatile (CLOCK_DELAY_NOP8::: "memory");
31 case 12: asm volatile (CLOCK_DELAY_NOP8::: "memory");
32 case 11: asm volatile (CLOCK_DELAY_NOP8::: "memory");
33 case 10: asm volatile (CLOCK_DELAY_NOP8::: "memory");
34 case 9: asm volatile (CLOCK_DELAY_NOP8::: "memory");
35 case 8: asm volatile (CLOCK_DELAY_NOP8::: "memory");
36 case 7: asm volatile (CLOCK_DELAY_NOP8::: "memory");
37 case 6: asm volatile (CLOCK_DELAY_NOP8::: "memory");
38 case 5: asm volatile (CLOCK_DELAY_NOP8::: "memory");
39 case 4: asm volatile (CLOCK_DELAY_NOP8::: "memory");
40 case 3: asm volatile (CLOCK_DELAY_NOP8::: "memory");
41 case 2: asm volatile (CLOCK_DELAY_NOP8::: "memory");
42 case 1: asm volatile (CLOCK_DELAY_NOP8::: "memory");
43 case 0: break;
44 }
45 }
46 switch (n) {
47 case 8: asm volatile ("nop"::: "memory");
48 case 7: asm volatile ("nop"::: "memory");
49 case 6: asm volatile ("nop"::: "memory");
50 case 5: asm volatile ("nop"::: "memory");
51 case 4: asm volatile ("nop"::: "memory");
52 case 3: asm volatile ("nop"::: "memory");
53 case 2: asm volatile ("nop"::: "memory");
54 case 1: asm volatile ("nop"::: "memory");
55 case 0: break;
56 }
57}
58#endif
59
9#if defined(__AVR__) 60#if defined(__AVR__)
10# include <util/delay.h> 61# include <util/delay.h>
11# define wait_ms(ms) _delay_ms(ms) 62# define wait_ms(ms) _delay_ms(ms)
12# define wait_us(us) _delay_us(us) 63# define wait_us(us) _delay_us(us)
64# define wait_cpuclock(x) __builtin_avr_delay_cycles(x)
13#elif defined PROTOCOL_CHIBIOS 65#elif defined PROTOCOL_CHIBIOS
14# include <ch.h> 66# include <ch.h>
15# define wait_ms(ms) \ 67# define wait_ms(ms) \