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.

timers.cpp 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /**
  2. * Marlin 3D Printer Firmware
  3. *
  4. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  5. * Copyright (c) 2016 Bob Cousins bobcousins42@googlemail.com
  6. * Copyright (c) 2015-2016 Nico Tonnhofer wurstnase.reprap@gmail.com
  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. #if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC)
  23. #include "../../inc/MarlinConfig.h"
  24. // ------------------------
  25. // Local defines
  26. // ------------------------
  27. // Default timer priorities. Override by specifying alternate priorities in the board pins file.
  28. // The TONE timer is not present here, as it currently cannot be set programmatically. It is set
  29. // by defining TIM_IRQ_PRIO in the variant.h or platformio.ini file, which adjusts the default
  30. // priority for STM32 HardwareTimer objects.
  31. #define SWSERIAL_TIMER_IRQ_PRIO_DEFAULT 1 // Requires tight bit timing to communicate reliably with TMC drivers
  32. #define SERVO_TIMER_IRQ_PRIO_DEFAULT 1 // Requires tight PWM timing to control a BLTouch reliably
  33. #define STEP_TIMER_IRQ_PRIO_DEFAULT 2
  34. #define TEMP_TIMER_IRQ_PRIO_DEFAULT 14 // Low priority avoids interference with other hardware and timers
  35. #ifndef STEP_TIMER_IRQ_PRIO
  36. #define STEP_TIMER_IRQ_PRIO STEP_TIMER_IRQ_PRIO_DEFAULT
  37. #endif
  38. #ifndef TEMP_TIMER_IRQ_PRIO
  39. #define TEMP_TIMER_IRQ_PRIO TEMP_TIMER_IRQ_PRIO_DEFAULT
  40. #endif
  41. #if HAS_TMC_SW_SERIAL
  42. #include <SoftwareSerial.h>
  43. #ifndef SWSERIAL_TIMER_IRQ_PRIO
  44. #define SWSERIAL_TIMER_IRQ_PRIO SWSERIAL_TIMER_IRQ_PRIO_DEFAULT
  45. #endif
  46. #endif
  47. #if HAS_SERVOS
  48. #include "Servo.h"
  49. #ifndef SERVO_TIMER_IRQ_PRIO
  50. #define SERVO_TIMER_IRQ_PRIO SERVO_TIMER_IRQ_PRIO_DEFAULT
  51. #endif
  52. #endif
  53. #if ENABLED(SPEAKER)
  54. // Ensure the default timer priority is somewhere between the STEP and TEMP priorities.
  55. // The STM32 framework defaults to interrupt 14 for all timers. This should be increased so that
  56. // timing-sensitive operations such as speaker output are not impacted by the long-running
  57. // temperature ISR. This must be defined in the platformio.ini file or the board's variant.h,
  58. // so that it will be consumed by framework code.
  59. #if !(TIM_IRQ_PRIO > STEP_TIMER_IRQ_PRIO && TIM_IRQ_PRIO < TEMP_TIMER_IRQ_PRIO)
  60. #error "Default timer interrupt priority is unspecified or set to a value which may degrade performance."
  61. #endif
  62. #endif
  63. #ifdef STM32F0xx
  64. #define MCU_STEP_TIMER 16
  65. #define MCU_TEMP_TIMER 17
  66. #elif defined(STM32F1xx)
  67. #define MCU_STEP_TIMER 4
  68. #define MCU_TEMP_TIMER 2
  69. #elif defined(STM32F401xC) || defined(STM32F401xE)
  70. #define MCU_STEP_TIMER 9
  71. #define MCU_TEMP_TIMER 10
  72. #elif defined(STM32F4xx) || defined(STM32F7xx)
  73. #define MCU_STEP_TIMER 6 // STM32F401 has no TIM6, TIM7, or TIM8
  74. #define MCU_TEMP_TIMER 14 // TIM7 is consumed by Software Serial if used.
  75. #endif
  76. #ifndef HAL_TIMER_RATE
  77. #define HAL_TIMER_RATE GetStepperTimerClkFreq()
  78. #endif
  79. #ifndef STEP_TIMER
  80. #define STEP_TIMER MCU_STEP_TIMER
  81. #endif
  82. #ifndef TEMP_TIMER
  83. #define TEMP_TIMER MCU_TEMP_TIMER
  84. #endif
  85. #define __TIMER_DEV(X) TIM##X
  86. #define _TIMER_DEV(X) __TIMER_DEV(X)
  87. #define STEP_TIMER_DEV _TIMER_DEV(STEP_TIMER)
  88. #define TEMP_TIMER_DEV _TIMER_DEV(TEMP_TIMER)
  89. #define __TIMER_IRQ_NAME(X) TIM##X##_IRQn
  90. #define _TIMER_IRQ_NAME(X) __TIMER_IRQ_NAME(X)
  91. #define STEP_TIMER_IRQ_NAME _TIMER_IRQ_NAME(STEP_TIMER)
  92. #define TEMP_TIMER_IRQ_NAME _TIMER_IRQ_NAME(TEMP_TIMER)
  93. // ------------------------
  94. // Private Variables
  95. // ------------------------
  96. HardwareTimer *timer_instance[NUM_HARDWARE_TIMERS] = { nullptr };
  97. // ------------------------
  98. // Public functions
  99. // ------------------------
  100. uint32_t GetStepperTimerClkFreq() {
  101. // Timer input clocks vary between devices, and in some cases between timers on the same device.
  102. // Retrieve at runtime to ensure device compatibility. Cache result to avoid repeated overhead.
  103. static uint32_t clkfreq = timer_instance[STEP_TIMER_NUM]->getTimerClkFreq();
  104. return clkfreq;
  105. }
  106. // frequency is in Hertz
  107. void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency) {
  108. if (!HAL_timer_initialized(timer_num)) {
  109. switch (timer_num) {
  110. case STEP_TIMER_NUM: // STEPPER TIMER - use a 32bit timer if possible
  111. timer_instance[timer_num] = new HardwareTimer(STEP_TIMER_DEV);
  112. /* Set the prescaler to the final desired value.
  113. * This will change the effective ISR callback frequency but when
  114. * HAL_timer_start(timer_num=0) is called in the core for the first time
  115. * the real frequency isn't important as long as, after boot, the ISR
  116. * gets called with the correct prescaler and count register. So here
  117. * we set the prescaler to the correct, final value and ignore the frequency
  118. * asked. We will call back the ISR in 1 second to start at full speed.
  119. *
  120. * The proper fix, however, would be a correct initialization OR a
  121. * HAL_timer_change(const uint8_t timer_num, const uint32_t frequency)
  122. * which changes the prescaler when an IRQ frequency change is needed
  123. * (for example when steppers are turned on)
  124. */
  125. timer_instance[timer_num]->setPrescaleFactor(STEPPER_TIMER_PRESCALE); //the -1 is done internally
  126. timer_instance[timer_num]->setOverflow(_MIN(hal_timer_t(HAL_TIMER_TYPE_MAX), (HAL_TIMER_RATE) / (STEPPER_TIMER_PRESCALE) /* /frequency */), TICK_FORMAT);
  127. break;
  128. case TEMP_TIMER_NUM: // TEMP TIMER - any available 16bit timer
  129. timer_instance[timer_num] = new HardwareTimer(TEMP_TIMER_DEV);
  130. // The prescale factor is computed automatically for HERTZ_FORMAT
  131. timer_instance[timer_num]->setOverflow(frequency, HERTZ_FORMAT);
  132. break;
  133. }
  134. // Disable preload. Leaving it default-enabled can cause the timer to stop if it happens
  135. // to exit the ISR after the start time for the next interrupt has already passed.
  136. timer_instance[timer_num]->setPreloadEnable(false);
  137. HAL_timer_enable_interrupt(timer_num);
  138. // Start the timer.
  139. timer_instance[timer_num]->resume(); // First call to resume() MUST follow the attachInterrupt()
  140. // This is fixed in Arduino_Core_STM32 1.8.
  141. // These calls can be removed and replaced with
  142. // timer_instance[timer_num]->setInterruptPriority
  143. switch (timer_num) {
  144. case STEP_TIMER_NUM:
  145. timer_instance[timer_num]->setInterruptPriority(STEP_TIMER_IRQ_PRIO, 0);
  146. break;
  147. case TEMP_TIMER_NUM:
  148. timer_instance[timer_num]->setInterruptPriority(TEMP_TIMER_IRQ_PRIO, 0);
  149. break;
  150. }
  151. }
  152. }
  153. void HAL_timer_enable_interrupt(const uint8_t timer_num) {
  154. if (HAL_timer_initialized(timer_num) && !timer_instance[timer_num]->hasInterrupt()) {
  155. switch (timer_num) {
  156. case STEP_TIMER_NUM:
  157. timer_instance[timer_num]->attachInterrupt(Step_Handler);
  158. break;
  159. case TEMP_TIMER_NUM:
  160. timer_instance[timer_num]->attachInterrupt(Temp_Handler);
  161. break;
  162. }
  163. }
  164. }
  165. void HAL_timer_disable_interrupt(const uint8_t timer_num) {
  166. if (HAL_timer_initialized(timer_num)) timer_instance[timer_num]->detachInterrupt();
  167. }
  168. bool HAL_timer_interrupt_enabled(const uint8_t timer_num) {
  169. return HAL_timer_initialized(timer_num) && timer_instance[timer_num]->hasInterrupt();
  170. }
  171. void SetTimerInterruptPriorities() {
  172. TERN_(HAS_TMC_SW_SERIAL, SoftwareSerial::setInterruptPriority(SWSERIAL_TIMER_IRQ_PRIO, 0));
  173. TERN_(HAS_SERVOS, libServo::setInterruptPriority(SERVO_TIMER_IRQ_PRIO, 0));
  174. }
  175. // This is a terrible hack to replicate the behavior used in the framework's SoftwareSerial.cpp
  176. // to choose a serial timer. It will select TIM7 on most boards used by Marlin, but this is more
  177. // resiliant to new MCUs which may not have a TIM7. Best practice is to explicitly specify
  178. // TIMER_SERIAL to avoid relying on framework selections which may not be predictable.
  179. #if !defined(TIMER_SERIAL)
  180. #if defined (TIM18_BASE)
  181. #define TIMER_SERIAL TIM18
  182. #elif defined (TIM7_BASE)
  183. #define TIMER_SERIAL TIM7
  184. #elif defined (TIM6_BASE)
  185. #define TIMER_SERIAL TIM6
  186. #elif defined (TIM22_BASE)
  187. #define TIMER_SERIAL TIM22
  188. #elif defined (TIM21_BASE)
  189. #define TIMER_SERIAL TIM21
  190. #elif defined (TIM17_BASE)
  191. #define TIMER_SERIAL TIM17
  192. #elif defined (TIM16_BASE)
  193. #define TIMER_SERIAL TIM16
  194. #elif defined (TIM15_BASE)
  195. #define TIMER_SERIAL TIM15
  196. #elif defined (TIM14_BASE)
  197. #define TIMER_SERIAL TIM14
  198. #elif defined (TIM13_BASE)
  199. #define TIMER_SERIAL TIM13
  200. #elif defined (TIM11_BASE)
  201. #define TIMER_SERIAL TIM11
  202. #elif defined (TIM10_BASE)
  203. #define TIMER_SERIAL TIM10
  204. #elif defined (TIM12_BASE)
  205. #define TIMER_SERIAL TIM12
  206. #elif defined (TIM19_BASE)
  207. #define TIMER_SERIAL TIM19
  208. #elif defined (TIM9_BASE)
  209. #define TIMER_SERIAL TIM9
  210. #elif defined (TIM5_BASE)
  211. #define TIMER_SERIAL TIM5
  212. #elif defined (TIM4_BASE)
  213. #define TIMER_SERIAL TIM4
  214. #elif defined (TIM3_BASE)
  215. #define TIMER_SERIAL TIM3
  216. #elif defined (TIM2_BASE)
  217. #define TIMER_SERIAL TIM2
  218. #elif defined (TIM20_BASE)
  219. #define TIMER_SERIAL TIM20
  220. #elif defined (TIM8_BASE)
  221. #define TIMER_SERIAL TIM8
  222. #elif defined (TIM1_BASE)
  223. #define TIMER_SERIAL TIM1
  224. #else
  225. #error No suitable timer found for SoftwareSerial, define TIMER_SERIAL in variant.h
  226. #endif
  227. #endif
  228. // Place all timers used into an array, then recursively check for duplicates during compilation.
  229. // This does not currently account for timers used for PWM, such as for fans.
  230. // Timers are actually pointers. Convert to integers to simplify constexpr logic.
  231. static constexpr uintptr_t timers_in_use[] = {
  232. uintptr_t(TEMP_TIMER_DEV), // Override in pins file
  233. uintptr_t(STEP_TIMER_DEV), // Override in pins file
  234. #if HAS_TMC_SW_SERIAL
  235. uintptr_t(TIMER_SERIAL), // Set in variant.h, or as a define in platformio.h if not present in variant.h
  236. #endif
  237. #if ENABLED(SPEAKER)
  238. uintptr_t(TIMER_TONE), // Set in variant.h, or as a define in platformio.h if not present in variant.h
  239. #endif
  240. #if HAS_SERVOS
  241. uintptr_t(TIMER_SERVO), // Set in variant.h, or as a define in platformio.h if not present in variant.h
  242. #endif
  243. };
  244. static constexpr bool verify_no_duplicate_timers() {
  245. LOOP_L_N(i, COUNT(timers_in_use))
  246. LOOP_S_L_N(j, i + 1, COUNT(timers_in_use))
  247. if (timers_in_use[i] == timers_in_use[j]) return false;
  248. return true;
  249. }
  250. // If this assertion fails at compile time, review the timers_in_use array. If default_envs is
  251. // defined properly in platformio.ini, VS Code can evaluate the array when hovering over it,
  252. // making it easy to identify the conflicting timers.
  253. static_assert(verify_no_duplicate_timers(), "One or more timer conflict detected");
  254. #endif // ARDUINO_ARCH_STM32 && !STM32GENERIC