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