-
Notifications
You must be signed in to change notification settings - Fork 3
/
eeprom.c
256 lines (240 loc) · 8.43 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
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
#include "eeprom.h"
#include "eepromConfig.h"
//##########################################################################################################
#define _X_BYTE_ROUNDUP(Dividend,Divisor) ((Dividend/Divisor)+(Dividend%Divisor==0?0:1))
//##########################################################################################################
//copy page to transfer page excluding the bad data
static bool EE_PageTransfer(uint32_t PageAddress,uint16_t BadDataOffset,uint16_t BadDataNum);
static uint32_t EE_FindPageAddressOfVirtualAddress(uint16_t VirtualAddress);
static uint16_t EE_FindVirtualAddressOffset(uint16_t VirtualAddress);
//##########################################################################################################
bool EE_FormatPage(uint32_t PageAddress)
{
uint32_t error;
FLASH_EraseInitTypeDef flashErase;
HAL_FLASH_Unlock();
flashErase.NbPages=1;
#if(_CORTEX_!=M0)
flashErase.Banks = FLASH_BANK_1;
#endif
flashErase.TypeErase = FLASH_TYPEERASE_PAGES;
flashErase.PageAddress = PageAddress;
if(HAL_FLASHEx_Erase(&flashErase,&error)==HAL_OK)
{
HAL_FLASH_Lock();
if(error != 0xFFFFFFFF)
return false;
else
return true;
}
HAL_FLASH_Lock();
return false;
}
//##########################################################################################################
bool EE_Read(uint16_t VirtualAddress, uint32_t* Data)
{
if(VirtualAddress >= _EEPROM_End_VirtualAddr)
return false;
*Data = (*(__IO uint32_t*)((VirtualAddress*4)+_EEPROM_START_FLASH_PAGE_ADDRESS));
return true;
}
//##########################################################################################################
bool EE_Reads(uint16_t StartVirtualAddress,uint16_t HowMuchToRead,uint32_t* Data)
{
if((StartVirtualAddress+HowMuchToRead) > _EEPROM_End_VirtualAddr)
return false;
for(uint16_t i=StartVirtualAddress ; i<HowMuchToRead+StartVirtualAddress ; i++)
{
*Data = (*(__IO uint32_t*)((i*4)+_EEPROM_START_FLASH_PAGE_ADDRESS));
Data++;
}
return true;
}
//##########################################################################################################
bool EE_ReadBytes(uint16_t StartVirtualAddress,uint16_t HowManyBytesToRead,void* Data)
{
union{uint8_t sbyte[4];uint32_t fbyte;}temp;
uint8_t *DataP = (uint8_t *)Data;
//check address validation
if((StartVirtualAddress + _X_BYTE_ROUNDUP(HowManyBytesToRead,4)) > _EEPROM_End_VirtualAddr)
return false;
for(uint16_t i=StartVirtualAddress , currentbyte=0 ; i < _X_BYTE_ROUNDUP(HowManyBytesToRead,4)+StartVirtualAddress ; i++)
{
temp.fbyte = (*(__IO uint32_t*)((i*4)+_EEPROM_START_FLASH_PAGE_ADDRESS));
// Fill bytes
for(uint8_t j=0;j<4;j++)
{
if (currentbyte<HowManyBytesToRead)
{
*DataP = temp.sbyte[j];
++currentbyte;
if (currentbyte<HowManyBytesToRead)
DataP++;
}
else
break;
}
}
return true;
}
//##########################################################################################################
bool EE_Write(uint16_t VirtualAddress, uint32_t Data)
{
if(VirtualAddress >= _EEPROM_End_VirtualAddr)
return false;
//If flash space is not empty make it empty
if((*(__IO uint32_t*)((VirtualAddress*4)+_EEPROM_START_FLASH_PAGE_ADDRESS)) != 0xFFFFFFFF)
{
//create backup of that page excluding bad data
EE_PageTransfer(EE_FindPageAddressOfVirtualAddress(VirtualAddress),EE_FindVirtualAddressOffset(VirtualAddress),1);
}
//Write data
if(Data!=0xFFFFFFFF)
{
HAL_FLASH_Unlock();
if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD,(VirtualAddress*4)+_EEPROM_START_FLASH_PAGE_ADDRESS,(uint64_t)Data)==HAL_OK)
{
HAL_FLASH_Lock();
return true;
}
else
{
HAL_FLASH_Lock();
return false;
}
}
HAL_FLASH_Lock();
return true;
}
//##########################################################################################################
bool EE_Writes(uint16_t StartVirtualAddress,uint16_t HowMuchToWrite,uint32_t* Data)
{
//check address validation
if((StartVirtualAddress+HowMuchToWrite) > _EEPROM_End_VirtualAddr)
return false;
//If flash space is not empty make it empty
for(uint16_t i=StartVirtualAddress ; i<StartVirtualAddress + HowMuchToWrite ; i++)
{
if((*(__IO uint32_t*)((i*4)+_EEPROM_START_FLASH_PAGE_ADDRESS)) != 0xFFFFFFFF)
{
//create backup of that page excluding bad data
EE_PageTransfer(EE_FindPageAddressOfVirtualAddress(StartVirtualAddress),EE_FindVirtualAddressOffset(StartVirtualAddress),HowMuchToWrite);
break;
}
}
//Write new data
HAL_FLASH_Unlock();
for(uint16_t i=StartVirtualAddress ; i<StartVirtualAddress + HowMuchToWrite ; i++, Data++)
{
if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD,(i*4)+_EEPROM_START_FLASH_PAGE_ADDRESS,(uint64_t)*Data)!=HAL_OK)
{
HAL_FLASH_Lock();
return false;
}
}
HAL_FLASH_Lock();
return true;
}
//##########################################################################################################
bool EE_WriteBytes(uint16_t StartVirtualAddress,uint16_t HowManyBytesToWrite,void* Data)
{
union{uint8_t sbyte[4];uint32_t fbyte;}temp;
uint8_t *DataP = (uint8_t *)Data;
//check address validation
if((StartVirtualAddress+_X_BYTE_ROUNDUP(HowManyBytesToWrite,4)) > _EEPROM_End_VirtualAddr)
return false;
//If flash space is not empty make it empty
for(uint16_t i=StartVirtualAddress ; i<StartVirtualAddress + _X_BYTE_ROUNDUP(HowManyBytesToWrite,4) ; i++)
{
if((*(__IO uint32_t*)((i*4)+_EEPROM_START_FLASH_PAGE_ADDRESS)) != 0xFFFFFFFF)
{
//create backup of that page excluding bad data
EE_PageTransfer(EE_FindPageAddressOfVirtualAddress(StartVirtualAddress),EE_FindVirtualAddressOffset(StartVirtualAddress),_X_BYTE_ROUNDUP(HowManyBytesToWrite,4));
break;
}
}
//Write new data
HAL_FLASH_Unlock();
for(uint16_t i=StartVirtualAddress, currentbyte=0 ; i<StartVirtualAddress + _X_BYTE_ROUNDUP(HowManyBytesToWrite,4) ; i++)
{
//Convert Bytes to uint32_t and pad the end if more than indicated size
for(uint8_t j=0;j<4;j++)
{
if (currentbyte<HowManyBytesToWrite)
{
temp.sbyte[j] = *DataP;
++currentbyte;
if (currentbyte<HowManyBytesToWrite)
DataP++;
}
else
temp.sbyte[j] = 0;
}
// program
if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD,(i*4)+_EEPROM_START_FLASH_PAGE_ADDRESS,(uint64_t)temp.fbyte)!=HAL_OK)
{
HAL_FLASH_Lock();
return false;
}
}
HAL_FLASH_Lock();
return true;
}
//##########################################################################################################
static bool EE_PageTransfer(uint32_t PageAddress,uint16_t BadDataOffset,uint16_t BadDataNum)
{
uint32_t tempdata;
//format transfer page
if(EE_FormatPage(_EEPROM_TRANSFER_PAGE_ADDRESS)==false)
return false;
//Write to transfer page but exclude the bad data
HAL_FLASH_Unlock();
for(uint16_t i=0 ; i<_EEPROM_FLASH_PAGE_SIZE/4 ; i++)
{
if(i>=BadDataOffset && i<BadDataOffset+BadDataNum)
{
continue;
}
//read original data
tempdata = (*(__IO uint32_t*)((i*4)+PageAddress));
//write to transfer page
if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD,(i*4)+_EEPROM_TRANSFER_PAGE_ADDRESS,(uint64_t)tempdata)!=HAL_OK)
{
HAL_FLASH_Lock();
return false;
}
}
//format original page
if(EE_FormatPage(PageAddress)==false)
return false;
HAL_FLASH_Unlock();
//Write back to Original Page
for(uint16_t i=0 ; i<_EEPROM_FLASH_PAGE_SIZE/4 ; i++)
{
tempdata = (*(__IO uint32_t*)((i*4)+_EEPROM_TRANSFER_PAGE_ADDRESS));
if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD,(i*4)+PageAddress,(uint64_t)tempdata)!=HAL_OK)
{
HAL_FLASH_Lock();
return false;
}
}
HAL_FLASH_Lock();
return true;
}
//##########################################################################################################
static uint32_t EE_FindPageAddressOfVirtualAddress(uint16_t VirtualAddress)
{
uint32_t temp;
temp = _EEPROM_Num_VirtualAdd_IN_PAGE;
temp = _EEPROM_START_FLASH_PAGE_ADDRESS + (VirtualAddress/temp)*((uint32_t)FLASH_PAGE_SIZE);
return temp;
}
//##########################################################################################################
static uint16_t EE_FindVirtualAddressOffset(uint16_t VirtualAddress)
{
uint16_t temp;
temp = _EEPROM_Num_VirtualAdd_IN_PAGE;
temp = (VirtualAddress % temp);
return temp;
}
//##########################################################################################################