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.

Servo.cpp 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. /**
  22. * This comes from Arduino library which at the moment is buggy and uncompilable
  23. */
  24. #ifdef __SAMD51__
  25. #include "../../inc/MarlinConfig.h"
  26. #if HAS_SERVOS
  27. #include "../shared/Marduino.h"
  28. #include "../shared/servo.h"
  29. #include "../shared/servo_private.h"
  30. #include "SAMD51.h"
  31. #include "timers.h"
  32. #define __TC_GCLK_ID(t) TC##t##_GCLK_ID
  33. #define _TC_GCLK_ID(t) __TC_GCLK_ID(t)
  34. #define TC_GCLK_ID _TC_GCLK_ID(SERVO_TC)
  35. #define _TC_PRESCALER(d) TC_CTRLA_PRESCALER_DIV##d##_Val
  36. #define TC_PRESCALER(d) _TC_PRESCALER(d)
  37. #define __SERVO_IRQn(t) TC##t##_IRQn
  38. #define _SERVO_IRQn(t) __SERVO_IRQn(t)
  39. #define SERVO_IRQn _SERVO_IRQn(SERVO_TC)
  40. #define HAL_SERVO_TIMER_ISR() TC_HANDLER(SERVO_TC)
  41. #define TIMER_TCCHANNEL(t) ((t) & 1)
  42. #define TC_COUNTER_START_VAL 0xFFFF
  43. static volatile int8_t currentServoIndex[_Nbr_16timers]; // index for the servo being pulsed for each timer (or -1 if refresh interval)
  44. FORCE_INLINE static uint16_t getTimerCount() {
  45. Tc * const tc = TimerConfig[SERVO_TC].pTimer;
  46. tc->COUNT16.CTRLBSET.reg = TC_CTRLBCLR_CMD_READSYNC;
  47. SYNC(tc->COUNT16.SYNCBUSY.bit.CTRLB || tc->COUNT16.SYNCBUSY.bit.COUNT);
  48. return tc->COUNT16.COUNT.reg;
  49. }
  50. // ----------------------------
  51. // Interrupt handler for the TC
  52. // ----------------------------
  53. HAL_SERVO_TIMER_ISR() {
  54. Tc * const tc = TimerConfig[SERVO_TC].pTimer;
  55. const timer16_Sequence_t timer =
  56. #ifndef _useTimer1
  57. _timer2
  58. #elif !defined(_useTimer2)
  59. _timer1
  60. #else
  61. (tc->COUNT16.INTFLAG.reg & tc->COUNT16.INTENSET.reg & TC_INTFLAG_MC0) ? _timer1 : _timer2
  62. #endif
  63. ;
  64. const uint8_t tcChannel = TIMER_TCCHANNEL(timer);
  65. if (currentServoIndex[timer] < 0) {
  66. #if defined(_useTimer1) && defined(_useTimer2)
  67. if (currentServoIndex[timer ^ 1] >= 0) {
  68. // Wait for both channels
  69. // Clear the interrupt
  70. tc->COUNT16.INTFLAG.reg = (tcChannel == 0) ? TC_INTFLAG_MC0 : TC_INTFLAG_MC1;
  71. return;
  72. }
  73. #endif
  74. tc->COUNT16.COUNT.reg = TC_COUNTER_START_VAL;
  75. SYNC(tc->COUNT16.SYNCBUSY.bit.COUNT);
  76. }
  77. else if (SERVO_INDEX(timer, currentServoIndex[timer]) < ServoCount && SERVO(timer, currentServoIndex[timer]).Pin.isActive)
  78. digitalWrite(SERVO(timer, currentServoIndex[timer]).Pin.nbr, LOW); // pulse this channel low if activated
  79. // Select the next servo controlled by this timer
  80. currentServoIndex[timer]++;
  81. if (SERVO_INDEX(timer, currentServoIndex[timer]) < ServoCount && currentServoIndex[timer] < SERVOS_PER_TIMER) {
  82. if (SERVO(timer, currentServoIndex[timer]).Pin.isActive) // check if activated
  83. digitalWrite(SERVO(timer, currentServoIndex[timer]).Pin.nbr, HIGH); // it's an active channel so pulse it high
  84. tc->COUNT16.CC[tcChannel].reg = getTimerCount() - (uint16_t)SERVO(timer, currentServoIndex[timer]).ticks;
  85. }
  86. else {
  87. // finished all channels so wait for the refresh period to expire before starting over
  88. currentServoIndex[timer] = -1; // this will get incremented at the end of the refresh period to start again at the first channel
  89. const uint16_t tcCounterValue = getTimerCount();
  90. if ((TC_COUNTER_START_VAL - tcCounterValue) + 4UL < usToTicks(REFRESH_INTERVAL)) // allow a few ticks to ensure the next OCR1A not missed
  91. tc->COUNT16.CC[tcChannel].reg = TC_COUNTER_START_VAL - (uint16_t)usToTicks(REFRESH_INTERVAL);
  92. else
  93. tc->COUNT16.CC[tcChannel].reg = (uint16_t)(tcCounterValue - 4UL); // at least REFRESH_INTERVAL has elapsed
  94. }
  95. if (tcChannel == 0) {
  96. SYNC(tc->COUNT16.SYNCBUSY.bit.CC0);
  97. // Clear the interrupt
  98. tc->COUNT16.INTFLAG.reg = TC_INTFLAG_MC0;
  99. }
  100. else {
  101. SYNC(tc->COUNT16.SYNCBUSY.bit.CC1);
  102. // Clear the interrupt
  103. tc->COUNT16.INTFLAG.reg = TC_INTFLAG_MC1;
  104. }
  105. }
  106. void initISR(timer16_Sequence_t timer) {
  107. Tc * const tc = TimerConfig[SERVO_TC].pTimer;
  108. const uint8_t tcChannel = TIMER_TCCHANNEL(timer);
  109. static bool initialized = false; // Servo TC has been initialized
  110. if (!initialized) {
  111. NVIC_DisableIRQ(SERVO_IRQn);
  112. // Disable the timer
  113. tc->COUNT16.CTRLA.bit.ENABLE = false;
  114. SYNC(tc->COUNT16.SYNCBUSY.bit.ENABLE);
  115. // Select GCLK0 as timer/counter input clock source
  116. GCLK->PCHCTRL[TC_GCLK_ID].bit.CHEN = false;
  117. SYNC(GCLK->PCHCTRL[TC_GCLK_ID].bit.CHEN);
  118. GCLK->PCHCTRL[TC_GCLK_ID].reg = GCLK_PCHCTRL_GEN_GCLK0 | GCLK_PCHCTRL_CHEN; // 120MHz startup code programmed
  119. SYNC(!GCLK->PCHCTRL[TC_GCLK_ID].bit.CHEN);
  120. // Reset the timer
  121. tc->COUNT16.CTRLA.bit.SWRST = true;
  122. SYNC(tc->COUNT16.SYNCBUSY.bit.SWRST);
  123. SYNC(tc->COUNT16.CTRLA.bit.SWRST);
  124. // Set timer counter mode to 16 bits
  125. tc->COUNT16.CTRLA.reg = TC_CTRLA_MODE_COUNT16;
  126. // Set timer counter mode as normal PWM
  127. tc->COUNT16.WAVE.bit.WAVEGEN = TCC_WAVE_WAVEGEN_NPWM_Val;
  128. // Set the prescaler factor
  129. tc->COUNT16.CTRLA.bit.PRESCALER = TC_PRESCALER(SERVO_TIMER_PRESCALER);
  130. // Count down
  131. tc->COUNT16.CTRLBSET.reg = TC_CTRLBCLR_DIR;
  132. SYNC(tc->COUNT16.SYNCBUSY.bit.CTRLB);
  133. // Reset all servo indexes
  134. memset((void *)currentServoIndex, 0xFF, sizeof(currentServoIndex));
  135. // Configure interrupt request
  136. NVIC_ClearPendingIRQ(SERVO_IRQn);
  137. NVIC_SetPriority(SERVO_IRQn, 5);
  138. NVIC_EnableIRQ(SERVO_IRQn);
  139. initialized = true;
  140. }
  141. if (!tc->COUNT16.CTRLA.bit.ENABLE) {
  142. // Reset the timer counter
  143. tc->COUNT16.COUNT.reg = TC_COUNTER_START_VAL;
  144. SYNC(tc->COUNT16.SYNCBUSY.bit.COUNT);
  145. // Enable the timer and start it
  146. tc->COUNT16.CTRLA.bit.ENABLE = true;
  147. SYNC(tc->COUNT16.SYNCBUSY.bit.ENABLE);
  148. }
  149. // First interrupt request after 1 ms
  150. tc->COUNT16.CC[tcChannel].reg = getTimerCount() - (uint16_t)usToTicks(1000UL);
  151. if (tcChannel == 0 ) {
  152. SYNC(tc->COUNT16.SYNCBUSY.bit.CC0);
  153. // Clear pending match interrupt
  154. tc->COUNT16.INTFLAG.reg = TC_INTENSET_MC0;
  155. // Enable the match channel interrupt request
  156. tc->COUNT16.INTENSET.reg = TC_INTENSET_MC0;
  157. }
  158. else {
  159. SYNC(tc->COUNT16.SYNCBUSY.bit.CC1);
  160. // Clear pending match interrupt
  161. tc->COUNT16.INTFLAG.reg = TC_INTENSET_MC1;
  162. // Enable the match channel interrupt request
  163. tc->COUNT16.INTENSET.reg = TC_INTENSET_MC1;
  164. }
  165. }
  166. void finISR(timer16_Sequence_t timer) {
  167. Tc * const tc = TimerConfig[SERVO_TC].pTimer;
  168. const uint8_t tcChannel = TIMER_TCCHANNEL(timer);
  169. // Disable the match channel interrupt request
  170. tc->COUNT16.INTENCLR.reg = (tcChannel == 0) ? TC_INTENCLR_MC0 : TC_INTENCLR_MC1;
  171. if (true
  172. #if defined(_useTimer1) && defined(_useTimer2)
  173. && (tc->COUNT16.INTENCLR.reg & (TC_INTENCLR_MC0|TC_INTENCLR_MC1)) == 0
  174. #endif
  175. ) {
  176. // Disable the timer if not used
  177. tc->COUNT16.CTRLA.bit.ENABLE = false;
  178. SYNC(tc->COUNT16.SYNCBUSY.bit.ENABLE);
  179. }
  180. }
  181. #endif // HAS_SERVOS
  182. #endif // __SAMD51__