From eecf3af9f15708b0e220c2785e8e21886e392ea4 Mon Sep 17 00:00:00 2001 From: Stephan Walter Date: Sun, 20 Feb 2011 20:55:22 +0100 Subject: [PATCH] analog mask calculated automagically --- analog.c | 87 ++++++++++++++++---------------------------------- config.gen3.h | 3 -- config.gen6.h | 3 -- config.h.dist | 3 -- config.ramps.h | 3 -- mendel.c | 3 +- temp.c | 27 +++++++--------- temp.h | 11 ++++++- 8 files changed, 52 insertions(+), 88 deletions(-) diff --git a/analog.c b/analog.c index ff5b03d..ddced7b 100644 --- a/analog.c +++ b/analog.c @@ -1,48 +1,21 @@ #include "analog.h" +#include "temp.h" #include -#ifndef ANALOG_MASK - #warning ANALOG_MASK not defined - analog subsystem disabled - #define ANALOG_MASK 0 -#endif +/* OR-combined mask of all channels */ +#undef DEFINE_TEMP_SENSOR +#define DEFINE_TEMP_SENSOR(name, type, pin) | (((type == TT_THERMISTOR) || (type == TT_AD595)) ? 1 << (pin) : 0) +static const uint8_t analog_mask = 0 +#include "config.h" +; +#undef DEFINE_TEMP_SENSOR -uint8_t adc_running_mask, adc_counter; - -#if ANALOG_MASK & 1 - #define ANALOG_START 0 - #define ANALOG_START_MASK 1 -#elif ANALOG_MASK & 2 - #define ANALOG_START 1 - #define ANALOG_START_MASK 2 -#elif ANALOG_MASK & 4 - #define ANALOG_START 2 - #define ANALOG_START_MASK 4 -#elif ANALOG_MASK & 8 - #define ANALOG_START 3 - #define ANALOG_START_MASK 8 -#elif ANALOG_MASK & 16 - #define ANALOG_START 4 - #define ANALOG_START_MASK 16 -#elif ANALOG_MASK & 32 - #define ANALOG_START 5 - #define ANALOG_START_MASK 32 -#elif ANALOG_MASK & 64 - #define ANALOG_START 6 - #define ANALOG_START_MASK 64 -#elif ANALOG_MASK & 128 - #define ANALOG_START 7 - #define ANALOG_START_MASK 128 -#else - // ANALOG_MASK == 0 - #define ANALOG_START 0 - #define ANALOG_START_MASK 1 -#endif - -volatile uint16_t adc_result[8] __attribute__ ((__section__ (".bss"))); +static uint8_t adc_counter; +static volatile uint16_t adc_result[8] __attribute__ ((__section__ (".bss"))); void analog_init() { - #if ANALOG_MASK > 0 + if (analog_mask > 0) { #ifdef PRR PRR &= ~MASK(PRADC); #elif defined PRR0 @@ -55,36 +28,32 @@ void analog_init() { ADCSRA = MASK(ADEN) | MASK(ADPS2) | MASK(ADPS1) | MASK(ADPS0); adc_counter = 0; - adc_running_mask = 1; - - AIO0_DDR &= ~(ANALOG_MASK); - DIDR0 = (ANALOG_MASK) & 0x3F; + AIO0_DDR &= ~analog_mask; + DIDR0 = analog_mask & 0x3F; // now we start the first conversion and leave the rest to the interrupt ADCSRA |= MASK(ADIE) | MASK(ADSC); - #endif /* ANALOG_MASK > 0 */ + } /* analog_mask > 0 */ } 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 - adc_result[ADMUX & 0x0F] = ADC; - // find next channel - do { - adc_counter++; - adc_running_mask <<= 1; - if (adc_counter == 8) { - adc_counter = ANALOG_START; - adc_running_mask = ANALOG_START_MASK; - } - } while ((adc_running_mask & (ANALOG_MASK)) == 0); + if (analog_mask > 0) { + adc_result[ADMUX & 0x0F] = ADC; + // find next channel + do { + adc_counter++; + adc_counter &= 0x07; + } while ((analog_mask & (1 << adc_counter)) == 0); - // start next conversion - ADMUX = (adc_counter) | REFERENCE; - ADCSRA |= MASK(ADSC); + // start next conversion + ADMUX = (adc_counter) | REFERENCE; + ADCSRA |= MASK(ADSC); + } } uint16_t analog_read(uint8_t channel) { - #if ANALOG_MASK > 0 + if (analog_mask > 0) { uint16_t r; uint8_t sreg; @@ -100,7 +69,7 @@ uint16_t analog_read(uint8_t channel) { SREG = sreg; return r; - #else + } else { return 0; - #endif + } } diff --git a/config.gen3.h b/config.gen3.h index 6c64b5f..2cd846c 100644 --- a/config.gen3.h +++ b/config.gen3.h @@ -212,9 +212,6 @@ undefine if you don't want to use them // #define TEMP_PT100 #define TEMP_INTERCOM -// ANALOG_MASK is a bitmask of all analog channels used- bitwise-or them all together -#define ANALOG_MASK 0 - /***************************************************************************\ * * * Define your temperature sensors here * diff --git a/config.gen6.h b/config.gen6.h index c6bd821..e9a5004 100644 --- a/config.gen6.h +++ b/config.gen6.h @@ -207,9 +207,6 @@ undefine if you don't want to use them // #define TEMP_PT100 // #define TEMP_INTERCOM -// ANALOG_MASK is a bitmask of all analog channels used- bitwise-or them all together -#define ANALOG_MASK MASK(AIO5_PIN) - /***************************************************************************\ * * * Define your temperature sensors here * diff --git a/config.h.dist b/config.h.dist index 60ec956..d508570 100644 --- a/config.h.dist +++ b/config.h.dist @@ -228,9 +228,6 @@ undefine if you don't want to use them // #define TEMP_PT100 // #define TEMP_INTERCOM -// ANALOG_MASK is a bitmask of all analog channels used- bitwise-or them all together -#define ANALOG_MASK MASK(AIO0_PIN) - /***************************************************************************\ * * * Define your temperature sensors here * diff --git a/config.ramps.h b/config.ramps.h index 72be769..dcd719e 100644 --- a/config.ramps.h +++ b/config.ramps.h @@ -217,9 +217,6 @@ undefine if you don't want to use them // #define TEMP_PT100 // #define TEMP_INTERCOM -// ANALOG_MASK is a bitmask of all analog channels used- bitwise-or them all together -#define ANALOG_MASK MASK(AIO1_PIN) | MASK(AIO2_PIN) - /***************************************************************************\ * * * Define your temperature sensors here * diff --git a/mendel.c b/mendel.c index 0102438..908d211 100644 --- a/mendel.c +++ b/mendel.c @@ -144,7 +144,8 @@ void init(void) { // set up default feedrate current_position.F = startpoint.F = next_target.target.F = SEARCH_FEEDRATE_Z; - // start up analog read interrupt loop, if anything uses analog as determined by ANALOG_MASK in your config.h + // start up analog read interrupt loop, + // if any of the temp sensors in your config.h use analog interface analog_init(); // set up temperature inputs diff --git a/temp.c b/temp.c index f5423b2..96d268c 100644 --- a/temp.c +++ b/temp.c @@ -15,24 +15,15 @@ #include "intercom.h" #endif -typedef enum { - TT_THERMISTOR, - TT_MAX6675, - TT_AD595, - TT_PT100, - TT_INTERCOM, - TT_DUMMY, -} temp_types; - typedef enum { PRESENT, TCOPEN } temp_flags_enum; typedef struct { - uint8_t temp_type; - uint8_t temp_pin; - uint8_t heater_index; + temp_type_t temp_type; + uint8_t temp_pin; + heater_t heater; } temp_sensor_definition_t; #undef DEFINE_TEMP_SENSOR @@ -95,6 +86,9 @@ void temp_init() { update_send_cmd(0); break; #endif + + default: /* prevent compiler warning */ + break; } } } @@ -214,7 +208,7 @@ void temp_sensor_tick() { #ifdef TEMP_AD595 case TT_AD595: - temp = analog_read(temp_pin); + temp = analog_read(temp_sensors[i].temp_pin); // convert // >>8 instead of >>10 because internal temp is stored as 14.2 fixed point @@ -255,6 +249,9 @@ void temp_sensor_tick() { break; #endif /* TEMP_DUMMY */ + + default: /* prevent compiler warning */ + break; } temp_sensors_runtime[i].last_read_temp = temp; @@ -266,8 +263,8 @@ void temp_sensor_tick() { temp_sensors_runtime[i].temp_residency = 0; } - if (temp_sensors[i].heater_index < NUM_HEATERS) { - heater_tick(temp_sensors[i].heater_index, i, temp_sensors_runtime[i].last_read_temp, temp_sensors_runtime[i].target_temp); + if (temp_sensors[i].heater < NUM_HEATERS) { + heater_tick(temp_sensors[i].heater, i, temp_sensors_runtime[i].last_read_temp, temp_sensors_runtime[i].target_temp); } } } diff --git a/temp.h b/temp.h index 1815c4c..6a1d5e5 100644 --- a/temp.h +++ b/temp.h @@ -21,6 +21,15 @@ typedef enum { } temp_sensor_t; #undef DEFINE_TEMP_SENSOR +typedef enum { + TT_THERMISTOR, + TT_MAX6675, + TT_AD595, + TT_PT100, + TT_INTERCOM, + TT_DUMMY, +} temp_type_t; + #define temp_tick temp_sensor_tick void temp_init(void); @@ -34,4 +43,4 @@ uint16_t temp_get(temp_sensor_t index); void temp_print(temp_sensor_t index); -#endif /* _TIMER_H */ +#endif /* _TEMP_H */