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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. bool timer_enabled[NUM_HARDWARE_TIMERS] = { false };
  103. // ------------------------
  104. // Public functions
  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. HAL_timer_enable_interrupt(timer_num);
  135. /*
  136. * Initializes (and unfortunately starts) the timer.
  137. * This is needed to set correct IRQ priority at the moment but causes
  138. * no harm since every call to HAL_timer_start() is actually followed by
  139. * a call to HAL_timer_enable_interrupt() which means that there isn't
  140. * a case in which you want the timer to run without a callback.
  141. */
  142. timer_instance[timer_num]->resume(); // First call to resume() MUST follow the attachInterrupt()
  143. // This is fixed in Arduino_Core_STM32 1.8.
  144. // These calls can be removed and replaced with
  145. // timer_instance[timer_num]->setInterruptPriority
  146. switch (timer_num) {
  147. case STEP_TIMER_NUM:
  148. HAL_NVIC_SetPriority(STEP_TIMER_IRQ_NAME, STEP_TIMER_IRQ_PRIO, 0);
  149. break;
  150. case TEMP_TIMER_NUM:
  151. HAL_NVIC_SetPriority(TEMP_TIMER_IRQ_NAME, TEMP_TIMER_IRQ_PRIO, 0);
  152. break;
  153. }
  154. }
  155. }
  156. void HAL_timer_enable_interrupt(const uint8_t timer_num) {
  157. if (HAL_timer_initialized(timer_num) && !timer_enabled[timer_num]) {
  158. timer_enabled[timer_num] = true;
  159. switch (timer_num) {
  160. case STEP_TIMER_NUM:
  161. timer_instance[timer_num]->attachInterrupt(Step_Handler);
  162. break;
  163. case TEMP_TIMER_NUM:
  164. timer_instance[timer_num]->attachInterrupt(Temp_Handler);
  165. break;
  166. }
  167. }
  168. }
  169. void HAL_timer_disable_interrupt(const uint8_t timer_num) {
  170. if (HAL_timer_interrupt_enabled(timer_num)) {
  171. timer_instance[timer_num]->detachInterrupt();
  172. timer_enabled[timer_num] = false;
  173. }
  174. }
  175. bool HAL_timer_interrupt_enabled(const uint8_t timer_num) {
  176. return HAL_timer_initialized(timer_num) && timer_enabled[timer_num];
  177. }
  178. // Only for use within the HAL
  179. TIM_TypeDef * HAL_timer_device(const uint8_t timer_num) {
  180. switch (timer_num) {
  181. case STEP_TIMER_NUM: return STEP_TIMER_DEV;
  182. case TEMP_TIMER_NUM: return TEMP_TIMER_DEV;
  183. }
  184. return nullptr;
  185. }
  186. void SetTimerInterruptPriorities() {
  187. TERN_(HAS_TMC_SW_SERIAL, SoftwareSerial::setInterruptPriority(SWSERIAL_TIMER_IRQ_PRIO, 0));
  188. TERN_(HAS_SERVOS, libServo::setInterruptPriority(SERVO_TIMER_IRQ_PRIO, 0));
  189. }
  190. #endif // ARDUINO_ARCH_STM32 && !STM32GENERIC