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 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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) || defined(STM32H7xx)
  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. // ------------------------
  90. // Private Variables
  91. // ------------------------
  92. HardwareTimer *timer_instance[NUM_HARDWARE_TIMERS] = { nullptr };
  93. // ------------------------
  94. // Public functions
  95. // ------------------------
  96. uint32_t GetStepperTimerClkFreq() {
  97. // Timer input clocks vary between devices, and in some cases between timers on the same device.
  98. // Retrieve at runtime to ensure device compatibility. Cache result to avoid repeated overhead.
  99. static uint32_t clkfreq = timer_instance[STEP_TIMER_NUM]->getTimerClkFreq();
  100. return clkfreq;
  101. }
  102. // frequency is in Hertz
  103. void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency) {
  104. if (!HAL_timer_initialized(timer_num)) {
  105. switch (timer_num) {
  106. case STEP_TIMER_NUM: // STEPPER TIMER - use a 32bit timer if possible
  107. timer_instance[timer_num] = new HardwareTimer(STEP_TIMER_DEV);
  108. /* Set the prescaler to the final desired value.
  109. * This will change the effective ISR callback frequency but when
  110. * HAL_timer_start(timer_num=0) is called in the core for the first time
  111. * the real frequency isn't important as long as, after boot, the ISR
  112. * gets called with the correct prescaler and count register. So here
  113. * we set the prescaler to the correct, final value and ignore the frequency
  114. * asked. We will call back the ISR in 1 second to start at full speed.
  115. *
  116. * The proper fix, however, would be a correct initialization OR a
  117. * HAL_timer_change(const uint8_t timer_num, const uint32_t frequency)
  118. * which changes the prescaler when an IRQ frequency change is needed
  119. * (for example when steppers are turned on)
  120. */
  121. timer_instance[timer_num]->setPrescaleFactor(STEPPER_TIMER_PRESCALE); //the -1 is done internally
  122. timer_instance[timer_num]->setOverflow(_MIN(hal_timer_t(HAL_TIMER_TYPE_MAX), (HAL_TIMER_RATE) / (STEPPER_TIMER_PRESCALE) /* /frequency */), TICK_FORMAT);
  123. break;
  124. case TEMP_TIMER_NUM: // TEMP TIMER - any available 16bit timer
  125. timer_instance[timer_num] = new HardwareTimer(TEMP_TIMER_DEV);
  126. // The prescale factor is computed automatically for HERTZ_FORMAT
  127. timer_instance[timer_num]->setOverflow(frequency, HERTZ_FORMAT);
  128. break;
  129. }
  130. // Disable preload. Leaving it default-enabled can cause the timer to stop if it happens
  131. // to exit the ISR after the start time for the next interrupt has already passed.
  132. timer_instance[timer_num]->setPreloadEnable(false);
  133. HAL_timer_enable_interrupt(timer_num);
  134. // Start the timer.
  135. timer_instance[timer_num]->resume(); // First call to resume() MUST follow the attachInterrupt()
  136. // This is fixed in Arduino_Core_STM32 1.8.
  137. // These calls can be removed and replaced with
  138. // timer_instance[timer_num]->setInterruptPriority
  139. switch (timer_num) {
  140. case STEP_TIMER_NUM:
  141. timer_instance[timer_num]->setInterruptPriority(STEP_TIMER_IRQ_PRIO, 0);
  142. break;
  143. case TEMP_TIMER_NUM:
  144. timer_instance[timer_num]->setInterruptPriority(TEMP_TIMER_IRQ_PRIO, 0);
  145. break;
  146. }
  147. }
  148. }
  149. void HAL_timer_enable_interrupt(const uint8_t timer_num) {
  150. if (HAL_timer_initialized(timer_num) && !timer_instance[timer_num]->hasInterrupt()) {
  151. switch (timer_num) {
  152. case STEP_TIMER_NUM:
  153. timer_instance[timer_num]->attachInterrupt(Step_Handler);
  154. break;
  155. case TEMP_TIMER_NUM:
  156. timer_instance[timer_num]->attachInterrupt(Temp_Handler);
  157. break;
  158. }
  159. }
  160. }
  161. void HAL_timer_disable_interrupt(const uint8_t timer_num) {
  162. if (HAL_timer_initialized(timer_num)) timer_instance[timer_num]->detachInterrupt();
  163. }
  164. bool HAL_timer_interrupt_enabled(const uint8_t timer_num) {
  165. return HAL_timer_initialized(timer_num) && timer_instance[timer_num]->hasInterrupt();
  166. }
  167. void SetTimerInterruptPriorities() {
  168. TERN_(HAS_TMC_SW_SERIAL, SoftwareSerial::setInterruptPriority(SWSERIAL_TIMER_IRQ_PRIO, 0));
  169. TERN_(HAS_SERVOS, libServo::setInterruptPriority(SERVO_TIMER_IRQ_PRIO, 0));
  170. }
  171. // ------------------------
  172. // Detect timer conflicts
  173. // ------------------------
  174. // This list serves two purposes. Firstly, it facilitates build-time mapping between
  175. // variant-defined timer names (such as TIM1) and timer numbers. It also replicates
  176. // the order of timers used in the framework's SoftwareSerial.cpp. The first timer in
  177. // this list will be automatically used by SoftwareSerial if it is not already defined
  178. // in the board's variant or compiler options.
  179. static constexpr struct {uintptr_t base_address; int timer_number;} stm32_timer_map[] = {
  180. #ifdef TIM18_BASE
  181. { uintptr_t(TIM18), 18 },
  182. #endif
  183. #ifdef TIM7_BASE
  184. { uintptr_t(TIM7), 7 },
  185. #endif
  186. #ifdef TIM6_BASE
  187. { uintptr_t(TIM6), 6 },
  188. #endif
  189. #ifdef TIM22_BASE
  190. { uintptr_t(TIM22), 22 },
  191. #endif
  192. #ifdef TIM21_BASE
  193. { uintptr_t(TIM21), 21 },
  194. #endif
  195. #ifdef TIM17_BASE
  196. { uintptr_t(TIM17), 17 },
  197. #endif
  198. #ifdef TIM16_BASE
  199. { uintptr_t(TIM16), 16 },
  200. #endif
  201. #ifdef TIM15_BASE
  202. { uintptr_t(TIM15), 15 },
  203. #endif
  204. #ifdef TIM14_BASE
  205. { uintptr_t(TIM14), 14 },
  206. #endif
  207. #ifdef TIM13_BASE
  208. { uintptr_t(TIM13), 13 },
  209. #endif
  210. #ifdef TIM11_BASE
  211. { uintptr_t(TIM11), 11 },
  212. #endif
  213. #ifdef TIM10_BASE
  214. { uintptr_t(TIM10), 10 },
  215. #endif
  216. #ifdef TIM12_BASE
  217. { uintptr_t(TIM12), 12 },
  218. #endif
  219. #ifdef TIM19_BASE
  220. { uintptr_t(TIM19), 19 },
  221. #endif
  222. #ifdef TIM9_BASE
  223. { uintptr_t(TIM9), 9 },
  224. #endif
  225. #ifdef TIM5_BASE
  226. { uintptr_t(TIM5), 5 },
  227. #endif
  228. #ifdef TIM4_BASE
  229. { uintptr_t(TIM4), 4 },
  230. #endif
  231. #ifdef TIM3_BASE
  232. { uintptr_t(TIM3), 3 },
  233. #endif
  234. #ifdef TIM2_BASE
  235. { uintptr_t(TIM2), 2 },
  236. #endif
  237. #ifdef TIM20_BASE
  238. { uintptr_t(TIM20), 20 },
  239. #endif
  240. #ifdef TIM8_BASE
  241. { uintptr_t(TIM8), 8 },
  242. #endif
  243. #ifdef TIM1_BASE
  244. { uintptr_t(TIM1), 1 }
  245. #endif
  246. };
  247. // Convert from a timer base address to its integer timer number.
  248. static constexpr int get_timer_num_from_base_address(uintptr_t base_address) {
  249. for (const auto &timer : stm32_timer_map)
  250. if (timer.base_address == base_address) return timer.timer_number;
  251. return 0;
  252. }
  253. // The platform's SoftwareSerial.cpp will use the first timer from stm32_timer_map.
  254. #if HAS_TMC_SW_SERIAL && !defined(TIMER_SERIAL)
  255. #define TIMER_SERIAL (stm32_timer_map[0].base_address)
  256. #endif
  257. // constexpr doesn't like using the base address pointers that timers evaluate to.
  258. // We can get away with casting them to uintptr_t, if we do so inside an array.
  259. // GCC will not currently do it directly to a uintptr_t.
  260. IF_ENABLED(HAS_TMC_SW_SERIAL, static constexpr uintptr_t timer_serial[] = {uintptr_t(TIMER_SERIAL)});
  261. IF_ENABLED(SPEAKER, static constexpr uintptr_t timer_tone[] = {uintptr_t(TIMER_TONE)});
  262. IF_ENABLED(HAS_SERVOS, static constexpr uintptr_t timer_servo[] = {uintptr_t(TIMER_SERVO)});
  263. enum TimerPurpose { TP_SERIAL, TP_TONE, TP_SERVO, TP_STEP, TP_TEMP };
  264. // List of timers, to enable checking for conflicts.
  265. // Includes the purpose of each timer to ease debugging when evaluating at build-time.
  266. // This cannot yet account for timers used for PWM output, such as for fans.
  267. static constexpr struct { TimerPurpose p; int t; } timers_in_use[] = {
  268. #if HAS_TMC_SW_SERIAL
  269. {TP_SERIAL, get_timer_num_from_base_address(timer_serial[0])}, // Set in variant.h, or as a define in platformio.h if not present in variant.h
  270. #endif
  271. #if ENABLED(SPEAKER)
  272. {TP_TONE, get_timer_num_from_base_address(timer_tone[0])}, // Set in variant.h, or as a define in platformio.h if not present in variant.h
  273. #endif
  274. #if HAS_SERVOS
  275. {TP_SERVO, get_timer_num_from_base_address(timer_servo[0])}, // Set in variant.h, or as a define in platformio.h if not present in variant.h
  276. #endif
  277. {TP_STEP, STEP_TIMER},
  278. {TP_TEMP, TEMP_TIMER},
  279. };
  280. static constexpr bool verify_no_timer_conflicts() {
  281. LOOP_L_N(i, COUNT(timers_in_use))
  282. LOOP_S_L_N(j, i + 1, COUNT(timers_in_use))
  283. if (timers_in_use[i].t == timers_in_use[j].t) return false;
  284. return true;
  285. }
  286. // If this assertion fails at compile time, review the timers_in_use array.
  287. // If default_envs is defined properly in platformio.ini, VS Code can evaluate the array
  288. // when hovering over it, making it easy to identify the conflicting timers.
  289. static_assert(verify_no_timer_conflicts(), "One or more timer conflict detected. Examine \"timers_in_use\" to help identify conflict.");
  290. #endif // ARDUINO_ARCH_STM32 && !STM32GENERIC