-
Notifications
You must be signed in to change notification settings - Fork 0
/
Flash.cpp
360 lines (322 loc) · 9.66 KB
/
Flash.cpp
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
* Flash.cpp
*
* Created on: Oct 1, 2015
* Author: lieven
* xtensa-lx106-elf-g++ -nostartfiles -nodefaultlibs -nostdlib -L"/home/lieven/workspace/Common/Debug" -L/home/lieven/esp_iot_sdk_v1.4.0/lib -Lgcc -u call_user_start -Wl,-static -T../ld/link.ld -Wl,--gc-sections -mlongcalls -Xlinker -L/home/lieven/esp-open-sdk/sdk/lib -Xlinker -lssl -lmain -lhal -lphy -lpp -lnet80211 -llwip -lwpa -lssl -lmain -lc -Xlinker --gc-sections -Xlinker --no-undefined -o "esp_cbor" ./wifi/Tcp.o ./wifi/Wifi.o ./mqtt/Mqtt.o ./mqtt/MqttFramer.o ./mqtt/MqttMsg.o ./mqtt/Topic.o ./cpp/Flash.o ./cpp/Gpio.o ./cpp/LedBlink.o ./cpp/Pump.o ./cpp/Stm32.o ./cpp/Sys.o ./cpp/Topics.o ./cpp/UartEsp8266.o ./cpp/stubs.o ./config.o ./gpio.o ./gpio16.o ./mutex.o ./uart.o ./user_main.o ./util.o ./utils.o ./watchdog.o -lCommon -lm -lssl -llwip -lwpa -lnet80211 -lphy -lpp -lmain -lc -lhal -lgcc
*
*-nostartfiles -nodefaultlibs -nostdlib -L"/home/lieven/workspace/Common/Debug"
*-nostartfiles -L/home/lieven/esp_iot_sdk_v1.4.0/lib -Lgcc -u call_user_start
*-nostartfiles -Wl,-static -T../ld/link.ld -Wl,--gc-sections -mlongcalls -Xlinker
*-nostartfiles -L/home/lieven/esp-open-sdk/sdk/lib -Xlinker -lssl -lmain -lhal -lphy -lpp -lnet80211 -llwip -lwpa -lssl -lmain
*-nostartfiles -lc -Xlinker --gc-sections -Xlinker --no-undefined
*
PAGE LAYOUT :
<MAGIC:32><SEQUENCE;32><itemIndex:16+length:16><data..length rounded 4 ><itemIndex+length><data:n><0xFFFFFFFF>
item idx==even : key
item idx+1==uneven : value of idx+1
Bin
DownloadHeader() address
mem size 512KB 1024KB 2048KB 4096KB
esp_init_data_default.bin 0x7C000 0xFC000 0x1FC000 0x3FC000
blank.bin 0x7E000 0xFE000 0x1FE000 0x3FE000
boot.bin 0x00000 0x00000 0x00000 0x00000
*/
extern "C" {
#include "espmissingincludes.h"
#include "esp8266.h"
#include "user_interface.h"
#include "osapi.h"
#include "espconn.h"
#include "os_type.h"
#include "mem.h"
#include "debug.h"
#include "user_config.h"
#include "queue.h"
#include "ets_sys.h"
}
#include "stdint.h"
#include "Flash.h"
#include "Sys.h"
#include <Log.h>
uint32_t roundQuad(uint32_t value) {
if (value & 0x3)
return (value & 0xFFFFFFFC) + 0x4;
else
return value;
}
Flash::Flash() {
_keyMax = 0;
_freePos = 0;
_page = 0;
_sequence = 0;
_highestIndex = 0;
}
Flash::~Flash() {
}
void Flash::init() {
findOrCreateActivePage();
_page = PAGE_START + PAGE_SIZE * (_sequence % PAGE_COUNT);
DEBUG("page address : 0x%X ", _page);
DEBUG("free start : 0x%X", _freePos);
DEBUG("sequence : %d", _sequence);
}
//-----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
bool Flash::isValidPage(uint32_t page, uint32_t& sequence) {
uint32_t magic;
uint32_t index = 0;
Erc erc = read(page, &magic);
if (erc)
return false;
if (magic != PAGE_SIGNATURE) {
return false;
}
erc = read(page + 4, &index);
if (erc)
return false;
sequence = index;
return true;
}
//-----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
Erc Flash::findOrCreateActivePage() {
_sequence = 0;
uint32_t sequence = 0;
for (uint32_t i = 0; i < PAGE_COUNT; i++) {
if (isValidPage(PAGE_START + i * PAGE_SIZE, sequence)
&& sequence > _sequence) {
_page = PAGE_START + i * PAGE_SIZE;
_sequence = sequence;
}
}
if (_sequence == 0) {
initPage(0);
_page = PAGE_START;
_sequence = 0;
}
uint32_t addr = _page + 8;
Quad q;
Erc erc;
while (true) {
erc = read(addr, &q.w);
if (erc)
return erc;
if (q.index == 0xFFFF)
break;
addr += 4 + roundQuad(q.length);
if (addr > (_page + PAGE_SIZE))
break;
}
_freePos = addr;
return E_OK;
}
//-----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
Erc Flash::read(uint32_t address, uint32_t* data) { // read QUad at quad boundary and cache
static uint32_t w; // last word read
static uint32_t lastAddr = 0; // last address where word was read
if (lastAddr != address) {
if (spi_flash_read(address, &w, 4) != SPI_FLASH_RESULT_OK)
return EINVAL;
lastAddr = address;
}
*data = w;
// INFO("@ 0x%X : 0x%X", addr, w);
return E_OK;
}
//-----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
Erc Flash::read(uint32_t address, uint8_t* pb) {
Quad W;
Erc erc;
if ((erc = read(address & 0xFFFFFFFC, &W.w)) != E_OK)
return erc; // take start int32
*pb = W.b[address & 0x03];
return E_OK;
}
//-----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
Erc Flash::write(uint32_t address, uint32_t w) {
// INFO("@ 0x%X : 0x%X", address, w);
if (spi_flash_write(address, &w, 4) != SPI_FLASH_RESULT_OK)
return EINVAL;
return E_OK;
}
//-----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
Erc Flash::initPage(uint32_t sequence) {
uint32_t page = PAGE_START + PAGE_SIZE * (sequence % PAGE_COUNT);
Erc erc = write(page, PAGE_SIGNATURE);
if (erc)
return erc;
return write(page + 4, sequence);
}
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
Erc Flash::put(uint16_t index, uint8_t* data, uint16_t length) {
Quad q;
q.index = index;
q.length = length;
Erc erc = write(_freePos, q.w);
if (erc)
return erc;
for (uint16_t i = 0; i < length + 3; i += 4) {
q.w = 0xFFFFFFFF;
for (uint32_t j = 0; i + j < length && j < 4; j++)
q.b[j] = data[i + j];
erc = write(_freePos + 4 + i, q.w);
if (erc)
return erc;
}
_freePos += 4 + roundQuad(length);
return E_OK;
}
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
Erc Flash::findIndex(uint16_t index, uint32_t* address) {
Quad q;
Erc erc;
uint32_t addr = _page + 8;
uint32_t addrIndex = 0;
while (true) {
erc = read(addr, &q.w);
if (erc)
return erc;
if (q.index == 0xFFFF)
break;
if (_highestIndex < q.index) {
_highestIndex = q.index;
}
if (q.index == index) {
addrIndex = addr;
}
addr += 4 + roundQuad(q.length);
if (addr > (_page + PAGE_SIZE))
break;
}
if (addrIndex != 0) {
*address = addrIndex;
return E_OK;
}
*address = 0;
return ENOENT;
}
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
Erc Flash::findValue(const char* key, uint16_t* index) {
Quad q;
Erc erc;
uint32_t addr = _page + 8;
uint32_t strLen = strlen(key);
while (true) {
erc = read(addr, &q.w);
if (q.index == 0xFFFF)
break;
if (q.length == strLen) {
char szKey[40];
uint32_t i;
uint32_t length = sizeof(szKey);
for (i = 0; i < q.length && i < length; i++) {
erc = read(addr + 4 + i, (uint8_t*) (szKey + i));
if (erc)
return erc;
}
szKey[i] = '\0';
if ( os_strcmp((const char*) szKey, key) == 0) {
*index = q.index;
return E_OK;
}
}
addr += 4 + roundQuad(q.length);
if (addr > (_page + PAGE_SIZE))
break;
}
return ENOENT;
}
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
Erc Flash::get(uint16_t index, uint8_t* data, uint16_t* maxLength) {
uint32_t address;
findIndex(index, &address);
if (address == 0)
return ENOENT;
Quad q;
Erc erc = read(address, &q.w);
uint32_t i, j;
Quad qd;
for (i = 0; i < q.length; i += 4) {
erc = read(address + 4 + i, &qd.w);
if (erc)
return ENOENT;
for (j = 0; i + j < q.length; j++) {
data[i + j] = qd.b[j];
}
}
*maxLength = q.length;
return E_OK;
}
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
/*
Erc get(const char* key,char* value,uint16_t* maxLength);*/
Erc Flash::put(const char* key, const char* value) {
return put(key, (uint8_t*) value, strlen(value));
}
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
Erc Flash::put(const char* key, uint8_t* value, uint16_t length) {
uint16_t index;
Erc erc;
if (findValue(key, &index) == ENOENT) {
index = 0xFFFC & (_highestIndex + 2);
erc = put(index, (uint8_t*) key, strlen(key));
if (erc)
return erc;
}
erc = put(index + 1, value, length);
return erc;
}
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
Erc Flash::get(const char* key, uint8_t* value, uint16_t* maxLength) {
uint16_t len = *maxLength;
uint16_t idx;
Erc erc = findValue(key, &idx);
if (erc)
return erc;
erc = get(idx+1, (uint8_t*) value, &len);
if (erc)
return erc;
*maxLength = len;
return E_OK;
}
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
Erc Flash::get(const char* key, char* value, uint16_t* maxLength) {
uint16_t len = *maxLength - 1;
uint16_t idx;
Erc erc = findValue(key, &idx);
if (erc)
return erc;
erc = get(idx+1, (uint8_t*) value, &len);
if (erc)
return erc;
value[len] = '\0';
*maxLength = len;
return E_OK;
}