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.

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