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.

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