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

Thread: using parallel ports with teensy 4.0

  1. #1
    Junior Member
    Join Date
    Aug 2020
    Posts
    5

    using parallel ports with teensy 4.0

    we want to move our projects from arduino to teensy 4.0. In arduino we usually write and read ports (for example PORTB or PORTD) because we need to output or input simultaneously 8 bits in a single instruction. In our applications we cannot manage these parallel 8 bits by 8 different instructions on single bits. Is it possible with teensy 4.0 ? Or do we have to remain with arduino ?

  2. #2
    Senior Member
    Join Date
    May 2015
    Location
    USA
    Posts
    644
    With T4.0, you can read more than 8 bits with a single instruction.

  3. #3
    Junior Member
    Join Date
    Aug 2020
    Posts
    5
    please, can you give me any example, because I am at the very beginning with T4.0

  4. #4
    Member
    Join Date
    Feb 2020
    Location
    Dublin, Ireland
    Posts
    42
    Hi,

    The list below has the Teensy 4.0 pins and the corresponding registers and bit numbers. The registers are 32-bit. A read from GPIO6_DR will get all 32 bits but only 12 of them correspond to pins on the 4.0 (and no 8 of them are contiguous). You'll need to do some bitwise shifting.

    The Teensy 4.1 has 16 pins that correspond to 16 contiguous bits in GPIO6 - that may be more convenient depending on your needs,
    All the best,
    Alan

    Pin Port Bit
    1 GPIO6_DR 2
    0 GPIO6_DR 3
    19 GPIO6_DR 16
    18 GPIO6_DR 17
    14 GPIO6_DR 18
    15 GPIO6_DR 19
    17 GPIO6_DR 22
    16 GPIO6_DR 23
    22 GPIO6_DR 24
    23 GPIO6_DR 25
    20 GPIO6_DR 26
    10 GPIO7_DR 0
    12 GPIO7_DR 1
    11 GPIO7_DR 2
    13 GPIO7_DR 3
    6 GPIO7_DR 10
    9 GPIO7_DR 11
    8 GPIO7_DR 16
    7 GPIO7_DR 17
    2 GPIO9_DR 4
    3 GPIO9_DR 5
    4 GPIO9_DR 6
    5 GPIO9_DR 8

  5. #5
    Junior Member
    Join Date
    Aug 2020
    Posts
    5
    Thank you very much Alan,
    can I read from GPIO6_DR in the arduino IDE with Teensyduino ?
    Or do I have to use another IDE ?
    Best regards
    Riccardo

  6. #6
    Member
    Join Date
    Feb 2020
    Location
    Dublin, Ireland
    Posts
    42
    Hi Ricardo,
    Yes. For example
    int x = GPIO6_DR;
    All the best,
    Alan

  7. #7
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    22,705
    Yes, you can read GPIO6_DR, but you probably want to read GPIO6_PSR instead. PSR stands for "Pin Status Register", which reads the actual signals at the pins.

    This register is documented on page 965 in the reference manual.

    https://www.pjrc.com/teensy/datasheets.html

    Normally you would only write to GPIO6_DR. Reading GPIO6_DR can be used to confirm the output values you previously wrote. But if the pin is configured as input, the corresponding bit in GPIO6_DR has no effect.

    If you're used to AVR programming, the pin mux & config might also be a new concept. On AVR, turning on a peripheral takes over control of its pins. But some AVR peripherals like SPI leave the pins sort-of configurable by the GPIO registers. What happens if you turn on 2 peripherals wanting the same pin isn't well defined.

    These new chips have a mux that selects which peripheral has control over the pin. GPIO isn't special, it's just another peripheral. Likewise, each pin as a config register which sets several options, like the output speed and drive strength. This stuff is all documented in the manual and it's been discussed many times on this forum. But if you only need to set the pins up at startup, the simplest way is to call pinMode(), which automatically takes care of all these details.

  8. #8
    Junior Member
    Join Date
    Aug 2020
    Posts
    5
    Thank you Paul, I am not used with AVR.
    If I have well understood your post, if I want to read 8 input pins simultaneously, you suggest me to set them as input by 8 calls to pinMode(), then I can get the status of these 8 input pins simultaneously by reading GPIO6_PSR with appropriate masking.
    Is it correct ?
    Best regards
    Riccardo

  9. #9
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    22,705
    Yes, exactly.

    Of course you must choose 8 pin which are accessed by GPIO6.

  10. #10
    Senior Member vjmuzik's Avatar
    Join Date
    Apr 2017
    Posts
    641
    Quote Originally Posted by PaulStoffregen View Post
    Yes, you can read GPIO6_DR, but you probably want to read GPIO6_PSR instead. PSR stands for "Pin Status Register", which reads the actual signals at the pins.

    This register is documented on page 965 in the reference manual.

    https://www.pjrc.com/teensy/datasheets.html

    Normally you would only write to GPIO6_DR. Reading GPIO6_DR can be used to confirm the output values you previously wrote. But if the pin is configured as input, the corresponding bit in GPIO6_DR has no effect.

    If you're used to AVR programming, the pin mux & config might also be a new concept. On AVR, turning on a peripheral takes over control of its pins. But some AVR peripherals like SPI leave the pins sort-of configurable by the GPIO registers. What happens if you turn on 2 peripherals wanting the same pin isn't well defined.

    These new chips have a mux that selects which peripheral has control over the pin. GPIO isn't special, it's just another peripheral. Likewise, each pin as a config register which sets several options, like the output speed and drive strength. This stuff is all documented in the manual and it's been discussed many times on this forum. But if you only need to set the pins up at startup, the simplest way is to call pinMode(), which automatically takes care of all these details.
    So if I have the pins configured as input and I'm trying to read them is there any downside to reading GPIO6_DR as opposed to GPIO6_PSR? I've been doing it this way for a while now with seemingly no issues, does GPIO6_PSR get the pin state faster than GPIO6_DR does?

  11. #11
    Junior Member
    Join Date
    Aug 2020
    Posts
    5
    Where can I find the correspondence between the bits of GPIO6 and the pins of Teensy 4.1?

  12. #12
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    12,388
    See the PDF linked in this post :: DOOM-running-on-the-Teensy-4-1-with-VGA-output


    NOTE: GPIO marked as #1 become #6 after startup code switched GPIO to FAST mode on 1062

Posting Permissions

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