]> git.tdb.fi Git - model-railway-devices.git/blob - common/delay.h
Reorganize the directory structure
[model-railway-devices.git] / common / delay.h
1 #ifndef DELAY_H_
2 #define DELAY_H_
3
4 static inline void __attribute__((always_inline)) delay_loop8(uint8_t count)
5 {
6         __asm__ volatile (
7                 "1: dec %0" "\n\t"
8                 "brne 1b"
9                 : "=r" (count)
10                 : "0" (count)
11         );
12 }
13
14 static inline void __attribute__((always_inline)) delay_loop16(uint16_t count)
15 {
16         __asm__ volatile (
17                 "1: sbiw %0, 1" "\n\t"
18                 "brne 1b"
19                 : "=r" (count)
20                 : "0" (count)
21         );
22 }
23
24 static inline void __attribute__((always_inline)) delay_us(uint16_t us)
25 {
26         uint16_t clocks = F_CPU/1000000*us;
27         if(clocks<768)
28                 delay_loop8(clocks/3);
29         else
30                 delay_loop16(clocks/4);
31 }
32
33 static inline void __attribute__((always_inline)) delay_ms(uint16_t ms)
34 {
35         if(ms<0x40000/(F_CPU/1000))
36                 delay_loop16(F_CPU/1000*ms/4);
37         else
38         {
39                 uint16_t i = ms*10;
40                 while(--i)
41                         delay_loop16(F_CPU/40000);
42         }
43 }
44
45 #endif