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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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) 2015-2016 Nico Tonnhofer wurstnase.reprap@gmail.com
  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. /**
  23. * HAL for stm32duino.com based on Libmaple and compatible (STM32F1)
  24. */
  25. #ifdef __STM32F1__
  26. #include "../../inc/MarlinConfig.h"
  27. // ------------------------
  28. // Local defines
  29. // ------------------------
  30. // ------------------------
  31. // Public functions
  32. // ------------------------
  33. /**
  34. * Timer_clock1: Prescaler 2 -> 36 MHz
  35. * Timer_clock2: Prescaler 8 -> 9 MHz
  36. * Timer_clock3: Prescaler 32 -> 2.25 MHz
  37. * Timer_clock4: Prescaler 128 -> 562.5 kHz
  38. */
  39. /**
  40. * TODO: Calculate Timer prescale value, so we get the 32bit to adjust
  41. */
  42. void timer_set_interrupt_priority(uint_fast8_t timer_num, uint_fast8_t priority) {
  43. nvic_irq_num irq_num;
  44. switch (timer_num) {
  45. case 1: irq_num = NVIC_TIMER1_CC; break;
  46. case 2: irq_num = NVIC_TIMER2; break;
  47. case 3: irq_num = NVIC_TIMER3; break;
  48. case 4: irq_num = NVIC_TIMER4; break;
  49. case 5: irq_num = NVIC_TIMER5; break;
  50. #ifdef STM32_HIGH_DENSITY
  51. // 6 & 7 are basic timers, avoid them
  52. case 8: irq_num = NVIC_TIMER8_CC; break;
  53. #endif
  54. default:
  55. /**
  56. * This should never happen. Add a Sanitycheck for timer number.
  57. * Should be a general timer since basic timers have no CC channels.
  58. */
  59. return;
  60. }
  61. nvic_irq_set_priority(irq_num, priority);
  62. }
  63. void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency) {
  64. /**
  65. * Give the Stepper ISR a higher priority (lower number)
  66. * so it automatically preempts the Temperature ISR.
  67. */
  68. switch (timer_num) {
  69. case STEP_TIMER_NUM:
  70. timer_pause(STEP_TIMER_DEV);
  71. timer_set_mode(STEP_TIMER_DEV, STEP_TIMER_CHAN, TIMER_OUTPUT_COMPARE); // counter
  72. timer_set_count(STEP_TIMER_DEV, 0);
  73. timer_set_prescaler(STEP_TIMER_DEV, (uint16_t)(STEPPER_TIMER_PRESCALE - 1));
  74. timer_set_reload(STEP_TIMER_DEV, 0xFFFF);
  75. timer_oc_set_mode(STEP_TIMER_DEV, STEP_TIMER_CHAN, TIMER_OC_MODE_FROZEN, TIMER_OC_NO_PRELOAD); // no output pin change
  76. timer_set_compare(STEP_TIMER_DEV, STEP_TIMER_CHAN, _MIN(hal_timer_t(HAL_TIMER_TYPE_MAX), (STEPPER_TIMER_RATE) / frequency));
  77. timer_no_ARR_preload_ARPE(STEP_TIMER_DEV); // Need to be sure no preload on ARR register
  78. timer_attach_interrupt(STEP_TIMER_DEV, STEP_TIMER_CHAN, stepTC_Handler);
  79. timer_set_interrupt_priority(STEP_TIMER_NUM, STEP_TIMER_IRQ_PRIO);
  80. timer_generate_update(STEP_TIMER_DEV);
  81. timer_resume(STEP_TIMER_DEV);
  82. break;
  83. case TEMP_TIMER_NUM:
  84. timer_pause(TEMP_TIMER_DEV);
  85. timer_set_mode(TEMP_TIMER_DEV, TEMP_TIMER_CHAN, TIMER_OUTPUT_COMPARE);
  86. timer_set_count(TEMP_TIMER_DEV, 0);
  87. timer_set_prescaler(TEMP_TIMER_DEV, (uint16_t)(TEMP_TIMER_PRESCALE - 1));
  88. timer_set_reload(TEMP_TIMER_DEV, 0xFFFF);
  89. timer_set_compare(TEMP_TIMER_DEV, TEMP_TIMER_CHAN, _MIN(hal_timer_t(HAL_TIMER_TYPE_MAX), (F_CPU) / (TEMP_TIMER_PRESCALE) / frequency));
  90. timer_attach_interrupt(TEMP_TIMER_DEV, TEMP_TIMER_CHAN, tempTC_Handler);
  91. timer_set_interrupt_priority(TEMP_TIMER_NUM, TEMP_TIMER_IRQ_PRIO);
  92. timer_generate_update(TEMP_TIMER_DEV);
  93. timer_resume(TEMP_TIMER_DEV);
  94. break;
  95. }
  96. }
  97. void HAL_timer_enable_interrupt(const uint8_t timer_num) {
  98. switch (timer_num) {
  99. case STEP_TIMER_NUM: ENABLE_STEPPER_DRIVER_INTERRUPT(); break;
  100. case TEMP_TIMER_NUM: ENABLE_TEMPERATURE_INTERRUPT(); break;
  101. }
  102. }
  103. void HAL_timer_disable_interrupt(const uint8_t timer_num) {
  104. switch (timer_num) {
  105. case STEP_TIMER_NUM: DISABLE_STEPPER_DRIVER_INTERRUPT(); break;
  106. case TEMP_TIMER_NUM: DISABLE_TEMPERATURE_INTERRUPT(); break;
  107. }
  108. }
  109. static inline bool timer_irq_enabled(const timer_dev * const dev, const uint8_t interrupt) {
  110. return bool(*bb_perip(&(dev->regs).gen->DIER, interrupt));
  111. }
  112. bool HAL_timer_interrupt_enabled(const uint8_t timer_num) {
  113. switch (timer_num) {
  114. case STEP_TIMER_NUM: return timer_irq_enabled(STEP_TIMER_DEV, STEP_TIMER_CHAN);
  115. case TEMP_TIMER_NUM: return timer_irq_enabled(TEMP_TIMER_DEV, TEMP_TIMER_CHAN);
  116. }
  117. return false;
  118. }
  119. timer_dev* get_timer_dev(int number) {
  120. switch (number) {
  121. #if STM32_HAVE_TIMER(1)
  122. case 1: return &timer1;
  123. #endif
  124. #if STM32_HAVE_TIMER(2)
  125. case 2: return &timer2;
  126. #endif
  127. #if STM32_HAVE_TIMER(3)
  128. case 3: return &timer3;
  129. #endif
  130. #if STM32_HAVE_TIMER(4)
  131. case 4: return &timer4;
  132. #endif
  133. #if STM32_HAVE_TIMER(5)
  134. case 5: return &timer5;
  135. #endif
  136. #if STM32_HAVE_TIMER(6)
  137. case 6: return &timer6;
  138. #endif
  139. #if STM32_HAVE_TIMER(7)
  140. case 7: return &timer7;
  141. #endif
  142. #if STM32_HAVE_TIMER(8)
  143. case 8: return &timer8;
  144. #endif
  145. #if STM32_HAVE_TIMER(9)
  146. case 9: return &timer9;
  147. #endif
  148. #if STM32_HAVE_TIMER(10)
  149. case 10: return &timer10;
  150. #endif
  151. #if STM32_HAVE_TIMER(11)
  152. case 11: return &timer11;
  153. #endif
  154. #if STM32_HAVE_TIMER(12)
  155. case 12: return &timer12;
  156. #endif
  157. #if STM32_HAVE_TIMER(13)
  158. case 13: return &timer13;
  159. #endif
  160. #if STM32_HAVE_TIMER(14)
  161. case 14: return &timer14;
  162. #endif
  163. default: return nullptr;
  164. }
  165. }
  166. #endif // __STM32F1__