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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. // ------------------------
  25. // Defines
  26. // ------------------------
  27. #define FORCE_INLINE __attribute__((always_inline)) inline
  28. #define hal_timer_t uint32_t // TODO: One is 16-bit, one 32-bit - does this need to be checked?
  29. #define HAL_TIMER_TYPE_MAX 0xFFFF
  30. #define HAL_TIMER_RATE (HAL_RCC_GetSysClockFreq() / 2) // frequency of timer peripherals
  31. #ifndef STEP_TIMER_NUM
  32. #define STEP_TIMER_NUM 0 // Timer Index for Stepper
  33. #endif
  34. #ifndef PULSE_TIMER_NUM
  35. #define PULSE_TIMER_NUM STEP_TIMER_NUM
  36. #endif
  37. #ifndef TEMP_TIMER_NUM
  38. #define TEMP_TIMER_NUM 1 // Timer Index for Temperature
  39. #endif
  40. #define TEMP_TIMER_PRESCALE 1000 // prescaler for setting Temp timer, 72Khz
  41. #define TEMP_TIMER_FREQUENCY 1000 // temperature interrupt frequency
  42. #define STEPPER_TIMER_PRESCALE 54 // was 40,prescaler for setting stepper timer, 2Mhz
  43. #define STEPPER_TIMER_RATE (HAL_TIMER_RATE / STEPPER_TIMER_PRESCALE) // frequency of stepper timer
  44. #define STEPPER_TIMER_TICKS_PER_US ((STEPPER_TIMER_RATE) / 1000000) // stepper timer ticks per µs
  45. #define PULSE_TIMER_RATE STEPPER_TIMER_RATE // frequency of pulse timer
  46. #define PULSE_TIMER_PRESCALE STEPPER_TIMER_PRESCALE
  47. #define PULSE_TIMER_TICKS_PER_US STEPPER_TIMER_TICKS_PER_US
  48. #define ENABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_enable_interrupt(STEP_TIMER_NUM)
  49. #define DISABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_disable_interrupt(STEP_TIMER_NUM)
  50. #define STEPPER_ISR_ENABLED() HAL_timer_interrupt_enabled(STEP_TIMER_NUM)
  51. #define ENABLE_TEMPERATURE_INTERRUPT() HAL_timer_enable_interrupt(TEMP_TIMER_NUM)
  52. #define DISABLE_TEMPERATURE_INTERRUPT() HAL_timer_disable_interrupt(TEMP_TIMER_NUM)
  53. // TODO change this
  54. #ifdef STM32GENERIC
  55. #define TC_TIMER_ARGS
  56. #else
  57. #define TC_TIMER_ARGS stimer_t *htim
  58. #endif
  59. extern void TC5_Handler(TC_TIMER_ARGS);
  60. extern void TC7_Handler(TC_TIMER_ARGS);
  61. #ifndef HAL_STEP_TIMER_ISR
  62. #define HAL_STEP_TIMER_ISR() void TC5_Handler(TC_TIMER_ARGS)
  63. #endif
  64. #ifndef HAL_TEMP_TIMER_ISR
  65. #define HAL_TEMP_TIMER_ISR() void TC7_Handler(TC_TIMER_ARGS)
  66. #endif
  67. // ------------------------
  68. // Types
  69. // ------------------------
  70. #ifdef STM32GENERIC
  71. typedef struct {
  72. TIM_HandleTypeDef handle;
  73. uint32_t callback;
  74. } tTimerConfig;
  75. typedef tTimerConfig stm32_timer_t;
  76. #else
  77. typedef stimer_t stm32_timer_t;
  78. #endif
  79. // ------------------------
  80. // Public Variables
  81. // ------------------------
  82. extern stm32_timer_t TimerHandle[];
  83. // ------------------------
  84. // Public functions
  85. // ------------------------
  86. void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency);
  87. void HAL_timer_enable_interrupt(const uint8_t timer_num);
  88. void HAL_timer_disable_interrupt(const uint8_t timer_num);
  89. bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
  90. FORCE_INLINE static uint32_t HAL_timer_get_count(const uint8_t timer_num) {
  91. return __HAL_TIM_GET_COUNTER(&TimerHandle[timer_num].handle);
  92. }
  93. FORCE_INLINE static void HAL_timer_set_compare(const uint8_t timer_num, const uint32_t compare) {
  94. __HAL_TIM_SET_AUTORELOAD(&TimerHandle[timer_num].handle, compare);
  95. if (HAL_timer_get_count(timer_num) >= compare)
  96. TimerHandle[timer_num].handle.Instance->EGR |= TIM_EGR_UG; // Generate an immediate update interrupt
  97. }
  98. FORCE_INLINE static hal_timer_t HAL_timer_get_compare(const uint8_t timer_num) {
  99. return __HAL_TIM_GET_AUTORELOAD(&TimerHandle[timer_num].handle);
  100. }
  101. #ifdef STM32GENERIC
  102. FORCE_INLINE static void HAL_timer_isr_prologue(const uint8_t timer_num) {
  103. if (__HAL_TIM_GET_FLAG(&TimerHandle[timer_num].handle, TIM_FLAG_UPDATE) == SET)
  104. __HAL_TIM_CLEAR_FLAG(&TimerHandle[timer_num].handle, TIM_FLAG_UPDATE);
  105. }
  106. #else
  107. #define HAL_timer_isr_prologue(TIMER_NUM)
  108. #endif
  109. #define HAL_timer_isr_epilogue(TIMER_NUM)