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

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