2
* fsm-eeprom.c: EEPROM API 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 "fsm-eeprom.h"
26
#ifdef EEPROM_OVERRIDE
29
uint8_t eeprom[EEPROM_BYTES];
32
uint8_t load_eeprom() {
34
delay_4ms(2); // wait for power to stabilize
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; }
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));
52
delay_4ms(2); // wait for power to stabilize
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]);
62
// save the marker last, to indicate the transaction is complete
63
eeprom_update_byte((uint8_t *)EEP_START, EEP_MARKER);
69
uint8_t eeprom_wl[EEPROM_WL_BYTES];
70
uint8_t * eep_wl_prev_offset;
72
uint8_t load_eeprom_wl() {
74
delay_4ms(2); // wait for power to stabilize
78
// check if eeprom has been initialized; abort if it hasn't
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) {
86
eep_wl_prev_offset = offset;
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);
101
void save_eeprom_wl() {
102
#ifdef LED_ENABLE_PIN
103
delay_4ms(2); // wait for power to stabilize
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);
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;
118
// FIXME: write the marker last, to signal completed transaction
119
eeprom_update_byte(offset, EEP_MARKER);
122
for(uint8_t i=0; i<EEPROM_WL_BYTES; i++, offset++) {
123
eeprom_update_byte(offset, eeprom_wl[i]);