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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. #if defined(STM32GENERIC) && defined(STM32F7)
  23. #include "../HAL.h"
  24. #include "timers.h"
  25. // ------------------------
  26. // Local defines
  27. // ------------------------
  28. #define NUM_HARDWARE_TIMERS 2
  29. //#define PRESCALER 1
  30. // ------------------------
  31. // Private Variables
  32. // ------------------------
  33. tTimerConfig timerConfig[NUM_HARDWARE_TIMERS];
  34. // ------------------------
  35. // Public functions
  36. // ------------------------
  37. bool timers_initialized[NUM_HARDWARE_TIMERS] = { false };
  38. void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency) {
  39. if (!timers_initialized[timer_num]) {
  40. switch (timer_num) {
  41. case STEP_TIMER_NUM:
  42. //STEPPER TIMER TIM5 //use a 32bit timer
  43. __HAL_RCC_TIM5_CLK_ENABLE();
  44. timerConfig[0].timerdef.Instance = TIM5;
  45. timerConfig[0].timerdef.Init.Prescaler = (STEPPER_TIMER_PRESCALE);
  46. timerConfig[0].timerdef.Init.CounterMode = TIM_COUNTERMODE_UP;
  47. timerConfig[0].timerdef.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  48. timerConfig[0].IRQ_Id = TIM5_IRQn;
  49. timerConfig[0].callback = (uint32_t)TC5_Handler;
  50. HAL_NVIC_SetPriority(timerConfig[0].IRQ_Id, 1, 0);
  51. #if PIN_EXISTS(STEPPER_ENABLE)
  52. OUT_WRITE(STEPPER_ENABLE_PIN, HIGH);
  53. #endif
  54. break;
  55. case TEMP_TIMER_NUM:
  56. //TEMP TIMER TIM7 // any available 16bit Timer (1 already used for PWM)
  57. __HAL_RCC_TIM7_CLK_ENABLE();
  58. timerConfig[1].timerdef.Instance = TIM7;
  59. timerConfig[1].timerdef.Init.Prescaler = (TEMP_TIMER_PRESCALE);
  60. timerConfig[1].timerdef.Init.CounterMode = TIM_COUNTERMODE_UP;
  61. timerConfig[1].timerdef.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  62. timerConfig[1].IRQ_Id = TIM7_IRQn;
  63. timerConfig[1].callback = (uint32_t)TC7_Handler;
  64. HAL_NVIC_SetPriority(timerConfig[1].IRQ_Id, 2, 0);
  65. break;
  66. }
  67. timers_initialized[timer_num] = true;
  68. }
  69. timerConfig[timer_num].timerdef.Init.Period = (((HAL_TIMER_RATE) / timerConfig[timer_num].timerdef.Init.Prescaler) / frequency) - 1;
  70. if (HAL_TIM_Base_Init(&timerConfig[timer_num].timerdef) == HAL_OK)
  71. HAL_TIM_Base_Start_IT(&timerConfig[timer_num].timerdef);
  72. }
  73. //forward the interrupt
  74. extern "C" void TIM5_IRQHandler() {
  75. ((void(*)())timerConfig[0].callback)();
  76. }
  77. extern "C" void TIM7_IRQHandler() {
  78. ((void(*)())timerConfig[1].callback)();
  79. }
  80. void HAL_timer_set_compare(const uint8_t timer_num, const uint32_t compare) {
  81. __HAL_TIM_SetAutoreload(&timerConfig[timer_num].timerdef, compare);
  82. }
  83. void HAL_timer_enable_interrupt(const uint8_t timer_num) {
  84. HAL_NVIC_EnableIRQ(timerConfig[timer_num].IRQ_Id);
  85. }
  86. void HAL_timer_disable_interrupt(const uint8_t timer_num) {
  87. HAL_NVIC_DisableIRQ(timerConfig[timer_num].IRQ_Id);
  88. // We NEED memory barriers to ensure Interrupts are actually disabled!
  89. // ( https://dzone.com/articles/nvic-disabling-interrupts-on-arm-cortex-m-and-the )
  90. __DSB();
  91. __ISB();
  92. }
  93. hal_timer_t HAL_timer_get_compare(const uint8_t timer_num) {
  94. return __HAL_TIM_GetAutoreload(&timerConfig[timer_num].timerdef);
  95. }
  96. uint32_t HAL_timer_get_count(const uint8_t timer_num) {
  97. return __HAL_TIM_GetCounter(&timerConfig[timer_num].timerdef);
  98. }
  99. void HAL_timer_isr_prologue(const uint8_t timer_num) {
  100. if (__HAL_TIM_GET_FLAG(&timerConfig[timer_num].timerdef, TIM_FLAG_UPDATE) == SET) {
  101. __HAL_TIM_CLEAR_FLAG(&timerConfig[timer_num].timerdef, TIM_FLAG_UPDATE);
  102. }
  103. }
  104. bool HAL_timer_interrupt_enabled(const uint8_t timer_num) {
  105. const uint32_t IRQ_Id = uint32_t(timerConfig[timer_num].IRQ_Id);
  106. return NVIC->ISER[IRQ_Id >> 5] & _BV32(IRQ_Id & 0x1F);
  107. }
  108. #endif // STM32GENERIC && STM32F7