-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstm32091c_eval_eeprom.c
477 lines (436 loc) · 15.4 KB
/
stm32091c_eval_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
257
258
259
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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/**
******************************************************************************
* @file stm32091c_eval_eeprom.c
* @author MCD Application Team
* @brief This file provides a set of functions needed to manage a M24LR64
* I2C EEPROM memory.
*
* ===================================================================
* Notes:
* - This driver is intended for STM32F0xx families devices only.
* - The I2C EEPROM memory (M24LR64) is available on separate daughter
* board ANT7-M24LR-A, which is provided with the STM32091C
* EVAL board.
* To use this driver with M24LR64, you have to connect
* the ANT7-M24LR-A to CN2 connector of STM32091C EVAL board.
* ===================================================================
*
* It implements a high level communication layer for read and write
* from/to this memory. The needed STM32F0xx hardware resources (I2C
* and GPIO) are defined in stm32091c_eval.h file,
* and the initialization is performed depending of EEPROMs
* in EEPROM_IO_Init() function declared in stm32091c_eval.c file.
* You can easily tailor this driver to any other development board,
* by just adapting the defines for hardware resources and
* EEPROM_IO_Init() function.
*
* @note In this driver, basic read and write functions
* (BSP_EEPROM_ReadBuffer() and BSP_EEPROM_WriteBuffer())
* use Polling mode to perform the data transfer to/from EEPROM memories.
* +-----------------------------------------------------------------+
* | Pin assignment for M24LR64 EEPROM |
* +---------------------------------------+-----------+-------------+
* | STM32F0xx I2C Pins | EEPROM | Pin |
* +---------------------------------------+-----------+-------------+
* | EEPROM_I2C_SDA_PIN (PB7)/ SDA | SDA | 1 |
* | . | NC | 2 |
* | EEPROM_I2C_SCL_PIN/ SCL | SCL | 3 |
* | EX_RESET(PD7) | RESET | 4 |
* | . | VDD | 5 |
* | . | NC | 6 |
* | . | GND | 7 |
* | . | NC | 8 |
* +---------------------------------------+-----------+-------------+
*
******************************************************************************
* @attention
*
* Copyright (c) 2016 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32091c_eval_eeprom.h"
/** @addtogroup BSP
* @{
*/
/** @addtogroup STM32091C_EVAL
* @{
*/
/** @addtogroup STM32091C_EVAL_EEPROM
* @brief This file includes the I2C EEPROM driver
* of STM32091C-EVAL board.
* @{
*/
/** @addtogroup STM32091C_EVAL_EEPROM_Private_Variables
* @{
*/
__IO uint16_t EEPROMAddress = 0;
__IO uint16_t EEPROMPageSize = 0;
__IO uint16_t EEPROMDataRead;
__IO uint8_t EEPROMDataWrite;
static EEPROM_DrvTypeDef *EEPROM_SelectedDevice = 0;
/**
* @}
*/
/** @addtogroup STM32091C_EVAL_EEPROM_Private_Functions
* @{
*/
static uint32_t EEPROM_I2C_Init(void);
static uint32_t EEPROM_I2C_ReadBuffer(uint8_t* pBuffer, uint16_t ReadAddr, uint32_t* NumByteToRead);
static uint32_t EEPROM_I2C_WritePage(uint8_t* pBuffer, uint16_t WriteAddr, uint32_t* NumByteToWrite);
static uint32_t EEPROM_I2C_WaitEepromStandbyState(void);
/**
* @}
*/
/** @defgroup STM32091C_EVAL_EEPROM_Private_Types Private Types
* @{
*/
/* EEPROM I2C driver typedef */
EEPROM_DrvTypeDef EEPROM_I2C_Drv =
{
EEPROM_I2C_Init,
EEPROM_I2C_ReadBuffer,
EEPROM_I2C_WritePage
};
/**
* @}
*/
/** @addtogroup STM32091C_EVAL_EEPROM_Exported_Functions
* @{
*/
/**
* @brief Initializes peripherals used by the I2C EEPROM driver.
*
* @note There are 2 different versions of M24LR64 (A01 & A02).
* Then try to connect on 1st one (EEPROM_I2C_ADDRESS_A01)
* and if problem, check the 2nd one (EEPROM_I2C_ADDRESS_A02)
* @retval EEPROM_OK (0) if operation is correctly performed, else return value
* different from EEPROM_OK (0)
*/
uint32_t BSP_EEPROM_Init(void)
{
EEPROM_SelectedDevice = &EEPROM_I2C_Drv;
if(EEPROM_SelectedDevice->Init != 0)
{
return (EEPROM_SelectedDevice->Init());
}
else
{
return EEPROM_FAIL;
}
}
/**
* @brief Reads a block of data from the EEPROM device selected.
* @param pBuffer pointer to the buffer that receives the data read from
* the EEPROM.
* @param ReadAddr EEPROM's internal address to start reading from.
* @param NumByteToRead pointer to the variable holding number of bytes to
* be read from the EEPROM.
*
* @note The variable pointed by NumByteToRead is reset to 0 when all the
* data are read from the EEPROM. Application should monitor this
* variable in order know when the transfer is complete.
*
* @retval EEPROM_OK (0) if operation is correctly performed, else return value
* different from EEPROM_OK (0) or the timeout user callback.
*/
uint32_t BSP_EEPROM_ReadBuffer(uint8_t* pBuffer, uint16_t ReadAddr, uint32_t* NumByteToRead)
{
if(EEPROM_SelectedDevice->ReadBuffer != 0)
{
return (EEPROM_SelectedDevice->ReadBuffer(pBuffer, ReadAddr, NumByteToRead));
}
else
{
return EEPROM_FAIL;
}
}
/**
* @brief Writes buffer of data to the EEPROM device selected.
* @param pBuffer pointer to the buffer containing the data to be written
* to the EEPROM.
* @param WriteAddr EEPROM's internal address to write to.
* @param NumByteToWrite number of bytes to write to the EEPROM.
* @retval EEPROM_OK (0) if operation is correctly performed, else return value
* different from EEPROM_OK (0) or the timeout user callback.
*/
uint32_t BSP_EEPROM_WriteBuffer(uint8_t* pBuffer, uint16_t WriteAddr, uint32_t NumByteToWrite)
{
uint16_t numofpage = 0, numofsingle = 0, count = 0;
uint16_t addr = 0;
uint32_t dataindex = 0;
uint32_t status = EEPROM_OK;
addr = WriteAddr % EEPROMPageSize;
count = EEPROMPageSize - addr;
numofpage = NumByteToWrite / EEPROMPageSize;
numofsingle = NumByteToWrite % EEPROMPageSize;
if(EEPROM_SelectedDevice->WritePage == 0)
{
return EEPROM_FAIL;
}
/*!< If WriteAddr is EEPROM_PAGESIZE aligned */
if(addr == 0)
{
/*!< If NumByteToWrite < EEPROM_PAGESIZE */
if(numofpage == 0)
{
/* Store the number of data to be written */
dataindex = numofsingle;
/* Start writing data */
status = EEPROM_SelectedDevice->WritePage(pBuffer, WriteAddr, (uint32_t*)(&dataindex));
if (status != EEPROM_OK)
{
return status;
}
}
/*!< If NumByteToWrite > EEPROM_PAGESIZE */
else
{
while(numofpage--)
{
/* Store the number of data to be written */
dataindex = EEPROMPageSize;
status = EEPROM_SelectedDevice->WritePage(pBuffer, WriteAddr, (uint32_t*)(&dataindex));
if (status != EEPROM_OK)
{
return status;
}
WriteAddr += EEPROMPageSize;
pBuffer += EEPROMPageSize;
}
if(numofsingle!=0)
{
/* Store the number of data to be written */
dataindex = numofsingle;
status = EEPROM_SelectedDevice->WritePage(pBuffer, WriteAddr, (uint32_t*)(&dataindex));
if (status != EEPROM_OK)
{
return status;
}
}
}
}
/*!< If WriteAddr is not EEPROM_PAGESIZE aligned */
else
{
/*!< If NumByteToWrite < EEPROM_PAGESIZE */
if(numofpage== 0)
{
/*!< If the number of data to be written is more than the remaining space
in the current page: */
if (NumByteToWrite > count)
{
/* Store the number of data to be written */
dataindex = count;
/*!< Write the data contained in same page */
status = EEPROM_SelectedDevice->WritePage(pBuffer, WriteAddr, (uint32_t*)(&dataindex));
if (status != EEPROM_OK)
{
return status;
}
/* Store the number of data to be written */
dataindex = (NumByteToWrite - count);
/*!< Write the remaining data in the following page */
status = EEPROM_SelectedDevice->WritePage((uint8_t*)(pBuffer + count), (WriteAddr + count), (uint32_t*)(&dataindex));
if (status != EEPROM_OK)
{
return status;
}
}
else
{
/* Store the number of data to be written */
dataindex = numofsingle;
status = EEPROM_SelectedDevice->WritePage(pBuffer, WriteAddr, (uint32_t*)(&dataindex));
if (status != EEPROM_OK)
{
return status;
}
}
}
/*!< If NumByteToWrite > EEPROM_PAGESIZE */
else
{
NumByteToWrite -= count;
numofpage = NumByteToWrite / EEPROMPageSize;
numofsingle = NumByteToWrite % EEPROMPageSize;
if(count != 0)
{
/* Store the number of data to be written */
dataindex = count;
status = EEPROM_SelectedDevice->WritePage(pBuffer, WriteAddr, (uint32_t*)(&dataindex));
if (status != EEPROM_OK)
{
return status;
}
WriteAddr += count;
pBuffer += count;
}
while(numofpage--)
{
/* Store the number of data to be written */
dataindex = EEPROMPageSize;
status = EEPROM_SelectedDevice->WritePage(pBuffer, WriteAddr, (uint32_t*)(&dataindex));
if (status != EEPROM_OK)
{
return status;
}
WriteAddr += EEPROMPageSize;
pBuffer += EEPROMPageSize;
}
if(numofsingle != 0)
{
/* Store the number of data to be written */
dataindex = numofsingle;
status = EEPROM_SelectedDevice->WritePage(pBuffer, WriteAddr, (uint32_t*)(&dataindex));
if (status != EEPROM_OK)
{
return status;
}
}
}
}
/* If all operations OK, return EEPROM_OK (0) */
return EEPROM_OK;
}
/**
* @brief Basic management of the timeout situation.
* @retval None
*/
__weak void BSP_EEPROM_TIMEOUT_UserCallback(void)
{
}
/**
* @}
*/
/** @addtogroup STM32091C_EVAL_EEPROM_Private_Functions
* @{
*/
/**
* @brief Initializes peripherals used by the I2C EEPROM driver.
*
* @note There are 2 different versions of M24LR64 (A01 & A02).
* Then try to connect on 1st one (EEPROM_I2C_ADDRESS_A01)
* and if problem, check the 2nd one (EEPROM_I2C_ADDRESS_A02)
* @retval EEPROM_OK (0) if operation is correctly performed, else return value
* different from EEPROM_OK (0)
*/
static uint32_t EEPROM_I2C_Init(void)
{
EEPROM_IO_Init();
/*Select the EEPROM address for M24LR64 A02 and check if OK*/
EEPROMAddress = EEPROM_ADDRESS_M24LR64_A01;
EEPROMPageSize = EEPROM_PAGESIZE_M24LR64;
if (EEPROM_IO_IsDeviceReady(EEPROMAddress, EEPROM_MAX_TRIALS) != HAL_OK)
{
/*Select the EEPROM address for M24LR64 A01 and check if OK*/
EEPROMAddress = EEPROM_ADDRESS_M24LR64_A02;
EEPROMPageSize = EEPROM_PAGESIZE_M24LR64;
if (EEPROM_IO_IsDeviceReady(EEPROMAddress, EEPROM_MAX_TRIALS) != HAL_OK)
{
return EEPROM_FAIL;
}
}
return EEPROM_OK;
}
/**
* @brief Reads a block of data from the I2C EEPROM.
* @param pBuffer pointer to the buffer that receives the data read from
* the EEPROM.
* @param ReadAddr EEPROM's internal address to start reading from.
* @param NumByteToRead pointer to the variable holding number of bytes to
* be read from the EEPROM.
*
* @retval EEPROM_OK (0) if operation is correctly performed, else return value
* different from EEPROM_OK (0) or the timeout user callback.
*/
static uint32_t EEPROM_I2C_ReadBuffer(uint8_t* pBuffer, uint16_t ReadAddr, uint32_t* NumByteToRead)
{
uint32_t buffersize = *NumByteToRead;
if (EEPROM_IO_ReadData(EEPROMAddress, ReadAddr, pBuffer, buffersize) != HAL_OK)
{
return EEPROM_FAIL;
}
/* If all operations OK, return EEPROM_OK (0) */
return EEPROM_OK;
}
/**
* @brief Writes more than one byte to the EEPROM with a single WRITE cycle.
*
* @note The number of bytes (combined to write start address) must not
* cross the EEPROM page boundary. This function can only write into
* the boundaries of an EEPROM page.
* This function doesn't check on boundaries condition (in this driver
* the function BSP_EEPROM_WriteBuffer() which calls EEPROM_WritePage() is
* responsible of checking on Page boundaries).
*
* @param pBuffer pointer to the buffer containing the data to be written to
* the EEPROM.
* @param WriteAddr EEPROM's internal address to write to.
* @param NumByteToWrite pointer to the variable holding number of bytes to
* be written into the EEPROM.
*
* @note The variable pointed by NumByteToWrite is reset to 0 when all the
* data are written to the EEPROM. Application should monitor this
* variable in order know when the transfer is complete.
*
*
* @retval EEPROM_OK (0) if operation is correctly performed, else return value
* different from EEPROM_OK (0) or the timeout user callback.
*/
static uint32_t EEPROM_I2C_WritePage(uint8_t* pBuffer, uint16_t WriteAddr, uint32_t* NumByteToWrite)
{
uint32_t buffersize = *NumByteToWrite;
if (EEPROM_IO_WriteData(EEPROMAddress, WriteAddr, pBuffer, buffersize) != HAL_OK)
{
return EEPROM_FAIL;
}
/* Wait for EEPROM Standby state */
if (EEPROM_I2C_WaitEepromStandbyState() != EEPROM_OK)
{
return EEPROM_FAIL;
}
return EEPROM_OK;
}
/**
* @brief Wait for EEPROM I2C Standby state.
*
* @note This function allows to wait and check that EEPROM has finished the
* last operation. It is mostly used after Write operation: after receiving
* the buffer to be written, the EEPROM may need additional time to actually
* perform the write operation. During this time, it doesn't answer to
* I2C packets addressed to it. Once the write operation is complete
* the EEPROM responds to its address.
*
* @retval EEPROM_OK (0) if operation is correctly performed, else return value
* different from EEPROM_OK (0) or the timeout user callback.
*/
static uint32_t EEPROM_I2C_WaitEepromStandbyState(void)
{
/* Check if the maximum allowed number of trials has bee reached */
if (EEPROM_IO_IsDeviceReady(EEPROMAddress, EEPROM_MAX_TRIALS) != HAL_OK)
{
/* If the maximum number of trials has been reached, exit the function */
BSP_EEPROM_TIMEOUT_UserCallback();
return EEPROM_TIMEOUT;
}
return EEPROM_OK;
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/