using parallel ports with teensy 4.0

Status
Not open for further replies.

ric

Member
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 ?
 
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
 
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
 
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.
 
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
 
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?
 
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?

To answer my own question I found this while browsing the data sheet:
Code:
[B]NOTE[/B]
While the GPIO direction is set to input (GPIO_GDIR = 0), a 
read access to GPIO_DR does not return GPIO_DR data. 
Instead, it returns the GPIO_PSR data, which is the 
corresponding input signal value.
 
Status
Not open for further replies.
Back
Top