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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. /**
  23. * SAMD51 HAL developed by Giuliano Zaro (AKA GMagician)
  24. */
  25. #ifdef __SAMD51__
  26. // --------------------------------------------------------------------------
  27. // Includes
  28. // --------------------------------------------------------------------------
  29. #include "../../inc/MarlinConfig.h"
  30. #include "ServoTimers.h" // for SERVO_TC
  31. // --------------------------------------------------------------------------
  32. // Local defines
  33. // --------------------------------------------------------------------------
  34. #define NUM_HARDWARE_TIMERS 9
  35. // --------------------------------------------------------------------------
  36. // Private Variables
  37. // --------------------------------------------------------------------------
  38. const tTimerConfig timer_config[NUM_HARDWARE_TIMERS] = {
  39. { {.pTc=TC0}, TC0_IRQn, TC_PRIORITY(0) }, // 0 - stepper (assigned priority 2)
  40. { {.pTc=TC1}, TC1_IRQn, TC_PRIORITY(1) }, // 1 - stepper (needed by 32 bit timers)
  41. { {.pTc=TC2}, TC2_IRQn, 5 }, // 2 - tone (reserved by framework and fixed assigned priority 5)
  42. { {.pTc=TC3}, TC3_IRQn, TC_PRIORITY(3) }, // 3 - servo (assigned priority 1)
  43. { {.pTc=TC4}, TC4_IRQn, TC_PRIORITY(4) }, // 4 - software serial (no interrupts used)
  44. { {.pTc=TC5}, TC5_IRQn, TC_PRIORITY(5) },
  45. { {.pTc=TC6}, TC6_IRQn, TC_PRIORITY(6) },
  46. { {.pTc=TC7}, TC7_IRQn, TC_PRIORITY(7) },
  47. { {.pRtc=RTC}, RTC_IRQn, TC_PRIORITY(8) } // 8 - temperature (assigned priority 6)
  48. };
  49. // --------------------------------------------------------------------------
  50. // Private functions
  51. // --------------------------------------------------------------------------
  52. FORCE_INLINE void Disable_Irq(IRQn_Type irq) {
  53. NVIC_DisableIRQ(irq);
  54. // We NEED memory barriers to ensure Interrupts are actually disabled!
  55. // ( https://dzone.com/articles/nvic-disabling-interrupts-on-arm-cortex-m-and-the )
  56. __DSB();
  57. __ISB();
  58. }
  59. // --------------------------------------------------------------------------
  60. // Public functions
  61. // --------------------------------------------------------------------------
  62. void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency) {
  63. IRQn_Type irq = timer_config[timer_num].IRQ_Id;
  64. // Disable interrupt, just in case it was already enabled
  65. Disable_Irq(irq);
  66. if (timer_num == MF_TIMER_RTC) {
  67. Rtc * const rtc = timer_config[timer_num].pRtc;
  68. // Disable timer interrupt
  69. rtc->MODE0.INTENCLR.reg = RTC_MODE0_INTENCLR_CMP0;
  70. // RTC clock setup
  71. OSC32KCTRL->RTCCTRL.reg = OSC32KCTRL_RTCCTRL_RTCSEL_XOSC32K; // External 32.768kHz oscillator
  72. // Stop timer, just in case, to be able to reconfigure it
  73. rtc->MODE0.CTRLA.bit.ENABLE = false;
  74. SYNC(rtc->MODE0.SYNCBUSY.bit.ENABLE);
  75. // Mode, reset counter on match
  76. rtc->MODE0.CTRLA.reg = RTC_MODE0_CTRLA_MODE_COUNT32 | RTC_MODE0_CTRLA_MATCHCLR;
  77. // Set compare value
  78. rtc->MODE0.COMP[0].reg = (32768 + frequency / 2) / frequency;
  79. SYNC(rtc->MODE0.SYNCBUSY.bit.COMP0);
  80. // Enable interrupt on compare
  81. rtc->MODE0.INTFLAG.reg = RTC_MODE0_INTFLAG_CMP0; // reset pending interrupt
  82. rtc->MODE0.INTENSET.reg = RTC_MODE0_INTENSET_CMP0; // enable compare 0 interrupt
  83. // And start timer
  84. rtc->MODE0.CTRLA.bit.ENABLE = true;
  85. SYNC(rtc->MODE0.SYNCBUSY.bit.ENABLE);
  86. }
  87. else {
  88. Tc * const tc = timer_config[timer_num].pTc;
  89. // Disable timer interrupt
  90. tc->COUNT32.INTENCLR.reg = TC_INTENCLR_OVF; // disable overflow interrupt
  91. // TCn clock setup
  92. const uint8_t clockID = GCLK_CLKCTRL_IDs[TCC_INST_NUM + timer_num]; // TC clock are preceded by TCC ones
  93. GCLK->PCHCTRL[clockID].bit.CHEN = false;
  94. SYNC(GCLK->PCHCTRL[clockID].bit.CHEN);
  95. GCLK->PCHCTRL[clockID].reg = GCLK_PCHCTRL_GEN_GCLK0 | GCLK_PCHCTRL_CHEN; // 120MHz startup code programmed
  96. SYNC(!GCLK->PCHCTRL[clockID].bit.CHEN);
  97. // Stop timer, just in case, to be able to reconfigure it
  98. tc->COUNT32.CTRLA.bit.ENABLE = false;
  99. SYNC(tc->COUNT32.SYNCBUSY.bit.ENABLE);
  100. // Reset timer
  101. tc->COUNT32.CTRLA.bit.SWRST = true;
  102. SYNC(tc->COUNT32.SYNCBUSY.bit.SWRST);
  103. // Wave mode, reset counter on compare match
  104. tc->COUNT32.WAVE.reg = TC_WAVE_WAVEGEN_MFRQ;
  105. tc->COUNT32.CTRLA.reg = TC_CTRLA_MODE_COUNT32 | TC_CTRLA_PRESCALER_DIV1;
  106. tc->COUNT32.CTRLBCLR.reg = TC_CTRLBCLR_DIR;
  107. SYNC(tc->COUNT32.SYNCBUSY.bit.CTRLB);
  108. // Set compare value
  109. tc->COUNT32.CC[0].reg = (HAL_TIMER_RATE) / frequency;
  110. tc->COUNT32.COUNT.reg = 0;
  111. // Enable interrupt on compare
  112. tc->COUNT32.INTFLAG.reg = TC_INTFLAG_OVF; // reset pending interrupt
  113. tc->COUNT32.INTENSET.reg = TC_INTENSET_OVF; // enable overflow interrupt
  114. // And start timer
  115. tc->COUNT32.CTRLA.bit.ENABLE = true;
  116. SYNC(tc->COUNT32.SYNCBUSY.bit.ENABLE);
  117. }
  118. // Finally, enable IRQ
  119. NVIC_SetPriority(irq, timer_config[timer_num].priority);
  120. NVIC_EnableIRQ(irq);
  121. }
  122. void HAL_timer_enable_interrupt(const uint8_t timer_num) {
  123. const IRQn_Type irq = timer_config[timer_num].IRQ_Id;
  124. NVIC_EnableIRQ(irq);
  125. }
  126. void HAL_timer_disable_interrupt(const uint8_t timer_num) {
  127. const IRQn_Type irq = timer_config[timer_num].IRQ_Id;
  128. Disable_Irq(irq);
  129. }
  130. // missing from CMSIS: Check if interrupt is enabled or not
  131. static bool NVIC_GetEnabledIRQ(IRQn_Type IRQn) {
  132. return TEST(NVIC->ISER[uint32_t(IRQn) >> 5], uint32_t(IRQn) & 0x1F);
  133. }
  134. bool HAL_timer_interrupt_enabled(const uint8_t timer_num) {
  135. const IRQn_Type irq = timer_config[timer_num].IRQ_Id;
  136. return NVIC_GetEnabledIRQ(irq);
  137. }
  138. #endif // __SAMD51__