My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

timers.h 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. *
  20. */
  21. #pragma once
  22. /**
  23. * HAL Timers for Arduino Due and compatible (SAM3X8E)
  24. */
  25. #include <stdint.h>
  26. // ------------------------
  27. // Defines
  28. // ------------------------
  29. #define FORCE_INLINE __attribute__((always_inline)) inline
  30. typedef uint32_t hal_timer_t;
  31. #define HAL_TIMER_TYPE_MAX 0xFFFFFFFF
  32. #define HAL_TIMER_PRESCALER 2
  33. #define HAL_TIMER_RATE ((F_CPU) / (HAL_TIMER_PRESCALER)) // frequency of timers peripherals
  34. #ifndef MF_TIMER_STEP
  35. #define MF_TIMER_STEP 2 // Timer Index for Stepper
  36. #endif
  37. #ifndef MF_TIMER_PULSE
  38. #define MF_TIMER_PULSE MF_TIMER_STEP
  39. #endif
  40. #ifndef MF_TIMER_TEMP
  41. #define MF_TIMER_TEMP 4 // Timer Index for Temperature
  42. #endif
  43. #ifndef MF_TIMER_TONE
  44. #define MF_TIMER_TONE 6 // index of timer to use for beeper tones
  45. #endif
  46. #define TEMP_TIMER_FREQUENCY 1000 // temperature interrupt frequency
  47. #define STEPPER_TIMER_RATE HAL_TIMER_RATE // frequency of stepper timer (HAL_TIMER_RATE / STEPPER_TIMER_PRESCALE)
  48. #define STEPPER_TIMER_TICKS_PER_US ((STEPPER_TIMER_RATE) / 1000000) // stepper timer ticks per µs
  49. #define STEPPER_TIMER_PRESCALE (CYCLES_PER_MICROSECOND / STEPPER_TIMER_TICKS_PER_US)
  50. #define PULSE_TIMER_RATE STEPPER_TIMER_RATE // frequency of pulse timer
  51. #define PULSE_TIMER_PRESCALE STEPPER_TIMER_PRESCALE
  52. #define PULSE_TIMER_TICKS_PER_US STEPPER_TIMER_TICKS_PER_US
  53. #define ENABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_enable_interrupt(MF_TIMER_STEP)
  54. #define DISABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_disable_interrupt(MF_TIMER_STEP)
  55. #define STEPPER_ISR_ENABLED() HAL_timer_interrupt_enabled(MF_TIMER_STEP)
  56. #define ENABLE_TEMPERATURE_INTERRUPT() HAL_timer_enable_interrupt(MF_TIMER_TEMP)
  57. #define DISABLE_TEMPERATURE_INTERRUPT() HAL_timer_disable_interrupt(MF_TIMER_TEMP)
  58. #ifndef HAL_STEP_TIMER_ISR
  59. #define HAL_STEP_TIMER_ISR() void TC2_Handler()
  60. #endif
  61. #ifndef HAL_TEMP_TIMER_ISR
  62. #define HAL_TEMP_TIMER_ISR() void TC4_Handler()
  63. #endif
  64. #ifndef HAL_TONE_TIMER_ISR
  65. #define HAL_TONE_TIMER_ISR() void TC6_Handler()
  66. #endif
  67. // ------------------------
  68. // Types
  69. // ------------------------
  70. typedef struct {
  71. Tc *pTimerRegs;
  72. uint16_t channel;
  73. IRQn_Type IRQ_Id;
  74. uint8_t priority;
  75. } tTimerConfig;
  76. // ------------------------
  77. // Public Variables
  78. // ------------------------
  79. extern const tTimerConfig timer_config[];
  80. // ------------------------
  81. // Public functions
  82. // ------------------------
  83. void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency);
  84. FORCE_INLINE static void HAL_timer_set_compare(const uint8_t timer_num, const hal_timer_t compare) {
  85. const tTimerConfig * const pConfig = &timer_config[timer_num];
  86. pConfig->pTimerRegs->TC_CHANNEL[pConfig->channel].TC_RC = compare;
  87. }
  88. FORCE_INLINE static hal_timer_t HAL_timer_get_compare(const uint8_t timer_num) {
  89. const tTimerConfig * const pConfig = &timer_config[timer_num];
  90. return pConfig->pTimerRegs->TC_CHANNEL[pConfig->channel].TC_RC;
  91. }
  92. FORCE_INLINE static hal_timer_t HAL_timer_get_count(const uint8_t timer_num) {
  93. const tTimerConfig * const pConfig = &timer_config[timer_num];
  94. return pConfig->pTimerRegs->TC_CHANNEL[pConfig->channel].TC_CV;
  95. }
  96. void HAL_timer_enable_interrupt(const uint8_t timer_num);
  97. void HAL_timer_disable_interrupt(const uint8_t timer_num);
  98. bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
  99. FORCE_INLINE static void HAL_timer_isr_prologue(const uint8_t timer_num) {
  100. const tTimerConfig * const pConfig = &timer_config[timer_num];
  101. // Reading the status register clears the interrupt flag
  102. pConfig->pTimerRegs->TC_CHANNEL[pConfig->channel].TC_SR;
  103. }
  104. #define HAL_timer_isr_epilogue(T) NOOP