Flipping byte of Port in pins_arduino.h

Hello,

Has anyone tried flipping the binary values of a port via readdressing the pins_arduino.h? That is to say if PORTA = 1000 0010 flip it in firmware to 0100 0001...

The method I intend to take is to change

PA, // PA7 ** D37   
PA, // PA6 ** D38
PA, // PA5 ** D39   
PA, // PA4 ** D40
PA, // PA3 ** D41
PA, // PA2 ** D42
PA, // PA1 ** D43
PA, // PA0 ** D44

To

PA, // PA7 ** D44   
PA, // PA6 ** D43
PA, // PA5 ** D42   
PA, // PA4 ** D41
PA, // PA3 ** D40
PA, // PA2 ** D39
PA, // PA1 ** D38
PA, // PA0 ** D37

And likewise for the NOT_ON TIMER and _BV(). Any idea if this will solve the issue. And I am aware I could bit-wise shift, or use a look up table, but this is for a parallel communication and requires maximum speed.

I'd just like to clarify before editing the file that it is a valid approach, and that it should not require further editing.

Incase the question I’m asking is unclear…

Does anyone know that the above solution would work without adverse effects.

The only thing that code in pins_arduino.h is doing is defining the port of each Arduino pin number (as set by the position in the array).

If you want maximum speed then you should not be using Arduino pin numbers at all. Accessing these arrays in progmem is done at runtime and thus is very slow.

You want to reassign which pin goes on which port? No, you can't do that. It is set by the hardware not by that header file.

In case someone else wants to tackle this problem; I gave it a day or two to think on, and realized I should just use a lookup table... Maximum efficiency of hardware, maybe not; but it's pretty quick and saves some hair pulling.

uint8_t flipByte[16] = {
0x0, 0x8, 0x4, 0xC,
0x2, 0xA, 0x6, 0xE,
0x1, 0x9, 0x5, 0xD,
0x3, 0xB, 0x7, 0xF };

Well, that will flip the low nibble, assuming the high nibble is zero.

To flip the entire byte you would need a table with 256 entries. That’s probably the fastest way to do it if the space is available in SRAM. The table need not be populated in the source file either – you can build it in SRAM on the fly (do in only once in setup()) and avoid all that ugly typing.

uint8_t byteFlipper[256];

setup()
{
  for (uint16_t k=0; k<256; k++)
    {
        byteFlipper[k] = byteFlippingFunction(k); // you get to come up with the function ;-)
    }
}

I presume you don’t have the luxury of simply re-wiring the hardware port pins so they’re in the proper order.

Reading PORTA gives you the bits in a particular position of the result, and you can't change that. The pin_arduino.h file changes which bits have what "pin number name" in what port, but has not effect on the actual hardware or on "direct port reads."

Swapping bits in a byte (or word) is a common "programming challenge." You can find all sorts of clever algorithms that use less space than a lookup table, but are much faster than the obvious loop. ARM CM3 (and higher, but not CM0) has a machine instruction that bitswaps an entire 32bit word. AVR has a nybble-swap that may or may not be useful; keep in mind that the "obvious" assembly language code only takes about 16 instructions/cycles to swap 8bits - a lot of the "clever" algorithms rely on hardware features that the AVR doesn't have (like being able to shift by N bits in a single cycle.)