-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathflash.c
More file actions
478 lines (388 loc) · 14.3 KB
/
flash.c
File metadata and controls
478 lines (388 loc) · 14.3 KB
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
#include "flash.h"
#include "stm32f4xx_flash.h"
//#define DEBUG 1
#include <string.h>
#ifdef DEBUG
#include "ch.h"
#include "printf.h"
#include "globalVar.h"
#endif
/* Private typedef -----------------------------------------------------------*/
typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
/* Private define ------------------------------------------------------------*/
#define FLASH_USER_START_ADDR ADDR_FLASH_SECTOR_11 /* Start @ of user Flash area */
#define FLASH_USER_END_ADDR ((ADDR_FLASH_SECTOR_11+(128*1024))-1) /* End @ of user Flash area */
/* Base address of the Flash sectors */
#define ADDR_FLASH_SECTOR_0 (0x08000000) /* Base @ of Sector 0, 16 Kbytes */
#define ADDR_FLASH_SECTOR_1 (0x08004000) /* Base @ of Sector 1, 16 Kbytes */
#define ADDR_FLASH_SECTOR_2 (0x08008000) /* Base @ of Sector 2, 16 Kbytes */
#define ADDR_FLASH_SECTOR_3 (0x0800C000) /* Base @ of Sector 3, 16 Kbytes */
#define ADDR_FLASH_SECTOR_4 (0x08010000) /* Base @ of Sector 4, 64 Kbytes */
#define ADDR_FLASH_SECTOR_5 (0x08020000) /* Base @ of Sector 5, 128 Kbytes */
#define ADDR_FLASH_SECTOR_6 (0x08040000) /* Base @ of Sector 6, 128 Kbytes */
#define ADDR_FLASH_SECTOR_7 (0x08060000) /* Base @ of Sector 7, 128 Kbytes */
#define ADDR_FLASH_SECTOR_8 (0x08080000) /* Base @ of Sector 8, 128 Kbytes */
#define ADDR_FLASH_SECTOR_9 (0x080A0000) /* Base @ of Sector 9, 128 Kbytes */
#define ADDR_FLASH_SECTOR_10 (0x080C0000) /* Base @ of Sector 10, 128 Kbytes */
#define ADDR_FLASH_SECTOR_11 (0x080E0000) /* Base @ of Sector 11, 128 Kbytes */
#define DATA_32 ((uint32_t)0x12345678)
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
static uint32_t GetSector(const uint32_t Address);
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
int testFlash(void)
{
uint32_t StartSector = 0, EndSector = 0;
uint32_t Address = 0;
volatile uint32_t data32 = 0 , MemoryProgramStatus = 0 ;
/* Unlock the Flash to enable the flash control register access *************/
FLASH_Unlock();
/* Erase the user Flash area
(area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
/* Clear pending flags (if any) */
FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR);
/* Get the number of the start and end sectors */
StartSector = GetSector(FLASH_USER_START_ADDR);
EndSector = GetSector(FLASH_USER_END_ADDR);
uint32_t sector = StartSector;
do
{
/* Device voltage range supposed to be [2.7V to 3.6V], the operation will
be done by word */
if (FLASH_EraseSector(sector, VoltageRange_3) != FLASH_COMPLETE)
{
/* Error occurred while sector erase.
User can add here some code to deal with this error */
FLASH_Lock();
return (ERASE_ERR);
} else {
sector+=8;
}
} while (sector< EndSector);
/* Program the user Flash area word by word
(area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
Address = FLASH_USER_START_ADDR;
while (Address < FLASH_USER_END_ADDR)
{
if (FLASH_ProgramWord(Address, DATA_32) == FLASH_COMPLETE)
{
Address += 4;
}
else
{
FLASH_Lock();
return (PROGRAM_ERR);
}
}
/* Lock the Flash to disable the flash control register access (recommended
to protect the FLASH memory against possible unwanted operation) *********/
FLASH_Lock();
/* Check if the programmed data is OK
MemoryProgramStatus = 0: data programmed correctly
MemoryProgramStatus != 0: number of words not programmed correctly ******/
Address = FLASH_USER_START_ADDR;
MemoryProgramStatus = 0x0;
while (Address < FLASH_USER_END_ADDR)
{
data32 = *(__IO uint32_t*)Address;
if (data32 != DATA_32)
{
MemoryProgramStatus++;
}
Address += 4;
}
return (MemoryProgramStatus);
}
ErrorCond eepromErase (void)
{
const uint32_t flashStartAddr = FLASH_USER_START_ADDR;
const uint32_t flashEndAddr = FLASH_USER_END_ADDR;
const uint32_t StartSector = GetSector(flashStartAddr);
const uint32_t EndSector = GetSector(flashEndAddr);
uint32_t sector = StartSector;
/* Unlock the Flash to enable the flash control register access *************/
FLASH_Unlock();
/* Erase the user Flash area
(area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
/* Clear pending flags (if any) */
FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR);
do
{
/* Device voltage range supposed to be [2.7V to 3.6V], the operation will
be done by word */
if (FLASH_EraseSector(sector, VoltageRange_3) != FLASH_COMPLETE)
{
/* Error occurred while sector erase.
User can add here some code to deal with this error */
FLASH_Lock();
return (ERASE_ERR);
} else {
sector+=8;
}
} while (sector< EndSector);
FLASH_Lock();
return (PROG_OK);
}
ErrorCond memcopyToFlash(const void *from, const void *flashAddr,
size_t length, bool_t eraseBefore)
{
const uint32_t flashStartAddr = (flashAddr != NULL) ? (const uint32_t) flashAddr
: FLASH_USER_START_ADDR;
const size_t alignOffset = (4-(length%4)) % 4;
const uint32_t flashEndAddr = flashStartAddr + length + alignOffset;
const uint32_t ramBeginAddr = (uint32_t) from;
const uint32_t ramEndAddr = ramBeginAddr+length;
volatile uint32_t data32 = 0 ;
uint32_t StartSector = 0, EndSector = 0;
if (flashEndAddr >= FLASH_USER_END_ADDR)
return (SECTOR_FULL_ERR);
/* Unlock the Flash to enable the flash control register access *************/
FLASH_Unlock();
#ifdef DEBUG
DebugTrace ("FLASH UNlock ;; len=%d alignOffset=%d", length, alignOffset);
#endif
/* Erase the user Flash area
(area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
/* Clear pending flags (if any) */
FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR);
if (eraseBefore) {
/* Get the number of the start and end sectors */
StartSector = GetSector(flashStartAddr);
EndSector = GetSector(flashEndAddr);
#ifdef DEBUG
DebugTrace ("startsector=%d, endsector=%d\n", StartSector, EndSector);
#endif
uint32_t sector = StartSector;
do
{
/* Device voltage range supposed to be [2.7V to 3.6V], the operation will
be done by word */
if (FLASH_EraseSector(sector, VoltageRange_3) != FLASH_COMPLETE)
{
/* Error occurred while sector erase.
User can add here some code to deal with this error */
FLASH_Lock();
return (ERASE_ERR);
} else {
sector+=8;
}
} while (sector< EndSector);
/* Program the user Flash area word by word
(area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
}
uint32_t flashPtr = flashStartAddr;
uint32_t ramPtr = ramBeginAddr;
while (flashPtr < flashEndAddr) {
// if we are on a non aligned length, we should program last bytes
if ((ramEndAddr - ramPtr) < 4) {
uint32_t pos=0, lastword=0;
char *rptr = (char *) ramPtr;
while (rptr < (char *) ramEndAddr) {
lastword |= (((*rptr) << pos) & (0xff << pos));
rptr++;
pos+=8;
}
#ifdef DEBUG
DebugTrace ("DBG> flash *LAST incomplete* value 0x%x @ 0x%x", lastword, flashPtr);
#endif
if (FLASH_ProgramWord (flashPtr, lastword) != FLASH_COMPLETE) {
FLASH_Lock();
return (PROGRAM_ERR);
} else {
flashPtr += 4;
}
} else {
#ifdef DEBUG
DebugTrace ("DBG> flash value 0x%x @ 0x%x", *((uint32_t *) ramPtr),
flashPtr);
#endif
if (FLASH_ProgramWord (flashPtr, *((uint32_t *) ramPtr) )== FLASH_COMPLETE) {
flashPtr += 4;
ramPtr += 4;
} else {
FLASH_Lock();
return (PROGRAM_ERR);
}
}
}
/* Lock the Flash to disable the flash control register access (recommended
to protect the FLASH memory against possible unwanted operation) *********/
FLASH_Lock();
#ifdef DEBUG
DebugTrace ("FLASH LOCK");
#endif
/* Check if the programmed data is OK
MemoryProgramStatus = 0: data programmed correctly
MemoryProgramStatus != 0: number of words not programmed correctly ******/
flashPtr = flashStartAddr;
ramPtr = ramBeginAddr;
while (flashPtr < flashEndAddr) {
#ifdef DEBUG
DebugTrace ("verify address 0x%x", flashPtr);
chThdSleepMilliseconds(100);
#endif
data32 = *(__IO uint32_t*) flashPtr;
// if we are on a non aligned length, we should test last bytes
if ((ramEndAddr - ramPtr) < 4) {
uint32_t pos=0, lastword=0;
char *rptr = (char *) ramPtr;
while (rptr < (char *) ramEndAddr) {
lastword |= (((*rptr) << pos) & (0xff << pos));
pos+=8;
rptr++;
}
flashPtr += 4;
if (data32 !=lastword) {
return VERIFY_READ_ERROR;
}
} else {
if (data32 != *((uint32_t *) ramPtr) ) {
return VERIFY_READ_ERROR;
}
flashPtr += 4;
ramPtr += 4;
}
}
return PROG_OK;
}
ErrorCond eepromStore (FlashStructType flashStructType, const void *data, uint32_t len)
{
const objInEeprom *flashStartAddr = (objInEeprom *) FLASH_USER_START_ADDR;
objInEeprom *currentObjInFlash = (objInEeprom *) flashStartAddr;
objInEeprom objToFlash;
ErrorCond retval = PROG_OK;
#ifdef DEBUG
DebugTrace ("before loop currentObjInFlash = 0x%x", currentObjInFlash);
chThdSleepMilliseconds(100);
#endif
while ((uint32_t) currentObjInFlash->nextObjPtr != 0xffffffff) {
currentObjInFlash = currentObjInFlash->nextObjPtr;
#ifdef DEBUG
DebugTrace ("IN loop currentObjInFlash = 0x%x", currentObjInFlash);
chThdSleepMilliseconds(100);
#endif
}
#ifdef DEBUG
DebugTrace ("currentObjInFlash = 0x%x", currentObjInFlash);
chThdSleepMilliseconds(100);
#endif
// now currentObjInFlash is the first free address in flash
objToFlash.flashStructType = flashStructType;
const uint32_t ptrCurr = (uint32_t) currentObjInFlash;
const uint32_t alignOffset = (4-(len%4)) % 4;
const uint32_t ptrNext = ptrCurr+len+alignOffset+sizeof(objToFlash);
const void * ptrDataInFlash = (void *) (ptrCurr + sizeof (objToFlash));
objToFlash.nextObjPtr = (objInEeprom *) ptrNext;
objToFlash.dataLen = len;
objToFlash.ramAdress = (uint32_t) data;
#ifdef DEBUG
DebugTrace (" objToFlash.nextObjPtr = 0x%x", objToFlash.nextObjPtr);
chThdSleepMilliseconds(100);
#endif
retval = memcopyToFlash (&objToFlash, currentObjInFlash, sizeof (objToFlash), FALSE);
if (retval != PROG_OK) {
return retval;
}
return memcopyToFlash (data, ptrDataInFlash, len, FALSE);
}
ErrorCond eepromLoad (FlashStructType flashStructType, void *data,
uint32_t len)
{
const objInEeprom *flashStartAddr = (objInEeprom *) FLASH_USER_START_ADDR;
objInEeprom *currentObjInFlash = (objInEeprom *) flashStartAddr;
objInEeprom *lastTypeObject = currentObjInFlash->flashStructType == flashStructType ?
currentObjInFlash : NULL;
#ifdef DEBUG
DebugTrace ("before loop currentObjInFlash = 0x%x", currentObjInFlash);
chThdSleepMilliseconds(100);
#endif
while ((uint32_t) currentObjInFlash->nextObjPtr != 0xffffffff) {
currentObjInFlash = currentObjInFlash->nextObjPtr;
#ifdef DEBUG
DebugTrace ("IN loop currentObjInFlash = 0x%x", currentObjInFlash);
chThdSleepMilliseconds(100);
#endif
if (currentObjInFlash->flashStructType == flashStructType) {
lastTypeObject=currentObjInFlash;
}
}
if (lastTypeObject == NULL) {
return NOT_FOUND;
}
if (len != lastTypeObject->dataLen) {
return SIZES_DIFFER;
}
if ((uint32_t) data != lastTypeObject->ramAdress) {
return PROGRAM_MODIFIED;
}
memcpy (data, lastTypeObject->data, lastTypeObject->dataLen);
#ifdef DEBUG
DebugTrace ("memcopy 0x%x, 0x%x, %d",
data, lastTypeObject->data, lastTypeObject->dataLen);
chThdSleepMilliseconds(100);
#endif
return PROG_OK;
}
/**
* @brief Gets the sector of a given address
* @param None
* @retval The sector of a given address
*/
static uint32_t GetSector(const uint32_t Address)
{
uint32_t sector = 0;
if((Address < ADDR_FLASH_SECTOR_1) && (Address >= ADDR_FLASH_SECTOR_0))
{
sector = FLASH_Sector_0;
}
else if((Address < ADDR_FLASH_SECTOR_2) && (Address >= ADDR_FLASH_SECTOR_1))
{
sector = FLASH_Sector_1;
}
else if((Address < ADDR_FLASH_SECTOR_3) && (Address >= ADDR_FLASH_SECTOR_2))
{
sector = FLASH_Sector_2;
}
else if((Address < ADDR_FLASH_SECTOR_4) && (Address >= ADDR_FLASH_SECTOR_3))
{
sector = FLASH_Sector_3;
}
else if((Address < ADDR_FLASH_SECTOR_5) && (Address >= ADDR_FLASH_SECTOR_4))
{
sector = FLASH_Sector_4;
}
else if((Address < ADDR_FLASH_SECTOR_6) && (Address >= ADDR_FLASH_SECTOR_5))
{
sector = FLASH_Sector_5;
}
else if((Address < ADDR_FLASH_SECTOR_7) && (Address >= ADDR_FLASH_SECTOR_6))
{
sector = FLASH_Sector_6;
}
else if((Address < ADDR_FLASH_SECTOR_8) && (Address >= ADDR_FLASH_SECTOR_7))
{
sector = FLASH_Sector_7;
}
else if((Address < ADDR_FLASH_SECTOR_9) && (Address >= ADDR_FLASH_SECTOR_8))
{
sector = FLASH_Sector_8;
}
else if((Address < ADDR_FLASH_SECTOR_10) && (Address >= ADDR_FLASH_SECTOR_9))
{
sector = FLASH_Sector_9;
}
else if((Address < ADDR_FLASH_SECTOR_11) && (Address >= ADDR_FLASH_SECTOR_10))
{
sector = FLASH_Sector_10;
}
else/*(Address < FLASH_END_ADDR) && (Address >= ADDR_FLASH_SECTOR_11))*/
{
sector = FLASH_Sector_11;
}
return sector;
}