2
* fsm-wdt.c: WDT (Watch Dog Timer) functions for SpaghettiMonster.
4
* Copyright (C) 2017 Selene Scriven
6
* This program is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation, either version 3 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program. If not, see <http://www.gnu.org/licenses/>.
23
#include <avr/interrupt.h>
28
#if (ATTINY == 25) || (ATTINY == 45) || (ATTINY == 85)
29
// interrupt every 16ms
30
//cli(); // Disable interrupts
31
wdt_reset(); // Reset the WDT
32
WDTCR |= (1<<WDCE) | (1<<WDE); // Start timed sequence
33
WDTCR = (1<<WDIE); // Enable interrupt every 16ms
34
//sei(); // Enable interrupts
35
#elif (ATTINY == 1634)
36
wdt_reset(); // Reset the WDT
37
WDTCSR = (1<<WDIE); // Enable interrupt every 16ms
39
#error Unrecognized MCU type
43
#ifdef TICK_DURING_STANDBY
44
inline void WDT_slow()
46
#if (ATTINY == 25) || (ATTINY == 45) || (ATTINY == 85)
48
//cli(); // Disable interrupts
49
wdt_reset(); // Reset the WDT
50
WDTCR |= (1<<WDCE) | (1<<WDE); // Start timed sequence
51
WDTCR = (1<<WDIE) | STANDBY_TICK_SPEED; // Enable interrupt every so often
52
//sei(); // Enable interrupts
53
#elif (ATTINY == 1634)
54
wdt_reset(); // Reset the WDT
55
WDTCSR = (1<<WDIE) | STANDBY_TICK_SPEED;
57
#error Unrecognized MCU type
64
#if (ATTINY == 25) || (ATTINY == 45) || (ATTINY == 85)
65
//cli(); // Disable interrupts
66
wdt_reset(); // Reset the WDT
67
MCUSR &= ~(1<<WDRF); // Clear Watchdog reset flag
68
WDTCR |= (1<<WDCE) | (1<<WDE); // Start timed sequence
69
WDTCR = 0x00; // Disable WDT
70
//sei(); // Enable interrupts
71
#elif (ATTINY == 1634)
72
cli(); // needed because CCP, below
73
wdt_reset(); // Reset the WDT
74
MCUSR &= ~(1<<WDRF); // clear watchdog reset flag
75
CCP = 0xD8; // enable config changes
76
WDTCSR = 0; // disable and clear all WDT settings
79
#error Unrecognized MCU type
83
// clock tick -- this runs every 16ms (62.5 fps)
85
irq_wdt = 1; // WDT event happened
89
irq_wdt = 0; // WDT event handled; reset flag
91
static uint8_t adc_trigger = 0;
93
// cache this here to reduce ROM size, because it's volatile
94
uint16_t ticks_since_last = ticks_since_last_event;
95
// increment, but loop from max back to half
96
ticks_since_last = (ticks_since_last + 1) \
97
| (ticks_since_last & 0x8000);
98
// copy back to the original
99
ticks_since_last_event = ticks_since_last;
101
// detect and emit button change events (even during standby)
102
uint8_t was_pressed = button_last_state;
103
uint8_t pressed = button_is_pressed();
104
if (was_pressed != pressed) {
106
PCINT_inner(pressed);
108
// cache again, in case the value changed
109
ticks_since_last = ticks_since_last_event;
111
#ifdef TICK_DURING_STANDBY
112
// handle standby mode specially
114
// emit a sleep tick, and process it
115
emit(EV_sleep_tick, ticks_since_last);
118
#ifndef USE_SLEEP_LVP
119
return; // no sleep LVP needed if nothing drains power while off
121
// stop here, usually... but proceed often enough for sleep LVP to work
122
if (0 != (ticks_since_last & 0x3f)) return;
124
adc_trigger = 0; // make sure a measurement will happen
125
ADC_on(); // enable ADC voltage measurement functions temporarily
128
else { // button handling should only happen while awake
131
// if time since last event exceeds timeout,
132
// append timeout to current event sequence, then
133
// send event to current state callback
135
// callback on each timer tick
136
if ((current_event & B_FLAGS) == (B_CLICK | B_HOLD | B_PRESS)) {
137
emit(EV_tick, 0); // override tick counter while holding button
140
emit(EV_tick, ticks_since_last);
143
// user held button long enough to count as a long click?
144
if (current_event & B_PRESS) {
145
// during a "hold", send a hold event each tick, with a timer
146
if (current_event & B_HOLD) {
147
emit_current_event(ticks_since_last);
149
// has button been down long enough to become a "hold"?
150
// (first frame of a "hold" event)
152
if (ticks_since_last >= HOLD_TIMEOUT) {
153
current_event |= B_HOLD;
154
emit_current_event(0);
159
// event in progress, but button not currently down
160
else if (current_event) {
161
// "hold" event just ended
162
// no timeout required when releasing a long-press
163
// TODO? move this logic to PCINT() and simplify things here?
164
if (current_event & B_HOLD) {
165
//emit_current_event(0); // should have been emitted by PCINT_inner()
166
empty_event_sequence();
168
// end and clear event after release timeout
169
else if (ticks_since_last >= RELEASE_TIMEOUT) {
170
current_event |= B_TIMEOUT;
171
emit_current_event(0);
172
empty_event_sequence();
176
#ifdef TICK_DURING_STANDBY
180
#if defined(USE_LVP) || defined(USE_THERMAL_REGULATION)
181
// enable the deferred ADC handler once in a while
183
ADC_start_measurement();
184
adc_deferred_enable = 1;
186
// timing for the ADC handler is every 32 ticks (~2Hz)
187
adc_trigger = (adc_trigger + 1) & 31;