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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 <http://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. #define STEP_TIMER_NUM 0 // index of timer to use for stepper (also +1 for 32bits counter)
  31. #define PULSE_TIMER_NUM STEP_TIMER_NUM
  32. #define TEMP_TIMER_NUM RTC_TIMER_NUM // index of timer to use for temperature
  33. #define TEMP_TIMER_FREQUENCY 1000 // temperature interrupt frequency
  34. #define STEPPER_TIMER_RATE HAL_TIMER_RATE // frequency of stepper timer (HAL_TIMER_RATE / STEPPER_TIMER_PRESCALE)
  35. #define STEPPER_TIMER_TICKS_PER_US (STEPPER_TIMER_RATE / 1000000) // stepper timer ticks per µs
  36. #define STEPPER_TIMER_PRESCALE (CYCLES_PER_MICROSECOND / STEPPER_TIMER_TICKS_PER_US)
  37. #define PULSE_TIMER_RATE STEPPER_TIMER_RATE
  38. #define PULSE_TIMER_PRESCALE STEPPER_TIMER_PRESCALE
  39. #define PULSE_TIMER_TICKS_PER_US STEPPER_TIMER_TICKS_PER_US
  40. #define ENABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_enable_interrupt(STEP_TIMER_NUM)
  41. #define DISABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_disable_interrupt(STEP_TIMER_NUM)
  42. #define STEPPER_ISR_ENABLED() HAL_timer_interrupt_enabled(STEP_TIMER_NUM)
  43. #define ENABLE_TEMPERATURE_INTERRUPT() HAL_timer_enable_interrupt(TEMP_TIMER_NUM)
  44. #define DISABLE_TEMPERATURE_INTERRUPT() HAL_timer_disable_interrupt(TEMP_TIMER_NUM)
  45. #define TC_PRIORITY(t) (t == STEP_TIMER_NUM || t == PULSE_TIMER_NUM) ? 2 \
  46. : (t == TEMP_TIMER_NUM) ? 6 \
  47. : 7
  48. #define _TC_HANDLER(t) void TC##t##_Handler()
  49. #define TC_HANDLER(t) _TC_HANDLER(t)
  50. #define HAL_STEP_TIMER_ISR() TC_HANDLER(STEP_TIMER_NUM)
  51. #if STEP_TIMER_NUM != PULSE_TIMER_NUM
  52. #define HAL_PULSE_TIMER_ISR() TC_HANDLER(PULSE_TIMER_NUM)
  53. #endif
  54. #if TEMP_TIMER_NUM == RTC_TIMER_NUM
  55. #define HAL_TEMP_TIMER_ISR() void RTC_Handler()
  56. #else
  57. #define HAL_TEMP_TIMER_ISR() TC_HANDLER(TEMP_TIMER_NUM)
  58. #endif
  59. // --------------------------------------------------------------------------
  60. // Types
  61. // --------------------------------------------------------------------------
  62. typedef struct {
  63. union {
  64. Tc *pTc;
  65. Rtc *pRtc;
  66. };
  67. IRQn_Type IRQ_Id;
  68. uint8_t priority;
  69. } tTimerConfig;
  70. // --------------------------------------------------------------------------
  71. // Public Variables
  72. // --------------------------------------------------------------------------
  73. extern const tTimerConfig TimerConfig[];
  74. // --------------------------------------------------------------------------
  75. // Public functions
  76. // --------------------------------------------------------------------------
  77. void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency);
  78. FORCE_INLINE static void HAL_timer_set_compare(const uint8_t timer_num, const hal_timer_t compare) {
  79. // Should never be called with timer RTC_TIMER_NUM
  80. Tc * const tc = TimerConfig[timer_num].pTc;
  81. tc->COUNT32.CC[0].reg = HAL_TIMER_TYPE_MAX - compare;
  82. }
  83. FORCE_INLINE static hal_timer_t HAL_timer_get_compare(const uint8_t timer_num) {
  84. // Should never be called with timer RTC_TIMER_NUM
  85. Tc * const tc = TimerConfig[timer_num].pTc;
  86. return (hal_timer_t)(HAL_TIMER_TYPE_MAX - tc->COUNT32.CC[0].reg);
  87. }
  88. FORCE_INLINE static hal_timer_t HAL_timer_get_count(const uint8_t timer_num) {
  89. // Should never be called with timer RTC_TIMER_NUM
  90. Tc * const tc = TimerConfig[timer_num].pTc;
  91. tc->COUNT32.CTRLBSET.reg = TC_CTRLBCLR_CMD_READSYNC;
  92. SYNC(tc->COUNT32.SYNCBUSY.bit.CTRLB || tc->COUNT32.SYNCBUSY.bit.COUNT);
  93. return HAL_TIMER_TYPE_MAX - tc->COUNT32.COUNT.reg;
  94. }
  95. void HAL_timer_enable_interrupt(const uint8_t timer_num);
  96. void HAL_timer_disable_interrupt(const uint8_t timer_num);
  97. bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
  98. FORCE_INLINE static void HAL_timer_isr_prologue(const uint8_t timer_num) {
  99. if (timer_num == RTC_TIMER_NUM) {
  100. Rtc * const rtc = TimerConfig[timer_num].pRtc;
  101. // Clear interrupt flag
  102. rtc->MODE0.INTFLAG.reg = RTC_MODE0_INTFLAG_CMP0;
  103. }
  104. else {
  105. Tc * const tc = TimerConfig[timer_num].pTc;
  106. // Clear interrupt flag
  107. tc->COUNT32.INTFLAG.reg = TC_INTFLAG_OVF;
  108. }
  109. }
  110. #define HAL_timer_isr_epilogue(timer_num)