Last active
January 6, 2020 23:28
-
-
Save lgbeno/01498b829f9f4d23262a43e51fa493b6 to your computer and use it in GitHub Desktop.
SAMD51 TC2 EVSYS Not working
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define PA13 22 | |
#define J5E PA13 | |
#define USE_EVSYS | |
void e_irq() { | |
TC2->COUNT16.CTRLBSET.bit.CMD = 4; | |
while(TC2->COUNT16.SYNCBUSY.bit.COUNT); | |
Serial.println(TC2->COUNT16.COUNT.reg); | |
} | |
void setup() { | |
Serial.begin(115200); | |
while(!Serial); | |
Serial.println("TC2 Test"); | |
MCLK->APBAMASK.reg |= MCLK_APBAMASK_EIC; | |
MCLK->APBBMASK.reg |= MCLK_APBBMASK_TC2 | MCLK_APBBMASK_EVSYS; | |
GCLK->GENCTRL[7].bit.SRC = 3; | |
GCLK->PCHCTRL[TC2_GCLK_ID].bit.GEN = 7; | |
GCLK->PCHCTRL[TC2_GCLK_ID].bit.CHEN = 1; | |
GCLK->GENCTRL[7].bit.GENEN = 1; | |
TC2->COUNT16.CTRLA.bit.SWRST = 1; | |
while(TC2->COUNT16.SYNCBUSY.bit.SWRST); | |
TC2->COUNT16.INTENSET.reg = TC_INTENSET_MC0 | TC_INTENSET_MC1 | TC_INTENSET_OVF; | |
#ifdef USE_EVSYS | |
TC2->COUNT16.EVCTRL.reg = TC_EVCTRL_EVACT_PPW | TC_EVCTRL_TCEI; | |
#endif | |
TC2->COUNT16.CTRLA.reg = TC_CTRLA_MODE_COUNT16 | | |
#ifndef USE_EVSYS | |
TC_CTRLA_COPEN0 | TC_CTRLA_COPEN1 | | |
#endif | |
TC_CTRLA_CAPTEN0 | TC_CTRLA_CAPTEN1 | | |
TC_CTRLA_ENABLE; | |
NVIC_EnableIRQ( TC2_IRQn ) ; | |
// PA13 connectomux to E | |
#ifdef USE_EVSYS | |
PORT->Group[0].PMUX[6].bit.PMUXO = 0; | |
#else | |
PORT->Group[0].PMUX[6].bit.PMUXO = 4; | |
#endif | |
PORT->Group[0].PINCFG[13].bit.PULLEN = 0; | |
PORT->Group[0].PINCFG[13].bit.INEN = 1; | |
PORT->Group[0].PINCFG[13].bit.PMUXEN = 1; | |
#ifdef USE_EVSYS | |
attachInterrupt(J5E,e_irq,RISING); | |
EIC->CTRLA.bit.ENABLE = 0; | |
EIC->INTENCLR.bit.EXTINT = (1<<13); | |
EIC->EVCTRL.bit.EXTINTEO = (1<<13); | |
EIC->CTRLA.bit.ENABLE = 1; | |
EVSYS->USER[EVSYS_ID_USER_TC2_EVU].reg = 1; | |
EVSYS->Channel[0].CHANNEL.reg =(uint32_t) (EVSYS_ID_GEN_EIC_EXTINT_13 | EVSYS_CHANNEL_PATH_ASYNCHRONOUS); | |
#endif | |
} | |
void TC2_Handler() { | |
Serial.print("TC2 IRQ "); | |
Serial.print(TC2->COUNT16.INTFLAG.reg,HEX); | |
TC2->COUNT16.INTFLAG.reg = 0x33; | |
Serial.print(" "); | |
Serial.print(TC2->COUNT16.CC[0].reg); | |
Serial.print(" "); | |
Serial.print(TC2->COUNT16.CC[1].reg); | |
Serial.print(" "); | |
Serial.println(TC2->COUNT16.INTFLAG.reg,HEX); | |
} | |
void loop() { | |
delay(500); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is a working version that I've tested on the Adafruit NeoTrellis, compiling with the Arduino IDE. It uses the Row0/Column0 button (upper left button) as a GPIO to measure the time between button presses. This example is amazing because it all happens in the background using the TC hardware (Timer/Counter) and EVSYS (Event System). In other words the button presses trigger the event system to capture the timer values, all without using any CPU. Meaning your main loop (and hence the CPU) can be completely consumed with other software tasks and this incredibly accurate timer will be happening completely in the background and very accurately (with 21.33 uSec resolution and a max value of 1.4 seconds before it wraps). For other timing values (longer timer with less resolution or shorter timer with higher resolution), tweak the TC prescaler or change to a 32-bit timer (this example uses a 16-bit timer).
Here is example output from the serial terminal while intermittently pressing on the Row0/Column0 NeoTrellis button:
And here is the full source code based on the above post (originally written by lgbeno) with added details referencing the huge 2,129 page Atmel SAMD51 datasheet (reference manual):