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 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 -> 42MHz
  72. Timer_clock2: Prescaler 8 -> 10.5MHz
  73. Timer_clock3: Prescaler 32 -> 2.625MHz
  74. Timer_clock4: Prescaler 128 -> 656.25kHz
  75. */
  76. /**
  77. * TODO: Calculate Timer prescale value, so we get the 32bit to adjust
  78. */
  79. void HAL_timer_start(uint8_t timer_num, uint32_t frequency) {
  80. switch (timer_num) {
  81. case STEP_TIMER_NUM:
  82. StepperTimer.pause();
  83. StepperTimer.setCount(0);
  84. StepperTimer.setPrescaleFactor(STEPPER_TIMER_PRESCALE);
  85. StepperTimer.setOverflow(0xFFFF);
  86. StepperTimer.setCompare(STEP_TIMER_CHAN, uint32_t(HAL_STEPPER_TIMER_RATE) / frequency);
  87. StepperTimer.refresh();
  88. StepperTimer.resume();
  89. break;
  90. case TEMP_TIMER_NUM:
  91. TempTimer.pause();
  92. TempTimer.setCount(0);
  93. TempTimer.setPrescaleFactor(TEMP_TIMER_PRESCALE);
  94. TempTimer.setOverflow(0xFFFF);
  95. TempTimer.setCompare(TEMP_TIMER_CHAN, (F_CPU) / (TEMP_TIMER_PRESCALE) / frequency);
  96. TempTimer.refresh();
  97. TempTimer.resume();
  98. break;
  99. }
  100. }
  101. void HAL_timer_enable_interrupt(uint8_t timer_num) {
  102. switch (timer_num) {
  103. case STEP_TIMER_NUM:
  104. StepperTimer.attachInterrupt(STEP_TIMER_CHAN, stepTC_Handler);
  105. break;
  106. case TEMP_TIMER_NUM:
  107. TempTimer.attachInterrupt(STEP_TIMER_CHAN, tempTC_Handler);
  108. break;
  109. default:
  110. break;
  111. }
  112. }
  113. void HAL_timer_disable_interrupt(uint8_t timer_num) {
  114. switch (timer_num) {
  115. case STEP_TIMER_NUM:
  116. StepperTimer.detachInterrupt(STEP_TIMER_CHAN);
  117. break;
  118. case TEMP_TIMER_NUM:
  119. TempTimer.detachInterrupt(STEP_TIMER_CHAN);
  120. break;
  121. default:
  122. break;
  123. }
  124. }
  125. #endif // __STM32F1__