Go Down

Topic: Help with DUE PIO programming please? (Read 650 times) previous topic - next topic

ghlawrence2000

I wonder if somebody would be so kind as to point me in the direction of some helpful information regarding DUE PIO register programming please?

Let me further explain what it is I am aiming to achieve.

Using the Sam3X8e datasheet I have gained a fair understanding of the PIOx->PIO_CODR, PIOx->PIO_SODR and PIOx->PIO_OER to the extent that I understand why this will flash the LED's on Pin 13 and Pin 72.
Code: [Select]
void setup() {
PIOB->PIO_OER=1<<27;
PIOC->PIO_OER=1<<30;
}

void loop() {
PIOB->PIO_CODR=1<<27;
PIOC->PIO_SODR=1<<30;
delay(1000);
PIOB->PIO_SODR=1<<27;
PIOC->PIO_CODR=1<<30;
delay(1000);
}


But how do I get around calculating manually that Pin 13 is PortB bit 27 and pin 72 is PortC bit 30?

I am aware of the cbi(reg, mask) concept and how to derive the reg and mask parameters, but this does not have the absolute performance of the PIOx->PIO_CODR method.

I think it may have something to do with 'g_APinDescription' but that is as far as I have managed to get.

Can somebody help me with this please?

Thank you.

Regards,

Graham
UTFT_SdRaw now included in library manager!! ;) High speed image drawing from SD card to UTFT displays for Mega & DUE.
UTFT_GHL - a VASTLY upgraded version of UTFT_CTE. Coming soon to a TFT near you! 8) Shipping April 1 2016!

Magician

Download DUE schematic from this web-site, than trace pin number to port mark on a chip

ghlawrence2000

But how do I get around calculating manually that Pin 13 is PortB bit 27 and pin 72 is PortC bit 30?
Yep that was the schematic method I was talking about when I said manually....

Regards,

Graham
UTFT_SdRaw now included in library manager!! ;) High speed image drawing from SD card to UTFT displays for Mega & DUE.
UTFT_GHL - a VASTLY upgraded version of UTFT_CTE. Coming soon to a TFT near you! 8) Shipping April 1 2016!

ghlawrence2000

I figured out how to do it......... it may not be the best way, but it works for my purposes.

Code: [Select]
#define LED_1 13
#define LED_2 72
#define LED_3 73

Pio * P_LED1, * P_LED2, * P_LED3;
uint32_t B_LED1, B_LED2, B_LED3;


uint8_t portbit(uint8_t digitalpin)
{ // Convert mask to bit
  uint32_t zz = g_APinDescription[digitalpin].ulPin;
  int n = 0;
  while (n < 32) {
    if (zz >> n == 1) break;
    n++;
  }
  return n;
}

void setup() {
  P_LED1 = digitalPinToPort(LED_1);
  B_LED1 = portbit(LED_1);
  P_LED2 = digitalPinToPort(LED_2);
  B_LED2 = portbit(LED_2);
  P_LED3 = digitalPinToPort(LED_3);
  B_LED3 = portbit(LED_3);
  P_LED1->PIO_OER = 1 << B_LED1; // 13
  P_LED2->PIO_OER = 1 << B_LED2; // 72
  P_LED3->PIO_OER = 1 << B_LED3; // 73
}

void loop() {
  P_LED1->PIO_CODR = 1 << B_LED1;
  P_LED2->PIO_SODR = 1 << B_LED2;
  P_LED3->PIO_CODR = 1 << B_LED3;
  delay(500);
  P_LED1->PIO_SODR = 1 << B_LED1;
  P_LED2->PIO_CODR = 1 << B_LED2;
  P_LED3->PIO_SODR = 1 << B_LED3;
  delay(500);
}


I put it here in case it will help others.

Regards,

Graham
UTFT_SdRaw now included in library manager!! ;) High speed image drawing from SD card to UTFT displays for Mega & DUE.
UTFT_GHL - a VASTLY upgraded version of UTFT_CTE. Coming soon to a TFT near you! 8) Shipping April 1 2016!

Magician

Here is another reference:
https://www.arduino.cc/en/Hacking/PinMappingSAM3X

So you always can "dissect" digitalWrite function from arduino core,
Code: [Select]
extern void digitalWrite( uint32_t ulPin, uint32_t ulVal )
{
  /* Handle */
if ( g_APinDescription[ulPin].ulPinType == PIO_NOT_A_PIN )
  {
    return ;
  }

  if ( PIO_GetOutputDataStatus( g_APinDescription[ulPin].pPort, g_APinDescription[ulPin].ulPin ) == 0 )
  {
    PIO_PullUp( g_APinDescription[ulPin].pPort, g_APinDescription[ulPin].ulPin, ulVal ) ;
  }
  else
  {
    PIO_SetOutput( g_APinDescription[ulPin].pPort, g_APinDescription[ulPin].ulPin, ulVal, 0, PIO_PULLUP ) ;
  }
}


david_prentice

The mapping tables for individual Arduino boards are stored under variants/

You can also find the port address and bit number from a pin number using the associated macros.
Just like a Uno or Mega.

Using D# or A# is an elegant and convenient way to address pins in any Arduino.
Finding the actual Port and Bit is messier but subsequent execution is faster.

Note that changing pinMode is a bit more complex with ARM than it is on the AVR.

In practice,   it is handy to print out the pin diagrams from the UTFT website.
After all,    you generally want to know which shield pin is going to go where.

David.

Go Up