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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. * Busy wait delay cycles routines:
  24. *
  25. * DELAY_CYCLES(count): Delay execution in cycles
  26. * DELAY_NS(count): Delay execution in nanoseconds
  27. * DELAY_US(count): Delay execution in microseconds
  28. */
  29. #ifndef MARLIN_DELAY_H
  30. #define MARLIN_DELAY_H
  31. #include "../core/macros.h"
  32. #if defined(__arm__) || defined(__thumb__)
  33. // https://blueprints.launchpad.net/gcc-arm-embedded/+spec/delay-cycles
  34. #define nop() __asm__ __volatile__("nop;\n\t":::)
  35. FORCE_INLINE static void __delay_4cycles(uint32_t cy) { // +1 cycle
  36. #if ARCH_PIPELINE_RELOAD_CYCLES < 2
  37. #define EXTRA_NOP_CYCLES A("nop")
  38. #else
  39. #define EXTRA_NOP_CYCLES ""
  40. #endif
  41. __asm__ __volatile__(
  42. A(".syntax unified") // is to prevent CM0,CM1 non-unified syntax
  43. L("1")
  44. A("subs %[cnt],#1")
  45. EXTRA_NOP_CYCLES
  46. A("bne 1b")
  47. : [cnt]"+r"(cy) // output: +r means input+output
  48. : // input:
  49. : "cc" // clobbers:
  50. );
  51. }
  52. // Delay in cycles
  53. FORCE_INLINE static void DELAY_CYCLES(uint32_t x) {
  54. if (__builtin_constant_p(x)) {
  55. #define MAXNOPS 4
  56. if (x <= (MAXNOPS)) {
  57. switch (x) { case 4: nop(); case 3: nop(); case 2: nop(); case 1: nop(); }
  58. }
  59. else { // because of +1 cycle inside delay_4cycles
  60. const uint32_t rem = (x - 1) % (MAXNOPS);
  61. switch (rem) { case 3: nop(); case 2: nop(); case 1: nop(); }
  62. if ((x = (x - 1) / (MAXNOPS)))
  63. __delay_4cycles(x); // if need more then 4 nop loop is more optimal
  64. }
  65. #undef MAXNOPS
  66. }
  67. else
  68. __delay_4cycles(x / 4);
  69. }
  70. #undef nop
  71. #elif defined(__AVR__)
  72. #define nop() __asm__ __volatile__("nop;\n\t":::)
  73. FORCE_INLINE static void __delay_4cycles(uint8_t cy) {
  74. __asm__ __volatile__(
  75. L("1")
  76. A("dec %[cnt]")
  77. A("nop")
  78. A("brne 1b")
  79. : [cnt] "+r"(cy) // output: +r means input+output
  80. : // input:
  81. : "cc" // clobbers:
  82. );
  83. }
  84. // Delay in cycles
  85. FORCE_INLINE static void DELAY_CYCLES(uint16_t x) {
  86. if (__builtin_constant_p(x)) {
  87. #define MAXNOPS 4
  88. if (x <= (MAXNOPS)) {
  89. switch (x) { case 4: nop(); case 3: nop(); case 2: nop(); case 1: nop(); }
  90. }
  91. else {
  92. const uint32_t rem = (x) % (MAXNOPS);
  93. switch (rem) { case 3: nop(); case 2: nop(); case 1: nop(); }
  94. if ((x = (x) / (MAXNOPS)))
  95. __delay_4cycles(x); // if need more then 4 nop loop is more optimal
  96. }
  97. #undef MAXNOPS
  98. }
  99. else
  100. __delay_4cycles(x / 4);
  101. }
  102. #undef nop
  103. #elif defined(ESP32)
  104. FORCE_INLINE static void DELAY_CYCLES(uint32_t x) {
  105. unsigned long ccount, stop;
  106. __asm__ __volatile__ ( "rsr %0, ccount" : "=a" (ccount) );
  107. stop = ccount + x; // This can overflow
  108. while (ccount < stop) { // This doesn't deal with overflows
  109. __asm__ __volatile__ ( "rsr %0, ccount" : "=a" (ccount) );
  110. }
  111. }
  112. #else
  113. #error "Unsupported MCU architecture"
  114. #endif
  115. // Delay in nanoseconds
  116. #define DELAY_NS(x) DELAY_CYCLES( (x) * (F_CPU/1000000L) / 1000L )
  117. // Delay in microseconds
  118. #define DELAY_US(x) DELAY_CYCLES( (x) * (F_CPU/1000000L) )
  119. #endif // MARLIN_DELAY_H