I am trying to adapt [@MartinL] (Frequency measurement on Seeeduino Xiao at 200kHz) to run on D1 (PA04 - preferred pin) or D2 pin (PA10) as all the other pins are occupied unfortunately.
The original code works great with no issued in D6 (PB08) as expected but when I try to adapt for example the port and EIC definition to D2 (PA10 - EIC : EXTINT[10]) for the TCC0 intitialisation based on the SAM D21 datasheet pg. 29, and the post using PA10
(attachInterrupt(12, NULL, HIGH) for external interrupt on samd21g M0 - #2 by MartinL) cant get it to count for the life of me and cant seem to figure out why, although I'm no expert on timers unfortunately.
Here is the adapted code for initialising TCC0 for PA10 (D2). All the previous sections of the code are unchanged, ie. "Generic Clock Initialisation" , "TCC2 Initialisation - reference timer" and the startConversion, TC2_Handler functions).
Any help for adapting it either of PA04 (preferred) or PA10 would be much appreciated!!
////////////////////////////////////////////////////////////////////////////////////////
// TCC0 Initialisation adapted for PA10 (D2 Xiao Seed) - measurement counter: counts the number of incoming of pulses
////////////////////////////////////////////////////////////////////////////////////////
PORT->Group[PORTA].PINCFG[10].bit.PMUXEN = 1; // Enable the port multiplexer on port pin PA10 (D2)
PORT->Group[PORTA].PMUX[10 >> 1].reg |= PORT_PMUX_PMUXE_A; // Set-up PA10 (D2) as an EIC (interrupt)
EIC->EVCTRL.reg |= EIC_EVCTRL_EXTINTEO10; // Enable event output on external interrupt 10
EIC->CONFIG[1].reg |= EIC_CONFIG_SENSE2_HIGH; // Set interrupt to detect a HIGH level
EIC->INTENCLR.reg = EIC_INTENCLR_EXTINT10; // Clear the interrupt flag on channel 10
EIC->CTRL.bit.ENABLE = 1; // Enable EIC peripheral
while (EIC->STATUS.bit.SYNCBUSY); // Wait for synchronization
EVSYS->USER.reg = EVSYS_USER_CHANNEL(1) | // Attach the event user (receiver) to channel 0 (n + 1)
EVSYS_USER_USER(EVSYS_ID_USER_TCC0_EV_0); // Set the event user (receiver) as timer TCC0, event 0
EVSYS->CHANNEL.reg = EVSYS_CHANNEL_EDGSEL_NO_EVT_OUTPUT | // No event edge detection
EVSYS_CHANNEL_PATH_ASYNCHRONOUS | // Set event path as asynchronous
EVSYS_CHANNEL_EVGEN(EVSYS_ID_GEN_EIC_EXTINT_10) | // Set event generator (sender) as external interrupt 10
EVSYS_CHANNEL_CHANNEL(0); // Attach the generator (sender) to channel 0
TCC0->EVCTRL.reg = TCC_EVCTRL_TCEI0 | // Enable TCC0 event 0 inputs
TCC_EVCTRL_EVACT0_INC; // Increment the TCC0 counter on receiving an event 0
TCC0->CTRLA.bit.ENABLE = 1; // Enable TCC0
while (TCC0->SYNCBUSY.bit.ENABLE); // Wait for synchronization