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
61
62
65
66
70
71
74
75
76
77
80
81
84
85
86
89
90
93
94
99
100
101
102
103
104
105
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
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
179
180
181
182
183
184
185
186
187
188
192
193
194
195
196
197
207
208
209
210
211
212
213
214
215
217
218
219
220
221
222
223
227
228
229
230
231
232
236
237
247
248
249
250
251
252
253
254
255
256
257
258
259
260
264
265
266
267
268
269
270
271
272
273
277
278
279
280
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
332
333
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
366
367
370
373
374
377
378
381
382
385
386
/* ... */
#include "stm324xg_eval_eeprom.h"
/* ... */
/* ... */
/* ... */
/* ... */
__IO uint32_t EEPROMTimeout = EEPROM_READ_TIMEOUT;
__IO uint16_t EEPROMDataRead;
__IO uint8_t EEPROMDataWrite;
/* ... */
/* ... */
static uint32_t EEPROM_WritePage(uint8_t* pBuffer, uint16_t WriteAddr, uint8_t* NumByteToWrite);
static uint32_t EEPROM_WaitEepromStandbyState(void);
/* ... */
/* ... */
/* ... */
uint32_t BSP_EEPROM_Init(void)
{
EEPROM_IO_Init();
if(EEPROM_IO_IsDeviceReady(EEPROM_I2C_ADDRESS, EEPROM_MAX_TRIALS) != HAL_OK)
{
return EEPROM_FAIL;
}if (EEPROM_IO_IsDeviceReady(EEPROM_I2C_ADDRESS, EEPROM_MAX_TRIALS) != HAL_OK) { ... }
return EEPROM_OK;
}{ ... }
/* ... */
uint32_t BSP_EEPROM_ReadBuffer(uint8_t* pBuffer, uint16_t ReadAddr, uint16_t* NumByteToRead)
{
uint32_t buffersize = *NumByteToRead;
EEPROMDataRead = *NumByteToRead;
if(EEPROM_IO_ReadData(EEPROM_I2C_ADDRESS, ReadAddr, pBuffer, buffersize) != HAL_OK)
{
BSP_EEPROM_TIMEOUT_UserCallback();
return EEPROM_FAIL;
}if (EEPROM_IO_ReadData(EEPROM_I2C_ADDRESS, ReadAddr, pBuffer, buffersize) != HAL_OK) { ... }
return EEPROM_OK;
}{ ... }
/* ... */
uint32_t BSP_EEPROM_WriteBuffer(uint8_t* pBuffer, uint16_t WriteAddr, uint16_t NumByteToWrite)
{
uint8_t numofpage = 0, numofsingle = 0, count = 0;
uint16_t addr = 0;
uint8_t dataindex = 0;
uint32_t status = EEPROM_OK;
addr = WriteAddr % EEPROM_PAGESIZE;
count = EEPROM_PAGESIZE - addr;
numofpage = NumByteToWrite / EEPROM_PAGESIZE;
numofsingle = NumByteToWrite % EEPROM_PAGESIZE;
if(addr == 0)
{
if(numofpage == 0)
{
dataindex = numofsingle;
status = EEPROM_WritePage(pBuffer, WriteAddr, (uint8_t*)(&dataindex));
if(status != EEPROM_OK)
{
return status;
}if (status != EEPROM_OK) { ... }
}if (numofpage == 0) { ... }
else
{
while(numofpage--)
{
dataindex = EEPROM_PAGESIZE;
status = EEPROM_WritePage(pBuffer, WriteAddr, (uint8_t*)(&dataindex));
if(status != EEPROM_OK)
{
return status;
}if (status != EEPROM_OK) { ... }
WriteAddr += EEPROM_PAGESIZE;
pBuffer += EEPROM_PAGESIZE;
}while (numofpage--) { ... }
if(numofsingle!=0)
{
dataindex = numofsingle;
status = EEPROM_WritePage(pBuffer, WriteAddr, (uint8_t*)(&dataindex));
if(status != EEPROM_OK)
{
return status;
}if (status != EEPROM_OK) { ... }
}if (numofsingle!=0) { ... }
}else { ... }
}if (addr == 0) { ... }
else
{
if(numofpage == 0)
{
/* ... */
if(NumByteToWrite > count)
{
dataindex = count;
status = EEPROM_WritePage(pBuffer, WriteAddr, (uint8_t*)(&dataindex));
if(status != EEPROM_OK)
{
return status;
}if (status != EEPROM_OK) { ... }
dataindex = (NumByteToWrite - count);
status = EEPROM_WritePage((uint8_t*)(pBuffer + count), (WriteAddr + count), (uint8_t*)(&dataindex));
if(status != EEPROM_OK)
{
return status;
}if (status != EEPROM_OK) { ... }
}if (NumByteToWrite > count) { ... }
else
{
dataindex = numofsingle;
status = EEPROM_WritePage(pBuffer, WriteAddr, (uint8_t*)(&dataindex));
if(status != EEPROM_OK)
{
return status;
}if (status != EEPROM_OK) { ... }
}else { ... }
}if (numofpage == 0) { ... }
else
{
NumByteToWrite -= count;
numofpage = NumByteToWrite / EEPROM_PAGESIZE;
numofsingle = NumByteToWrite % EEPROM_PAGESIZE;
if(count != 0)
{
dataindex = count;
status = EEPROM_WritePage(pBuffer, WriteAddr, (uint8_t*)(&dataindex));
if(status != EEPROM_OK)
{
return status;
}if (status != EEPROM_OK) { ... }
WriteAddr += count;
pBuffer += count;
}if (count != 0) { ... }
while(numofpage--)
{
dataindex = EEPROM_PAGESIZE;
status = EEPROM_WritePage(pBuffer, WriteAddr, (uint8_t*)(&dataindex));
if(status != EEPROM_OK)
{
return status;
}if (status != EEPROM_OK) { ... }
WriteAddr += EEPROM_PAGESIZE;
pBuffer += EEPROM_PAGESIZE;
}while (numofpage--) { ... }
if(numofsingle != 0)
{
dataindex = numofsingle;
status = EEPROM_WritePage(pBuffer, WriteAddr, (uint8_t*)(&dataindex));
if(status != EEPROM_OK)
{
return status;
}if (status != EEPROM_OK) { ... }
}if (numofsingle != 0) { ... }
}else { ... }
}else { ... }
return EEPROM_OK;
}{ ... }
/* ... */
static uint32_t EEPROM_WritePage(uint8_t* pBuffer, uint16_t WriteAddr, uint8_t* NumByteToWrite)
{
uint32_t buffersize = *NumByteToWrite;
uint32_t status = EEPROM_OK;
EEPROMDataWrite = *NumByteToWrite;
if(EEPROM_IO_WriteData(EEPROM_I2C_ADDRESS, WriteAddr, pBuffer, buffersize) != HAL_OK)
{
BSP_EEPROM_TIMEOUT_UserCallback();
status = EEPROM_FAIL;
}if (EEPROM_IO_WriteData(EEPROM_I2C_ADDRESS, WriteAddr, pBuffer, buffersize) != HAL_OK) { ... }
while(EEPROM_WaitEepromStandbyState() != EEPROM_OK)
{
return EEPROM_FAIL;
}while (EEPROM_WaitEepromStandbyState() != EEPROM_OK) { ... }
return status;
}{ ... }
/* ... */
static uint32_t EEPROM_WaitEepromStandbyState(void)
{
if(EEPROM_IO_IsDeviceReady(EEPROM_I2C_ADDRESS, EEPROM_MAX_TRIALS) != HAL_OK)
{
BSP_EEPROM_TIMEOUT_UserCallback();
return EEPROM_TIMEOUT;
}if (EEPROM_IO_IsDeviceReady(EEPROM_I2C_ADDRESS, EEPROM_MAX_TRIALS) != HAL_OK) { ... }
return EEPROM_OK;
}{ ... }
/* ... */
__weak void BSP_EEPROM_TIMEOUT_UserCallback(void)
{
}{ ... }
/* ... */
/* ... */
/* ... */
/* ... */