code8825 / arduino

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

condense register initialization in init(). #367

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
init() in wiring.c currently does a series of sbi() calls to set bits in the 
configuration registers for timers, ADC, and etc.

There are two problems with this:

1) since the registers are all volatile, they get read and re-written for each 
bit, resulting in rather bulky code.
2) The code makes assumptions that the other bits in the register are already 
in the desired state, which need not be true (leading to 
http://code.google.com/p/arduino/issues/detail?id=364 for example.)

The attached code sets the desired state of the registers all 8 bits at once.  
Faster, smaller, and less prone to errors caused by unexpected initial states.

(I checked that the initial state of the registers in question is 0, and This 
compiles for all three platforms, and I checked that Fading.pde works on all 
PWM pins on an Uno, but making this sort of large change makes me nervous...)

Original issue reported on code.google.com by wes...@gmail.com on 3 Oct 2010 at 5:59

Attachments:

GoogleCodeExporter commented 9 years ago
Things like this:
-   sbi(ADCSRA, ADPS2);
-   sbi(ADCSRA, ADPS1);
-   sbi(ADCSRA, ADPS0);

Could be made like this:
ADCSRA |= ((1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0));

The compiler will handle all this and there will only be made one load to the 
ADCSRA register containing all the desired bits set.

Original comment by x86....@gmail.com on 20 Oct 2010 at 1:36

GoogleCodeExporter commented 9 years ago

Original comment by dmel...@gmail.com on 20 Oct 2010 at 1:39