My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Delay.h 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2019 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. #pragma once
  23. /**
  24. * 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. #include "../../core/millis_t.h"
  31. #include "../../core/macros.h"
  32. #if defined(__arm__) || defined(__thumb__)
  33. #if WITHIN(__CORTEX_M, 3, 7)
  34. // Cortex-M3 through M7 can use the cycle counter of the DWT unit
  35. // http://www.anthonyvh.com/2017/05/18/cortex_m-cycle_counter/
  36. FORCE_INLINE static void enableCycleCounter() {
  37. CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
  38. #if __CORTEX_M == 7
  39. DWT->LAR = 0xC5ACCE55; // Unlock DWT on the M7
  40. #endif
  41. DWT->CYCCNT = 0;
  42. DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
  43. }
  44. FORCE_INLINE volatile uint32_t getCycleCount() { return DWT->CYCCNT; }
  45. FORCE_INLINE static void DELAY_CYCLES(const uint32_t x) {
  46. const uint32_t endCycles = getCycleCount() + x;
  47. while (PENDING(getCycleCount(), endCycles)) {}
  48. }
  49. #else
  50. // https://blueprints.launchpad.net/gcc-arm-embedded/+spec/delay-cycles
  51. #define nop() __asm__ __volatile__("nop;\n\t":::)
  52. FORCE_INLINE static void __delay_4cycles(uint32_t cy) { // +1 cycle
  53. #if ARCH_PIPELINE_RELOAD_CYCLES < 2
  54. #define EXTRA_NOP_CYCLES A("nop")
  55. #else
  56. #define EXTRA_NOP_CYCLES ""
  57. #endif
  58. __asm__ __volatile__(
  59. A(".syntax unified") // is to prevent CM0,CM1 non-unified syntax
  60. L("1")
  61. A("subs %[cnt],#1")
  62. EXTRA_NOP_CYCLES
  63. A("bne 1b")
  64. : [cnt]"+r"(cy) // output: +r means input+output
  65. : // input:
  66. : "cc" // clobbers:
  67. );
  68. }
  69. // Delay in cycles
  70. FORCE_INLINE static void DELAY_CYCLES(uint32_t x) {
  71. if (__builtin_constant_p(x)) {
  72. #define MAXNOPS 4
  73. if (x <= (MAXNOPS)) {
  74. switch (x) { case 4: nop(); case 3: nop(); case 2: nop(); case 1: nop(); }
  75. }
  76. else { // because of +1 cycle inside delay_4cycles
  77. const uint32_t rem = (x - 1) % (MAXNOPS);
  78. switch (rem) { case 3: nop(); case 2: nop(); case 1: nop(); }
  79. if ((x = (x - 1) / (MAXNOPS)))
  80. __delay_4cycles(x); // if need more then 4 nop loop is more optimal
  81. }
  82. #undef MAXNOPS
  83. }
  84. else if ((x >>= 2))
  85. __delay_4cycles(x);
  86. }
  87. #undef nop
  88. #endif
  89. #elif defined(__AVR__)
  90. #define nop() __asm__ __volatile__("nop;\n\t":::)
  91. FORCE_INLINE static void __delay_4cycles(uint8_t cy) {
  92. __asm__ __volatile__(
  93. L("1")
  94. A("dec %[cnt]")
  95. A("nop")
  96. A("brne 1b")
  97. : [cnt] "+r"(cy) // output: +r means input+output
  98. : // input:
  99. : "cc" // clobbers:
  100. );
  101. }
  102. // Delay in cycles
  103. FORCE_INLINE static void DELAY_CYCLES(uint16_t x) {
  104. if (__builtin_constant_p(x)) {
  105. #define MAXNOPS 4
  106. if (x <= (MAXNOPS)) {
  107. switch (x) { case 4: nop(); case 3: nop(); case 2: nop(); case 1: nop(); }
  108. }
  109. else {
  110. const uint32_t rem = (x) % (MAXNOPS);
  111. switch (rem) { case 3: nop(); case 2: nop(); case 1: nop(); }
  112. if ((x = (x) / (MAXNOPS)))
  113. __delay_4cycles(x); // if need more then 4 nop loop is more optimal
  114. }
  115. #undef MAXNOPS
  116. }
  117. else if ((x >>= 2))
  118. __delay_4cycles(x);
  119. }
  120. #undef nop
  121. #elif defined(ESP32)
  122. FORCE_INLINE static void DELAY_CYCLES(uint32_t x) {
  123. unsigned long ccount, stop;
  124. __asm__ __volatile__ ( "rsr %0, ccount" : "=a" (ccount) );
  125. stop = ccount + x; // This can overflow
  126. while (ccount < stop) { // This doesn't deal with overflows
  127. __asm__ __volatile__ ( "rsr %0, ccount" : "=a" (ccount) );
  128. }
  129. }
  130. #elif defined(__PLAT_LINUX__)
  131. // specified inside platform
  132. #else
  133. #error "Unsupported MCU architecture"
  134. #endif
  135. // Delay in nanoseconds
  136. #define DELAY_NS(x) DELAY_CYCLES( (x) * (F_CPU / 1000000UL) / 1000UL )
  137. // Delay in microseconds
  138. #define DELAY_US(x) DELAY_CYCLES( (x) * (F_CPU / 1000000UL) )