My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

HAL_timers_STM32F4.cpp 5.0KB

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