~toykeeper/flashlight-firmware/trunk

« back to all changes in this revision

Viewing changes to ToyKeeper/spaghetti-monster/fsm-wdt.c

  • Committer: Selene Scriven
  • Date: 2015-03-17 08:56:50 UTC
  • mto: This revision was merged to the branch mainline in revision 124.
  • Revision ID: ubuntu@toykeeper.net-20150317085650-s89wr9h28n2co7z1
Added TheStar firmwares from _the_

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * fsm-wdt.c: WDT (Watch Dog Timer) functions for SpaghettiMonster.
3
 
 *
4
 
 * Copyright (C) 2017 Selene Scriven
5
 
 *
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.
10
 
 *
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.
15
 
 *
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/>.
18
 
 */
19
 
 
20
 
#ifndef FSM_WDT_C
21
 
#define FSM_WDT_C
22
 
 
23
 
#include <avr/interrupt.h>
24
 
#include <avr/wdt.h>
25
 
 
26
 
void WDT_on()
27
 
{
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
38
 
    #else
39
 
        #error Unrecognized MCU type
40
 
    #endif
41
 
}
42
 
 
43
 
#ifdef TICK_DURING_STANDBY
44
 
inline void WDT_slow()
45
 
{
46
 
    #if (ATTINY == 25) || (ATTINY == 45) || (ATTINY == 85)
47
 
        // interrupt slower
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;
56
 
    #else
57
 
        #error Unrecognized MCU type
58
 
    #endif
59
 
}
60
 
#endif
61
 
 
62
 
inline void WDT_off()
63
 
{
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
77
 
        sei();
78
 
    #else
79
 
        #error Unrecognized MCU type
80
 
    #endif
81
 
}
82
 
 
83
 
// clock tick -- this runs every 16ms (62.5 fps)
84
 
ISR(WDT_vect) {
85
 
    irq_wdt = 1;  // WDT event happened
86
 
}
87
 
 
88
 
void WDT_inner() {
89
 
    irq_wdt = 0;  // WDT event handled; reset flag
90
 
 
91
 
    static uint8_t adc_trigger = 0;
92
 
 
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;
100
 
 
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) {
105
 
        go_to_standby = 0;
106
 
        PCINT_inner(pressed);
107
 
    }
108
 
    // cache again, in case the value changed
109
 
    ticks_since_last = ticks_since_last_event;
110
 
 
111
 
    #ifdef TICK_DURING_STANDBY
112
 
    // handle standby mode specially
113
 
    if (go_to_standby) {
114
 
        // emit a sleep tick, and process it
115
 
        emit(EV_sleep_tick, ticks_since_last);
116
 
        process_emissions();
117
 
 
118
 
        #ifndef USE_SLEEP_LVP
119
 
        return;  // no sleep LVP needed if nothing drains power while off
120
 
        #else
121
 
        // stop here, usually...  but proceed often enough for sleep LVP to work
122
 
        if (0 != (ticks_since_last & 0x3f)) return;
123
 
 
124
 
        adc_trigger = 0;  // make sure a measurement will happen
125
 
        ADC_on();  // enable ADC voltage measurement functions temporarily
126
 
        #endif
127
 
    }
128
 
    else {  // button handling should only happen while awake
129
 
    #endif
130
 
 
131
 
    // if time since last event exceeds timeout,
132
 
    // append timeout to current event sequence, then
133
 
    // send event to current state callback
134
 
 
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
138
 
    }
139
 
    else {
140
 
        emit(EV_tick, ticks_since_last);
141
 
    }
142
 
 
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);
148
 
        }
149
 
        // has button been down long enough to become a "hold"?
150
 
        // (first frame of a "hold" event)
151
 
        else {
152
 
            if (ticks_since_last >= HOLD_TIMEOUT) {
153
 
                current_event |= B_HOLD;
154
 
                emit_current_event(0);
155
 
            }
156
 
        }
157
 
    }
158
 
 
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();
167
 
        }
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();
173
 
        }
174
 
    }
175
 
 
176
 
    #ifdef TICK_DURING_STANDBY
177
 
    }
178
 
    #endif
179
 
 
180
 
    #if defined(USE_LVP) || defined(USE_THERMAL_REGULATION)
181
 
    // enable the deferred ADC handler once in a while
182
 
    if (! adc_trigger) {
183
 
        ADC_start_measurement();
184
 
        adc_deferred_enable = 1;
185
 
    }
186
 
    // timing for the ADC handler is every 32 ticks (~2Hz)
187
 
    adc_trigger = (adc_trigger + 1) & 31;
188
 
    #endif
189
 
}
190
 
 
191
 
#endif