Skip to content

Commit 63ba6aa

Browse files
committed
DM: EIC and VARIANT_MCK fix
1 parent e131567 commit 63ba6aa

File tree

3 files changed

+126
-20
lines changed

3 files changed

+126
-20
lines changed
Binary file not shown.

cores/arduino/WInterrupts.c

Lines changed: 125 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,18 @@ static void __initialize()
2828
{
2929
memset(callbacksInt, 0, sizeof(callbacksInt));
3030

31-
#if defined(__SAMD51__) //TODO: verify the correct interrupts
32-
33-
///EIC MCLK is enabled by default
34-
35-
NVIC_DisableIRQ(EIC_0_IRQn);
36-
NVIC_ClearPendingIRQ(EIC_0_IRQn);
37-
NVIC_SetPriority(EIC_0_IRQn, 0);
38-
NVIC_EnableIRQ(EIC_0_IRQn);
39-
40-
GCLK->PCHCTRL[EIC_GCLK_ID].reg = GCLK_PCHCTRL_GEN_GCLK2_Val | (1 << GCLK_PCHCTRL_CHEN_Pos);
31+
#if defined(__SAMD51__)
32+
///EIC MCLK is enabled by default
33+
for (uint32_t i = 0; i <= 15; i++) // EIC_0_IRQn = 12 ... EIC_15_IRQn = 27
34+
{
35+
uint8_t irqn = EIC_0_IRQn + i;
36+
NVIC_DisableIRQ(irqn);
37+
NVIC_ClearPendingIRQ(irqn);
38+
NVIC_SetPriority(irqn, 0);
39+
NVIC_EnableIRQ(irqn);
40+
}
41+
42+
GCLK->PCHCTRL[EIC_GCLK_ID].reg = GCLK_PCHCTRL_GEN_GCLK2_Val | (1 << GCLK_PCHCTRL_CHEN_Pos);
4143
#else
4244
NVIC_DisableIRQ(EIC_IRQn);
4345
NVIC_ClearPendingIRQ(EIC_IRQn);
@@ -56,8 +58,8 @@ static void __initialize()
5658

5759
// Enable EIC
5860
#if defined(__SAMD51__)
59-
EIC->CTRLA.bit.ENABLE = 1;
60-
while (EIC->SYNCBUSY.bit.ENABLE == 1) { }
61+
EIC->CTRLA.bit.ENABLE = 1;
62+
while (EIC->SYNCBUSY.bit.ENABLE == 1) { }
6163
#else
6264
EIC->CTRL.bit.ENABLE = 1;
6365
while (EIC->STATUS.bit.SYNCBUSY == 1) { }
@@ -79,6 +81,7 @@ void attachInterrupt(uint32_t pin, voidFuncPtr callback, uint32_t mode)
7981
#else
8082
EExt_Interrupts in = digitalPinToInterrupt(pin);
8183
#endif
84+
8285
if (in == NOT_AN_INTERRUPT || in == EXTERNAL_INT_NMI)
8386
return;
8487

@@ -89,7 +92,7 @@ void attachInterrupt(uint32_t pin, voidFuncPtr callback, uint32_t mode)
8992

9093
// Enable wakeup capability on pin in case being used during sleep
9194
#if defined(__SAMD51__)
92-
//TODO: find how to do
95+
//I believe this is done automatically
9396
#else
9497
EIC->WAKEUP.reg |= (1 << in);
9598
#endif
@@ -104,11 +107,17 @@ void attachInterrupt(uint32_t pin, voidFuncPtr callback, uint32_t mode)
104107
if (in > EXTERNAL_INT_7) {
105108
config = 1;
106109
} else {
107-
config = 0;
110+
config = 0;
108111
}
109112

110113
// Configure the interrupt mode
111114
pos = (in - (8 * config)) << 2;
115+
116+
#if defined (__SAMD51__)
117+
EIC->CTRLA.bit.ENABLE = 0;
118+
while (EIC->SYNCBUSY.bit.ENABLE == 1) { }
119+
#endif
120+
112121
switch (mode)
113122
{
114123
case LOW:
@@ -131,12 +140,12 @@ void attachInterrupt(uint32_t pin, voidFuncPtr callback, uint32_t mode)
131140
EIC->CONFIG[config].reg |= EIC_CONFIG_SENSE0_RISE_Val << pos;
132141
break;
133142
}
134-
135-
#if defined(__SAMD51__)
136-
//TODO: find how to do
137-
#else
138143
// Enable the interrupt
139144
EIC->INTENSET.reg = EIC_INTENSET_EXTINT(1 << in);
145+
146+
#if defined (__SAMD51__)
147+
EIC->CTRLA.bit.ENABLE = 1;
148+
while (EIC->SYNCBUSY.bit.ENABLE == 1) { }
140149
#endif
141150
}
142151

