diff --git a/mote/v2/avr/ctrl.c b/mote/v2/avr/ctrl.c index 0820311..543cec6 100644 --- a/mote/v2/avr/ctrl.c +++ b/mote/v2/avr/ctrl.c @@ -287,7 +287,7 @@ void ctrlCmdGet(uint8_t cmd) ctrlWriteCharToTxBuffer(version.sw_minor); break; - case 'e': /* port enabled | disabled */ + case 'e': /* sensor enabled | disabled */ ctrlReadCharFromRxBuffer(&i); if (i < MAX_SENSORS) { @@ -340,12 +340,11 @@ void ctrlCmdGet(uint8_t cmd) cli(); tmp32 = sensor[i].counter; tmp32_bis = (i < 3) ? state[i].power : state[i].timestamp; + state[i].flags &= ~(STATE_PULSE | STATE_POWER); sei(); ctrlWriteLongToTxBuffer(tmp32); ctrlWriteLongToTxBuffer(tmp32_bis); - - state[i].flags &= ~(STATE_PULSE | STATE_POWER); } } break; @@ -375,7 +374,7 @@ void ctrlCmdSet(uint8_t cmd) ctrlWriteCharToTxBuffer(version.sw_minor); break; - case 'e': /* port enabled | disabled */ + case 'e': /* sensor enabled | disabled */ ctrlReadCharFromRxBuffer(&i); if (i < MAX_SENSORS) { diff --git a/mote/v2/avr/global.h b/mote/v2/avr/global.h index ae82c25..bef7dc6 100644 --- a/mote/v2/avr/global.h +++ b/mote/v2/avr/global.h @@ -39,8 +39,14 @@ #define CYCLES_PER_US ((F_CPU+500000)/1000000) // cpu cycles per microsecond +#define DISABLE_PORT 0xff + #define MAX_SENSORS 6 #define ENABLE_ALL_SENSORS ((1 << MAX_SENSORS) - 1) #define DISABLE_ALL_SENSORS 0x00 +/* 0xff is the default sensor id for non-assigned ports and is disabled by default + a further check is done against the 'enabled' bitfield */ +#define ENABLED(x) (x != 0xff) && (enabled & (1 << x)) + #endif diff --git a/mote/v2/avr/main.c b/mote/v2/avr/main.c index fb91bb4..b35fd4a 100644 --- a/mote/v2/avr/main.c +++ b/mote/v2/avr/main.c @@ -50,13 +50,14 @@ struct event_struct event; uint8_t EEMEM EEPROM_enabled = DISABLE_ALL_SENSORS; uint8_t enabled; -uint8_t EEMEM EEPROM_phy_to_log[MAX_SENSORS] = {0, 1, 2, 3, 4, 5}; +uint8_t EEMEM EEPROM_phy_to_log[MAX_SENSORS] = + {DISABLE_PORT, DISABLE_PORT, DISABLE_PORT, DISABLE_PORT, DISABLE_PORT, DISABLE_PORT}; uint8_t phy_to_log[MAX_SENSORS]; struct sensor_struct EEMEM EEPROM_sensor[MAX_SENSORS]; -struct sensor_struct sensor[MAX_SENSORS]; +volatile struct sensor_struct sensor[MAX_SENSORS]; -struct state_struct state[MAX_SENSORS]; +volatile struct state_struct state[MAX_SENSORS]; uint8_t muxn = 0; uint16_t timer = 0; @@ -163,10 +164,10 @@ ISR(INT0_vect) { DBG_ISR_BEGIN(); - uint8_t muxn_l = phy_to_log[PORT_PULSE_1]; + uint8_t sensor_id = phy_to_log[PORT_PULSE_1]; - if (enabled & (1 << PORT_PULSE_1)) - register_pulse(&sensor[muxn_l], &state[muxn_l]); + if (ENABLED(sensor_id)) + register_pulse(&sensor[sensor_id], &state[sensor_id]); DBG_ISR_END(); } @@ -175,10 +176,10 @@ ISR(INT1_vect) { DBG_ISR_BEGIN(); - uint8_t muxn_l = phy_to_log[PORT_PULSE_2]; + uint8_t sensor_id = phy_to_log[PORT_PULSE_2]; - if (enabled & (1 << PORT_PULSE_2)) - register_pulse(&sensor[muxn_l], &state[muxn_l]); + if (ENABLED(sensor_id)) + register_pulse(&sensor[sensor_id], &state[sensor_id]); DBG_ISR_END(); } @@ -194,25 +195,30 @@ ISR(TIMER1_COMPA_vect) { DBG_ISR_BEGIN(); - if (enabled & (1 << muxn)) { - uint8_t muxn_l = phy_to_log[muxn]; + uint8_t sensor_id = phy_to_log[muxn]; - MacU16X16to32(state[muxn_l].nano, sensor[muxn_l].meterconst, ADC); + if (ENABLED(sensor_id)) { + /* clear the power calculation lock when starting a new 1sec cycle */ + if (timer == 0) + state[sensor_id].flags &= ~STATE_POWER_LOCK; - if (state[muxn_l].nano > WATT) { - sensor[muxn_l].counter++; - state[muxn_l].flags |= STATE_PULSE; - state[muxn_l].nano -= WATT; - state[muxn_l].pulse_count++; + MacU16X16to32(state[sensor_id].nano, sensor[sensor_id].meterconst, ADC); + + if (state[sensor_id].nano > WATT) { + sensor[sensor_id].counter++; + + state[sensor_id].flags |= STATE_PULSE; + state[sensor_id].nano -= WATT; + state[sensor_id].pulse_count++; } - if ((timer == SECOND) && !(state[muxn_l].flags & STATE_POWER_CALC)) { - state[muxn_l].nano_start = state[muxn_l].nano_end; - state[muxn_l].nano_end = state[muxn_l].nano; - state[muxn_l].pulse_count_final = state[muxn_l].pulse_count; - state[muxn_l].pulse_count = 0; - state[muxn_l].flags |= STATE_POWER_CALC; + if ((timer == SECOND) && !(state[sensor_id].flags & STATE_POWER_LOCK)) { + state[sensor_id].nano_start = state[sensor_id].nano_end; + state[sensor_id].nano_end = state[sensor_id].nano; + state[sensor_id].pulse_count_final = state[sensor_id].pulse_count; + state[sensor_id].pulse_count = 0; + state[sensor_id].flags |= STATE_POWER_CALC | STATE_POWER_LOCK; } } diff --git a/mote/v2/avr/main.h b/mote/v2/avr/main.h index 4289d08..e5a2978 100644 --- a/mote/v2/avr/main.h +++ b/mote/v2/avr/main.h @@ -34,10 +34,11 @@ struct sensor_struct { # define WATT 1000000000 # define SECOND 665 // 666Hz - 1 -#define STATE_PULSE 1 -#define STATE_SKIP 2 -#define STATE_POWER_CALC 4 -#define STATE_POWER 8 +#define STATE_PULSE 0x01 +#define STATE_SKIP 0x02 +#define STATE_POWER_LOCK 0x04 +#define STATE_POWER_CALC 0x08 +#define STATE_POWER 0x10 struct state_struct { uint8_t flags;