Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 4 of 4

Thread: Teensy 4 Interval Timer

  1. #1
    Junior Member
    Join Date
    Oct 2019
    Location
    Germany
    Posts
    3

    Teensy 4 Interval Timer

    Hello,

    I am trying to use the IntervalTimer on a Teensy 4 to schedule two tasks periodically with different priority. It seems like even though I set two priorities for my two instances of the IntervalTimer the faster (and higher priority) one is not able to interrupt the other function. It seems like there is just a single priority for PIT_IRQ, even though the PIT has four channels so I guess my plan will not work.
    What are possible alternatives to solve the problem of two periodic tasks (40us and 1ms) where the faster one can interrupt the other on the Teensy 4?

    Thanks in advance for your help!

  2. #2
    Senior Member
    Join Date
    Jul 2014
    Posts
    2,398
    priority levels?
    There are in steps of 16 (at least for T3.6, not checked yet for T4)
    e.g levels 16 to 31 are the same level

  3. #3
    Junior Member
    Join Date
    Oct 2019
    Location
    Germany
    Posts
    3
    I have tried to set the priorities to 0 and 128

  4. #4
    Junior Member
    Join Date
    Oct 2019
    Location
    Germany
    Posts
    3
    Looking further into the code of the IntervalTimer library and the documentation of the processor, I found out that all four channels of the IntervalTimer trigger the same interrupt and therefore must have the same priority.
    I solved my problem having two periodic tasks using one of the General Purpose Timers that are available on the Teensy 4. I looked into the reference manual of the processor and some code, that I found on this forum to produce the following:

    Code:
    void setup()
    {
        Serial.begin(1);
        uint32_t us = 1000;
    
        CCM_CCGR1 |= CCM_CCGR1_GPT(CCM_CCGR_ON); //enable clock for General Purpose Timer 1
        GPT1_CR = 0;
        GPT1_PR = 0;
        GPT1_SR = 0x3F;                         // clear all prior status
        GPT1_IR = GPT_IR_OF1IE;                 // enable GPT1 COMPARE 1 Interrupt
        GPT1_CR = GPT_CR_EN | GPT_CR_CLKSRC(1); // sets timer clock to 24 MHz (Teensy 4)
        GPT1_OCR1 = 24000000 / 1000000 * us;    // set compare value
    
        attachInterruptVector(IRQ_GPT1, timer_test);
        NVIC_SET_PRIORITY(IRQ_GPT1, 0);
        NVIC_ENABLE_IRQ(IRQ_GPT1);
    }
    
    void timer_test()
    {
        GPT1_SR = GPT_SR_OF1; //clear the compare flag, the timer resets automatically
        Serial.println(micros());
    }
    
    void loop()
    {
    }
    This way I have a different interrupt (GPT1_IRQ), that I can assign a different priority to. This allows the IntervalTimer to interrupt or be interrupted by the other task.

    Is this the way you would do it, or has the General Purpose Timer (GPT) some drawbacks in comparison to the Periodic Interrupt Timer (PIT) that is used in the IntervalTimer library? Should I use the PIT for the faster Task where timing needs to be more accurate or is there no difference?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •