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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. #define HAL_TIMER_TYPE uint16_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 TEMP_TIMER_PRESCALE 1000 // prescaler for setting Temp timer, 72Khz
  50. #define TEMP_TIMER_FREQUENCY 1000 // temperature interrupt frequency
  51. #define ENABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_enable_interrupt (STEP_TIMER_NUM)
  52. #define DISABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_disable_interrupt (STEP_TIMER_NUM)
  53. #define ENABLE_TEMPERATURE_INTERRUPT() HAL_timer_enable_interrupt (TEMP_TIMER_NUM)
  54. #define DISABLE_TEMPERATURE_INTERRUPT() HAL_timer_disable_interrupt (TEMP_TIMER_NUM)
  55. #define HAL_ENABLE_ISRs() do { if (thermalManager.in_temp_isr)DISABLE_TEMPERATURE_INTERRUPT(); else ENABLE_TEMPERATURE_INTERRUPT(); ENABLE_STEPPER_DRIVER_INTERRUPT(); } while(0)
  56. // TODO change this
  57. #define HAL_TEMP_TIMER_ISR extern "C" void tempTC_Handler(void)
  58. #define HAL_STEP_TIMER_ISR extern "C" void stepTC_Handler(void)
  59. extern "C" void tempTC_Handler(void);
  60. extern "C" void stepTC_Handler(void);
  61. // --------------------------------------------------------------------------
  62. // Types
  63. // --------------------------------------------------------------------------
  64. // --------------------------------------------------------------------------
  65. // Public Variables
  66. // --------------------------------------------------------------------------
  67. static HardwareTimer StepperTimer(STEP_TIMER_NUM);
  68. static HardwareTimer TempTimer(TEMP_TIMER_NUM);
  69. // --------------------------------------------------------------------------
  70. // Public functions
  71. // --------------------------------------------------------------------------
  72. void HAL_timer_start (uint8_t timer_num, uint32_t frequency);
  73. void HAL_timer_enable_interrupt(uint8_t timer_num);
  74. void HAL_timer_disable_interrupt(uint8_t timer_num);
  75. /**
  76. * NOTE: By default libmaple sets ARPE = 1, which means the Auto reload register is preloaded (will only update with an update event)
  77. * Thus we have to pause the timer, update the value, refresh, resume the timer.
  78. * 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.
  79. * 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
  80. * 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.
  81. * This mode pretty much makes 2 timers unusable for PWM since they have their counts updated all the time on ISRs.
  82. * The way Marlin manages timer interrupts doesn't make for an efficient usage in STM32F1
  83. * Todo: Look at that possibility later.
  84. */
  85. static FORCE_INLINE void HAL_timer_set_count (uint8_t timer_num, uint32_t count) {
  86. switch (timer_num) {
  87. case STEP_TIMER_NUM:
  88. StepperTimer.pause();
  89. StepperTimer.setCompare (STEP_TIMER_CHAN, count);
  90. StepperTimer.refresh ();
  91. StepperTimer.resume ();
  92. break;
  93. case TEMP_TIMER_NUM:
  94. TempTimer.pause();
  95. TempTimer.setCompare (TEMP_TIMER_CHAN, count);
  96. TempTimer.refresh ();
  97. TempTimer.resume ();
  98. break;
  99. default:
  100. break;
  101. }
  102. }
  103. static FORCE_INLINE HAL_TIMER_TYPE HAL_timer_get_count (uint8_t timer_num) {
  104. HAL_TIMER_TYPE temp;
  105. switch (timer_num) {
  106. case STEP_TIMER_NUM:
  107. temp = StepperTimer.getCompare(STEP_TIMER_CHAN);
  108. break;
  109. case TEMP_TIMER_NUM:
  110. temp = TempTimer.getCompare(TEMP_TIMER_CHAN);
  111. break;
  112. default:
  113. temp = 0;
  114. break;
  115. }
  116. return temp;
  117. }
  118. static FORCE_INLINE HAL_TIMER_TYPE HAL_timer_get_current_count(uint8_t timer_num) {
  119. HAL_TIMER_TYPE temp;
  120. switch (timer_num) {
  121. case STEP_TIMER_NUM:
  122. temp = StepperTimer.getCount();
  123. break;
  124. case TEMP_TIMER_NUM:
  125. temp = TempTimer.getCount();
  126. break;
  127. default:
  128. temp = 0;
  129. break;
  130. }
  131. return temp;
  132. }
  133. //void HAL_timer_isr_prologue (uint8_t timer_num);
  134. static FORCE_INLINE void HAL_timer_isr_prologue(uint8_t timer_num) {
  135. switch (timer_num) {
  136. case STEP_TIMER_NUM:
  137. StepperTimer.pause();
  138. StepperTimer.setCount(0);
  139. StepperTimer.refresh();
  140. StepperTimer.resume();
  141. break;
  142. case TEMP_TIMER_NUM:
  143. TempTimer.pause();
  144. TempTimer.setCount(0);
  145. TempTimer.refresh();
  146. TempTimer.resume();
  147. break;
  148. default:
  149. break;
  150. }
  151. }
  152. #endif // _HAL_TIMERS_STM32F1_H