forked from semerad/gt3b
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eeprom.c
127 lines (94 loc) · 2.99 KB
/
eeprom.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
eeprom - routines to accessing eeprom
Copyright (C) 2011 Pavel Semerad
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include "eeprom.h"
void eeprom_init(void) {
// enable writing to eeprom
/* moved to eeprom_write to do it only temporary
FLASH_DUKR = 0xAE;
FLASH_DUKR = 0x56;
*/
}
// read from eeprom
@inline static void eeprom_read(u8 *ee_addr, void *ram_addr, u16 length) {
memcpy(ram_addr, ee_addr, length);
}
void eeprom_read_global(void) {
eeprom_read(EEPROM_CONFIG_GLOBAL, &config_global, sizeof(config_global_s));
}
void eeprom_read_model(u8 model) {
u8 size = sizeof(config_model_s);
eeprom_read(EEPROM_CONFIG_MODEL + model * size, &config_model, size);
}
// write to eeprom
static void eeprom_write(u8 *ee_addr, u8 *ram_addr, u16 length) {
_Bool writed = 0;
u8 i = 10;
// enable write to eeprom
FLASH_DUKR = 0xAE;
FLASH_DUKR = 0x56;
// wait for write enabled
do {
if (BCHK(FLASH_IAPSR, 3)) break;
} while (--i);
// write only values, which are different
do {
if (*ee_addr != *ram_addr) {
*ee_addr = *ram_addr;
writed = 1;
}
ee_addr++;
ram_addr++;
} while (--length);
// wait to end of write and disable write to eeprom, but only when
// some byte was writed
if (writed)
while (!BCHK(FLASH_IAPSR, 2)) pause();
BRES(FLASH_IAPSR, 3);
}
void eeprom_write_global(void) {
eeprom_write(EEPROM_CONFIG_GLOBAL, (u8 *)&config_global, sizeof(config_global_s));
}
void eeprom_write_model(u8 model) {
u8 size = sizeof(config_model_s);
eeprom_write(EEPROM_CONFIG_MODEL + model * size, (u8 *)&config_model, size);
}
// initialize model memories to empty one
void eeprom_empty_models(void) {
_Bool writed = 0;
u8 i = 0;
u8 cnt = CONFIG_MODEL_MAX;
config_model_s *cm = (config_model_s *)EEPROM_CONFIG_MODEL;
// enable write to eeprom
FLASH_DUKR = 0xAE;
FLASH_DUKR = 0x56;
// wait for write enabled
do {
if (BCHK(FLASH_IAPSR, 3)) break;
} while (--i);
// write 0xFF to first letter of each model config
do {
if (cm->name[0] != CONFIG_MODEL_EMPTY) {
cm->name[0] = CONFIG_MODEL_EMPTY;
writed = 1;
}
cm++;
} while (--cnt);
// wait to end of write and disable write to eeprom, but only when
// some byte was writed
if (writed)
while (!BCHK(FLASH_IAPSR, 2)) pause();
BRES(FLASH_IAPSR, 3);
}