-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path47x04.c
494 lines (425 loc) · 26 KB
/
47x04.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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*!*****************************************************************************
* @file 47x04.c
* @author Fabien 'Emandhal' MAILLY
* @version 1.2.1
* @date 04/01/2025
* @brief EERAM47x04 driver
* @details I2C-Compatible (2-wire) 4-Kbit (512B x 8) Serial EERAM
* Follow datasheet 47L04/47C04/47L16/47C16 Rev.C (Jun 2016)
******************************************************************************/
//-----------------------------------------------------------------------------
#include "47x04.h"
//-----------------------------------------------------------------------------
#ifdef USE_ERROR_CONTEXT
# define UNIT_ERR_CONTEXT ERRCONTEXT__47x04 // Error context of this unit
#else
# define ERR_GENERATE(error) error
# define ERR_ERROR_Get(error) error
#endif
//-----------------------------------------------------------------------------
#ifdef __cplusplus
#include <cstdint>
extern "C" {
#endif
//-----------------------------------------------------------------------------
#ifdef USE_DYNAMIC_INTERFACE
# define GET_I2C_INTERFACE pComp->Eeprom.I2C
#else
# define GET_I2C_INTERFACE &pComp->Eeprom.I2C
#endif
//-----------------------------------------------------------------------------
#ifdef USE_EEPROM_GENERICNESS
// 47(L/C)04 EERAM configurations
const EEPROM_Conf EERAM47L04_Conf = { .ChipAddress = 0xA0, .ChipSelect = EEPROM_CHIP_ADDRESS_A2A1, .AddressType = EEPROM_ADDRESS_2Bytes, .PageWriteTime = 8, .PageSize = 512, .OffsetAddress = 0, .TotalByteSize = 512, .MaxI2CclockSpeed = 1000000, };
const EEPROM_Conf EERAM47C04_Conf = { .ChipAddress = 0xA0, .ChipSelect = EEPROM_CHIP_ADDRESS_A2A1, .AddressType = EEPROM_ADDRESS_2Bytes, .PageWriteTime = 8, .PageSize = 512, .OffsetAddress = 0, .TotalByteSize = 512, .MaxI2CclockSpeed = 1000000, };
#endif
//-----------------------------------------------------------------------------
//=============================================================================
// Prototypes for private functions
//=============================================================================
// Write 1 or 2-bytes address the EERAM47x04 (DO NOT USE DIRECTLY)
static eERRORRESULT __EERAM47x04_WriteAddress(EERAM47x04 *pComp, const uint8_t chipAddr, const uint16_t address, const bool useNonBlocking, const eI2C_TransferType transferType);
// Write data to the EERAM47x04 (DO NOT USE DIRECTLY, use EERAM47x04_WriteSRAMData() or EERAM47x04_WriteRegister() instead)
static eERRORRESULT __EERAM47x04_WriteData(EERAM47x04 *pComp, const uint8_t chipAddr, uint16_t address, const uint8_t* data, size_t size);
//-----------------------------------------------------------------------------
#define EERAM47x04_TIME_DIFF(begin,end) ( ((end) >= (begin)) ? ((end) - (begin)) : (UINT32_MAX - ((begin) - (end) - 1)) ) // Works only if time difference is strictly inferior to (UINT32_MAX/2) and call often
//-----------------------------------------------------------------------------
//**********************************************************************************************************************************************************
//=============================================================================
// EERAM47x04 initialization
//=============================================================================
eERRORRESULT Init_EERAM47x04(EERAM47x04 *pComp)
{
#ifdef CHECK_NULL_PARAM
if (pComp == NULL) return ERR_GENERATE(ERR__PARAMETER_ERROR);
#endif
I2C_Interface* pI2C = GET_I2C_INTERFACE;
#if defined(CHECK_NULL_PARAM)
# if defined(USE_DYNAMIC_INTERFACE)
if (pI2C == NULL) return ERR_GENERATE(ERR__PARAMETER_ERROR);
# endif
if (pI2C->fnI2C_Init == NULL) return ERR_GENERATE(ERR__PARAMETER_ERROR);
#endif
eERRORRESULT Error;
if (pComp->Eeprom.I2CclockSpeed > EERAM47x04_I2CCLOCK_MAX) return ERR_GENERATE(ERR__I2C_FREQUENCY_ERROR);
Error = pI2C->fnI2C_Init(pI2C, pComp->Eeprom.I2CclockSpeed);
if (Error != ERR_NONE) return Error; // If there is an error while calling fnI2C_Init() then return the Error
pComp->Eeprom.InternalConfig = 0;
return (EERAM47x04_IsReady(pComp) ? ERR_NONE : ERR_GENERATE(ERR__NO_DEVICE_DETECTED));
}
//=============================================================================
// Is the EERAM47x04 device ready
//=============================================================================
bool EERAM47x04_IsReady(EERAM47x04 *pComp)
{
#ifdef CHECK_NULL_PARAM
if (pComp == NULL) return false;
#endif
I2C_Interface* pI2C = GET_I2C_INTERFACE;
#if defined(CHECK_NULL_PARAM)
# if defined(USE_DYNAMIC_INTERFACE)
if (pI2C == NULL) return false;
# endif
if (pI2C->fnI2C_Transfer == NULL) return false;
#endif
I2CInterface_Packet PacketDesc = I2C_INTERFACE8_NO_DATA_DESC(EERAM47x04_SRAM_CHIPADDRESS_BASE | pComp->Eeprom.AddrA2A1A0);
return (pI2C->fnI2C_Transfer(pI2C, &PacketDesc) == ERR_NONE); // Send only the chip address and get the Ack flag
}
//-----------------------------------------------------------------------------
//=============================================================================
// [STATIC] Write 2-bytes address the EERAM47x04 (DO NOT USE DIRECTLY)
//=============================================================================
eERRORRESULT __EERAM47x04_WriteAddress(EERAM47x04 *pComp, const uint8_t chipAddr, const uint16_t address, const bool useNonBlocking, const eI2C_TransferType transferType)
{
#ifdef CHECK_NULL_PARAM
if (pComp == NULL) return ERR_GENERATE(ERR__PARAMETER_ERROR);
#endif
I2C_Interface* pI2C = GET_I2C_INTERFACE;
#if defined(CHECK_NULL_PARAM)
# if defined(USE_DYNAMIC_INTERFACE)
if (pI2C == NULL) return ERR_GENERATE(ERR__PARAMETER_ERROR);
# endif
if (pI2C->fnI2C_Transfer == NULL) return ERR_GENERATE(ERR__PARAMETER_ERROR);
#endif
const uint8_t AddrBytes = ((chipAddr & EERAM47x04_CHIPADDRESS_BASE_MASK) == EERAM47x04_SRAM_CHIPADDRESS_BASE ? 2 : 1); // If the base chip address is the SRAM then the address is 2 bytes else 1 byte (control registers)
eERRORRESULT Error;
//--- Send the address ---
uint8_t Address[sizeof(uint16_t)] = { (uint8_t)((address >> 8) & 0xFF), (uint8_t)(address & 0xFF) };
I2CInterface_Packet PacketDesc =
{
I2C_MEMBER(Config.Value) (useNonBlocking ? I2C_USE_NON_BLOCKING : I2C_BLOCKING) | I2C_USE_8BITS_ADDRESS | I2C_ENDIAN_TRANSFORM_SET(I2C_NO_ENDIAN_CHANGE) | I2C_TRANSFER_TYPE_SET(transferType),
I2C_MEMBER(ChipAddr ) chipAddr & I2C_WRITE_ANDMASK,
I2C_MEMBER(Start ) true,
I2C_MEMBER(pBuffer ) &Address[2 - AddrBytes],
I2C_MEMBER(BufferSize ) AddrBytes,
I2C_MEMBER(Stop ) false,
};
Error = pI2C->fnI2C_Transfer(pI2C, &PacketDesc); // Transfer the address
if (ERR_ERROR_Get(Error) == ERR__I2C_NACK) return ERR_GENERATE(ERR__NOT_READY); // If the device receive a NAK, then the device is not ready
if (ERR_ERROR_Get(Error) == ERR__I2C_NACK_DATA) return ERR_GENERATE(ERR__I2C_INVALID_ADDRESS); // If the device receive a NAK while transferring data, then this is an invalid address
return Error;
}
//=============================================================================
// Read SRAM data from the EERAM47x04 device
//=============================================================================
eERRORRESULT EERAM47x04_ReadSRAMData(EERAM47x04 *pComp, uint16_t address, uint8_t* data, size_t size)
{
#ifdef CHECK_NULL_PARAM
if ((pComp == NULL) || (data == NULL)) return ERR_GENERATE(ERR__PARAMETER_ERROR);
#endif
I2C_Interface* pI2C = GET_I2C_INTERFACE;
#if defined(CHECK_NULL_PARAM)
# if defined(USE_DYNAMIC_INTERFACE)
if (pI2C == NULL) return ERR_GENERATE(ERR__PARAMETER_ERROR);
# endif
if (pI2C->fnI2C_Transfer == NULL) return ERR_GENERATE(ERR__PARAMETER_ERROR);
#endif
if ((address + (uint16_t)size) > EERAM47x04_EERAM_SIZE) return ERR_GENERATE(ERR__OUT_OF_MEMORY);
const uint8_t ChipAddr = ((EERAM47x04_SRAM_CHIPADDRESS_BASE | pComp->Eeprom.AddrA2A1A0) & EERAM47x04_CHIPADDRESS_MASK);
eERRORRESULT Error;
//--- Read data ---
Error = __EERAM47x04_WriteAddress(pComp, ChipAddr, address, false, I2C_WRITE_THEN_READ_FIRST_PART); // Start a write at address with the device
if (Error == ERR_NONE) // If there is no error while writing address then
{
I2CInterface_Packet PacketDesc = I2C_INTERFACE8_RX_DATA_DESC(ChipAddr, true, data, size, true, I2C_WRITE_THEN_READ_SECOND_PART);
Error = pI2C->fnI2C_Transfer(pI2C, &PacketDesc); // Restart a read transfer, get the data and stop transfer
}
return Error;
}
//=============================================================================
// Read Control register from the EERAM47x04 device
//=============================================================================
eERRORRESULT EERAM47x04_ReadRegister(EERAM47x04 *pComp, uint8_t* data)
{
#ifdef CHECK_NULL_PARAM
if ((pComp == NULL) || (data == NULL)) return ERR_GENERATE(ERR__PARAMETER_ERROR);
#endif
I2C_Interface* pI2C = GET_I2C_INTERFACE;
#if defined(CHECK_NULL_PARAM)
# if defined(USE_DYNAMIC_INTERFACE)
if (pI2C == NULL) return ERR_GENERATE(ERR__PARAMETER_ERROR);
# endif
if (pI2C->fnI2C_Transfer == NULL) return ERR_GENERATE(ERR__PARAMETER_ERROR);
#endif
const uint8_t ChipAddr = ((EERAM47x04_REG_CHIPADDRESS_BASE | pComp->Eeprom.AddrA2A1A0) & EERAM47x04_CHIPADDRESS_MASK);
//--- Read data from I2C ---
I2CInterface_Packet PacketDesc = I2C_INTERFACE8_RX_DATA_DESC(ChipAddr, true, data, 1, true, I2C_SIMPLE_TRANSFER);
return pI2C->fnI2C_Transfer(pI2C, &PacketDesc); // Start a read transfer, get the data and stop transfer
}
//=============================================================================
// Read SRAM data with DMA from the EERAM47x04 device
//=============================================================================
eERRORRESULT EERAM47x04_ReadSRAMDataWithDMA(EERAM47x04 *pComp, uint16_t address, uint8_t* data, size_t size)
{
#ifdef CHECK_NULL_PARAM
if (pComp == NULL) return ERR_GENERATE(ERR__PARAMETER_ERROR);
#endif
I2C_Interface* pI2C = GET_I2C_INTERFACE;
#if defined(CHECK_NULL_PARAM)
# if defined(USE_DYNAMIC_INTERFACE)
if (pI2C == NULL) return ERR_GENERATE(ERR__PARAMETER_ERROR);
# endif
if (pI2C->fnI2C_Transfer == NULL) return ERR_GENERATE(ERR__PARAMETER_ERROR);
#endif
if ((address + (uint16_t)size) > EERAM47x04_EERAM_SIZE) return ERR_GENERATE(ERR__OUT_OF_MEMORY);
const uint8_t ChipAddr = ((EERAM47x04_SRAM_CHIPADDRESS_BASE | pComp->Eeprom.AddrA2A1A0) & EERAM47x04_CHIPADDRESS_MASK);
eERRORRESULT Error;
//--- Check DMA ---
if (EERAM47x04_IS_DMA_TRANSFER_IN_PROGRESS(pComp->Eeprom.InternalConfig))
{
const uint16_t CurrTransactionNumber = EERAM47x04_TRANSACTION_NUMBER_GET(pComp->Eeprom.InternalConfig);
I2CInterface_Packet PacketDescCheck = I2C_INTERFACE8_CHECK_DMA_DESC(ChipAddr, CurrTransactionNumber);
Error = pI2C->fnI2C_Transfer(pI2C, &PacketDescCheck); // Send only the chip address and get the Ack flag, to return the status of the current transfer
if ((ERR_ERROR_Get(Error) != ERR__I2C_BUSY) && (ERR_ERROR_Get(Error) != ERR__I2C_OTHER_BUSY))
{
pComp->Eeprom.InternalConfig &= EERAM47x04_NO_DMA_TRANSFER_IN_PROGRESS_SET;
}
return Error;
}
//--- Read data ---
Error = __EERAM47x04_WriteAddress(pComp, ChipAddr, address, true, I2C_WRITE_THEN_READ_FIRST_PART); // Start a read at address with the device
if (Error == ERR_NONE) // If there is no error while writing address then
{
I2CInterface_Packet PacketDescData = I2C_INTERFACE8_RX_DATA_DESC(ChipAddr, true, data, size, true, I2C_WRITE_THEN_READ_SECOND_PART);
Error = pI2C->fnI2C_Transfer(pI2C, &PacketDescData); // Restart at first data read transfer, get the data and stop transfer at last data
if (ERR_ERROR_Get(Error) != ERR__I2C_OTHER_BUSY) pComp->Eeprom.InternalConfig &= EERAM47x04_NO_DMA_TRANSFER_IN_PROGRESS_SET;
if (ERR_ERROR_Get(Error) == ERR__I2C_BUSY) pComp->Eeprom.InternalConfig |= EERAM47x04_DMA_TRANSFER_IN_PROGRESS;
EERAM47x04_TRANSACTION_NUMBER_CLEAR(pComp->Eeprom.InternalConfig);
pComp->Eeprom.InternalConfig |= EERAM47x04_TRANSACTION_NUMBER_SET(I2C_TRANSACTION_NUMBER_GET(PacketDescData.Config.Value));
}
return Error;
}
//-----------------------------------------------------------------------------
//=============================================================================
// [STATIC] Write data to the EERAM47x04 (DO NOT USE DIRECTLY, use EERAM47x04_WriteSRAMData() or EERAM47x04_WriteRegister() instead)
//=============================================================================
eERRORRESULT __EERAM47x04_WriteData(EERAM47x04 *pComp, const uint8_t chipAddr, uint16_t address, const uint8_t* data, size_t size)
{
#ifdef CHECK_NULL_PARAM
if ((pComp == NULL) || (data == NULL)) return ERR_GENERATE(ERR__PARAMETER_ERROR);
#endif
I2C_Interface* pI2C = GET_I2C_INTERFACE;
#if defined(CHECK_NULL_PARAM)
# if defined(USE_DYNAMIC_INTERFACE)
if (pI2C == NULL) return ERR_GENERATE(ERR__PARAMETER_ERROR);
# endif
if (pI2C->fnI2C_Transfer == NULL) return ERR_GENERATE(ERR__PARAMETER_ERROR);
#endif
if ((address + (uint16_t)size) > EERAM47x04_EERAM_SIZE) return ERR_GENERATE(ERR__OUT_OF_MEMORY);
const uint8_t ChipAddr = (chipAddr | pComp->Eeprom.AddrA2A1A0) & EERAM47x04_CHIPADDRESS_MASK;
uint8_t* pData = (uint8_t*)data;
eERRORRESULT Error;
//--- Write data ---
Error = __EERAM47x04_WriteAddress(pComp, ChipAddr, address, false, I2C_WRITE_THEN_WRITE_FIRST_PART); // Start a write at address with the device
if (Error == ERR_NONE) // If there is no error while writing address then
{
I2CInterface_Packet PacketDesc = I2C_INTERFACE8_TX_DATA_DESC(ChipAddr, false, pData, size, true, I2C_WRITE_THEN_WRITE_SECOND_PART);
Error = pI2C->fnI2C_Transfer(pI2C, &PacketDesc); // Continue the transfer by sending the data and stop transfer
}
return Error;
}
//=============================================================================
// Write SRAM data to the EERAM47x04 device
//=============================================================================
eERRORRESULT EERAM47x04_WriteSRAMData(EERAM47x04 *pComp, uint16_t address, const uint8_t* data, size_t size)
{
return __EERAM47x04_WriteData(pComp, EERAM47x04_SRAM_CHIPADDRESS_BASE, address, data, size);
}
//=============================================================================
// Write Control register to the EERAM47x04 device
//=============================================================================
eERRORRESULT EERAM47x04_WriteRegister(EERAM47x04 *pComp, uint8_t address, const uint8_t data)
{
eERRORRESULT Error = __EERAM47x04_WriteData(pComp, EERAM47x04_REG_CHIPADDRESS_BASE, address, &data, 1);
if (ERR_ERROR_Get(Error) == ERR__I2C_NACK_DATA) Error = ERR_GENERATE(ERR__I2C_INVALID_COMMAND); // Here a NACK indicate that the command (aka 'data') is invalid. It cannot be anything else
return Error;
}
//=============================================================================
// Write SRAM data with DMA to the EERAM47x04 device
//=============================================================================
eERRORRESULT EERAM47x04_WriteSRAMDataWithDMA(EERAM47x04 *pComp, uint16_t address, const uint8_t* data, size_t size)
{
#ifdef CHECK_NULL_PARAM
if (pComp == NULL) return ERR_GENERATE(ERR__PARAMETER_ERROR);
#endif
I2C_Interface* pI2C = GET_I2C_INTERFACE;
#if defined(CHECK_NULL_PARAM)
# if defined(USE_DYNAMIC_INTERFACE)
if (pI2C == NULL) return ERR_GENERATE(ERR__PARAMETER_ERROR);
# endif
if (pI2C->fnI2C_Transfer == NULL) return ERR_GENERATE(ERR__PARAMETER_ERROR);
#endif
if ((address + (uint16_t)size) > EERAM47x04_EERAM_SIZE) return ERR_GENERATE(ERR__OUT_OF_MEMORY);
const uint8_t ChipAddr = ((EERAM47x04_SRAM_CHIPADDRESS_BASE | pComp->Eeprom.AddrA2A1A0) & EERAM47x04_CHIPADDRESS_MASK);
uint8_t* pData = (uint8_t*)data;
eERRORRESULT Error;
//--- Check DMA ---
if (EERAM47x04_IS_DMA_TRANSFER_IN_PROGRESS(pComp->Eeprom.InternalConfig))
{
const uint16_t CurrTransactionNumber = EERAM47x04_TRANSACTION_NUMBER_GET(pComp->Eeprom.InternalConfig);
I2CInterface_Packet PacketDescCheck = I2C_INTERFACE8_CHECK_DMA_DESC(ChipAddr, CurrTransactionNumber);
Error = pI2C->fnI2C_Transfer(pI2C, &PacketDescCheck); // Send only the chip address and get the Ack flag, to return the status of the current transfer
if ((ERR_ERROR_Get(Error) != ERR__I2C_BUSY) && (ERR_ERROR_Get(Error) != ERR__I2C_OTHER_BUSY))
{
pComp->Eeprom.InternalConfig &= EERAM47x04_NO_DMA_TRANSFER_IN_PROGRESS_SET;
}
return Error;
}
//--- Write data ---
Error = __EERAM47x04_WriteAddress(pComp, ChipAddr, address, true, I2C_WRITE_THEN_WRITE_FIRST_PART); // Start a read at address with the device
if (Error == ERR_NONE) // If there is no error while writing address then
{
I2CInterface_Packet PacketDescData = I2C_INTERFACE8_TX_DATA_DESC(ChipAddr, true, pData, size, true, I2C_WRITE_THEN_WRITE_SECOND_PART);
Error = pI2C->fnI2C_Transfer(pI2C, &PacketDescData); // Restart at first data read transfer, get the data and stop transfer at last data
if (ERR_ERROR_Get(Error) != ERR__I2C_OTHER_BUSY) pComp->Eeprom.InternalConfig &= EERAM47x04_NO_DMA_TRANSFER_IN_PROGRESS_SET;
if (ERR_ERROR_Get(Error) == ERR__I2C_BUSY) pComp->Eeprom.InternalConfig |= EERAM47x04_DMA_TRANSFER_IN_PROGRESS;
EERAM47x04_TRANSACTION_NUMBER_CLEAR(pComp->Eeprom.InternalConfig);
pComp->Eeprom.InternalConfig |= EERAM47x04_TRANSACTION_NUMBER_SET(I2C_TRANSACTION_NUMBER_GET(PacketDescData.Config.Value));
}
return Error;
}
//-----------------------------------------------------------------------------
//=============================================================================
// Store all the SRAM to the EEPROM of the EERAM47x04 device
//=============================================================================
eERRORRESULT EERAM47x04_StoreSRAMtoEEPROM(EERAM47x04 *pComp, bool forceStore, bool waitEndOfStore)
{
#ifdef CHECK_NULL_PARAM
if (pComp->Eeprom.fnGetCurrentms == NULL) return ERR_GENERATE(ERR__PARAMETER_ERROR);
#endif
eERRORRESULT Error = ERR_NONE;
EERAM47x04_Status_Register Reg;
bool Store = forceStore;
//--- Check the need of store operation ---
if (forceStore == false) // Check register Status.AM if the store is not forced
{
Error = EERAM47x04_ReadRegister(pComp, &Reg.Status); // Get the status register
if (Error != ERR_NONE) return Error; // If there is an error while reading register, then return the error
Store = ((Reg.Status & EERAM47x04_ARRAY_MODIFIED) > 0); // If the array has been modified, flag the store
}
//--- Send the store operation if necessary or asked ---
if (Store)
{
uint8_t Data = EERAM47x04_STORE_COMMAND;
Error = EERAM47x04_WriteRegister(pComp, EERAM47x04_COMMAND_REGISTER_ADDR, Data); // Execute a store
if (Error != ERR_NONE) return Error; // If there is an error while reading register, then return the error
//--- Wait the end of store if asked ---
if (waitEndOfStore)
{
Reg.Status = EERAM47x04_ARRAY_MODIFIED;
uint32_t StartTime = pComp->Eeprom.fnGetCurrentms(); // Start the timeout
while (true)
{
Error = EERAM47x04_ReadRegister(pComp, &Reg.Status); // Get the status register
if ((ERR_ERROR_Get(Error) != ERR_NONE) && (ERR_ERROR_Get(Error) != ERR__I2C_NACK)) return Error; // If there is an error while calling EERAM47x04_ReadRegister() then return the error
if ((Reg.Status & EERAM47x04_ARRAY_MODIFIED) == 0) break; // The store is finished, all went fine
if (EERAM47x04_TIME_DIFF(StartTime, pComp->Eeprom.fnGetCurrentms()) > EERAM47x04_STORE_TIMEOUT + 1) // Wait at least STORE_TIMEOUT + 1ms because GetCurrentms can be 1 cycle before the new ms
{
return ERR_GENERATE(ERR__DEVICE_TIMEOUT); // Timeout? return the error
}
}
}
}
return Error;
}
//=============================================================================
// Recall all data from EEPROM to SRAM of the EERAM47x04 device
//=============================================================================
eERRORRESULT EERAM47x04_RecallEEPROMtoSRAM(EERAM47x04 *pComp, bool waitEndOfRecall)
{
#ifdef CHECK_NULL_PARAM
if (pComp->Eeprom.fnGetCurrentms == NULL) return ERR_GENERATE(ERR__PARAMETER_ERROR);
#endif
eERRORRESULT Error = ERR_NONE;
EERAM47x04_Status_Register Reg;
//--- Send a recall operation ---
uint8_t Data = EERAM47x04_RECALL_COMMAND;
Error = EERAM47x04_WriteRegister(pComp, EERAM47x04_COMMAND_REGISTER_ADDR, Data); // Execute a recall
if (Error != ERR_NONE) return Error; // If there is an error while reading register, then return the error
//--- Wait the end of recall if asked ---
if (waitEndOfRecall)
{
Reg.Status = EERAM47x04_ARRAY_MODIFIED;
uint32_t StartTime = pComp->Eeprom.fnGetCurrentms(); // Start the timeout
while (true)
{
Error = EERAM47x04_ReadRegister(pComp, &Reg.Status); // Get the status register
if ((ERR_ERROR_Get(Error) != ERR_NONE) && (ERR_ERROR_Get(Error) != ERR__I2C_NACK)) return Error; // If there is an error while calling EERAM47x04_ReadRegister() then return the error
if ((Reg.Status & EERAM47x04_ARRAY_MODIFIED) == 0) break; // The store is finished, all went fine
if (EERAM47x04_TIME_DIFF(StartTime, pComp->Eeprom.fnGetCurrentms()) > EERAM47x04_RECALL_TIMEOUT + 1) // Wait at least RECALL_TIMEOUT + 1ms because GetCurrentms can be 1 cycle before the new ms
{
return ERR_GENERATE(ERR__DEVICE_TIMEOUT); // Timeout? return the error
}
}
}
return ERR_NONE;
}
//-----------------------------------------------------------------------------
//=============================================================================
// Activate the Auto-Store of the EERAM47x04 device
//=============================================================================
eERRORRESULT EERAM47x04_ActivateAutoStore(EERAM47x04 *pComp)
{
eERRORRESULT Error = ERR_NONE;
EERAM47x04_Status_Register Reg;
//--- Get the status register ---
Error = EERAM47x04_ReadRegister(pComp, &Reg.Status); // Get the status register
if (Error != ERR_NONE) return Error; // If there is an error while sending data to I2C then stop the transfer
//--- Set the status register ---
Reg.Status |= EERAM47x04_ASE_ENABLE; // Set the Auto-Store flag
return EERAM47x04_WriteRegister(pComp, EERAM47x04_STATUS_REGISTER_ADDR, Reg.Status); // Save the status register
}
//=============================================================================
// Deactivate the Auto-Store of the EERAM47x04 device
//=============================================================================
eERRORRESULT EERAM47x04_DeactivateAutoStore(EERAM47x04 *pComp)
{
eERRORRESULT Error = ERR_NONE;
EERAM47x04_Status_Register Reg;
//--- Get the status register ---
Error = EERAM47x04_ReadRegister(pComp, &Reg.Status); // Get the status register
if (Error != ERR_NONE) return Error; // If there is an error while sending data to I2C then stop the transfer
//--- Set the status register ---
Reg.Status &= ~EERAM47x04_ASE_ENABLE; // Unset the Auto-Store flag
return EERAM47x04_WriteRegister(pComp, EERAM47x04_STATUS_REGISTER_ADDR, Reg.Status); // Save the status register
}
//-----------------------------------------------------------------------------
//=============================================================================
// Set block write protect of the EERAM47x04 device
//=============================================================================
eERRORRESULT EERAM47x04_SetBlockWriteProtect(EERAM47x04 *pComp, eEERAM47x04_BlockProtect blockProtect)
{
eERRORRESULT Error = ERR_NONE;
EERAM47x04_Status_Register Reg;
//--- Get the status register ---
Error = EERAM47x04_ReadRegister(pComp, &Reg.Status); // Get the status register
if (Error != ERR_NONE) return Error; // If there is an error while sending data to I2C then stop the transfer
//--- Set the status register ---
Reg.Bits.BP = (uint8_t)blockProtect; // Set the block protect
return EERAM47x04_WriteRegister(pComp, EERAM47x04_STATUS_REGISTER_ADDR, Reg.Status); // Save the status register
}
//-----------------------------------------------------------------------------
#ifdef __cplusplus
}
#endif
//-----------------------------------------------------------------------------