1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- #ifndef MARLIN_DELAY_H
- #define MARLIN_DELAY_H
- #define nop() __asm__ __volatile__("nop;\n\t":::)
- FORCE_INLINE static void __delay_4cycles(uint8_t cy) {
- __asm__ __volatile__(
- L("1")
- A("dec %[cnt]")
- A("nop")
- A("brne 1b")
- : [cnt] "+r"(cy)
- :
- : "cc"
- );
- }
- FORCE_INLINE static void DELAY_CYCLES(uint16_t x) {
- if (__builtin_constant_p(x)) {
- #define MAXNOPS 4
- if (x <= (MAXNOPS)) {
- switch (x) { case 4: nop(); case 3: nop(); case 2: nop(); case 1: nop(); }
- }
- else {
- const uint32_t rem = (x) % (MAXNOPS);
- switch (rem) { case 3: nop(); case 2: nop(); case 1: nop(); }
- if ((x = (x) / (MAXNOPS)))
- __delay_4cycles(x);
- }
- #undef MAXNOPS
- }
- else
- __delay_4cycles(x / 4);
- }
- #undef nop
- #define DELAY_NS(x) DELAY_CYCLES( (x) * (F_CPU/1000000L) / 1000L )
- #define DELAY_US(x) DELAY_CYCLES( (x) * (F_CPU/1000000L) )
- #endif
|