Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

Having Trouble Finding Interrupt Resources
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Having Trouble Finding Interrupt Resources

by MatthewMenze on Thu Jan 30, 2020 3:01 pm

Hello,

I have been searching around for examples/guides of setting up a timer based interrupt with the Metro M4 Airlift, I was wondering if you could point me towards some resources?

I did find a SAMD51 Interrupt Library: https://github.com/Dennis-van-Gils/SAMD ... rruptTimer

But I had little luck implementing it on the board, I couldn't seem to get the interrupt to trigger. I was hoping to find something that worked with Arduino's interrupt handlers, as they work pretty slick for setting up simple timer interrupts on other boards, and blocking interrupts on specific code is quick and easy.

Currently I am using periodic checks to millis to run a periodic update function, but it's not a true interrupt so I am running into some limitations.

Any reference material you know about would be hugely appreciated!

Thank you!
Matt

MatthewMenze
 
Posts: 4
Joined: Wed Jan 22, 2020 6:00 pm

Re: Having Trouble Finding Interrupt Resources

by adafruit_support_mike on Fri Jan 31, 2020 5:47 am

I'm afraid we haven't done any work with timer-based interrupts on the SAMD51. Maybe someone from the community will have more information.

adafruit_support_mike
 
Posts: 61422
Joined: Thu Feb 11, 2010 2:51 pm

Re: Having Trouble Finding Interrupt Resources

by westfw on Fri Jan 31, 2020 6:13 am

What kind of time intervals and behavior are you looking for?
"Normally", timer libraries can conflict with the Arduino built-in uses of the timers (ie for PWM.)
I think the RTC module of the SAMD51 chips is currently unused, but it's limited to a 32kHz clock

westfw
 
Posts: 1722
Joined: Fri Apr 27, 2007 1:01 pm
Location: SF Bay area

Re: Having Trouble Finding Interrupt Resources

by User_UMjT7KxnxP8YN8 on Sat Feb 01, 2020 7:04 pm

I wrote code for the SAMD51x that implements real-time clock interrupts. Not exactly what you're looking for but you might glean helpful information from it.

https://github.com/SapientHetero/Real-time-Clock-for-ATSAMD51

User_UMjT7KxnxP8YN8
 
Posts: 154
Joined: Tue Jul 17, 2018 1:28 pm

Re: Having Trouble Finding Interrupt Resources

by stratosfear on Sun Feb 02, 2020 7:55 am

I think what I've got below will work for you. I chose to use a TC peripheral and save the more flexible TCC counters for possible future use. Note that D13 will toggle every interrupt, thus appearing to give you a 0.5Hz clock.

Code: Select all | TOGGLE FULL SIZE
/*
    Adafruit Grand Central M4 Only: 1Hz timer for flight timer
*/

static volatile bool toggle = 0;
#define togglePin 13

//=======================================
void setup()  {

  // Set up the generic clock (GCLK7) to clock timer TC6
  GCLK->GENCTRL[7].reg = GCLK_GENCTRL_DIV(3) |        // Divide the 48MHz clock source by divisor 3: 48MHz/3 = 16MHz
                         GCLK_GENCTRL_IDC |           // Set the duty cycle to 50/50 HIGH/LOW
                         GCLK_GENCTRL_GENEN |         // Enable GCLK7
                         GCLK_GENCTRL_SRC_DFLL;       // Select 48MHz DFLL clock source
                         //GCLK_GENCTRL_SRC_DPLL0;    // Select 100MHz DPLL clock source
                         //GCLK_GENCTRL_SRC_DPLL0;    // Select 120MHz DPLL clock source
  while (GCLK->SYNCBUSY.bit.GENCTRL7);                // Wait for synchronization

  GCLK->PCHCTRL[TC6_GCLK_ID].reg = GCLK_PCHCTRL_CHEN |       // Enable the TC6 peripheral channel
                                   GCLK_PCHCTRL_GEN_GCLK7;   // Connect generic clock 7 to TC6

  TC6->COUNT16.CTRLA.bit.ENABLE = 0;                         // Disable timer TC6
  while (TC6->COUNT16.SYNCBUSY.bit.ENABLE);                  // Wait for synchronization

  TC6->COUNT16.CTRLA.reg = TC_CTRLA_PRESCALER_DIV1024 |       // Set prescaler to 4, 16MHz/1024 = 15.625KHz
                           TC_CTRLA_PRESCSYNC_PRESC |         // Set the reset/reload to trigger on prescaler clock
                           TC_CTRLA_MODE_COUNT16;

  TC6->COUNT16.WAVE.reg |= TC_WAVE_WAVEGEN_MFRQ;             // Use MFRQ so TOP can be set by CC[0] in COUNT16 mode

  TC6->COUNT16.CC[0].reg = 0x3d08;                           // Sets ~1Hz
  while (TC6->COUNT16.SYNCBUSY.bit.CC0);                     // Wait for synchronization

  TC6->COUNT16.INTENSET.bit.MC0 = 1;                         // enable interrupt on COMPARE0

  TC6->COUNT16.CTRLA.bit.ENABLE = 1;                         // Enable timer TC6
  while (TC6->COUNT16.SYNCBUSY.bit.ENABLE);                  // Wait for synchronization

  NVIC_SetPriority(TC6_IRQn, 0);
  NVIC_EnableIRQ(TC6_IRQn);

  pinMode(togglePin, OUTPUT);

}

