Subversion Repositories Tronxy-X3A-Marlin

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 ron 1
/**
2
 * Marlin 3D Printer Firmware
3
 * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
 *
5
 * Based on Sprinter and grbl.
6
 * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
 
23
/**
24
 * AVR busy wait delay Cycles routines:
25
 *
26
 *  DELAY_CYCLES(count): Delay execution in cycles
27
 *  DELAY_NS(count): Delay execution in nanoseconds
28
 *  DELAY_US(count): Delay execution in microseconds
29
 */
30
 
31
#ifndef MARLIN_DELAY_H
32
#define MARLIN_DELAY_H
33
 
34
#define nop() __asm__ __volatile__("nop;\n\t":::)
35
 
36
FORCE_INLINE static void __delay_4cycles(uint8_t cy) {
37
  __asm__ __volatile__(
38
    L("1")
39
    A("dec %[cnt]")
40
    A("nop")
41
    A("brne 1b")
42
    : [cnt] "+r"(cy)  // output: +r means input+output
43
    :                 // input:
44
    : "cc"            // clobbers:
45
  );
46
}
47
 
48
/* ---------------- Delay in cycles */
49
FORCE_INLINE static void DELAY_CYCLES(uint16_t x) {
50
 
51
  if (__builtin_constant_p(x)) {
52
    #define MAXNOPS 4
53
 
54
    if (x <= (MAXNOPS)) {
55
      switch (x) { case 4: nop(); case 3: nop(); case 2: nop(); case 1: nop(); }
56
    }
57
    else {
58
      const uint32_t rem = (x) % (MAXNOPS);
59
      switch (rem) { case 3: nop(); case 2: nop(); case 1: nop(); }
60
      if ((x = (x) / (MAXNOPS)))
61
        __delay_4cycles(x); // if need more then 4 nop loop is more optimal
62
    }
63
 
64
    #undef MAXNOPS
65
  }
66
  else
67
    __delay_4cycles(x / 4);
68
}
69
#undef nop
70
 
71
/* ---------------- Delay in nanoseconds */
72
#define DELAY_NS(x) DELAY_CYCLES( (x) * (F_CPU/1000000L) / 1000L )
73
 
74
/* ---------------- Delay in microseconds */
75
#define DELAY_US(x) DELAY_CYCLES( (x) * (F_CPU/1000000L) )
76
 
77
#endif // MARLIN_DELAY_H