Skip to content

Commit 93242a9

Browse files
committed
[taiko] improve reading adc result
from: arduino/ArduinoCore-avr#345 7.3.0-atmel3.6.1-arduino7 gcc fails to optimize separate reading from ADCL and ADCH. It produces additionally three eor commands or in some cases two mov commands in the assembly code. These commands swap register contents before store them to data area. So they are completely unnecessary. Reading ADC result with ADC macro fixes it and gcc generates the right code..
1 parent 9e25798 commit 93242a9

File tree

1 file changed

+5
-13
lines changed

1 file changed

+5
-13
lines changed

nonblock_read.c

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,29 +37,21 @@ void analogPrepareRead(uint8_t pin)
3737
#endif
3838
#endif
3939

40-
#if defined(ADCSRA) && defined(ADCL)
40+
#if defined(ADCSRA) && defined(ADC)
4141
// start the conversion
4242
sbi(ADCSRA, ADSC);
4343
#endif
4444
}
4545

4646
int analogReadData(void)
4747
{
48-
#if defined(ADCSRA) && defined(ADCL)
48+
#if defined(ADCSRA) && defined(ADC)
4949
// ADSC is cleared when the conversion finishes
5050
while (bit_is_set(ADCSRA, ADSC));
5151

52-
// we have to read ADCL first; doing so locks both ADCL
53-
// and ADCH until ADCH is read. reading ADCL second would
54-
// cause the results of each conversion to be discarded,
55-
// as ADCL and ADCH would be locked when it completed.
56-
uint8_t low, high;
57-
58-
low = ADCL;
59-
high = ADCH;
60-
61-
// combine the two bytes
62-
return (high << 8) | low;
52+
// ADC macro takes care of reading ADC register.
53+
// avr-gcc implements the proper reading order: ADCL is read first.
54+
return ADC;
6355
#else
6456
// we dont have an ADC, return 0
6557
return 0;

0 commit comments

Comments
 (0)