Hi all.

While debugging a library on the Teensy4.1 I suspected that one of my IO was changing modes (it wasn't), and went looking for a way to determine the current direction of a given pin. I found what appeared to be a solution on another forum, but it was an AVR specific function defined in the sketch itself. Oddly, the function name was already a keyword in the Arduino IDE, but is not implemented for the Teensy line. So, after about an hour of digging around the guts of the Teensy core folder, I came up with this:

uint8_t getPinMode(uint8_t pin) {
        const struct digital_pin_bitband_and_config_table_struct *p;

        if (pin >= CORE_NUM_DIGITAL) return;
        p = digital_pin_to_info_PGM + pin;
        return ( (*(p->reg + 1) & digitalPinToBitMask(pin)) ? 1 : 0);
It sits adjacent to the `pinMode` function in digital.c. One possible caveat mentioned in the i.MX RT1060 manual is that the value returned is only valid if the IOMUX is set GPIO mode for that pin. No attempt was made to check the IOMUX for pin assignment to a peripheral, which might be necessary if you're trying to determine the direction of say an SPI or PWM pin. Hope someone finds this useful.