//=======================================
void loop()  {

}

//=======================================
void TC6_Handler() {
  TC6->COUNT16.INTFLAG.bit.MC0 = 1;                         // IMPORTANT! reset interrupt flag!

  toggle = !toggle;
  digitalWrite(togglePin, toggle);
  //increment flight timer here
}



I also needed a one-shot timer to erase alerts on a TFT screen. In this example a momentary switch simulates the print routine and retriggers the timer.

Code: Select all | TOGGLE FULL SIZE
/*
   Adafruit Grand Central M4 Only: Set-up 2 second timer to erase screen alerts
*/

static volatile bool toggle = 0;
#define togglePin 13

//=======================================
void setup()  {

  pinMode(2, INPUT_PULLUP);
  attachInterrupt(2, swISR, FALLING);

  // Set up the generic clock (GCLK7) to clock timer TC3
  GCLK->GENCTRL[7].reg = GCLK_GENCTRL_DIV(3) |        // Divide the 48MHz clock source by divisor 3: 48MHz/3 = 16MHz
                         GCLK_GENCTRL_IDC |           // Set the duty cycle to 50/50 HIGH/LOW
                         GCLK_GENCTRL_GENEN |         // Enable GCLK7
                         GCLK_GENCTRL_SRC_DFLL;       // Select 48MHz DFLL clock source
                         //GCLK_GENCTRL_SRC_DPLL0;    // Select 100MHz DPLL clock source
                         //GCLK_GENCTRL_SRC_DPLL0;    // Select 120MHz DPLL clock source
  while (GCLK->SYNCBUSY.bit.GENCTRL7);                // Wait for synchronization

  GCLK->PCHCTRL[TC3_GCLK_ID].reg = GCLK_PCHCTRL_CHEN |       // Enable the TC3 peripheral channel
                                   GCLK_PCHCTRL_GEN_GCLK7;   // Connect generic clock 7 to TC3

  TC3->COUNT16.CTRLA.reg = TC_CTRLA_PRESCALER_DIV1024 |      // Set prescaler to 1024, 16MHz/1024 = 15.625KHz
                           TC_CTRLA_PRESCSYNC_PRESC |        // Set the reset/reload to trigger on prescaler clock
                           TC_CTRLA_MODE_COUNT16;

  TC3->COUNT16.WAVE.reg |= TC_WAVE_WAVEGEN_MFRQ;             // Use MFRQ so TOP can be set by CC[0] in COUNT16 mode

  TC3->COUNT16.CC[0].reg = 0x7a12;                           // Sets 2 sec pulse
  while (TC3->COUNT16.SYNCBUSY.bit.CC0);                     // Wait for synchronization

  TC3->COUNT16.INTENSET.bit.MC0 = 1;                         // enable interrupt on COMPARE0

  TC3->COUNT16.CTRLA.bit.ENABLE = 1;                         // Enable timer TC3
  while (TC3->COUNT16.SYNCBUSY.bit.ENABLE);                  // Wait for synchronization

  NVIC_SetPriority(TC3_IRQn, 0);
  NVIC_EnableIRQ(TC3_IRQn);

  pinMode(togglePin, OUTPUT);

}

//=======================================
void loop()  {

}

//=======================================
void TC3_Handler() {
  TC3->COUNT16.INTFLAG.bit.MC0 = 1;                         // IMPORTANT! reset interrupt flag!
  digitalWrite(togglePin, 0);
  //set flag for erase function
}

//=======================================
void swISR()  {
  //trigger one-shot timer here
  TC3->COUNT16.CTRLBSET.reg |= 0b00100000;   //RETRIGGER counter
  digitalWrite(togglePin, 1);
  //print notificaton on TFT.  Erase in interrupt handler.
}



Jon

stratosfear
 
Posts: 10
Joined: Tue Dec 31, 2013 8:21 am

Re: Having Trouble Finding Interrupt Resources

by FlatCat on Thu Jul 16, 2020 4:30 pm

These are great! Thanks stratosfear.

FlatCat
 
Posts: 20
Joined: Wed Aug 13, 2014 11:07 pm

Please be positive and constructive with your questions and comments.