forked from arpruss/USBCECAdapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheeprom8.ino
executable file
·128 lines (101 loc) · 3.12 KB
/
eeprom8.ino
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
#include <EEPROM.h>
#include <flash_stm32.h>
#ifndef EEPROM_PAGE_SIZE
#define EEPROM_PAGE_SIZE 0x400
#endif
// This is a module that lets you store up to 255 byte-long configuration variables numbered from 0 to 254 in a single
// flash page.
//
// The storage method is incompatible with the one implemented by the EEPROM-emulation library, as it's optimized
// for our single-byte usage case.
uint32_t pageBase;
uint8 storage[254];
static boolean invalid = true;
#define EEPROM8_MEMORY_SIZE (2*EEPROM_PAGE_SIZE)
#define GET_BYTE(address) (*(__IO uint8_t*)(address))
#define GET_HALF_WORD(address) (*(__IO uint16_t*)(address))
#define GET_WORD(address) (*(__IO uint32_t*)(address))
#define EEPROM8_MAGIC (uint32_t)0x1b70f1ce
static bool erasePage(uint32_t base) {
bool success;
FLASH_Unlock();
success = ( FLASH_COMPLETE == FLASH_ErasePage(base) );
success = success &&
FLASH_COMPLETE == FLASH_ProgramHalfWord(base, (uint16_t)EEPROM8_MAGIC) &&
FLASH_COMPLETE == FLASH_ProgramHalfWord(base+2, (uint16_t)(EEPROM8_MAGIC>>16));
FLASH_Lock();
return success && EEPROM8_MAGIC == GET_WORD(base);
}
/*
static bool erasePages() {
return erasePage(pageBase);
}
*/
uint8 EEPROM8_getValue(uint8_t variable) {
if (variable>=255)
return 0;
else
return storage[variable];
}
static bool writeHalfWord(uint32_t address, uint16_t halfWord) {
if (!(pageBase <= address && address+1 < pageBase + EEPROM_PAGE_SIZE ))
return false;
FLASH_Unlock();
boolean success = FLASH_COMPLETE == FLASH_ProgramHalfWord(address, halfWord);
FLASH_Lock();
return success && GET_HALF_WORD(address) == halfWord;
}
boolean EEPROM8_storeValue(uint8_t variable, uint8_t value) {
if (invalid || variable >= 255)
return false;
if (storage[variable] == value)
return true;
storage[variable] = value;
for (uint32_t offset = 4 ; offset < EEPROM_PAGE_SIZE ; offset+=2) {
if (GET_HALF_WORD(pageBase+offset) == 0xFFFF) {
return writeHalfWord(pageBase+offset, variable | ((uint16_t)value<<8));
}
}
// page is full
if (!erasePage(pageBase))
return false;
uint32 offset = 4;
bool err = false;
for (uint32_t variable = 0 ; variable < 255; variable++)
if (0 != storage[variable]) {
if (!writeHalfWord(pageBase+offset, variable | ((uint16_t)storage[variable]<<8))) {
err = true;
offset += 2;
}
else {
offset += 2;
}
}
return !err;
}
static void EEPROM8_reset(void) {
if (erasePage(pageBase)) {
invalid = false;
}
else {
invalid = true;
}
for(uint32_t i=0; i<255; i++)
storage[i] = 0;
}
void EEPROM8_init(void) {
uint32_t flashSize = *(uint16 *) (0x1FFFF7E0);
pageBase = 0x8000000 + flashSize * 1024 - EEPROM_PAGE_SIZE;
for(uint32_t i=0; i<255; i++)
storage[i] = 0;
if (EEPROM8_MAGIC != GET_WORD(pageBase) && ! erasePage(pageBase) ) {
invalid = true;
return;
}
for (uint32_t offset = 4 ; offset < EEPROM_PAGE_SIZE ; offset+=2) {
uint8_t i = GET_BYTE(pageBase+offset);
if (i < 255)
storage[i] = GET_BYTE(pageBase+offset+1);
}
invalid = false;
}