Go Down

Topic: Arduino Due Port C (Read 326 times) previous topic - next topic

jags15

I am having trouble using Port C directly.  I have read these:

http://forum.arduino.cc/index.php?topic=260731.0

https://www.arduino.cc/en/Hacking/PinMappingSAM3X

and thought I understood what to do, but it is not working. 

I have an 8-bit FIFO attached to the Due and want to read the data in.
These are the pins I am using:

Code: [Select]
const int FIFO_BIT7 = 34;
const int FIFO_BIT6 = 35;
const int FIFO_BIT5 = 36;
const int FIFO_BIT4 = 37;
const int FIFO_BIT3 = 38;
const int FIFO_BIT2 = 39;
const int FIFO_BIT1 = 40;
const int FIFO_BIT0 = 41;


If I do this everything works correctly:
Code: [Select]
            Data[i] = digitalRead(FIFO_BIT6) * 64 + digitalRead(FIFO_BIT5) * 32 + digitalRead(FIFO_BIT4) * 16 + digitalRead(FIFO_BIT3) * 8 + digitalRead(FIFO_BIT2) * 4 + digitalRead(FIFO_BIT1) * 2 + digitalRead(FIFO_BIT0);



If I do this it does not and it is not the shifting of the bits, the data just isn't right.  It's close.

Code: [Select]
Data[i] = (PIOC->PIO_PDSR >> 1) & 0x7F;

Thanks.

ard_newbie


Did you power PIOC in your code ?

jags15

I don't understand.  I have not seen anything about power the PIOC.  Could you point me in the right direction?

ard_newbie


Before using a pin as a GPIO, either for output or input, you want to power the PIO with PMC.

See page 38, Sam3x datasheet:

For example, for PIOC: PMC->PMC_PCER0 |= PMC_PCER0_PID13;   // PIOC Power ON

westfw

#4
Feb 03, 2019, 03:19 am Last Edit: Feb 03, 2019, 03:40 am by westfw
Hmm.  How does the Arduino Core get away with ... not turning on any of the PIOs in the PMC?

Edit: oops - never mind.   PCER0 is a write-only register; if you want to read which clocks are turned on, you need to access PCSR0 instead.

edit2: the arduino code enabled the PIO clocks when you set the pinMode() of some pin in the port to INPUT.  If you haven't done pinMode(33, INPUT) or similar in your code, the PIOC clock won't be enabled.
It turns the clock off again if all the pins are set to outputs ("to save power"; seems like a pretty pointless optimization for a Due Class board...)

jags15

I tried the power on and the result was the same.  I do set the pinMode for the pins to INPUT.  The digitalReads work.  Looking at the data it seems like the bits are scrambled.  If I have all zeros the PORTC read returns a zero.  If I have repeatable waveform (a sine wave) it just looks like noise.

ard_newbie

PIOC pins capture EMI, you want first to add pull down or pull up resistors to the 8 pins you are reading.

An example sketch to read PIOC:

Code: [Select]

void setup() {
  Serial.begin(250000);
  PMC->PMC_PCER0 = PMC_PCER0_PID13; // PIOC power ON
}

void loop() {

  uint32_t var;
  var = PIOC->PIO_PDSR;
  Serial.println(var, BIN);
  delay(1000);

}

Go Up