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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. #define NUM_HARDWARE_TIMERS 2
  40. #ifndef STEP_TIMER_NUM
  41. #define STEP_TIMER_NUM 0 // Timer Index for Stepper
  42. #endif
  43. #ifndef PULSE_TIMER_NUM
  44. #define PULSE_TIMER_NUM STEP_TIMER_NUM
  45. #endif
  46. #ifndef TEMP_TIMER_NUM
  47. #define TEMP_TIMER_NUM 1 // Timer Index for Temperature
  48. #endif
  49. #define TEMP_TIMER_FREQUENCY 1000 // Temperature::isr() is expected to be called at around 1kHz
  50. // TODO: get rid of manual rate/prescale/ticks/cycles taken for procedures in stepper.cpp
  51. #define STEPPER_TIMER_RATE 2000000 // 2 Mhz
  52. extern uint32_t GetStepperTimerClkFreq();
  53. #define STEPPER_TIMER_PRESCALE (GetStepperTimerClkFreq() / (STEPPER_TIMER_RATE))
  54. #define STEPPER_TIMER_TICKS_PER_US ((STEPPER_TIMER_RATE) / 1000000) // stepper timer ticks per µs
  55. #define PULSE_TIMER_RATE STEPPER_TIMER_RATE
  56. #define PULSE_TIMER_PRESCALE STEPPER_TIMER_PRESCALE
  57. #define PULSE_TIMER_TICKS_PER_US STEPPER_TIMER_TICKS_PER_US
  58. #define ENABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_enable_interrupt(STEP_TIMER_NUM)
  59. #define DISABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_disable_interrupt(STEP_TIMER_NUM)
  60. #define STEPPER_ISR_ENABLED() HAL_timer_interrupt_enabled(STEP_TIMER_NUM)
  61. #define ENABLE_TEMPERATURE_INTERRUPT() HAL_timer_enable_interrupt(TEMP_TIMER_NUM)
  62. #define DISABLE_TEMPERATURE_INTERRUPT() HAL_timer_disable_interrupt(TEMP_TIMER_NUM)
  63. extern void Step_Handler();
  64. extern void Temp_Handler();
  65. #ifndef HAL_STEP_TIMER_ISR
  66. #define HAL_STEP_TIMER_ISR() void Step_Handler()
  67. #endif
  68. #ifndef HAL_TEMP_TIMER_ISR
  69. #define HAL_TEMP_TIMER_ISR() void Temp_Handler()
  70. #endif
  71. // ------------------------
  72. // Public Variables
  73. // ------------------------
  74. extern HardwareTimer *timer_instance[];
  75. // ------------------------
  76. // Public functions
  77. // ------------------------
  78. void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency);
  79. void HAL_timer_enable_interrupt(const uint8_t timer_num);
  80. void HAL_timer_disable_interrupt(const uint8_t timer_num);
  81. bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
  82. // Configure timer priorities for peripherals such as Software Serial or Servos.
  83. // Exposed here to allow all timer priority information to reside in timers.cpp
  84. void SetTimerInterruptPriorities();
  85. // FORCE_INLINE because these are used in performance-critical situations
  86. FORCE_INLINE bool HAL_timer_initialized(const uint8_t timer_num) {
  87. return timer_instance[timer_num] != nullptr;
  88. }
  89. FORCE_INLINE static hal_timer_t HAL_timer_get_count(const uint8_t timer_num) {
  90. return HAL_timer_initialized(timer_num) ? timer_instance[timer_num]->getCount() : 0;
  91. }
  92. // NOTE: Method name may be misleading.
  93. // STM32 has an Auto-Reload Register (ARR) as opposed to a "compare" register
  94. FORCE_INLINE static void HAL_timer_set_compare(const uint8_t timer_num, const hal_timer_t overflow) {
  95. if (HAL_timer_initialized(timer_num)) {
  96. timer_instance[timer_num]->setOverflow(overflow + 1, TICK_FORMAT); // Value decremented by setOverflow()
  97. // wiki: "force all registers (Autoreload, prescaler, compare) to be taken into account"
  98. // So, if the new overflow value is less than the count it will trigger a rollover interrupt.
  99. if (overflow < timer_instance[timer_num]->getCount()) // Added 'if' here because reports say it won't boot without it
  100. timer_instance[timer_num]->refresh();
  101. }
  102. }
  103. #define HAL_timer_isr_prologue(TIMER_NUM)
  104. #define HAL_timer_isr_epilogue(TIMER_NUM)