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

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