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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /**
  2. * Marlin 3D Printer Firmware
  3. *
  4. * Copyright (C) 2016 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. /**
  23. * HAL for stm32duino.com based on Libmaple and compatible (STM32F1)
  24. */
  25. #ifndef _HAL_TIMERS_STM32F1_H
  26. #define _HAL_TIMERS_STM32F1_H
  27. // --------------------------------------------------------------------------
  28. // Includes
  29. // --------------------------------------------------------------------------
  30. #include <stdint.h>
  31. // --------------------------------------------------------------------------
  32. // Defines
  33. // --------------------------------------------------------------------------
  34. /**
  35. * TODO: Check and confirm what timer we will use for each Temps and stepper driving.
  36. * We should probable drive temps with PWM.
  37. */
  38. #define FORCE_INLINE __attribute__((always_inline)) inline
  39. typedef uint16_t hal_timer_t;
  40. #define HAL_TIMER_TYPE_MAX 0xFFFF
  41. #define STEP_TIMER_NUM 5 // index of timer to use for stepper
  42. #define STEP_TIMER_CHAN 1 // Channel of the timer to use for compare and interrupts
  43. #define TEMP_TIMER_NUM 2 // index of timer to use for temperature
  44. #define TEMP_TIMER_CHAN 1 // Channel of the timer to use for compare and interrupts
  45. #define HAL_TIMER_RATE (F_CPU) // frequency of timers peripherals
  46. #define STEPPER_TIMER_PRESCALE 36 // prescaler for setting stepper timer, 2Mhz
  47. #define HAL_STEPPER_TIMER_RATE (HAL_TIMER_RATE / STEPPER_TIMER_PRESCALE) // frequency of stepper timer (HAL_TIMER_RATE / STEPPER_TIMER_PRESCALE)
  48. #define HAL_TICKS_PER_US ((HAL_STEPPER_TIMER_RATE) / 1000000) // stepper timer ticks per us
  49. #define PULSE_TIMER_NUM STEP_TIMER_NUM
  50. #define PULSE_TIMER_PRESCALE STEPPER_TIMER_PRESCALE
  51. #define TEMP_TIMER_PRESCALE 1000 // prescaler for setting Temp timer, 72Khz
  52. #define TEMP_TIMER_FREQUENCY 1000 // temperature interrupt frequency
  53. #define ENABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_enable_interrupt(STEP_TIMER_NUM)
  54. #define DISABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_disable_interrupt(STEP_TIMER_NUM)
  55. #define ENABLE_TEMPERATURE_INTERRUPT() HAL_timer_enable_interrupt(TEMP_TIMER_NUM)
  56. #define DISABLE_TEMPERATURE_INTERRUPT() HAL_timer_disable_interrupt(TEMP_TIMER_NUM)
  57. #define HAL_ENABLE_ISRs() do { if (thermalManager.in_temp_isr)DISABLE_TEMPERATURE_INTERRUPT(); else ENABLE_TEMPERATURE_INTERRUPT(); ENABLE_STEPPER_DRIVER_INTERRUPT(); } while(0)
  58. // TODO change this
  59. #define HAL_TEMP_TIMER_ISR extern "C" void tempTC_Handler(void)
  60. #define HAL_STEP_TIMER_ISR extern "C" void stepTC_Handler(void)
  61. extern "C" void tempTC_Handler(void);
  62. extern "C" void stepTC_Handler(void);
  63. // --------------------------------------------------------------------------
  64. // Types
  65. // --------------------------------------------------------------------------
  66. // --------------------------------------------------------------------------
  67. // Public Variables
  68. // --------------------------------------------------------------------------
  69. static HardwareTimer StepperTimer(STEP_TIMER_NUM);
  70. static HardwareTimer TempTimer(TEMP_TIMER_NUM);
  71. // --------------------------------------------------------------------------
  72. // Public functions
  73. // --------------------------------------------------------------------------
  74. void HAL_timer_start(uint8_t timer_num, uint32_t frequency);
  75. void HAL_timer_enable_interrupt(uint8_t timer_num);
  76. void HAL_timer_disable_interrupt(uint8_t timer_num);
  77. /**
  78. * NOTE: By default libmaple sets ARPE = 1, which means the Auto reload register is preloaded (will only update with an update event)
  79. * Thus we have to pause the timer, update the value, refresh, resume the timer.
  80. * 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.
  81. * 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
  82. * 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.
  83. * This mode pretty much makes 2 timers unusable for PWM since they have their counts updated all the time on ISRs.
  84. * The way Marlin manages timer interrupts doesn't make for an efficient usage in STM32F1
  85. * Todo: Look at that possibility later.
  86. */
  87. FORCE_INLINE static void HAL_timer_set_count(const uint8_t timer_num, const hal_timer_t count) {
  88. switch (timer_num) {
  89. case STEP_TIMER_NUM:
  90. StepperTimer.pause();
  91. StepperTimer.setCompare(STEP_TIMER_CHAN, count);
  92. StepperTimer.refresh();
  93. StepperTimer.resume();
  94. break;
  95. case TEMP_TIMER_NUM:
  96. TempTimer.pause();
  97. TempTimer.setCompare(TEMP_TIMER_CHAN, count);
  98. TempTimer.refresh();
  99. TempTimer.resume();
  100. break;
  101. default:
  102. break;
  103. }
  104. }
  105. FORCE_INLINE static hal_timer_t HAL_timer_get_count(const uint8_t timer_num) {
  106. hal_timer_t temp;
  107. switch (timer_num) {
  108. case STEP_TIMER_NUM:
  109. temp = StepperTimer.getCompare(STEP_TIMER_CHAN);
  110. break;
  111. case TEMP_TIMER_NUM:
  112. temp = TempTimer.getCompare(TEMP_TIMER_CHAN);
  113. break;
  114. default:
  115. temp = 0;
  116. break;
  117. }
  118. return temp;
  119. }
  120. FORCE_INLINE static void HAL_timer_set_current_count(const uint8_t timer_num, const hal_timer_t count) {
  121. switch (timer_num) {
  122. case STEP_TIMER_NUM: StepperTimer.setCount(count); break;
  123. case TEMP_TIMER_NUM: TempTimer.setCount(count); break;
  124. }
  125. }
  126. FORCE_INLINE static hal_timer_t HAL_timer_get_current_count(const uint8_t timer_num) {
  127. hal_timer_t temp;
  128. switch (timer_num) {
  129. case STEP_TIMER_NUM:
  130. temp = StepperTimer.getCount();
  131. break;
  132. case TEMP_TIMER_NUM:
  133. temp = TempTimer.getCount();
  134. break;
  135. default:
  136. temp = 0;
  137. break;
  138. }
  139. return temp;
  140. }
  141. //void HAL_timer_isr_prologue (const uint8_t timer_num);
  142. FORCE_INLINE static void HAL_timer_isr_prologue(const uint8_t timer_num) {
  143. switch (timer_num) {
  144. case STEP_TIMER_NUM:
  145. StepperTimer.pause();
  146. StepperTimer.setCount(0);
  147. StepperTimer.refresh();
  148. StepperTimer.resume();
  149. break;
  150. case TEMP_TIMER_NUM:
  151. TempTimer.pause();
  152. TempTimer.setCount(0);
  153. TempTimer.refresh();
  154. TempTimer.resume();
  155. break;
  156. default:
  157. break;
  158. }
  159. }
  160. #endif // _HAL_TIMERS_STM32F1_H