My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

timers.cpp 5.7KB

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