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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. /**
  24. * SAMD51 HAL developed by Giuliano Zaro (AKA GMagician)
  25. */
  26. #include <stdint.h>
  27. // --------------------------------------------------------------------------
  28. // Defines
  29. // --------------------------------------------------------------------------
  30. typedef uint32_t hal_timer_t;
  31. #define HAL_TIMER_TYPE_MAX 0xFFFFFFFF
  32. #define HAL_TIMER_RATE F_CPU // frequency of timers peripherals
  33. #define MF_TIMER_RTC 8 // This is not a TC but a RTC
  34. #ifndef MF_TIMER_STEP
  35. #define MF_TIMER_STEP 0 // 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 MF_TIMER_RTC // Timer Index for Temperature
  42. #endif
  43. #define TEMP_TIMER_FREQUENCY 1000 // temperature interrupt frequency
  44. #define STEPPER_TIMER_RATE HAL_TIMER_RATE // frequency of stepper timer (HAL_TIMER_RATE / STEPPER_TIMER_PRESCALE)
  45. #define STEPPER_TIMER_TICKS_PER_US (STEPPER_TIMER_RATE / 1000000) // stepper timer ticks per µs
  46. #define STEPPER_TIMER_PRESCALE (CYCLES_PER_MICROSECOND / STEPPER_TIMER_TICKS_PER_US)
  47. #define PULSE_TIMER_RATE STEPPER_TIMER_RATE
  48. #define PULSE_TIMER_PRESCALE STEPPER_TIMER_PRESCALE
  49. #define PULSE_TIMER_TICKS_PER_US STEPPER_TIMER_TICKS_PER_US
  50. #define ENABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_enable_interrupt(MF_TIMER_STEP)
  51. #define DISABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_disable_interrupt(MF_TIMER_STEP)
  52. #define STEPPER_ISR_ENABLED() HAL_timer_interrupt_enabled(MF_TIMER_STEP)
  53. #define ENABLE_TEMPERATURE_INTERRUPT() HAL_timer_enable_interrupt(MF_TIMER_TEMP)
  54. #define DISABLE_TEMPERATURE_INTERRUPT() HAL_timer_disable_interrupt(MF_TIMER_TEMP)
  55. #define TC_PRIORITY(t) ( t == SERVO_TC ? 1 \
  56. : (t == MF_TIMER_STEP || t == MF_TIMER_PULSE) ? 2 \
  57. : (t == MF_TIMER_TEMP) ? 6 : 7 )
  58. #define _TC_HANDLER(t) void TC##t##_Handler()
  59. #define TC_HANDLER(t) _TC_HANDLER(t)
  60. #ifndef HAL_STEP_TIMER_ISR
  61. #define HAL_STEP_TIMER_ISR() TC_HANDLER(MF_TIMER_STEP)
  62. #endif
  63. #if MF_TIMER_STEP != MF_TIMER_PULSE
  64. #define HAL_PULSE_TIMER_ISR() TC_HANDLER(MF_TIMER_PULSE)
  65. #endif
  66. #if MF_TIMER_TEMP == MF_TIMER_RTC
  67. #define HAL_TEMP_TIMER_ISR() void RTC_Handler()
  68. #else
  69. #define HAL_TEMP_TIMER_ISR() TC_HANDLER(MF_TIMER_TEMP)
  70. #endif
  71. // --------------------------------------------------------------------------
  72. // Types
  73. // --------------------------------------------------------------------------
  74. typedef struct {
  75. union {
  76. Tc *pTc;
  77. Rtc *pRtc;
  78. };
  79. IRQn_Type IRQ_Id;
  80. uint8_t priority;
  81. } tTimerConfig;
  82. // --------------------------------------------------------------------------
  83. // Public Variables
  84. // --------------------------------------------------------------------------
  85. extern const tTimerConfig timer_config[];
  86. // --------------------------------------------------------------------------
  87. // Public functions
  88. // --------------------------------------------------------------------------
  89. void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency);
  90. FORCE_INLINE static void HAL_timer_set_compare(const uint8_t timer_num, const hal_timer_t compare) {
  91. // Should never be called with timer MF_TIMER_RTC
  92. Tc * const tc = timer_config[timer_num].pTc;
  93. tc->COUNT32.CC[0].reg = compare;
  94. }
  95. FORCE_INLINE static hal_timer_t HAL_timer_get_compare(const uint8_t timer_num) {
  96. // Should never be called with timer MF_TIMER_RTC
  97. Tc * const tc = timer_config[timer_num].pTc;
  98. return (hal_timer_t)tc->COUNT32.CC[0].reg;
  99. }
  100. FORCE_INLINE static hal_timer_t HAL_timer_get_count(const uint8_t timer_num) {
  101. // Should never be called with timer MF_TIMER_RTC
  102. Tc * const tc = timer_config[timer_num].pTc;
  103. tc->COUNT32.CTRLBSET.reg = TC_CTRLBCLR_CMD_READSYNC;
  104. SYNC(tc->COUNT32.SYNCBUSY.bit.CTRLB || tc->COUNT32.SYNCBUSY.bit.COUNT);
  105. return tc->COUNT32.COUNT.reg;
  106. }
  107. void HAL_timer_enable_interrupt(const uint8_t timer_num);
  108. void HAL_timer_disable_interrupt(const uint8_t timer_num);
  109. bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
  110. FORCE_INLINE static void HAL_timer_isr_prologue(const uint8_t timer_num) {
  111. if (timer_num == MF_TIMER_RTC) {
  112. Rtc * const rtc = timer_config[timer_num].pRtc;
  113. // Clear interrupt flag
  114. rtc->MODE0.INTFLAG.reg = RTC_MODE0_INTFLAG_CMP0;
  115. }
  116. else {
  117. Tc * const tc = timer_config[timer_num].pTc;
  118. // Clear interrupt flag
  119. tc->COUNT32.INTFLAG.reg = TC_INTFLAG_OVF;
  120. }
  121. }
  122. #define HAL_timer_isr_epilogue(timer_num)