From 8b59e880614455bd7bc7d8595de847dd6fe9b5b2 Mon Sep 17 00:00:00 2001 From: Selene ToyKeeper Date: Thu, 14 Nov 2019 01:52:50 -0700 Subject: refactored how interrupts work... set a flag and return immediately, then handle the actual logic later during a less-critical code path Enables smarter responses to standby wakeups. Seems to fix missed button presses during standby, and most of the too-fast sleep ticks. Also eliminated waits from button state measurement, so it can happen easier during standby. (also eliminates the chance of an infinite loop on extra-noisy hardware) Also might improve timing-sensitive interrupts like attiny85 PWM channel 4, or a PWM-DSM hybrid technique I'd like to try. BUT this change also appears to break the thermal sensor, so that needs to be fixed. --- spaghetti-monster/fsm-adc.c | 45 ++++++++++++++++++++++++++++++++--------- spaghetti-monster/fsm-adc.h | 8 +++++++- spaghetti-monster/fsm-events.c | 3 +++ spaghetti-monster/fsm-main.c | 21 +++++++++++++++++++ spaghetti-monster/fsm-main.h | 2 ++ spaghetti-monster/fsm-pcint.c | 21 +++++++------------ spaghetti-monster/fsm-pcint.h | 1 + spaghetti-monster/fsm-standby.c | 19 +++++++++++++---- spaghetti-monster/fsm-wdt.c | 22 +++++++++++++------- spaghetti-monster/fsm-wdt.h | 4 ++-- 10 files changed, 108 insertions(+), 38 deletions(-) diff --git a/spaghetti-monster/fsm-adc.c b/spaghetti-monster/fsm-adc.c index 6832e32..1fc5472 100644 --- a/spaghetti-monster/fsm-adc.c +++ b/spaghetti-monster/fsm-adc.c @@ -113,15 +113,44 @@ static inline uint8_t calc_voltage_divider(uint16_t value) { #else #define ADC_CYCLES_PER_SECOND 8 #endif -// TODO: is this better done in main() or WDT()? + +// save the measurement result, set a flag to show something happened, +// and count how many times we've triggered since last counter reset ISR(ADC_vect) { - // For some reason, the ADC interrupt is getting called a *lot* - // more often than it should be, like it's auto-triggering after each - // measurement, but I don't know why, or how to turn that off... - // So, skip every call except when explicitly requested. + #if 0 // the fancy method is probably not even needed + // count up but wrap around from 255 to 128; not 255 to 0 + // TODO: find a way to do this faster if possible + uint8_t val = irq_adc; // cache volatile value + irq_adc = (val + 1) | (val & 0b10000000); + #else + irq_adc ++; + #endif + adc_value = ADC; // save this for later use +} + +void ADC_inner() { + // ignore the first measurement; the docs say it's junk + if (irq_adc < 2) { + ADC_start_measurement(); // start a second measurement + return; + } + + // the ADC triggers repeatedly when it's on, but we only want one value + // (so ignore everything after the first value, until it's manually reset) if (! adcint_enable) return; + + // if we're actually runnning, reset the status flags / counters + irq_adc = 0; adcint_enable = 0; + #ifdef TICK_DURING_STANDBY + // in sleep mode, turn off after just one measurement + // (having the ADC on raises standby power by about 250 uA) + // (and the usual standby level is only ~20 uA) + if (go_to_standby) ADC_off(); + #endif + + // what is being measured? 0/1 = battery voltage, 2/3 = temperature static uint8_t adc_step = 0; // LVP declarations @@ -155,7 +184,7 @@ ISR(ADC_vect) { #define ADC_STEPS 2 #endif - uint16_t measurement = ADC; // latest 10-bit ADC reading + uint16_t measurement = adc_value; // latest 10-bit ADC reading #ifdef USE_PSEUDO_RAND // real-world entropy makes this a true random, not pseudo @@ -376,10 +405,6 @@ ISR(ADC_vect) { #endif #endif - #ifdef TICK_DURING_STANDBY - // if we were asleep, go back to sleep - if (go_to_standby) ADC_off(); - #endif } #ifdef USE_BATTCHECK diff --git a/spaghetti-monster/fsm-adc.h b/spaghetti-monster/fsm-adc.h index 274fb4d..acb3da6 100644 --- a/spaghetti-monster/fsm-adc.h +++ b/spaghetti-monster/fsm-adc.h @@ -38,9 +38,15 @@ #define VOLTAGE_FUDGE_FACTOR 5 #endif #endif + +volatile uint8_t irq_adc = 0; // ADC interrupt happened? +uint16_t adc_value; // last ADC measurement +uint8_t adcint_enable = 0; // is the current ADC result needed? +void ADC_inner(); // do the actual ADC-related calculations + volatile uint8_t voltage = 0; -volatile uint8_t adcint_enable; // kludge, because adc auto-retrigger won't turn off void low_voltage(); + #ifdef USE_BATTCHECK void battcheck(); #ifdef BATTCHECK_VpT diff --git a/spaghetti-monster/fsm-events.c b/spaghetti-monster/fsm-events.c index 3e9a12d..e125e84 100644 --- a/spaghetti-monster/fsm-events.c +++ b/spaghetti-monster/fsm-events.c @@ -145,6 +145,9 @@ uint8_t nice_delay_ms(uint16_t ms) { _delay_loop_2(BOGOMIPS*98/100); #endif // ifdef USE_DYNAMIC_UNDERCLOCKING + // run pending system processes while we wait + handle_deferred_interrupts(); + if ((nice_delay_interrupt) || (old_state != current_state)) { return 0; // state changed; abort } diff --git a/spaghetti-monster/fsm-main.c b/spaghetti-monster/fsm-main.c index e537a9e..a898f41 100644 --- a/spaghetti-monster/fsm-main.c +++ b/spaghetti-monster/fsm-main.c @@ -160,6 +160,9 @@ int main() { standby_mode(); } + // catch up on interrupts + handle_deferred_interrupts(); + // give the recipe some time slices loop(); @@ -168,4 +171,22 @@ int main() { } } + +void handle_deferred_interrupts() { + /* + if (irq_pcint) { // button pressed or released + // nothing to do here + // (PCINT only matters during standby) + } + */ + if (irq_adc) { // ADC done measuring + ADC_inner(); + // irq_adc = 0; // takes care of itself + } + if (irq_wdt) { // the clock ticked + WDT_inner(); + // irq_wdt = 0; // takes care of itself + } +} + #endif diff --git a/spaghetti-monster/fsm-main.h b/spaghetti-monster/fsm-main.h index cc469d7..55ae2ff 100644 --- a/spaghetti-monster/fsm-main.h +++ b/spaghetti-monster/fsm-main.h @@ -21,5 +21,7 @@ #define FSM_MAIN_H int main(); +// needs to run frequently to execute the logic for WDT and ADC and stuff +void handle_deferred_interrupts(); #endif diff --git a/spaghetti-monster/fsm-pcint.c b/spaghetti-monster/fsm-pcint.c index 4928980..a4a496a 100644 --- a/spaghetti-monster/fsm-pcint.c +++ b/spaghetti-monster/fsm-pcint.c @@ -24,19 +24,9 @@ #include uint8_t button_is_pressed() { - // remember the past 32 measurements - static uint32_t readings = 0; - // take at least one new measurement, - // and wait for measurements to settle to all zeroes or all ones - do { - // shift past readings and add current value - readings = (readings << 1) | ((SWITCH_PORT & (1<