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_STM32F4.cpp 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. #if defined(STM32GENERIC) && defined(STM32F4)
  23. // --------------------------------------------------------------------------
  24. // Includes
  25. // --------------------------------------------------------------------------
  26. #include "HAL.h"
  27. #include "HAL_timers_STM32F4.h"
  28. // --------------------------------------------------------------------------
  29. // Externals
  30. // --------------------------------------------------------------------------
  31. // --------------------------------------------------------------------------
  32. // Local defines
  33. // --------------------------------------------------------------------------
  34. #define NUM_HARDWARE_TIMERS 2
  35. #define STEP_TIMER_IRQ_ID TIM5_IRQn
  36. #define TEMP_TIMER_IRQ_ID TIM7_IRQn
  37. //#define PRESCALER 1
  38. // --------------------------------------------------------------------------
  39. // Types
  40. // --------------------------------------------------------------------------
  41. // --------------------------------------------------------------------------
  42. // Public Variables
  43. // --------------------------------------------------------------------------
  44. // --------------------------------------------------------------------------
  45. // Private Variables
  46. // --------------------------------------------------------------------------
  47. stm32f4_timer_t TimerHandle[NUM_HARDWARE_TIMERS];
  48. // --------------------------------------------------------------------------
  49. // Function prototypes
  50. // --------------------------------------------------------------------------
  51. // --------------------------------------------------------------------------
  52. // Private functions
  53. // --------------------------------------------------------------------------
  54. // --------------------------------------------------------------------------
  55. // Public functions
  56. // --------------------------------------------------------------------------
  57. bool timers_initialized[NUM_HARDWARE_TIMERS] = {false};
  58. void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency) {
  59. if (!timers_initialized[timer_num]) {
  60. constexpr uint32_t step_prescaler = STEPPER_TIMER_PRESCALE - 1,
  61. temp_prescaler = TEMP_TIMER_PRESCALE - 1;
  62. switch (timer_num) {
  63. case STEP_TIMER_NUM:
  64. // STEPPER TIMER TIM5 - use a 32bit timer
  65. #ifdef STM32GENERIC
  66. __HAL_RCC_TIM5_CLK_ENABLE();
  67. TimerHandle[timer_num].handle.Instance = TIM5;
  68. TimerHandle[timer_num].handle.Init.Prescaler = step_prescaler;
  69. TimerHandle[timer_num].handle.Init.CounterMode = TIM_COUNTERMODE_UP;
  70. TimerHandle[timer_num].handle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  71. TimerHandle[timer_num].callback = (uint32_t)TC5_Handler;
  72. #else
  73. TimerHandle[timer_num].timer = TIM5;
  74. TimerHandle[timer_num].irqHandle = TC5_Handler;
  75. TimerHandleInit(&TimerHandle[timer_num], (((HAL_TIMER_RATE) / step_prescaler) / frequency) - 1, step_prescaler);
  76. #endif
  77. HAL_NVIC_SetPriority(STEP_TIMER_IRQ_ID, 1, 0);
  78. break;
  79. case TEMP_TIMER_NUM:
  80. // TEMP TIMER TIM7 - any available 16bit Timer (1 already used for PWM)
  81. #ifdef STM32GENERIC
  82. __HAL_RCC_TIM7_CLK_ENABLE();
  83. TimerHandle[timer_num].handle.Instance = TIM7;
  84. TimerHandle[timer_num].handle.Init.Prescaler = temp_prescaler;
  85. TimerHandle[timer_num].handle.Init.CounterMode = TIM_COUNTERMODE_UP;
  86. TimerHandle[timer_num].handle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  87. TimerHandle[timer_num].callback = (uint32_t)TC7_Handler;
  88. #else
  89. TimerHandle[timer_num].timer = TIM7;
  90. TimerHandle[timer_num].irqHandle = TC7_Handler;
  91. TimerHandleInit(&TimerHandle[timer_num], (((HAL_TIMER_RATE) / temp_prescaler) / frequency) - 1, temp_prescaler);
  92. #endif
  93. HAL_NVIC_SetPriority(TEMP_TIMER_IRQ_ID, 2, 0);
  94. break;
  95. }
  96. timers_initialized[timer_num] = true;
  97. }
  98. #ifdef STM32GENERIC
  99. TimerHandle[timer_num].handle.Init.Period = (((HAL_TIMER_RATE) / TimerHandle[timer_num].handle.Init.Prescaler) / frequency) - 1;
  100. if (HAL_TIM_Base_Init(&TimerHandle[timer_num].handle) == HAL_OK)
  101. HAL_TIM_Base_Start_IT(&TimerHandle[timer_num].handle);
  102. #endif
  103. }
  104. #ifdef STM32GENERIC
  105. extern "C" void TIM5_IRQHandler() {
  106. ((void(*)(void))TimerHandle[0].callback)();
  107. }
  108. extern "C" void TIM7_IRQHandler() {
  109. ((void(*)(void))TimerHandle[1].callback)();
  110. }
  111. #endif
  112. void HAL_timer_enable_interrupt(const uint8_t timer_num) {
  113. switch (timer_num) {
  114. case STEP_TIMER_NUM: HAL_NVIC_EnableIRQ(STEP_TIMER_IRQ_ID); break;
  115. case TEMP_TIMER_NUM: HAL_NVIC_EnableIRQ(TEMP_TIMER_IRQ_ID); break;
  116. }
  117. }
  118. void HAL_timer_disable_interrupt(const uint8_t timer_num) {
  119. switch (timer_num) {
  120. case STEP_TIMER_NUM: HAL_NVIC_DisableIRQ(STEP_TIMER_IRQ_ID); break;
  121. case TEMP_TIMER_NUM: HAL_NVIC_DisableIRQ(TEMP_TIMER_IRQ_ID); break;
  122. }
  123. // We NEED memory barriers to ensure Interrupts are actually disabled!
  124. // ( https://dzone.com/articles/nvic-disabling-interrupts-on-arm-cortex-m-and-the )
  125. __DSB();
  126. __ISB();
  127. }
  128. bool HAL_timer_interrupt_enabled(const uint8_t timer_num) {
  129. switch (timer_num) {
  130. case STEP_TIMER_NUM: return NVIC->ISER[(uint32_t)((int32_t)STEP_TIMER_IRQ_ID) >> 5] & (uint32_t)(1 << ((uint32_t)((int32_t)STEP_TIMER_IRQ_ID) & (uint32_t)0x1F));
  131. case TEMP_TIMER_NUM: return NVIC->ISER[(uint32_t)((int32_t)TEMP_TIMER_IRQ_ID) >> 5] & (uint32_t)(1 << ((uint32_t)((int32_t)TEMP_TIMER_IRQ_ID) & (uint32_t)0x1F));
  132. }
  133. return false;
  134. }
  135. #endif // STM32GENERIC && STM32F4