Defining "Friendly" Names for More IO Pins

Obviously, the SAM3X8E has IO that aren't broken out on the Due board. I'm looking for some guidance as to how one would go defining names for these other IO (i.e.: PB24) and making them usable in the simplified Arduino pin manipulation functions (digitalWrite).

digitalWrite and the other Arduino shortcut functions have their downsides, but direct port manipulation is a little beyond the scope of my needs. I'm willing to sacrifice the extra clock cycles in order to simplify my code, since I'm mostly just having fun anyway.

I haven't yet found the original Due pin definitions in the IDE files, as I expect that would be a big help. I welcome your input, thanks!

Do this:

  • Tools > Board > Arduino Due (Programming Port)
  • File > Examples > SPI > BarometricPressureSensor
  • Sketch > Show Sketch Folder
  • Move up four folder levels.
  • You will find the Arduino pin definitions for the board in the variants/arduino_due_x folder. You can use the existing pin definitions as a reference for adding new ones.
1 Like

Thanks Pert! What a genius way to easily find that file, 13 folders deep in the filesystem!

I was able to find the pin definitions, sort of. In "variant.cpp":286, the pins are defined thus:

{ PIOA, PIO_PA1A_CANRX0|PIO_PA0A_CANTX0, ID_PIOA, PIO_PERIPH_A, PIO_DEFAULT, (PIN_ATTR_DIGITAL|PIN_ATTR_COMBO), NO_ADC,  NO_ADC, NOT_ON_PWM,  NOT_ON_TIMER }

And in "variant.h":196:

static const uint8_t CANRX = 68;

I have searched the entire folder, and I don't find were PIO_PA1A_CANRX0 (or pin 68) is defined. I was expecting to see ports with their bits (PIOA, 1) somewhere. What am I missing?

finchamp:
Thanks Pert! What a genius way to easily find that file, 13 folders deep in the filesystem!

I'm glad you like it. To make things more complicated, it's often under a hidden folder and the location depends on OS, IDE version, and whether the IDE is in portable mode. With Arduino AVR Boards, there are actually two locations where it might be installed but only one of those is actually in use. So I came up with this as the easiest way that will always find the active hardware package for any board.

finchamp:
I have searched the entire folder, and I don't find were PIO_PA1A_CANRX0 (or pin 68) is defined. I was expecting to see ports with their bits (PIOA, 1) somewhere. What am I missing?

I found it by another trick:

  • Tools > Preferences > Compiler warnings > All > OK
  • Compile this sketch:
#define PIO_PA1A_CANRX0 foobar
void setup() {}
void loop() {}

After compilation finishes, examine the contents of the black console window at the bottom of the Arduino IDE window. You need to scroll up to see it all. There I found this warning:

C:\Users\per\AppData\Local\Temp\arduino_modified_sketch_754366\sketch_aug23a.ino:1:0: warning: "PIO_PA1A_CANRX0" redefined [enabled by default]

 #define PIO_PA1A_CANRX0 foobar

 ^

In file included from C:\Users\per\AppData\Local\Arduino15\packages\arduino\hardware\sam\1.6.11\system/CMSIS/Device/ATMEL/sam3xa/include/sam3x8e.h:513:0,

                 from C:\Users\per\AppData\Local\Arduino15\packages\arduino\hardware\sam\1.6.11\system/CMSIS/Device/ATMEL/sam3xa/include/sam3xa.h:44,

                 from C:\Users\per\AppData\Local\Arduino15\packages\arduino\hardware\sam\1.6.11\system/CMSIS/Device/ATMEL/sam3.h:59,

                 from C:\Users\per\AppData\Local\Arduino15\packages\arduino\hardware\sam\1.6.11\system/CMSIS/Device/ATMEL/sam.h:198,

                 from C:\Users\per\AppData\Local\Arduino15\packages\arduino\hardware\sam\1.6.11\system/libsam/chip.h:25,

                 from C:\Users\per\AppData\Local\Arduino15\packages\arduino\hardware\sam\1.6.11\cores\arduino/Arduino.h:42,

                 from C:\Users\per\AppData\Local\Temp\arduino_build_713066\sketch\sketch_aug23a.ino.cpp:1:

C:\Users\per\AppData\Local\Arduino15\packages\arduino\hardware\sam\1.6.11\system/CMSIS/Device/ATMEL/sam3xa/include/pio/pio_sam3x8e.h:157:0: note: this is the location of the previous definition

 #define PIO_PA1A_CANRX0      (1u << 1)  /**< \brief Can0 signal: CANRX0 */

 ^

From that warning I can tell that on my computer, it's defined at:
C:\Users\per\AppData\Local\Arduino15\packages\arduino\hardware\sam\1.6.11\system/CMSIS/Device/ATMEL/sam3xa/include/pio/pio_sam3x8e.h line 157.
From the all-caps name with underscores I knew it was a macro so I simply redefined the macro to a value that was sure to be different from the original definition (you don't get a warning from an identical redefinition).

1 Like