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

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