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

Thread: Teensy 4 GPIO Interrupt Handler

  1. #1
    Junior Member
    Join Date
    Sep 2020

    Teensy 4 GPIO Interrupt Handler


    I'm having trouble finding information about how to handle (external) GPIO Interrupts on the Teensy 4.0 running on Windows.
    I essentially want to use direct registers as much as possible (so without using attachInterrupt etc).

    I've already figured out from the reference manual how to configure these GPIO Interrupts, of which the summary is given below (please correct me if I'm wrong!)
    Choose a digital pin on the Teensy 4 -> Lookup the corresponding NXP iMXRT1060 GPIO (or use the given macros) -> Use the faster GPIO6-9 pins (p375) -> Configure ICR1 and ICR2 registers (p966) ->
    Find the IRQ number (p47) -> Enable and set the interrupt priority using the NVIC functions

    If what is written above is correct, I should be able to correctly configure the interrupt registers for a pin on the Teensy 4.0. However, I'm struggling with what comes after that. How do I actually "bind" a function
    to that interrupt when it occurs?

    I have looked through the core Teensy code, but could not find any predetermined function names (e.g. GPIO9_IRQ_HANDLER() ), so my guess is that there are no predetermined function names. I tried looking in the
    interrupt.c file to see how the attachInterrupt function does it, and could see that it uses a function pointer table to store the ISR for specific pins. However again, I do not understand how the function is actually bound to a digital pin.

    If someone could point me in the right direction/explain how it could be done, it would be greatly appreciated!

  2. #2
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    The simplest thing to do is to use the attachInterrupt()... code.

    But if you don't want to use it, look at it... (interrupt.c)

    You will see that for T4.x we don't have fixed names for interrupts.

    To setup to use a specific interrupt handler for a specific IRQ use the attachInterruptVector(irq, function) API.
    Which will put your handler into the RAM version of the interrupt vector table.

    You will also need to look at things like NVIC_ENABLE_IRQ(irq_number);
    To enable the IRQ...

    As for GPIO. When you have pins configured for High speed GPIO (i.e. uses GPIO6-9), there is one ISR for all of them. (IRQ_GPIO6789)
    The list of IRQ numbers is contained in the header file imxrt.h

    If however IO pins are configured to be in SLOW mode... Than there are several GPIO IRQs.
            IRQ_GPIO1_0_15 =        80,
            IRQ_GPIO1_16_31 =       81,
            IRQ_GPIO2_0_15 =        82,
            IRQ_GPIO2_16_31 =       83,
            IRQ_GPIO3_0_15 =        84,
            IRQ_GPIO3_16_31 =       85,
            IRQ_GPIO4_0_15 =        86,
            IRQ_GPIO4_16_31 =       87,
            IRQ_GPIO5_0_15 =        88,
            IRQ_GPIO5_16_31 =       89,
    These like the other IRQ handle IRQs for multiple pins. (like 16 each). You again need to configure the underlying registers on which pins should generate the Interrupt and you need to look at the status to figure out which one triggered.

    I played around some with these at one point and I believe I was able to get some of these to work. Not sure it saved me anything.

    There are a few one pin specific ones as well:

            IRQ_GPIO1_INT0 =        72,
            IRQ_GPIO1_INT1 =        73,
            IRQ_GPIO1_INT2 =        74,
            IRQ_GPIO1_INT3 =        75,
            IRQ_GPIO1_INT4 =        76,
            IRQ_GPIO1_INT5 =        77,
            IRQ_GPIO1_INT6 =        78,
            IRQ_GPIO1_INT7 =        79,
    I played a little with these before, did not have much luck. Again they need to be in slow mode:
    And I would have to double check if these were only for GPIO1.1-1.7 of which we maybe only have D0 and D1 in this range...

Posting Permissions

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