Reading digital inputs

Hi,

I'm trying to read several digital input lines on a DUE in a single program line. In other words, I don't want to use "digitalRead()", since it is too slow. It takes ~3 ms each pin, and I'm pretty sure that it can be done much faster by reading the port registers.
I'm not having trouble when I write something directly on the port registers. That's what I'm using to write to PORTC:

PIOC->PIO_PER = 0x01FF; // Configure PORTC to PIO controller, pins 33-40
PIOC->PIO_OER = 0x01FF; // Enable PORTC to output
....
PIOC->PIO_ODSR = something; // Write something on pins 33-40.

This is working, and it takes only 0.15 ms to write 32 bits on PORTC.

However, I thought that something similar could be used to read the same port. So the program to read the PORTC that I'm using is

PIOC->PIO_PER = 0x01FF; // Configure PORTC to PIO controller, pins 33-40
PIOC->PIO_ODR = 0x0FFE; // Disable PORTC to output (enable to input?)
...
something = PIOC->PIO_ODSR; // Read PORTC????

But it didn't work. BTW, in this case pins 33-40 are grounded or attached to 3.3V throught a 1k resistor. The variable "something" returns with zero ("0"), even on the bits corresponding to the 3.3V.

Does someone knows what I'm doing wrong in this program?

Thanks

You mean 3us, not 3ms!!!

Have you looked at the source code to pinMode, digitalWrite and digitalRead? That's
where to start.

MarkT:
You mean 3us, not 3ms!!!

Have you looked at the source code to pinMode, digitalWrite and digitalRead? That's
where to start.

You're right. It was 3us for a single pin. But when writing directly to registers, it took 150ns
for 32 pins.

I'll try to find the source code for digitalRead. My previous experience on this tells me that,
as a newbie, for each answer I got by looking the code, one hundred questions arrise.

Thanks anyway.

I haven't really learned bitmath yet, but this works for me. However I use "regular" pinMode etc. and only use bitmath for reading the port.

comState = REG_PIOD_PDSR & 0xF; //reads the 4 lower bits of port D

Yes, I tried and it worked. I mean, if the input pins are defined by pinMode it realy works.
However, I'm trying to configure the port by direct access on the registers, and I didn't
succeed yet. The reason why I need to configure the registers to input is that my app
needs lots of pins, and I must have to mutiplex them. Sometimes the port act as
output, sometimes the same port serve as input. (In fact, I want to use an external sram module of 1 Mbtyte, IS62WV51216BLL chip). Useless to say that access time is critical,
which explains why I need direct port access either to output (I already made it) as to input (not yet).
I'll keep trying :D. Helps are welcome.

Hi all!!!, I have a similar problem, but I do not how to write directly to registers when the bit is greater than 8, ie, phisical pin 1 (B.26) or phisical pin 32 (D.10)

PIOB->PIO_PER = ??; // Configure PORTB to PIO controller, number pin 22, phisical pin 1 (B.26)

PIOB->PIO_OER = ??;

PIOB->PIO_ODSR = ??;

Any suggestions are welcomed,

Thanks!!!!

Remember ints on the Due have 32 bits. :slight_smile: