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
77
78
81
82
86
87
90
93
94
97
100
101
104
107
108
111
112
113
114
115
116
119
120
123
126
127
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
181
182
183
187
188
189
190
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
238
239
240
244
245
246
247
255
256
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
300
301
302
303
304
305
306
307
308
309
313
314
315
316
317
318
328
329
330
331
332
333
334
335
336
338
339
340
341
342
343
344
348
349
350
351
352
353
357
358
368
369
370
371
372
373
374
375
376
377
378
379
380
381
385
386
387
388
389
390
391
392
393
394
398
399
400
401
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
442
443
447
451
452
456
460
461
464
467
468
469
470
473
474
477
478
481
482
/* ... */
#include "stm32f429i_discovery_eeprom.h"
#ifdef EE_M24LR64
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
__IO uint16_t EEPROMAddress = 0;
__IO uint32_t EEPROMTimeout = EEPROM_READ_TIMEOUT;
__IO uint16_t EEPROMDataRead;
__IO uint8_t EEPROMDataWrite;
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
uint32_t BSP_EEPROM_Init(void)
{
EEPROM_IO_Init();
EEPROMAddress = EEPROM_I2C_ADDRESS_A01;
if (EEPROM_IO_IsDeviceReady(EEPROMAddress, EEPROM_MAX_TRIALS) != HAL_OK)
{
EEPROMAddress = EEPROM_I2C_ADDRESS_A02;
if (EEPROM_IO_IsDeviceReady(EEPROMAddress, EEPROM_MAX_TRIALS) != HAL_OK)
{
return EEPROM_FAIL;
}if (EEPROM_IO_IsDeviceReady(EEPROMAddress, EEPROM_MAX_TRIALS) != HAL_OK) { ... }
}if (EEPROM_IO_IsDeviceReady(EEPROMAddress, 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(EEPROMAddress, ReadAddr, pBuffer, buffersize) != HAL_OK)
{
return EEPROM_FAIL;
}if (EEPROM_IO_ReadData(EEPROMAddress, ReadAddr, pBuffer, buffersize) != HAL_OK) { ... }
EEPROMTimeout = HAL_GetTick() + EEPROM_READ_TIMEOUT;
while (EEPROMDataRead > 0)
{
if (HAL_GetTick() > EEPROMTimeout)
{
BSP_EEPROM_TIMEOUT_UserCallback();
return EEPROM_TIMEOUT;
}if (HAL_GetTick() > EEPROMTimeout) { ... }
}while (EEPROMDataRead > 0) { ... }
return EEPROM_OK;
}{ ... }
/* ... */
uint32_t BSP_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(EEPROMAddress, WriteAddr, pBuffer, buffersize) != HAL_OK)
{
status = EEPROM_FAIL;
}if (EEPROM_IO_WriteData(EEPROMAddress, WriteAddr, pBuffer, buffersize) != HAL_OK) { ... }
EEPROMTimeout = HAL_GetTick() + EEPROM_WRITE_TIMEOUT;
while (EEPROMDataWrite > 0)
{
if (HAL_GetTick() > EEPROMTimeout)
{
BSP_EEPROM_TIMEOUT_UserCallback();
return EEPROM_TIMEOUT;
}if (HAL_GetTick() > EEPROMTimeout) { ... }
}while (EEPROMDataWrite > 0) { ... }
if (BSP_EEPROM_WaitEepromStandbyState() != EEPROM_OK)
{
return EEPROM_FAIL;
}if (BSP_EEPROM_WaitEepromStandbyState() != EEPROM_OK) { ... }
return status;
}{ ... }
/* ... */
uint32_t BSP_EEPROM_WriteBuffer(uint8_t *pBuffer, uint16_t WriteAddr, uint16_t NumByteToWrite)
{
uint16_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 = BSP_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 = BSP_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 = BSP_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 = BSP_EEPROM_WritePage(pBuffer, WriteAddr, (uint8_t *)(&dataindex));
if (status != EEPROM_OK)
{
return status;
}if (status != EEPROM_OK) { ... }
dataindex = (NumByteToWrite - count);
status = BSP_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 = BSP_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 = BSP_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 = BSP_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 = BSP_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;
}{ ... }
/* ... */
uint32_t BSP_EEPROM_WaitEepromStandbyState(void)
{
if (EEPROM_IO_IsDeviceReady(EEPROMAddress, EEPROM_MAX_TRIALS) != HAL_OK)
{
BSP_EEPROM_TIMEOUT_UserCallback();
return EEPROM_TIMEOUT;
}if (EEPROM_IO_IsDeviceReady(EEPROMAddress, EEPROM_MAX_TRIALS) != HAL_OK) { ... }
return EEPROM_OK;
}{ ... }
/* ... */
void HAL_I2C_MemTxCpltCallback(I2C_HandleTypeDef *hi2c)
{
EEPROMDataWrite = 0;
}{ ... }
/* ... */
void HAL_I2C_MemRxCpltCallback(I2C_HandleTypeDef *hi2c)
{
EEPROMDataRead = 0;
}{ ... }
/* ... */
__weak void BSP_EEPROM_TIMEOUT_UserCallback(void)
{
}{ ... }
/* ... */#endif
/* ... */
/* ... */
/* ... */
/* ... */