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.

HAL_timers_STM32F4.h 5.1KB

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