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.h 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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) 2017 Victor Perez
  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. #pragma once
  23. #include "../../inc/MarlinConfig.h"
  24. // ------------------------
  25. // Defines
  26. // ------------------------
  27. // STM32 timers may be 16 or 32 bit. Limiting HAL_TIMER_TYPE_MAX to 16 bits
  28. // avoids issues with STM32F0 MCUs, which seem to pause timers if UINT32_MAX
  29. // is written to the register. STM32F4 timers do not manifest this issue,
  30. // even when writing to 16 bit timers.
  31. //
  32. // The range of the timer can be queried at runtime using IS_TIM_32B_COUNTER_INSTANCE.
  33. // This is a more expensive check than a simple compile-time constant, so its
  34. // implementation is deferred until the desire for a 32-bit range outweighs the cost
  35. // of adding a run-time check and HAL_TIMER_TYPE_MAX is refactored to allow unique
  36. // values for each timer.
  37. #define hal_timer_t uint32_t
  38. #define HAL_TIMER_TYPE_MAX UINT16_MAX
  39. // Marlin timer_instance[] content (unrelated to timer selection)
  40. #define MF_TIMER_STEP 0 // Timer Index for Stepper
  41. #define MF_TIMER_TEMP 1 // Timer Index for Temperature
  42. #define MF_TIMER_PULSE MF_TIMER_STEP
  43. #define TIMER_INDEX_(T) TIMER##T##_INDEX // TIMER#_INDEX enums (timer_index_t) depend on TIM#_BASE defines.
  44. #define TIMER_INDEX(T) TIMER_INDEX_(T) // Convert Timer ID to HardwareTimer_Handle index.
  45. #define TEMP_TIMER_FREQUENCY 1000 // Temperature::isr() is expected to be called at around 1kHz
  46. // TODO: get rid of manual rate/prescale/ticks/cycles taken for procedures in stepper.cpp
  47. #define STEPPER_TIMER_RATE 2000000 // 2 Mhz
  48. extern uint32_t GetStepperTimerClkFreq();
  49. #define STEPPER_TIMER_PRESCALE (GetStepperTimerClkFreq() / (STEPPER_TIMER_RATE))
  50. #define STEPPER_TIMER_TICKS_PER_US ((STEPPER_TIMER_RATE) / 1000000) // stepper timer ticks per µs
  51. #define PULSE_TIMER_RATE STEPPER_TIMER_RATE
  52. #define PULSE_TIMER_PRESCALE STEPPER_TIMER_PRESCALE
  53. #define PULSE_TIMER_TICKS_PER_US STEPPER_TIMER_TICKS_PER_US
  54. #define ENABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_enable_interrupt(MF_TIMER_STEP)
  55. #define DISABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_disable_interrupt(MF_TIMER_STEP)
  56. #define STEPPER_ISR_ENABLED() HAL_timer_interrupt_enabled(MF_TIMER_STEP)
  57. #define ENABLE_TEMPERATURE_INTERRUPT() HAL_timer_enable_interrupt(MF_TIMER_TEMP)
  58. #define DISABLE_TEMPERATURE_INTERRUPT() HAL_timer_disable_interrupt(MF_TIMER_TEMP)
  59. extern void Step_Handler();
  60. extern void Temp_Handler();
  61. #ifndef HAL_STEP_TIMER_ISR
  62. #define HAL_STEP_TIMER_ISR() void Step_Handler()
  63. #endif
  64. #ifndef HAL_TEMP_TIMER_ISR
  65. #define HAL_TEMP_TIMER_ISR() void Temp_Handler()
  66. #endif
  67. // ------------------------
  68. // Public Variables
  69. // ------------------------
  70. extern HardwareTimer *timer_instance[];
  71. // ------------------------
  72. // Public functions
  73. // ------------------------
  74. void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency);
  75. void HAL_timer_enable_interrupt(const uint8_t timer_num);
  76. void HAL_timer_disable_interrupt(const uint8_t timer_num);
  77. bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
  78. // Configure timer priorities for peripherals such as Software Serial or Servos.
  79. // Exposed here to allow all timer priority information to reside in timers.cpp
  80. void SetTimerInterruptPriorities();
  81. // FORCE_INLINE because these are used in performance-critical situations
  82. FORCE_INLINE bool HAL_timer_initialized(const uint8_t timer_num) {
  83. return timer_instance[timer_num] != nullptr;
  84. }
  85. FORCE_INLINE static hal_timer_t HAL_timer_get_count(const uint8_t timer_num) {
  86. return HAL_timer_initialized(timer_num) ? timer_instance[timer_num]->getCount() : 0;
  87. }
  88. // NOTE: Method name may be misleading.
  89. // STM32 has an Auto-Reload Register (ARR) as opposed to a "compare" register
  90. FORCE_INLINE static void HAL_timer_set_compare(const uint8_t timer_num, const hal_timer_t overflow) {
  91. if (HAL_timer_initialized(timer_num)) {
  92. timer_instance[timer_num]->setOverflow(overflow + 1, TICK_FORMAT); // Value decremented by setOverflow()
  93. // wiki: "force all registers (Autoreload, prescaler, compare) to be taken into account"
  94. // So, if the new overflow value is less than the count it will trigger a rollover interrupt.
  95. if (overflow < timer_instance[timer_num]->getCount()) // Added 'if' here because reports say it won't boot without it
  96. timer_instance[timer_num]->refresh();
  97. }
  98. }
  99. #define HAL_timer_isr_prologue(T) NOOP
  100. #define HAL_timer_isr_epilogue(T) NOOP