~toykeeper/flashlight-firmware/trunk

« back to all changes in this revision

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

  • Committer: Selene Scriven
  • Date: 2018-07-05 10:22:48 UTC
  • mto: This revision was merged to the branch mainline in revision 209.
  • Revision ID: bzr@toykeeper.net-20180705102248-4157s7ly7ascx0w3
Forked Bistro as a base for Dragon.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * fsm-eeprom.c: EEPROM API 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_EEPROM_C
21
 
#define FSM_EEPROM_C
22
 
 
23
 
#include "fsm-eeprom.h"
24
 
 
25
 
#ifdef USE_EEPROM
26
 
#ifdef EEPROM_OVERRIDE
27
 
uint8_t *eeprom;
28
 
#else
29
 
uint8_t eeprom[EEPROM_BYTES];
30
 
#endif
31
 
 
32
 
uint8_t load_eeprom() {
33
 
    #ifdef LED_ENABLE_PIN
34
 
    delay_4ms(2);  // wait for power to stabilize
35
 
    #endif
36
 
 
37
 
    cli();
38
 
    // check if eeprom has been initialized; abort if it hasn't
39
 
    uint8_t marker = eeprom_read_byte((uint8_t *)EEP_START);
40
 
    if (marker != EEP_MARKER) { sei(); return 0; }
41
 
 
42
 
    // load the actual data
43
 
    for(uint8_t i=0; i<EEPROM_BYTES; i++) {
44
 
        eeprom[i] = eeprom_read_byte((uint8_t *)(EEP_START+1+i));
45
 
    }
46
 
    sei();
47
 
    return 1;
48
 
}
49
 
 
50
 
void save_eeprom() {
51
 
    #ifdef LED_ENABLE_PIN
52
 
    delay_4ms(2);  // wait for power to stabilize
53
 
    #endif
54
 
 
55
 
    cli();
56
 
 
57
 
    // save the actual data
58
 
    for(uint8_t i=0; i<EEPROM_BYTES; i++) {
59
 
        eeprom_update_byte((uint8_t *)(EEP_START+1+i), eeprom[i]);
60
 
    }
61
 
 
62
 
    // save the marker last, to indicate the transaction is complete
63
 
    eeprom_update_byte((uint8_t *)EEP_START, EEP_MARKER);
64
 
    sei();
65
 
}
66
 
#endif
67
 
 
68
 
#ifdef USE_EEPROM_WL
69
 
uint8_t eeprom_wl[EEPROM_WL_BYTES];
70
 
uint8_t * eep_wl_prev_offset;
71
 
 
72
 
uint8_t load_eeprom_wl() {
73
 
    #ifdef LED_ENABLE_PIN
74
 
    delay_4ms(2);  // wait for power to stabilize
75
 
    #endif
76
 
 
77
 
    cli();
78
 
    // check if eeprom has been initialized; abort if it hasn't
79
 
    uint8_t found = 0;
80
 
    uint8_t * offset;
81
 
    for(offset = 0;
82
 
        offset < (uint8_t *)(EEP_WL_SIZE - EEPROM_WL_BYTES - 1);
83
 
        offset += (EEPROM_WL_BYTES + 1)) {
84
 
        if (eeprom_read_byte(offset) == EEP_MARKER) {
85
 
            found = 1;
86
 
            eep_wl_prev_offset = offset;
87
 
            break;
88
 
        }
89
 
    }
90
 
 
91
 
    if (found) {
92
 
        // load the actual data
93
 
        for(uint8_t i=0; i<EEPROM_WL_BYTES; i++) {
94
 
            eeprom_wl[i] = eeprom_read_byte(offset+1+i);
95
 
        }
96
 
    }
97
 
    sei();
98
 
    return found;
99
 
}
100
 
 
101
 
void save_eeprom_wl() {
102
 
    #ifdef LED_ENABLE_PIN
103
 
    delay_4ms(2);  // wait for power to stabilize
104
 
    #endif
105
 
 
106
 
    cli();
107
 
    // erase old state
108
 
    uint8_t * offset = eep_wl_prev_offset;
109
 
    for (uint8_t i = 0; i < EEPROM_WL_BYTES+1; i ++) {
110
 
        eeprom_update_byte(offset+i, 0xFF);
111
 
    }
112
 
 
113
 
    // save new state
114
 
    offset += EEPROM_WL_BYTES+1;
115
 
    if (offset > (uint8_t *)(EEP_WL_SIZE-EEPROM_WL_BYTES-1)) offset = 0;
116
 
    eep_wl_prev_offset = offset;
117
 
    // marker byte
118
 
    // FIXME: write the marker last, to signal completed transaction
119
 
    eeprom_update_byte(offset, EEP_MARKER);
120
 
    offset ++;
121
 
    // user data
122
 
    for(uint8_t i=0; i<EEPROM_WL_BYTES; i++, offset++) {
123
 
        eeprom_update_byte(offset, eeprom_wl[i]);
124
 
    }
125
 
    sei();
126
 
}
127
 
#endif
128
 
 
129
 
 
130
 
#endif