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.

HAL_timers_STM32F1.h 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /**
  2. * Marlin 3D Printer Firmware
  3. *
  4. * Copyright (c) 2019 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. // ------------------------
  29. // Defines
  30. // ------------------------
  31. /**
  32. * TODO: Check and confirm what timer we will use for each Temps and stepper driving.
  33. * We should probable drive temps with PWM.
  34. */
  35. #define FORCE_INLINE __attribute__((always_inline)) inline
  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. #define STEP_TIMER_CHAN 1 // Channel of the timer to use for compare and interrupts
  40. #define TEMP_TIMER_CHAN 1 // Channel of the timer to use for compare and interrupts
  41. #if defined(MCU_STM32F103CB) || defined(MCU_STM32F103C8)
  42. #define STEP_TIMER_NUM 4 // For C8/CB boards, use timer 4
  43. #else
  44. #define STEP_TIMER_NUM 5 // for other boards, five is fine.
  45. #endif
  46. #define TEMP_TIMER_NUM 2 // index of timer to use for temperature
  47. //#define TEMP_TIMER_NUM 4 // 2->4, Timer 2 for Stepper Current PWM
  48. #define PULSE_TIMER_NUM STEP_TIMER_NUM
  49. #define SERVO0_TIMER_NUM 1 // SERVO0 or BLTOUCH
  50. #define STEP_TIMER_IRQ_PRIO 1
  51. #define TEMP_TIMER_IRQ_PRIO 2
  52. #define TEMP_TIMER_PRESCALE 1000 // prescaler for setting Temp timer, 72Khz
  53. #define TEMP_TIMER_FREQUENCY 1000 // temperature interrupt frequency
  54. #define STEPPER_TIMER_PRESCALE 18 // prescaler for setting stepper timer, 4Mhz
  55. #define STEPPER_TIMER_RATE (HAL_TIMER_RATE / STEPPER_TIMER_PRESCALE) // frequency of stepper timer
  56. #define STEPPER_TIMER_TICKS_PER_US ((STEPPER_TIMER_RATE) / 1000000) // stepper timer ticks per µs
  57. #define PULSE_TIMER_RATE STEPPER_TIMER_RATE // frequency of pulse timer
  58. #define PULSE_TIMER_PRESCALE STEPPER_TIMER_PRESCALE
  59. #define PULSE_TIMER_TICKS_PER_US STEPPER_TIMER_TICKS_PER_US
  60. timer_dev* get_timer_dev(int number);
  61. #define TIMER_DEV(num) get_timer_dev(num)
  62. #define STEP_TIMER_DEV TIMER_DEV(STEP_TIMER_NUM)
  63. #define TEMP_TIMER_DEV TIMER_DEV(TEMP_TIMER_NUM)
  64. #define ENABLE_STEPPER_DRIVER_INTERRUPT() timer_enable_irq(STEP_TIMER_DEV, STEP_TIMER_CHAN)
  65. #define DISABLE_STEPPER_DRIVER_INTERRUPT() timer_disable_irq(STEP_TIMER_DEV, STEP_TIMER_CHAN)
  66. #define STEPPER_ISR_ENABLED() HAL_timer_interrupt_enabled(STEP_TIMER_NUM)
  67. #define ENABLE_TEMPERATURE_INTERRUPT() timer_enable_irq(TEMP_TIMER_DEV, TEMP_TIMER_CHAN)
  68. #define DISABLE_TEMPERATURE_INTERRUPT() timer_disable_irq(TEMP_TIMER_DEV, TEMP_TIMER_CHAN)
  69. #define HAL_timer_get_count(timer_num) timer_get_count(TIMER_DEV(timer_num))
  70. // TODO change this
  71. #define HAL_TEMP_TIMER_ISR() extern "C" void tempTC_Handler(void)
  72. #define HAL_STEP_TIMER_ISR() extern "C" void stepTC_Handler(void)
  73. extern "C" void tempTC_Handler(void);
  74. extern "C" void stepTC_Handler(void);
  75. // ------------------------
  76. // Public Variables
  77. // ------------------------
  78. //static HardwareTimer StepperTimer(STEP_TIMER_NUM);
  79. //static HardwareTimer TempTimer(TEMP_TIMER_NUM);
  80. // ------------------------
  81. // Public functions
  82. // ------------------------
  83. void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency);
  84. void HAL_timer_enable_interrupt(const uint8_t timer_num);
  85. void HAL_timer_disable_interrupt(const uint8_t timer_num);
  86. bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
  87. /**
  88. * NOTE: By default libmaple sets ARPE = 1, which means the Auto reload register is preloaded (will only update with an update event)
  89. * Thus we have to pause the timer, update the value, refresh, resume the timer.
  90. * 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.
  91. * 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
  92. * 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.
  93. * This mode pretty much makes 2 timers unusable for PWM since they have their counts updated all the time on ISRs.
  94. * The way Marlin manages timer interrupts doesn't make for an efficient usage in STM32F1
  95. * Todo: Look at that possibility later.
  96. */
  97. FORCE_INLINE static void HAL_timer_set_compare(const uint8_t timer_num, const hal_timer_t compare) {
  98. switch (timer_num) {
  99. case STEP_TIMER_NUM:
  100. // NOTE: WE have set ARPE = 0, which means the Auto reload register is not preloaded
  101. // and there is no need to use any compare, as in the timer mode used, setting ARR to the compare value
  102. // will result in exactly the same effect, ie trigerring an interrupt, and on top, set counter to 0
  103. timer_set_reload(STEP_TIMER_DEV, compare); // We reload direct ARR as needed during counting up
  104. break;
  105. case TEMP_TIMER_NUM:
  106. timer_set_compare(TEMP_TIMER_DEV, TEMP_TIMER_CHAN, compare);
  107. break;
  108. }
  109. }
  110. FORCE_INLINE static void HAL_timer_isr_prologue(const uint8_t timer_num) {
  111. switch (timer_num) {
  112. case STEP_TIMER_NUM:
  113. // No counter to clear
  114. timer_generate_update(STEP_TIMER_DEV);
  115. return;
  116. case TEMP_TIMER_NUM:
  117. timer_set_count(TEMP_TIMER_DEV, 0);
  118. timer_generate_update(TEMP_TIMER_DEV);
  119. return;
  120. }
  121. }
  122. #define HAL_timer_isr_epilogue(TIMER_NUM)
  123. // No command is available in framework to turn off ARPE bit, which is turned on by default in libmaple.
  124. // Needed here to reset ARPE=0 for stepper timer
  125. FORCE_INLINE static void timer_no_ARR_preload_ARPE(timer_dev *dev) {
  126. bb_peri_set_bit(&(dev->regs).gen->CR1, TIMER_CR1_ARPE_BIT, 0);
  127. }
  128. #define TIMER_OC_NO_PRELOAD 0 // Need to disable preload also on compare registers.