From 7805e741cd54192dde777875600e8c011840c221 Mon Sep 17 00:00:00 2001 From: Markus Hitter Date: Thu, 6 Aug 2015 20:01:14 +0200 Subject: [PATCH] analog-arm.c: read not by ADC channel number, but by Teacup number. Analog reading should be complete by now :-) --- analog-arm.c | 10 ++++++++-- analog-avr.c | 16 ++++------------ analog.c | 11 +++++++++++ 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/analog-arm.c b/analog-arm.c index 8f3ae0e..64a1577 100644 --- a/analog-arm.c +++ b/analog-arm.c @@ -68,7 +68,7 @@ void analog_init() { /** Read analog value. - \param channel Channel to be read. + \param channel Channel to be read. Channel numbering starts at zero. \return Analog reading, 10-bit right aligned. @@ -76,7 +76,13 @@ void analog_init() { there's no need to hold them in a copy. */ uint16_t analog_read(uint8_t index) { - return (LPC_ADC->DR[index] & 0xFFC0) >> 6; + uint16_t result = 0; + + if (index < sizeof(adc_channel) && adc_channel[index] < 8) { + result = (LPC_ADC->DR[adc_channel[index]] & 0xFFC0) >> 6; + } + + return result; } #endif /* defined TEACUP_C_INCLUDE && defined __ARMEL__ */ diff --git a/analog-avr.c b/analog-avr.c index a6ba286..c5bdf5c 100644 --- a/analog-avr.c +++ b/analog-avr.c @@ -12,18 +12,10 @@ static uint8_t adc_counter; static volatile uint16_t BSS adc_result[NUM_TEMP_SENSORS]; -#undef DEFINE_TEMP_SENSOR -#define DEFINE_TEMP_SENSOR(name, type, pin, additional) \ - ((type == TT_THERMISTOR) || (type == TT_AD595)) ? (pin ## _ADC) : 255, -static uint8_t adc_channel[NUM_TEMP_SENSORS] = -{ - #include "config_wrapper.h" -}; -#undef DEFINE_TEMP_SENSOR - //! Configure all registers, start interrupt loop void analog_init() { - if (analog_mask > 0) { + + if (analog_mask) { // At least one temp sensor uses an analog channel. // clear ADC bit in power reduction register because of ADC use. #ifdef PRR PRR &= ~MASK(PRADC); @@ -56,7 +48,7 @@ void analog_init() { // now we start the first conversion and leave the rest to the interrupt ADCSRA |= MASK(ADIE) | MASK(ADSC); - } /* analog_mask > 0 */ + } /* analog_mask */ } /*! Analog Interrupt @@ -65,7 +57,7 @@ void analog_init() { */ ISR(ADC_vect, ISR_NOBLOCK) { // emulate free-running mode but be more deterministic about exactly which result we have, since this project has long-running interrupts - if (analog_mask > 0) { // at least one temp sensor uses an analog channel + if (analog_mask) { // store next result adc_result[adc_counter] = ADC; diff --git a/analog.c b/analog.c index 459cba0..3043af3 100644 --- a/analog.c +++ b/analog.c @@ -22,6 +22,17 @@ ; #undef DEFINE_TEMP_SENSOR +/** + A map of the ADC channels of the defined sensors. +*/ +#undef DEFINE_TEMP_SENSOR +#define DEFINE_TEMP_SENSOR(name, type, pin, additional) \ + ((type == TT_THERMISTOR) || (type == TT_AD595)) ? (pin ## _ADC) : 255, +static uint8_t adc_channel[NUM_TEMP_SENSORS] = { + #include "config_wrapper.h" +}; +#undef DEFINE_TEMP_SENSOR + #define TEACUP_C_INCLUDE #include "analog-avr.c" #include "analog-arm.c"