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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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) 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. // --------------------------------------------------------------------------
  27. // Includes
  28. // --------------------------------------------------------------------------
  29. #include "../HAL.h"
  30. #include "HAL_timers_Stm32f1.h"
  31. // --------------------------------------------------------------------------
  32. // Externals
  33. // --------------------------------------------------------------------------
  34. // --------------------------------------------------------------------------
  35. // Local defines
  36. // --------------------------------------------------------------------------
  37. #define NUM_HARDWARE_TIMERS 4
  38. //#define PRESCALER 1
  39. // --------------------------------------------------------------------------
  40. // Types
  41. // --------------------------------------------------------------------------
  42. // --------------------------------------------------------------------------
  43. // Public Variables
  44. // --------------------------------------------------------------------------
  45. // --------------------------------------------------------------------------
  46. // Private Variables
  47. // --------------------------------------------------------------------------
  48. /* VGPV
  49. const tTimerConfig TimerConfig [NUM_HARDWARE_TIMERS] = {
  50. { TC0, 0, TC0_IRQn, 0}, // 0 - [servo timer5]
  51. { TC0, 1, TC1_IRQn, 0}, // 1
  52. { TC0, 2, TC2_IRQn, 0}, // 2
  53. { TC1, 0, TC3_IRQn, 2}, // 3 - stepper
  54. { TC1, 1, TC4_IRQn, 15}, // 4 - temperature
  55. { TC1, 2, TC5_IRQn, 0}, // 5 - [servo timer3]
  56. { TC2, 0, TC6_IRQn, 0}, // 6
  57. { TC2, 1, TC7_IRQn, 0}, // 7
  58. { TC2, 2, TC8_IRQn, 0}, // 8
  59. };
  60. */
  61. // --------------------------------------------------------------------------
  62. // Function prototypes
  63. // --------------------------------------------------------------------------
  64. // --------------------------------------------------------------------------
  65. // Private functions
  66. // --------------------------------------------------------------------------
  67. // --------------------------------------------------------------------------
  68. // Public functions
  69. // --------------------------------------------------------------------------
  70. /**
  71. * Timer_clock1: Prescaler 2 -> 36 MHz
  72. * Timer_clock2: Prescaler 8 -> 9 MHz
  73. * Timer_clock3: Prescaler 32 -> 2.25 MHz
  74. * Timer_clock4: Prescaler 128 -> 562.5 kHz
  75. */
  76. /**
  77. * TODO: Calculate Timer prescale value, so we get the 32bit to adjust
  78. */
  79. void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency) {
  80. nvic_irq_num irq_num;
  81. switch (timer_num) {
  82. case 1: irq_num = NVIC_TIMER1_CC; break;
  83. case 2: irq_num = NVIC_TIMER2; break;
  84. case 3: irq_num = NVIC_TIMER3; break;
  85. case 4: irq_num = NVIC_TIMER4; break;
  86. case 5: irq_num = NVIC_TIMER5; break;
  87. default:
  88. /**
  89. * We should not get here, add Sanitycheck for timer number. Should be a general timer
  90. * since basic timers do not have CC channels.
  91. * Advanced timers should be skipped if possible too, and are not listed above.
  92. */
  93. break;
  94. }
  95. nvic_irq_set_priority(irq_num, 0xF); // this is the lowest settable priority, but should still be over USB
  96. switch (timer_num) {
  97. case STEP_TIMER_NUM:
  98. timer_pause(STEP_TIMER_DEV);
  99. timer_set_count(STEP_TIMER_DEV, 0);
  100. timer_set_prescaler(STEP_TIMER_DEV, (uint16)(STEPPER_TIMER_PRESCALE - 1));
  101. timer_set_reload(STEP_TIMER_DEV, 0xFFFF);
  102. timer_set_compare(STEP_TIMER_DEV, STEP_TIMER_CHAN, min(HAL_TIMER_TYPE_MAX, (HAL_STEPPER_TIMER_RATE / frequency)));
  103. timer_attach_interrupt(STEP_TIMER_DEV, STEP_TIMER_CHAN, stepTC_Handler);
  104. timer_generate_update(STEP_TIMER_DEV);
  105. timer_resume(STEP_TIMER_DEV);
  106. break;
  107. case TEMP_TIMER_NUM:
  108. timer_pause(TEMP_TIMER_DEV);
  109. timer_set_count(TEMP_TIMER_DEV, 0);
  110. timer_set_prescaler(TEMP_TIMER_DEV, (uint16)(TEMP_TIMER_PRESCALE - 1));
  111. timer_set_reload(TEMP_TIMER_DEV, 0xFFFF);
  112. timer_set_compare(TEMP_TIMER_DEV, TEMP_TIMER_CHAN, min(HAL_TIMER_TYPE_MAX, ((F_CPU / TEMP_TIMER_PRESCALE) / frequency)));
  113. timer_attach_interrupt(TEMP_TIMER_DEV, TEMP_TIMER_CHAN, tempTC_Handler);
  114. timer_generate_update(TEMP_TIMER_DEV);
  115. timer_resume(TEMP_TIMER_DEV);
  116. break;
  117. }
  118. }
  119. void HAL_timer_enable_interrupt(const uint8_t timer_num) {
  120. switch (timer_num) {
  121. case STEP_TIMER_NUM:
  122. timer_enable_irq(STEP_TIMER_DEV, STEP_TIMER_CHAN);
  123. break;
  124. case TEMP_TIMER_NUM:
  125. timer_enable_irq(TEMP_TIMER_DEV, TEMP_TIMER_CHAN);
  126. break;
  127. default:
  128. break;
  129. }
  130. }
  131. void HAL_timer_disable_interrupt(const uint8_t timer_num) {
  132. switch (timer_num) {
  133. case STEP_TIMER_NUM:
  134. timer_disable_irq(STEP_TIMER_DEV, STEP_TIMER_CHAN);
  135. break;
  136. case TEMP_TIMER_NUM:
  137. timer_disable_irq(TEMP_TIMER_DEV, TEMP_TIMER_CHAN);
  138. break;
  139. default:
  140. break;
  141. }
  142. }
  143. bool HAL_timer_interrupt_enabled(const uint8_t timer_num) {
  144. switch (timer_num) {
  145. case STEP_TIMER_NUM: return bool(TIM_DIER(STEP_TIMER_DEV) & STEP_TIMER_CHAN);
  146. case TEMP_TIMER_NUM: return bool(TIM_DIER(TEMP_TIMER_DEV) & TEMP_TIMER_CHAN);
  147. }
  148. return false;
  149. }
  150. #endif // __STM32F1__