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.h 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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) 2017 Victor Perez
  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. #pragma once
  23. /**
  24. * HAL for stm32duino.com based on Libmaple and compatible (STM32F1)
  25. */
  26. #include <stdint.h>
  27. #include <libmaple/timer.h>
  28. #include "../../core/boards.h"
  29. // ------------------------
  30. // Defines
  31. // ------------------------
  32. /**
  33. * TODO: Check and confirm what timer we will use for each Temps and stepper driving.
  34. * We should probable drive temps with PWM.
  35. */
  36. #define FORCE_INLINE __attribute__((always_inline)) inline
  37. typedef uint16_t hal_timer_t;
  38. #define HAL_TIMER_TYPE_MAX 0xFFFF
  39. #define HAL_TIMER_RATE uint32_t(F_CPU) // frequency of timers peripherals
  40. #ifndef STEP_TIMER_CHAN
  41. #define STEP_TIMER_CHAN 1 // Channel of the timer to use for compare and interrupts
  42. #endif
  43. #ifndef TEMP_TIMER_CHAN
  44. #define TEMP_TIMER_CHAN 1 // Channel of the timer to use for compare and interrupts
  45. #endif
  46. /**
  47. * Note: Timers may be used by platforms and libraries
  48. *
  49. * FAN PWMs:
  50. * With FAN_SOFT_PWM disabled the Temperature class uses
  51. * FANx_PIN timers to generate FAN PWM signals.
  52. *
  53. * Speaker:
  54. * When SPEAKER is enabled, one timer is allocated by maple/tone.cpp.
  55. * - If BEEPER_PIN has a timer channel (and USE_PIN_TIMER is
  56. * defined in tone.cpp) it uses the pin's own timer.
  57. * - Otherwise it uses Timer 8 on boards with STM32_HIGH_DENSITY
  58. * or Timer 4 on other boards.
  59. */
  60. #ifndef STEP_TIMER_NUM
  61. #if defined(MCU_STM32F103CB) || defined(MCU_STM32F103C8)
  62. #define STEP_TIMER_NUM 4 // For C8/CB boards, use timer 4
  63. #else
  64. #define STEP_TIMER_NUM 5 // for other boards, five is fine.
  65. #endif
  66. #endif
  67. #ifndef PULSE_TIMER_NUM
  68. #define PULSE_TIMER_NUM STEP_TIMER_NUM
  69. #endif
  70. #ifndef TEMP_TIMER_NUM
  71. #define TEMP_TIMER_NUM 2 // Timer Index for Temperature
  72. //#define TEMP_TIMER_NUM 4 // 2->4, Timer 2 for Stepper Current PWM
  73. #endif
  74. #if MB(BTT_SKR_MINI_E3_V1_0, BTT_SKR_E3_DIP, BTT_SKR_MINI_E3_V1_2, MKS_ROBIN_LITE)
  75. // SKR Mini E3 boards use PA8 as FAN_PIN, so TIMER 1 is used for Fan PWM.
  76. #ifdef STM32_HIGH_DENSITY
  77. #define SERVO0_TIMER_NUM 8 // tone.cpp uses Timer 4
  78. #else
  79. #define SERVO0_TIMER_NUM 3 // tone.cpp uses Timer 8
  80. #endif
  81. #else
  82. #define SERVO0_TIMER_NUM 1 // SERVO0 or BLTOUCH
  83. #endif
  84. #define STEP_TIMER_IRQ_PRIO 2
  85. #define TEMP_TIMER_IRQ_PRIO 3
  86. #define SERVO0_TIMER_IRQ_PRIO 1
  87. #define TEMP_TIMER_PRESCALE 1000 // prescaler for setting Temp timer, 72Khz
  88. #define TEMP_TIMER_FREQUENCY 1000 // temperature interrupt frequency
  89. #define STEPPER_TIMER_PRESCALE 18 // prescaler for setting stepper timer, 4Mhz
  90. #define STEPPER_TIMER_RATE (HAL_TIMER_RATE / STEPPER_TIMER_PRESCALE) // frequency of stepper timer
  91. #define STEPPER_TIMER_TICKS_PER_US ((STEPPER_TIMER_RATE) / 1000000) // stepper timer ticks per µs
  92. #define PULSE_TIMER_RATE STEPPER_TIMER_RATE // frequency of pulse timer
  93. #define PULSE_TIMER_PRESCALE STEPPER_TIMER_PRESCALE
  94. #define PULSE_TIMER_TICKS_PER_US STEPPER_TIMER_TICKS_PER_US
  95. timer_dev* get_timer_dev(int number);
  96. #define TIMER_DEV(num) get_timer_dev(num)
  97. #define STEP_TIMER_DEV TIMER_DEV(STEP_TIMER_NUM)
  98. #define TEMP_TIMER_DEV TIMER_DEV(TEMP_TIMER_NUM)
  99. #define ENABLE_STEPPER_DRIVER_INTERRUPT() timer_enable_irq(STEP_TIMER_DEV, STEP_TIMER_CHAN)
  100. #define DISABLE_STEPPER_DRIVER_INTERRUPT() timer_disable_irq(STEP_TIMER_DEV, STEP_TIMER_CHAN)
  101. #define STEPPER_ISR_ENABLED() HAL_timer_interrupt_enabled(STEP_TIMER_NUM)
  102. #define ENABLE_TEMPERATURE_INTERRUPT() timer_enable_irq(TEMP_TIMER_DEV, TEMP_TIMER_CHAN)
  103. #define DISABLE_TEMPERATURE_INTERRUPT() timer_disable_irq(TEMP_TIMER_DEV, TEMP_TIMER_CHAN)
  104. #define HAL_timer_get_count(timer_num) timer_get_count(TIMER_DEV(timer_num))
  105. // TODO change this
  106. #ifndef HAL_TEMP_TIMER_ISR
  107. #define HAL_TEMP_TIMER_ISR() extern "C" void tempTC_Handler()
  108. #endif
  109. #ifndef HAL_STEP_TIMER_ISR
  110. #define HAL_STEP_TIMER_ISR() extern "C" void stepTC_Handler()
  111. #endif
  112. extern "C" void tempTC_Handler();
  113. extern "C" void stepTC_Handler();
  114. // ------------------------
  115. // Public Variables
  116. // ------------------------
  117. //static HardwareTimer StepperTimer(STEP_TIMER_NUM);
  118. //static HardwareTimer TempTimer(TEMP_TIMER_NUM);
  119. // ------------------------
  120. // Public functions
  121. // ------------------------
  122. void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency);
  123. void HAL_timer_enable_interrupt(const uint8_t timer_num);
  124. void HAL_timer_disable_interrupt(const uint8_t timer_num);
  125. bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
  126. /**
  127. * NOTE: By default libmaple sets ARPE = 1, which means the Auto reload register is preloaded (will only update with an update event)
  128. * Thus we have to pause the timer, update the value, refresh, resume the timer.
  129. * That seems like a big waste of time and may be better to change the timer config to ARPE = 0, so ARR can be updated any time.
  130. * We are using a Channel in each timer in Capture/Compare mode. We could also instead use the Time Update Event Interrupt, but need to disable ARPE
  131. * so we can change the ARR value on the fly (without calling refresh), and not get an interrupt right there because we caused an UEV.
  132. * This mode pretty much makes 2 timers unusable for PWM since they have their counts updated all the time on ISRs.
  133. * The way Marlin manages timer interrupts doesn't make for an efficient usage in STM32F1
  134. * Todo: Look at that possibility later.
  135. */
  136. FORCE_INLINE static void HAL_timer_set_compare(const uint8_t timer_num, const hal_timer_t compare) {
  137. switch (timer_num) {
  138. case STEP_TIMER_NUM:
  139. // NOTE: WE have set ARPE = 0, which means the Auto reload register is not preloaded
  140. // and there is no need to use any compare, as in the timer mode used, setting ARR to the compare value
  141. // will result in exactly the same effect, ie trigerring an interrupt, and on top, set counter to 0
  142. timer_set_reload(STEP_TIMER_DEV, compare); // We reload direct ARR as needed during counting up
  143. break;
  144. case TEMP_TIMER_NUM:
  145. timer_set_compare(TEMP_TIMER_DEV, TEMP_TIMER_CHAN, compare);
  146. break;
  147. }
  148. }
  149. FORCE_INLINE static void HAL_timer_isr_prologue(const uint8_t timer_num) {
  150. switch (timer_num) {
  151. case STEP_TIMER_NUM:
  152. // No counter to clear
  153. timer_generate_update(STEP_TIMER_DEV);
  154. return;
  155. case TEMP_TIMER_NUM:
  156. timer_set_count(TEMP_TIMER_DEV, 0);
  157. timer_generate_update(TEMP_TIMER_DEV);
  158. return;
  159. }
  160. }
  161. #define HAL_timer_isr_epilogue(TIMER_NUM)
  162. // No command is available in framework to turn off ARPE bit, which is turned on by default in libmaple.
  163. // Needed here to reset ARPE=0 for stepper timer
  164. FORCE_INLINE static void timer_no_ARR_preload_ARPE(timer_dev *dev) {
  165. bb_peri_set_bit(&(dev->regs).gen->CR1, TIMER_CR1_ARPE_BIT, 0);
  166. }
  167. void timer_set_interrupt_priority(uint_fast8_t timer_num, uint_fast8_t priority);
  168. #define TIMER_OC_NO_PRELOAD 0 // Need to disable preload also on compare registers.