diff --git a/analog.c b/analog.c index 5684bc4..20cf3d5 100644 --- a/analog.c +++ b/analog.c @@ -12,34 +12,31 @@ /* OR-combined mask of all channels */ #undef DEFINE_TEMP_SENSOR //! automagically generate analog_mask from DEFINE_TEMP_SENSOR entries in config.h -#define DEFINE_TEMP_SENSOR(name, type, pin, additional) | (((type == TT_THERMISTOR) || (type == TT_AD595)) ? (1 << (pin ## _ADC)) : 0) - +#define DEFINE_TEMP_SENSOR(name, type, pin, additional) \ + | (((type == TT_THERMISTOR) || (type == TT_AD595)) ? (1 << (pin ## _ADC)) : 0) #ifdef AIO8_PIN static const uint16_t analog_mask = 0 #else static const uint8_t analog_mask = 0 #endif - #include "config.h" ; #undef DEFINE_TEMP_SENSOR -#ifdef AIO8_PIN - #define AINDEX_MASK 0x1F - #define AINDEX_MAX 15 - #define AINDEX_CURRENT ((ADMUX & 0x07) | ((ADCSRB & MASK(MUX5))?0x08:0)) -#else - #define AINDEX_MASK 0x0F - #define AINDEX_MAX 7 - #define AINDEX_CURRENT (ADMUX & 0x07) -#endif - static uint8_t adc_counter; -static volatile uint16_t adc_result[AINDEX_MAX + 1] __attribute__ ((__section__ (".bss"))); +static volatile uint16_t adc_result[NUM_TEMP_SENSORS] __attribute__ ((__section__ (".bss"))); + +#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.h" +}; +#undef DEFINE_TEMP_SENSOR //! Configure all registers, start interrupt loop void analog_init() { - if (NUM_TEMP_SENSORS > 0) { + if (analog_mask > 0) { // clear ADC bit in power reduction register because of ADC use. #ifdef PRR PRR &= ~MASK(PRADC); @@ -84,24 +81,21 @@ ISR(ADC_vect, ISR_NOBLOCK) { uint8_t sreg_save = SREG; // emulate free-running mode but be more deterministic about exactly which result we have, since this project has long-running interrupts - if (NUM_TEMP_SENSORS > 0) { + if (analog_mask > 0) { // at least one temp sensor uses an analog channel // store next result - adc_result[AINDEX_CURRENT] = ADC; + adc_result[adc_counter] = ADC; - // find next channel - /* TODO: this is not exactly high performance. adc_result[] should be of - size NUM_TEMP_SENSORS only, along with an array of the corresponding - ADC channel(s). This also requires changes to the callers of - analog_read(). */ + // next channel do { adc_counter++; - adc_counter &= AINDEX_MAX; - } while ((analog_mask & (1 << adc_counter)) == 0); + if (adc_counter >= sizeof(adc_channel)) + adc_counter = 0; + } while (adc_channel[adc_counter] == 255); // start next conversion - ADMUX = (adc_counter & 0x07) | REFERENCE; + ADMUX = (adc_channel[adc_counter] & 0x07) | REFERENCE; #ifdef MUX5 - if (adc_counter & 0x08) + if (adc_channel[adc_counter] & 0x08) ADCSRB |= MASK(MUX5); else ADCSRB &= ~MASK(MUX5); @@ -120,8 +114,8 @@ ISR(ADC_vect, ISR_NOBLOCK) { \param channel Channel to be read \return analog reading, 10-bit right aligned */ -uint16_t analog_read(uint8_t channel) { - if (NUM_TEMP_SENSORS > 0) { +uint16_t analog_read(uint8_t index) { + if (analog_mask > 0) { uint16_t r; uint8_t sreg; @@ -131,7 +125,7 @@ uint16_t analog_read(uint8_t channel) { cli(); // atomic 16-bit copy - r = adc_result[channel]; + r = adc_result[index]; // restore interrupt flag SREG = sreg; diff --git a/analog.h b/analog.h index ba7396e..d75a7b6 100644 --- a/analog.h +++ b/analog.h @@ -27,6 +27,6 @@ void analog_init(void); -uint16_t analog_read(uint8_t channel); +uint16_t analog_read(uint8_t index); #endif /* _ANALOG_H */ diff --git a/temp.c b/temp.c index d2de970..9faad33 100644 --- a/temp.c +++ b/temp.c @@ -178,7 +178,7 @@ void temp_sensor_tick() { do { uint8_t j, table_num; //Read current temperature - temp = analog_read(temp_sensors[i].temp_pin); + temp = analog_read(i); // for thermistors the thermistor table number is in the additional field table_num = temp_sensors[i].additional; @@ -237,7 +237,7 @@ void temp_sensor_tick() { #ifdef TEMP_AD595 case TT_AD595: - temp = analog_read(temp_sensors[i].temp_pin); + temp = analog_read(i); // convert // >>8 instead of >>10 because internal temp is stored as 14.2 fixed point