My Marlin configs for Fabrikator Mini and CTC i3 Pro B
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

timers.h 7.1KB

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