analog-avr.c: take the opportunity to rewrite analog_read() here.
The former implementation easily led to memory corruption, because no range check happened. Costs 22 bytes binary size, unfortunately.
This commit is contained in:
parent
7805e741cd
commit
1a2973a2de
29
analog-avr.c
29
analog-avr.c
|
|
@ -82,23 +82,28 @@ ISR(ADC_vect, ISR_NOBLOCK) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*! Read analog value from saved result array
|
/** Read analog value from saved result array.
|
||||||
\param channel Channel to be read
|
|
||||||
\return analog reading, 10-bit right aligned
|
\param channel Channel to be read. Channel numbering starts at zero.
|
||||||
|
|
||||||
|
\return Analog reading, 10-bit right aligned.
|
||||||
*/
|
*/
|
||||||
uint16_t analog_read(uint8_t index) {
|
uint16_t analog_read(uint8_t index) {
|
||||||
if (analog_mask > 0) {
|
uint16_t result = 0;
|
||||||
uint16_t r;
|
|
||||||
|
#ifdef AIO8_PIN
|
||||||
|
if (index < sizeof(adc_channel) && adc_channel[index] < 16) {
|
||||||
|
#else
|
||||||
|
if (index < sizeof(adc_channel) && adc_channel[index] < 8) {
|
||||||
|
#endif
|
||||||
|
|
||||||
ATOMIC_START
|
ATOMIC_START
|
||||||
// atomic 16-bit copy
|
// Atomic 16-bit copy.
|
||||||
r = adc_result[index];
|
result = adc_result[index];
|
||||||
ATOMIC_END
|
ATOMIC_END
|
||||||
|
}
|
||||||
|
|
||||||
return r;
|
return result;
|
||||||
} else {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* defined TEACUP_C_INCLUDE && defined __AVR__ */
|
#endif /* defined TEACUP_C_INCLUDE && defined __AVR__ */
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue