forked from sebnil/DueFlashStorage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DueFlashStorage.cpp
executable file
·90 lines (73 loc) · 2.45 KB
/
DueFlashStorage.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
#include "DueFlashStorage.h"
DueFlashStorage::DueFlashStorage() {
uint32_t retCode;
/* Initialize flash: 6 wait states for flash writing. */
retCode = flash_init(FLASH_ACCESS_MODE_128, 6);
if (retCode != FLASH_RC_OK) {
_FLASH_DEBUG("Flash init failed\n");
}
}
byte DueFlashStorage::read(uint32_t address) {
return FLASH_START[address];
}
byte* DueFlashStorage::readAddress(uint32_t address) {
return FLASH_START+address;
}
boolean DueFlashStorage::write(uint32_t address, byte value) {
uint32_t retCode;
uint32_t byteLength = 1;
byte *data;
retCode = flash_unlock((uint32_t)FLASH_START+address, (uint32_t)FLASH_START+address + byteLength - 1, 0, 0);
if (retCode != FLASH_RC_OK) {
_FLASH_DEBUG("Failed to unlock flash for write\n");
return false;
}
// write data
retCode = flash_write((uint32_t)FLASH_START+address, &value, byteLength, 1);
//retCode = flash_write((uint32_t)FLASH_START, data, byteLength, 1);
if (retCode != FLASH_RC_OK) {
_FLASH_DEBUG("Flash write failed\n");
return false;
}
// Lock page
retCode = flash_lock((uint32_t)FLASH_START+address, (uint32_t)FLASH_START+address + byteLength - 1, 0, 0);
if (retCode != FLASH_RC_OK) {
_FLASH_DEBUG("Failed to lock flash page\n");
return false;
}
return true;
}
boolean DueFlashStorage::write(uint32_t address, byte *data, uint32_t dataLength) {
uint32_t retCode;
if ((uint32_t)FLASH_START+address < IFLASH1_ADDR) {
_FLASH_DEBUG("Flash write address too low\n");
return false;
}
if ((uint32_t)FLASH_START+address >= (IFLASH1_ADDR + IFLASH1_SIZE)) {
_FLASH_DEBUG("Flash write address too high\n");
return false;
}
if (((uint32_t)FLASH_START+address & 3) != 0) {
_FLASH_DEBUG("Flash start address must be on four byte boundary\n");
return false;
}
// Unlock page
retCode = flash_unlock((uint32_t)FLASH_START+address, (uint32_t)FLASH_START+address + dataLength - 1, 0, 0);
if (retCode != FLASH_RC_OK) {
_FLASH_DEBUG("Failed to unlock flash for write\n");
return false;
}
// write data
retCode = flash_write((uint32_t)FLASH_START+address, data, dataLength, 1);
if (retCode != FLASH_RC_OK) {
_FLASH_DEBUG("Flash write failed\n");
return false;
}
// Lock page
retCode = flash_lock((uint32_t)FLASH_START+address, (uint32_t)FLASH_START+address + dataLength - 1, 0, 0);
if (retCode != FLASH_RC_OK) {
_FLASH_DEBUG("Failed to lock flash page\n");
return false;
}
return true;
}