Read multiple input pins simultaneously

mkoch

Active member
Is it possible to read multiple digital input pins on Teensy 4.0 simultaneously, for example an 8-bit port? If yes, which pins belong together to a port?

Michael
 
Yes you can read in multiple pins at the same time. Typically after startup all of the pins will be on GPIO ports 6-9

So you could read in all of the pins on port 6 using either:
uint32_t port6_val = GPIO6_PSR;
or IMXRT_GPIO6.PSR;

There are several ways to find out which pins or on which port, I often look that the spreadsheet I setup during the beta tests of the board. Example page in GPIO pin order.
Screenshot.png

Note: the GPIO column which this page is sorted on. where pin 1 is shown as GPIO 1.02. GPIO1 maps to GPIO6 (2->7, 3->8, 4->9) The lower numbers are when the pins are in the standard mode, and the higher numbers are when they are in high-speed mode.... We switch all of the pins to the higher numbers during sketch startup (startup.c)
And the .02 is this is bit 2 of the 32 bit register.
 
Here is a sort of quick and dirty sketch to print out mapping...
Code:
uint32_t *port_regsp[] = {(uint32_t*)&IMXRT_GPIO6, (uint32_t*)&IMXRT_GPIO7, (uint32_t*)&IMXRT_GPIO8, (uint32_t*)&IMXRT_GPIO9};
uint8_t pin_numbers[4][32];

void setup() {
  memset(pin_numbers, 0xff, sizeof(pin_numbers));
  // put your setup code here, to run once:
  for (uint8_t pin = 0; pin < CORE_NUM_TOTAL_PINS; pin++) {
    uint32_t *port = digital_pin_to_info_PGM[pin].reg;
    uint8_t port_pin = __builtin_ctz(digital_pin_to_info_PGM[pin].mask);
    for (uint8_t i = 0; i < 4; i++) {
      if (port == port_regsp[i]) {
        pin_numbers[i][port_pin] = pin;
        break;
      }
    }
  }

  while (!Serial) ; // wait for serial.

  Serial.println("\n       31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00");
  for (int i = 0; i < 4; i++) {
    Serial.printf("GPIO%d:", i + 6);
    for (int j = 31; j >= 0; j--) {
      if (pin_numbers[i][j] != 0xff) Serial.printf(" %02u", pin_numbers[i][j]);
      else Serial.print(" --");
    }
    Serial.println();
  }


}

void loop() {
  // put your main code here, to run repeatedly:

}
Sorry could probably be cleaner, but here is output run on Micromod:
Code:
       31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
GPIO6: 27 26 -- -- 21 20 23 22 16 17 -- -- 15 14 18 19 -- -- 25 24 -- -- -- -- -- -- -- -- 00 01 -- --
GPIO7: -- -- -- -- -- -- -- -- -- -- -- -- -- -- 07 08 -- -- -- 32 09 06 45 44 43 42 41 40 13 11 12 10
GPIO8: -- -- -- -- -- -- -- -- 30 31 -- -- -- 28 39 38 34 35 36 37 -- -- -- -- -- -- -- -- -- -- -- --
GPIO9: 29 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 05 33 04 03 02 -- -- -- --
 
Back
Top