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.

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