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

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