hasselk / digitalwritefast

Automatically exported from code.google.com/p/digitalwritefast
0 stars 0 forks source link

Optimizer removes calls #5

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Code :
[code]
digitalWriteFast(MY_PIN, HIGH);
digitalWriteFast(MY_PIN, LOW);
[/code]

in my case, MY_PIN was set to 28.

2. Build flags
Use -O3 for optimized code

What is the expected output? What do you see instead?
The expected output would a pulse on the PIN. Instead, the code is purely and 
simply removed by the optimizer. This is expected since the 2nd statement 
"cancels" the 1st one, and the optimizer can see that.
In order to fix this, one should add the "volatile" keyword in the 
"digitalWriteFast" macro definition (instead of casting to uint8_t* the cast on 
line 134 should be to "volatile uint8_t*). This keyword tells the optimizer 
that the referenced variable has a "meaning" outside of the program (i.e. it 
can be modified by an external source, or its modifications affect the outside 
world). It is a good practice (if not mandatory) to add this keyword to 
anything that act as a register.
By the way, the macro _MMIO_BYTE in sfr_defs.h:128 uses volatile : the problem 
actually resides in the cast, which removes it.

The modification was successfully tested.

the fix should be added to the following macros :
-pinModeFast
-digitalWriteFast
-noAnalogWrite
-digitalReadFast (not sure : there is no cast here, so the keyword from 
_MMIO_BYTE is not removed)

What version of the product are you using? On what operating system?
avr-gcc (Fedora 4.7.2-1.fc17) 4.7.2

Please provide any additional information below.
Great work on this library!

By the way, I use it for 8 bit parallel transfers. So I added some macros 
(digitalWriteFastPort, portModeFast, digitalReadFastPort) which make those 
8-bit reads in 1 single instruction : this may be a good addition to your 
library. The only constraint on this is that the user must ensure that its 8 
IOs are on the same port, but it allows a rather fast communication.

Frederic.

Original issue reported on code.google.com by frederic...@gmail.com on 28 Dec 2012 at 2:36