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 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. Copyright (c) 2013 Arduino LLC. All right reserved.
  24. This library is free software; you can redistribute it and/or
  25. modify it under the terms of the GNU Lesser General Public
  26. License as published by the Free Software Foundation; either
  27. version 2.1 of the License, or (at your option) any later version.
  28. This library is distributed in the hope that it will be useful,
  29. but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  31. Lesser General Public License for more details.
  32. You should have received a copy of the GNU Lesser General Public
  33. License along with this library; if not, write to the Free Software
  34. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  35. */
  36. #ifdef ARDUINO_ARCH_SAM
  37. #include "../../inc/MarlinConfig.h"
  38. #if HAS_SERVOS
  39. #include "../shared/servo.h"
  40. #include "../shared/servo_private.h"
  41. static volatile int8_t Channel[_Nbr_16timers]; // counter for the servo being pulsed for each timer (or -1 if refresh interval)
  42. // ------------------------
  43. /// Interrupt handler for the TC0 channel 1.
  44. // ------------------------
  45. void Servo_Handler(timer16_Sequence_t timer, Tc *pTc, uint8_t channel);
  46. #ifdef _useTimer1
  47. void HANDLER_FOR_TIMER1() { Servo_Handler(_timer1, TC_FOR_TIMER1, CHANNEL_FOR_TIMER1); }
  48. #endif
  49. #ifdef _useTimer2
  50. void HANDLER_FOR_TIMER2() { Servo_Handler(_timer2, TC_FOR_TIMER2, CHANNEL_FOR_TIMER2); }
  51. #endif
  52. #ifdef _useTimer3
  53. void HANDLER_FOR_TIMER3() { Servo_Handler(_timer3, TC_FOR_TIMER3, CHANNEL_FOR_TIMER3); }
  54. #endif
  55. #ifdef _useTimer4
  56. void HANDLER_FOR_TIMER4() { Servo_Handler(_timer4, TC_FOR_TIMER4, CHANNEL_FOR_TIMER4); }
  57. #endif
  58. #ifdef _useTimer5
  59. void HANDLER_FOR_TIMER5() { Servo_Handler(_timer5, TC_FOR_TIMER5, CHANNEL_FOR_TIMER5); }
  60. #endif
  61. void Servo_Handler(timer16_Sequence_t timer, Tc *tc, uint8_t channel) {
  62. // clear interrupt
  63. tc->TC_CHANNEL[channel].TC_SR;
  64. if (Channel[timer] < 0)
  65. tc->TC_CHANNEL[channel].TC_CCR |= TC_CCR_SWTRG; // channel set to -1 indicated that refresh interval completed so reset the timer
  66. else if (SERVO_INDEX(timer, Channel[timer]) < ServoCount && SERVO(timer, Channel[timer]).Pin.isActive)
  67. extDigitalWrite(SERVO(timer, Channel[timer]).Pin.nbr, LOW); // pulse this channel low if activated
  68. Channel[timer]++; // increment to the next channel
  69. if (SERVO_INDEX(timer, Channel[timer]) < ServoCount && Channel[timer] < SERVOS_PER_TIMER) {
  70. tc->TC_CHANNEL[channel].TC_RA = tc->TC_CHANNEL[channel].TC_CV + SERVO(timer,Channel[timer]).ticks;
  71. if (SERVO(timer,Channel[timer]).Pin.isActive) // check if activated
  72. extDigitalWrite(SERVO(timer, Channel[timer]).Pin.nbr, HIGH); // its an active channel so pulse it high
  73. }
  74. else {
  75. // finished all channels so wait for the refresh period to expire before starting over
  76. tc->TC_CHANNEL[channel].TC_RA =
  77. tc->TC_CHANNEL[channel].TC_CV < usToTicks(REFRESH_INTERVAL) - 4
  78. ? (unsigned int)usToTicks(REFRESH_INTERVAL) // allow a few ticks to ensure the next OCR1A not missed
  79. : tc->TC_CHANNEL[channel].TC_CV + 4; // at least REFRESH_INTERVAL has elapsed
  80. Channel[timer] = -1; // this will get incremented at the end of the refresh period to start again at the first channel
  81. }
  82. }
  83. static void _initISR(Tc *tc, uint32_t channel, uint32_t id, IRQn_Type irqn) {
  84. pmc_enable_periph_clk(id);
  85. TC_Configure(tc, channel,
  86. TC_CMR_TCCLKS_TIMER_CLOCK3 | // MCK/32
  87. TC_CMR_WAVE | // Waveform mode
  88. TC_CMR_WAVSEL_UP_RC ); // Counter running up and reset when equals to RC
  89. /* 84MHz, MCK/32, for 1.5ms: 3937 */
  90. TC_SetRA(tc, channel, 2625); // 1ms
  91. /* Configure and enable interrupt */
  92. NVIC_EnableIRQ(irqn);
  93. // TC_IER_CPAS: RA Compare
  94. tc->TC_CHANNEL[channel].TC_IER = TC_IER_CPAS;
  95. // Enables the timer clock and performs a software reset to start the counting
  96. TC_Start(tc, channel);
  97. }
  98. void initISR(timer16_Sequence_t timer) {
  99. #ifdef _useTimer1
  100. if (timer == _timer1)
  101. _initISR(TC_FOR_TIMER1, CHANNEL_FOR_TIMER1, ID_TC_FOR_TIMER1, IRQn_FOR_TIMER1);
  102. #endif
  103. #ifdef _useTimer2
  104. if (timer == _timer2)
  105. _initISR(TC_FOR_TIMER2, CHANNEL_FOR_TIMER2, ID_TC_FOR_TIMER2, IRQn_FOR_TIMER2);
  106. #endif
  107. #ifdef _useTimer3
  108. if (timer == _timer3)
  109. _initISR(TC_FOR_TIMER3, CHANNEL_FOR_TIMER3, ID_TC_FOR_TIMER3, IRQn_FOR_TIMER3);
  110. #endif
  111. #ifdef _useTimer4
  112. if (timer == _timer4)
  113. _initISR(TC_FOR_TIMER4, CHANNEL_FOR_TIMER4, ID_TC_FOR_TIMER4, IRQn_FOR_TIMER4);
  114. #endif
  115. #ifdef _useTimer5
  116. if (timer == _timer5)
  117. _initISR(TC_FOR_TIMER5, CHANNEL_FOR_TIMER5, ID_TC_FOR_TIMER5, IRQn_FOR_TIMER5);
  118. #endif
  119. }
  120. void finISR(timer16_Sequence_t) {
  121. #ifdef _useTimer1
  122. TC_Stop(TC_FOR_TIMER1, CHANNEL_FOR_TIMER1);
  123. #endif
  124. #ifdef _useTimer2
  125. TC_Stop(TC_FOR_TIMER2, CHANNEL_FOR_TIMER2);
  126. #endif
  127. #ifdef _useTimer3
  128. TC_Stop(TC_FOR_TIMER3, CHANNEL_FOR_TIMER3);
  129. #endif
  130. #ifdef _useTimer4
  131. TC_Stop(TC_FOR_TIMER4, CHANNEL_FOR_TIMER4);
  132. #endif
  133. #ifdef _useTimer5
  134. TC_Stop(TC_FOR_TIMER5, CHANNEL_FOR_TIMER5);
  135. #endif
  136. }
  137. #endif // HAS_SERVOS
  138. #endif // ARDUINO_ARCH_SAM