@@ -157,7 +166,7 @@ void detachInterrupt(uint32_t pin)
157166

158167
// Disable wakeup capability on pin during sleep
159168
#if defined(__SAMD51__)
160-
//TODO: find how to do
169+
//I believe this is done automatically
161170
#else
162171
EIC->WAKEUP.reg &= ~(1 << in);
163172
#endif
@@ -166,6 +175,102 @@ void detachInterrupt(uint32_t pin)
166175
/*
167176
* External Interrupt Controller NVIC Interrupt Handler
168177
*/
178+
#if defined(__SAMD51__)
179+
void InterruptHandler(uint32_t i)
180+
{
181+
if ((EIC->INTFLAG.reg & (1 << i)) != 0)
182+
{
183+
// Call the callback function if assigned
184+
if (callbacksInt[i]) {
185+
callbacksInt[i]();
186+
}
187+
188+
// Clear the interrupt
189+
EIC->INTFLAG.reg = 1 << i;
190+
}
191+
}
192+
193+
void EIC_0_Handler(void)
194+
{
195+
InterruptHandler(EXTERNAL_INT_0);
196+
}
197+
198+
void EIC_1_Handler(void)
199+
{
200+
InterruptHandler(EXTERNAL_INT_1);
201+
}
202+
203+
void EIC_2_Handler(void)
204+
{
205+
InterruptHandler(EXTERNAL_INT_2);
206+
}
207+
208+
void EIC_3_Handler(void)
209+
{
210+
InterruptHandler(EXTERNAL_INT_3);
211+
}
212+
213+
void EIC_4_Handler(void)
214+
{
215+
InterruptHandler(EXTERNAL_INT_4);
216+
}
217+
218+
void EIC_5_Handler(void)
219+
{
220+
InterruptHandler(EXTERNAL_INT_5);
221+
}
222+
223+
void EIC_6_Handler(void)
224+
{
225+
InterruptHandler(EXTERNAL_INT_6);
226+
}
227+
228+
void EIC_7_Handler(void)
229+
{
230+
InterruptHandler(EXTERNAL_INT_7);
231+
}
232+
233+
void EIC_8_Handler(void)
234+
{
235+
InterruptHandler(EXTERNAL_INT_8);
236+
}
237+
238+
void EIC_9_Handler(void)
239+
{
240+
InterruptHandler(EXTERNAL_INT_9);
241+
}
242+
243+
void EIC_10_Handler(void)
244+
{
245+
InterruptHandler(EXTERNAL_INT_10);
246+
}
247+
248+
void EIC_11_Handler(void)
249+
{
250+
InterruptHandler(EXTERNAL_INT_11);
251+
}
252+
253+
void EIC_12_Handler(void)
254+
{
255+
InterruptHandler(EXTERNAL_INT_12);
256+
}
257+
258+
void EIC_13_Handler(void)
259+
{
260+
InterruptHandler(EXTERNAL_INT_13);
261+
}
262+
263+
void EIC_14_Handler(void)
264+
{
265+
InterruptHandler(EXTERNAL_INT_14);
266+
}
267+
268+
void EIC_15_Handler(void)
269+
{
270+
InterruptHandler(EXTERNAL_INT_15);
271+
}
272+
#else
273+
169274
void EIC_Handler(void)
170275
{
171276
// Test the 16 normal interrupts
@@ -183,3 +288,4 @@ void EIC_Handler(void)
183288
}
184289
}
185290
}
291+
#endif

variants/metro_m4/variant.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#define VARIANT_MAINOSC (32768ul)
3131

3232
/** Master clock frequency */
33-
#define VARIANT_MCK (48000000ul)
33+
#define VARIANT_MCK (120000000ul)
3434

3535
/*----------------------------------------------------------------------------
3636
* Headers

0 commit comments

Comments
 (0)