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

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