Fast bitbanding gpio/sram access

Post your cool example code here.
arpruss
Posts: 156
Joined: Sat Sep 30, 2017 3:34 am

Re: Fast bitbanding gpio/sram access

Post by arpruss » Sun Nov 26, 2017 8:40 pm

stevestrong wrote:
Sun Nov 26, 2017 4:13 pm
You have to add to your benchmark the instructions which load the constant values into registers r1, r2, r3.
Good point. If I access more pins in a function, the compiler eventually runs out of registers and does things like:

Code: Select all

aa=DIGITAL_READ(PA4);
 80028aa:	491e      	ldr	r1, [pc, #120]	; (8002924 <_Z4loopv+0x700>)
 80028ac:	6809      	ldr	r1, [r1, #0]
 80028ae:	6019      	str	r1, [r3, #0]
That's two extra bytes, and probably two extra CPU cycles. But this will similarly affect other methods of accessing ports, since they all will need to load the port address from memory once the compiler runs out of registers. So I think one will still be ahead.

arpruss
Posts: 156
Joined: Sat Sep 30, 2017 3:34 am

Re: Fast bitbanding gpio/sram access

Post by arpruss » Sun Nov 26, 2017 9:21 pm

I added a fallback to digitalRead() and digitalWrite() when the argument is not a constant of the format PXxx. This lets one just use DIGITAL_READ() and DIGITAL_WRITE() as pretty much drop-in replacements for digitalRead() and digitalWrite(), saving space and time when the argument is a constant, and being no worse (assuming gcc optimization does its job) otherwise.

arpruss
Posts: 156
Joined: Sat Sep 30, 2017 3:34 am

Re: Fast bitbanding gpio/sram access

Post by arpruss » Sun Nov 26, 2017 9:23 pm

Pito wrote:
Sun Nov 26, 2017 4:16 pm
..trades space for speed. If you want to trade speed for space,..
How could you trade speed for size and vice versa while manipulating the pins?
The size means clocks here..
As far as I can tell, writing to (and probably reading from, but I haven't tested this) the bitbanded memory area takes several more clock cycles than writing to ordinary memory. So you can have fewer instructions in the code, but with one of the instructions taking rather longer.

Post Reply