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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 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 <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #pragma once
  23. #include "../../inc/MarlinConfigPre.h"
  24. /**
  25. * Busy wait delay cycles routines:
  26. *
  27. * DELAY_CYCLES(count): Delay execution in cycles
  28. * DELAY_NS(count): Delay execution in nanoseconds
  29. * DELAY_US(count): Delay execution in microseconds
  30. */
  31. #include "../../core/macros.h"
  32. void calibrate_delay_loop();
  33. #if defined(__arm__) || defined(__thumb__)
  34. // We want to have delay_cycle function with the lowest possible overhead, so we adjust at the function at runtime based on the current CPU best feature
  35. typedef void (*DelayImpl)(uint32_t);
  36. extern DelayImpl DelayCycleFnc;
  37. // I've measured 36 cycles on my system to call the cycle waiting method, but it shouldn't change much to have a bit more margin, it only consume a bit more flash
  38. #define TRIP_POINT_FOR_CALLING_FUNCTION 40
  39. // A simple recursive template class that output exactly one 'nop' of code per recursion
  40. template <int N> struct NopWriter {
  41. FORCE_INLINE static void build() {
  42. __asm__ __volatile__("nop");
  43. NopWriter<N-1>::build();
  44. }
  45. };
  46. // End the loop
  47. template <> struct NopWriter<0> { FORCE_INLINE static void build() {} };
  48. namespace Private {
  49. // Split recursing template in 2 different class so we don't reach the maximum template instantiation depth limit
  50. template <bool belowTP, int N> struct Helper {
  51. FORCE_INLINE static void build() {
  52. DelayCycleFnc(N - 2); // Approximative cost of calling the function (might be off by one or 2 cycles)
  53. }
  54. };
  55. template <int N> struct Helper<true, N> {
  56. FORCE_INLINE static void build() {
  57. NopWriter<N - 1>::build();
  58. }
  59. };
  60. template <> struct Helper<true, 0> {
  61. FORCE_INLINE static void build() {}
  62. };
  63. }
  64. // Select a behavior based on the constexpr'ness of the parameter
  65. // If called with a compile-time parameter, then write as many NOP as required to reach the asked cycle count
  66. // (there is some tripping point here to start looping when it's more profitable than gruntly executing NOPs)
  67. // If not called from a compile-time parameter, fallback to a runtime loop counting version instead
  68. template <bool compileTime, int Cycles>
  69. struct SmartDelay {
  70. FORCE_INLINE SmartDelay(int) {
  71. if (Cycles == 0) return;
  72. Private::Helper<Cycles < TRIP_POINT_FOR_CALLING_FUNCTION, Cycles>::build();
  73. }
  74. };
  75. // Runtime version below. There is no way this would run under less than ~TRIP_POINT_FOR_CALLING_FUNCTION cycles
  76. template <int T>
  77. struct SmartDelay<false, T> {
  78. FORCE_INLINE SmartDelay(int v) { DelayCycleFnc(v); }
  79. };
  80. #define DELAY_CYCLES(X) do { SmartDelay<IS_CONSTEXPR(X), IS_CONSTEXPR(X) ? X : 0> _smrtdly_X(X); } while(0)
  81. #if GCC_VERSION <= 70000
  82. #define DELAY_CYCLES_VAR(X) DelayCycleFnc(X)
  83. #else
  84. #define DELAY_CYCLES_VAR DELAY_CYCLES
  85. #endif
  86. // For delay in microseconds, no smart delay selection is required, directly call the delay function
  87. // Teensy compiler is too old and does not accept smart delay compile-time / run-time selection correctly
  88. #define DELAY_US(x) DelayCycleFnc((x) * ((F_CPU) / 1000000UL))
  89. #elif defined(__AVR__)
  90. FORCE_INLINE static void __delay_up_to_3c(uint8_t cycles) {
  91. switch (cycles) {
  92. case 3:
  93. __asm__ __volatile__(A("RJMP .+0") A("NOP"));
  94. break;
  95. case 2:
  96. __asm__ __volatile__(A("RJMP .+0"));
  97. break;
  98. case 1:
  99. __asm__ __volatile__(A("NOP"));
  100. break;
  101. }
  102. }
  103. // Delay in cycles
  104. FORCE_INLINE static void DELAY_CYCLES(uint16_t cycles) {
  105. if (__builtin_constant_p(cycles)) {
  106. if (cycles <= 3) {
  107. __delay_up_to_3c(cycles);
  108. }
  109. else if (cycles == 4) {
  110. __delay_up_to_3c(2);
  111. __delay_up_to_3c(2);
  112. }
  113. else {
  114. cycles -= 1 + 4; // Compensate for the first LDI (1) and the first round (4)
  115. __delay_up_to_3c(cycles % 4);
  116. cycles /= 4;
  117. // The following code burns [1 + 4 * (rounds+1)] cycles
  118. uint16_t dummy;
  119. __asm__ __volatile__(
  120. // "manually" load counter from constants, otherwise the compiler may optimize this part away
  121. A("LDI %A[rounds], %[l]") // 1c
  122. A("LDI %B[rounds], %[h]") // 1c (compensating the non branching BRCC)
  123. L("1")
  124. A("SBIW %[rounds], 1") // 2c
  125. A("BRCC 1b") // 2c when branching, else 1c (end of loop)
  126. : // Outputs ...
  127. [rounds] "=w" (dummy) // Restrict to a wo (=) 16 bit register pair (w)
  128. : // Inputs ...
  129. [l] "M" (cycles%256), // Restrict to 0..255 constant (M)
  130. [h] "M" (cycles/256) // Restrict to 0..255 constant (M)
  131. :// Clobbers ...
  132. "cc" // Indicate we are modifying flags like Carry (cc)
  133. );
  134. }
  135. }
  136. else {
  137. __asm__ __volatile__(
  138. L("1")
  139. A("SBIW %[cycles], 4") // 2c
  140. A("BRCC 1b") // 2c when branching, else 1c (end of loop)
  141. : [cycles] "+w" (cycles) // output: Restrict to a rw (+) 16 bit register pair (w)
  142. : // input: -
  143. : "cc" // clobbers: We are modifying flags like Carry (cc)
  144. );
  145. }
  146. }
  147. // Delay in microseconds
  148. #define DELAY_US(x) DELAY_CYCLES((x) * ((F_CPU) / 1000000UL))
  149. #define DELAY_CYCLES_VAR DELAY_CYCLES
  150. #elif defined(ESP32) || defined(__PLAT_LINUX__) || defined(__PLAT_NATIVE_SIM__)
  151. // DELAY_CYCLES specified inside platform
  152. // Delay in microseconds
  153. #define DELAY_US(x) DELAY_CYCLES((x) * ((F_CPU) / 1000000UL))
  154. #else
  155. #error "Unsupported MCU architecture"
  156. #endif
  157. /**************************************************************
  158. * Delay in nanoseconds. Requires the F_CPU macro.
  159. * These macros follow avr-libc delay conventions.
  160. *
  161. * For AVR there are three possible operation modes, due to its
  162. * slower clock speeds and thus coarser delay resolution. For
  163. * example, when F_CPU = 16000000 the resolution is 62.5ns.
  164. *
  165. * Round up (default)
  166. * Round up the delay according to the CPU clock resolution.
  167. * e.g., 100 will give a delay of 2 cycles (125ns).
  168. *
  169. * Round down (DELAY_NS_ROUND_DOWN)
  170. * Round down the delay according to the CPU clock resolution.
  171. * e.g., 100 will be rounded down to 1 cycle (62.5ns).
  172. *
  173. * Nearest (DELAY_NS_ROUND_CLOSEST)
  174. * Round the delay to the nearest number of clock cycles.
  175. * e.g., 165 will be rounded up to 3 cycles (187.5ns) because
  176. * it's closer to the requested delay than 2 cycle (125ns).
  177. */
  178. #ifndef __AVR__
  179. #undef DELAY_NS_ROUND_DOWN
  180. #undef DELAY_NS_ROUND_CLOSEST
  181. #endif
  182. #if ENABLED(DELAY_NS_ROUND_DOWN)
  183. #define _NS_TO_CYCLES(x) ( (x) * ((F_CPU) / 1000000UL) / 1000UL) // floor
  184. #elif ENABLED(DELAY_NS_ROUND_CLOSEST)
  185. #define _NS_TO_CYCLES(x) (((x) * ((F_CPU) / 1000000UL) + 500) / 1000UL) // round
  186. #else
  187. #define _NS_TO_CYCLES(x) (((x) * ((F_CPU) / 1000000UL) + 999) / 1000UL) // "ceil"
  188. #endif
  189. #define DELAY_NS(x) DELAY_CYCLES(_NS_TO_CYCLES(x))
  190. #define DELAY_NS_VAR(x) DELAY_CYCLES_VAR(_NS_TO_CYCLES(x))