Access 16 bits of PORTD?

Status
Not open for further replies.

bsumlin

Member
I'm building a project based around the Teensy 3.6 and I'd like to be able to access 16 bits of PTD[0..15]. I realize PTD10 isn't available on the Teensy, but all the others are. Here's my test code, it's very simple:

Code:
void setup() {
  DDRD = 0;
  Serial.begin(9600);
}

void loop() {
  int16_t x = GPIOD_PDIR;
  Serial.println(x, BIN);
  delay(1000);
}

I expect that this will set all of PORTD to input, and then once per second produce the binary output of the pin state. I have wires soldered to Teensy pins 47, 48, 51, 52, 53, 54, and 55, so I can (or so I thought) read individual bits of PORTD, which I change by moving jumper wires to 3V3 or GND.

However, I can still only read the values of PTD[0..7]. 8, 9, and 11-15 do nothing. Is there a way to read all 16 bits with one GPIOD_PDIR read? I'm digging through the datasheet now and trying various things, and I'll update this thread if I answer it myself, but for now, I'd really appreciate some help on this.
 
First use pinMode to put all the pins into INPUT mode.

DDRD is an avr emulation which doesn't actually access the native PTD pins. Don't use it.

If you *really* want to use native registers, you'd need to write to all 15 config registers, to assign the pins to GPIO mode (by default they're low-power disabled, not GPIO by default as on avr), then write to the GPIO register to put them into input mode. For something you only need to do once at setup, a loop with pinMode is simpler. The pinMode function does all that setup stuff.
 
First use pinMode to put all the pins into INPUT mode.

DDRD is an avr emulation which doesn't actually access the native PTD pins. Don't use it.

If you *really* want to use native registers, you'd need to write to all 15 config registers, to assign the pins to GPIO mode (by default they're low-power disabled, not GPIO by default as on avr), then write to the GPIO register to put them into input mode. For something you only need to do once at setup, a loop with pinMode is simpler. The pinMode function does all that setup stuff.

Thanks for the reply! What about for PTD10, which isn't available on the Teensy? I'd like to eventually put the MK66 chip on my own PCB.
 
Well then, route that pin on your board.

To configure it, you'll need to edit the tables pinMode uses, or access the config & gpio registers directly.
 
Well then, route that pin on your board.

To configure it, you'll need to edit the tables pinMode uses, or access the config & gpio registers directly.

Thanks again for the assistance. I've set all the available pins of port D as inputs via pinMode() and I'm able to read them all with a single command.

Where would I find the pin tables? I assume I'd have to map it to an int that's unused, like 99 or something.

EDIT: I edited core_pins.h to add a new pin, pin 99. Under
Code:
elif defined(__MK64FX512__) || defined(__MK66FX1M0__)
I added:

Code:
#define CORE_PIN99_BIT		10 // added
#define CORE_PIN99_BITMASK	(1<<(CORE_PIN99_BIT))
#define CORE_PIN99_PORTREG	GPIOD_PDOR
#define CORE_PIN99_PORTSET	GPIOD_PSOR
#define CORE_PIN99_PORTCLEAR	GPIOD_PCOR
#define CORE_PIN99_DDRREG	GPIOD_PDDR
#define CORE_PIN99_PINREG	GPIOD_PDIR
#define CORE_PIN99_CONFIG	PORTD_PCR10
#define CORE_INT99_PIN		99

In the digitalWrite(etc etc) region, I added what I believe are appropriate language to include my new pin 99. Is that on the right track? Thanks for the help and please excuse the novice-ness, it's been FOREVER since I've had to do any programming like this.
 
Last edited:
Status
Not open for further replies.
Back
Top