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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  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 <driver/timer.h>
  25. // Includes needed to get I2S_STEPPER_STREAM. Note that pins.h
  26. // is included in case this header is being included early.
  27. #include "../../inc/MarlinConfig.h"
  28. #include "../../pins/pins.h"
  29. // ------------------------
  30. // Defines
  31. // ------------------------
  32. //
  33. #define FORCE_INLINE __attribute__((always_inline)) inline
  34. typedef uint64_t hal_timer_t;
  35. #define HAL_TIMER_TYPE_MAX 0xFFFFFFFFFFFFFFFFULL
  36. #ifndef STEP_TIMER_NUM
  37. #define STEP_TIMER_NUM 0 // Timer Index for Stepper
  38. #endif
  39. #ifndef PULSE_TIMER_NUM
  40. #define PULSE_TIMER_NUM STEP_TIMER_NUM
  41. #endif
  42. #ifndef TEMP_TIMER_NUM
  43. #define TEMP_TIMER_NUM 1 // Timer Index for Temperature
  44. #endif
  45. #ifndef PWM_TIMER_NUM
  46. #define PWM_TIMER_NUM 2 // index of timer to use for PWM outputs
  47. #endif
  48. #define HAL_TIMER_RATE APB_CLK_FREQ // frequency of timer peripherals
  49. #if ENABLED(I2S_STEPPER_STREAM)
  50. #define STEPPER_TIMER_PRESCALE 1
  51. #define STEPPER_TIMER_RATE 250000 // 250khz, 4µs pulses of i2s word clock
  52. #define STEPPER_TIMER_TICKS_PER_US ((STEPPER_TIMER_RATE) / 1000000) // stepper timer ticks per µs // wrong would be 0.25
  53. #else
  54. #define STEPPER_TIMER_PRESCALE 40
  55. #define STEPPER_TIMER_RATE ((HAL_TIMER_RATE) / (STEPPER_TIMER_PRESCALE)) // frequency of stepper timer, 2MHz
  56. #define STEPPER_TIMER_TICKS_PER_US ((STEPPER_TIMER_RATE) / 1000000) // stepper timer ticks per µs
  57. #endif
  58. #define STEP_TIMER_MIN_INTERVAL 8 // minimum time in µs between stepper interrupts
  59. #define TEMP_TIMER_PRESCALE 1000 // prescaler for setting Temp timer, 72Khz
  60. #define TEMP_TIMER_FREQUENCY 1000 // temperature interrupt frequency
  61. #define PWM_TIMER_PRESCALE 10
  62. #if ENABLED(FAST_PWM_FAN)
  63. #define PWM_TIMER_FREQUENCY FAST_PWM_FAN_FREQUENCY
  64. #else
  65. #define PWM_TIMER_FREQUENCY (50*128) // 50Hz and 7bit resolution
  66. #endif
  67. #define MAX_PWM_PINS 32 // Number of PWM pin-slots
  68. #define PULSE_TIMER_RATE STEPPER_TIMER_RATE // frequency of pulse timer
  69. #define PULSE_TIMER_PRESCALE STEPPER_TIMER_PRESCALE
  70. #define PULSE_TIMER_TICKS_PER_US STEPPER_TIMER_TICKS_PER_US
  71. #define ENABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_enable_interrupt(STEP_TIMER_NUM)
  72. #define DISABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_disable_interrupt(STEP_TIMER_NUM)
  73. #define STEPPER_ISR_ENABLED() HAL_timer_interrupt_enabled(STEP_TIMER_NUM)
  74. #define ENABLE_TEMPERATURE_INTERRUPT() HAL_timer_enable_interrupt(TEMP_TIMER_NUM)
  75. #define DISABLE_TEMPERATURE_INTERRUPT() HAL_timer_disable_interrupt(TEMP_TIMER_NUM)
  76. #ifndef HAL_TEMP_TIMER_ISR
  77. #define HAL_TEMP_TIMER_ISR() extern "C" void tempTC_Handler()
  78. #endif
  79. #ifndef HAL_STEP_TIMER_ISR
  80. #define HAL_STEP_TIMER_ISR() extern "C" void stepTC_Handler()
  81. #endif
  82. #ifndef HAL_PWM_TIMER_ISR
  83. #define HAL_PWM_TIMER_ISR() extern "C" void pwmTC_Handler()
  84. #endif
  85. extern "C" void tempTC_Handler();
  86. extern "C" void stepTC_Handler();
  87. extern "C" void pwmTC_Handler();
  88. // ------------------------
  89. // Types
  90. // ------------------------
  91. typedef struct {
  92. timer_group_t group;
  93. timer_idx_t idx;
  94. uint32_t divider;
  95. void (*fn)();
  96. } tTimerConfig;
  97. // ------------------------
  98. // Public Variables
  99. // ------------------------
  100. extern const tTimerConfig TimerConfig[];
  101. // ------------------------
  102. // Public functions
  103. // ------------------------
  104. void HAL_timer_start (const uint8_t timer_num, uint32_t frequency);
  105. void HAL_timer_set_compare(const uint8_t timer_num, const hal_timer_t count);
  106. hal_timer_t HAL_timer_get_compare(const uint8_t timer_num);
  107. hal_timer_t HAL_timer_get_count(const uint8_t timer_num);
  108. void HAL_timer_enable_interrupt(const uint8_t timer_num);
  109. void HAL_timer_disable_interrupt(const uint8_t timer_num);
  110. bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
  111. #define HAL_timer_isr_prologue(TIMER_NUM)
  112. #define HAL_timer_isr_epilogue(TIMER_NUM)