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.4KB

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