-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path23LCxxx.c
490 lines (406 loc) · 24.5 KB
/
23LCxxx.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
/*!*****************************************************************************
* @file 23LCxxx.c
* @author Fabien 'Emandhal' MAILLY
* @version 1.1.0
* @date 04/01/2025
* @brief Generic SRAM 23LCxxx driver
* @details Generic driver for Microchip (c) Serial SRAM 23LCxxx. Works with:
* 23A640/23K640: 64K SPI Bus Low-Power Serial SRAM
* 23A256/23K256: 256K SPI Bus Low-Power Serial SRAM
* 23A512/23LC512: 512Kbit SPI Serial SRAM with SDI and SQI Interface
* 23A1024/23LC1024: 1Mbit SPI Serial SRAM with SDI and SQI Interface
* 23LCV512: 512-Kbit SPI Serial SRAM with Battery Backup and SDI Interface
* 23LCV1024: 1Mbit SPI Serial SRAM with Battery Backup and SDI Interface
******************************************************************************/
//-----------------------------------------------------------------------------
#include "23LCxxx.h"
//-----------------------------------------------------------------------------
#ifdef USE_ERROR_CONTEXT
# define UNIT_ERR_CONTEXT ERRCONTEXT__23LCxxx // 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->SPI
#else
# define GET_SPI_INTERFACE &pComp->SPI
#endif
//-----------------------------------------------------------------------------
//=== 23xxx devices ======================================================
// 23x640 configurations
const SRAM23LCxxx_Conf SRAM23A640_Conf = { .ModeSet = SRAM23LCxxx_SPI , .UseHold = true , .AddressBytes = 2, .PageSize = 32, .ArrayByteSize = 8192/*Bytes*/, .MaxSPIclockSpeed = 20000000, };
const SRAM23LCxxx_Conf SRAM23K640_Conf = { .ModeSet = SRAM23LCxxx_SPI , .UseHold = true , .AddressBytes = 2, .PageSize = 32, .ArrayByteSize = 8192/*Bytes*/, .MaxSPIclockSpeed = 20000000, };
// 23x256 configurations
const SRAM23LCxxx_Conf SRAM23A256_Conf = { .ModeSet = SRAM23LCxxx_SPI , .UseHold = true , .AddressBytes = 2, .PageSize = 32, .ArrayByteSize = 32768/*Bytes*/, .MaxSPIclockSpeed = 20000000, };
const SRAM23LCxxx_Conf SRAM23K256_Conf = { .ModeSet = SRAM23LCxxx_SPI , .UseHold = true , .AddressBytes = 2, .PageSize = 32, .ArrayByteSize = 32768/*Bytes*/, .MaxSPIclockSpeed = 20000000, };
// 23x512 configurations
const SRAM23LCxxx_Conf SRAM23A512_Conf = { .ModeSet = SRAM23LCxxx_SPI | SRAM23LCxxx_SDI | SRAM23LCxxx_SQI, .UseHold = false, .AddressBytes = 2, .PageSize = 32, .ArrayByteSize = 65536/*Bytes*/, .MaxSPIclockSpeed = 20000000, };
const SRAM23LCxxx_Conf SRAM23LC512_Conf = { .ModeSet = SRAM23LCxxx_SPI | SRAM23LCxxx_SDI | SRAM23LCxxx_SQI, .UseHold = false, .AddressBytes = 2, .PageSize = 32, .ArrayByteSize = 65536/*Bytes*/, .MaxSPIclockSpeed = 20000000, };
// 23x1024 configurations
const SRAM23LCxxx_Conf SRAM23A1024_Conf = { .ModeSet = SRAM23LCxxx_SPI | SRAM23LCxxx_SDI | SRAM23LCxxx_SQI, .UseHold = false, .AddressBytes = 3, .PageSize = 32, .ArrayByteSize = 131072/*Bytes*/, .MaxSPIclockSpeed = 20000000, };
const SRAM23LCxxx_Conf SRAM23LC1024_Conf = { .ModeSet = SRAM23LCxxx_SPI | SRAM23LCxxx_SDI | SRAM23LCxxx_SQI, .UseHold = false, .AddressBytes = 3, .PageSize = 32, .ArrayByteSize = 131072/*Bytes*/, .MaxSPIclockSpeed = 20000000, };
// 23LCV512 configuration
const SRAM23LCxxx_Conf SRAM23LCV512_Conf = { .ModeSet = SRAM23LCxxx_SPI | SRAM23LCxxx_SDI , .UseHold = false, .AddressBytes = 2, .PageSize = 32, .ArrayByteSize = 65536/*Bytes*/, .MaxSPIclockSpeed = 20000000, };
// 23LCV512 configuration
const SRAM23LCxxx_Conf SRAM23LCV1024_Conf = { .ModeSet = SRAM23LCxxx_SPI | SRAM23LCxxx_SDI , .UseHold = false, .AddressBytes = 3, .PageSize = 32, .ArrayByteSize = 131072/*Bytes*/, .MaxSPIclockSpeed = 20000000, };
//**********************************************************************************************************************************************************
//=============================================================================
// Prototypes for private functions
//=============================================================================
/*! @brief Write address of the SRAM23LCxxx device
*
* This function asserts the device, write the instruction and send the address. The function takes care of dummy byte on read on SDI and SQI modes
* The device remains asserted after this function
* @param[in] *pComp Is the pointed structure of the device to be used
* @param[in] instruction Is the instruction to send
* @param[in] address Is the address to send
* @param[in] onlyInstruction Indicate if only the instruction have to be sent
* @return Returns an #eERRORRESULT value enum
*/
static eERRORRESULT __SRAM23LCxxx_WriteAddress(SRAM23LCxxx *pComp, const uint8_t instruction, const uint32_t address, const bool onlyInstruction);
/*! @brief Read data from the SRAM23LCxxx device
*
* @param[in] *pComp Is the pointed structure of the device to be used
* @param[in] instruction Is the instruction to send
* @param[in] address Is the address to read
* @param[out] *data Is where the data will be stored
* @param[in] size Is the size of the data array to read
* @return Returns an #eERRORRESULT value enum
*/
static eERRORRESULT __SRAM23LCxxx_ReadData(SRAM23LCxxx *pComp, const eSRAM23LCxxx_InstructionSet instruction, uint32_t address, uint8_t* data, size_t size);
/*! @brief Write data to the SRAM23LCxxx device
*
* @param[in] *pComp Is the pointed structure of the device to be used
* @param[in] instruction Is the instruction to send
* @param[in] address Is the address to send
* @param[in] *data Is the data array to store
* @param[in] size Is the size of the data array to write
* @return Returns an #eERRORRESULT value enum
*/
static eERRORRESULT __SRAM23LCxxx_WriteData(SRAM23LCxxx *pComp, const eSRAM23LCxxx_InstructionSet instruction, uint32_t address, const uint8_t* data, size_t size);
//-----------------------------------------------------------------------------
//**********************************************************************************************************************************************************
//=============================================================================
// SRAM23LCxxx initialization
//=============================================================================
eERRORRESULT Init_SRAM23LCxxx(SRAM23LCxxx *pComp, const SRAM23LCxxx_Config* pConf)
{
#ifdef CHECK_NULL_PARAM
if ((pComp == NULL) || (pConf == NULL)) return ERR_GENERATE(ERR__PARAMETER_ERROR);
if (pComp->Conf == NULL) return ERR_GENERATE(ERR__PARAMETER_ERROR);
#endif
SPI_Interface* pSPI = GET_SPI_INTERFACE;
#if defined(CHECK_NULL_PARAM)
# if defined(USE_DYNAMIC_INTERFACE)
if (pSPI == NULL) return ERR_GENERATE(ERR__PARAMETER_ERROR);
# endif
if (pSPI->fnSPI_Transfer == NULL) return ERR_GENERATE(ERR__PARAMETER_ERROR);
#endif
pComp->InternalConfig = SRAM23LCxxx_IO_MODE_SET(SRAM23LCxxx_SPI);
eERRORRESULT Error;
//--- Recover I/O access mode ---
if (pConf->RecoverSPIbus)
{
if ((pComp->Conf->ModeSet & SRAM23LCxxx_SDI) > 0) // Device supports SDI?
{
Error = pSPI->fnSPI_Init(pSPI, pComp->SPIchipSelect, DUAL_SPI_MODE0, pComp->SPIclockSpeed); // Configure interface in SDI mode
if (Error != ERR_NONE) return Error; // If there is an error while calling fnSPI_Init() then return the error
Error = SRAM23LCxxx_WriteInstruction(pComp, SRAM23LCxxx_RSTIO); // Return to SPI mode
if (Error != ERR_NONE) return Error; // If there is an error while calling SRAM23LCxxx_WriteInstruction() then return the error
}
if ((pComp->Conf->ModeSet & SRAM23LCxxx_SQI) > 0) // Device supports SQI?
{
Error = pSPI->fnSPI_Init(pSPI, pComp->SPIchipSelect, QUAD_SPI_MODE0, pComp->SPIclockSpeed); // Configure interface in SQI mode
if (Error != ERR_NONE) return Error; // If there is an error while calling fnSPI_Init() then return the error
Error = SRAM23LCxxx_WriteInstruction(pComp, SRAM23LCxxx_RSTIO); // Return to SPI mode
if (Error != ERR_NONE) return Error; // If there is an error while calling SRAM23LCxxx_WriteInstruction() then return the error
}
Error = pSPI->fnSPI_Init(pSPI, pComp->SPIchipSelect, STD_SPI_MODE0, pComp->SPIclockSpeed); // Configure interface in SDI mode
if (Error != ERR_NONE) return Error; // If there is an error while calling fnSPI_Init() then return the error
}
//--- Configure SPI interface ---
Error = SRAM23LCxxx_SetIOmode(pComp, pConf->IOmode);
if (Error != ERR_NONE) return Error; // If there is an error while calling SRAM23LCxxx_SetIOmode() then return the error
//--- Configure memory mode ---
return SRAM23LCxxx_SetOperationMode(pComp, pConf->OperationMode, pConf->DisableHold);
}
//**********************************************************************************************************************************************************
//=============================================================================
// [STATIC] Write address of the SRAM23LCxxx device
//=============================================================================
eERRORRESULT __SRAM23LCxxx_WriteAddress(SRAM23LCxxx *pComp, const uint8_t instruction, const uint32_t address, const bool onlyInstruction)
{
#ifdef CHECK_NULL_PARAM
if (pComp == NULL) return ERR_GENERATE(ERR__PARAMETER_ERROR);
if (pComp->Conf == NULL) return ERR_GENERATE(ERR__PARAMETER_ERROR);
#endif
SPI_Interface* pSPI = GET_SPI_INTERFACE;
#if defined(CHECK_NULL_PARAM)
# if defined(USE_DYNAMIC_INTERFACE)
if (pSPI == NULL) return ERR_GENERATE(ERR__PARAMETER_ERROR);
# endif
if (pSPI->fnSPI_Transfer == NULL) return ERR_GENERATE(ERR__PARAMETER_ERROR);
#endif
//--- Create address ---
uint8_t Address[1/*Instruction*/ + 4/*Bytes address*/] = { instruction, 0, 0, 0, 0 }; // Up to 32-bits address
const uint8_t AddrBytes = (onlyInstruction ? 0 : pComp->Conf->AddressBytes);
for (int_fast8_t z = AddrBytes; --z >=0;) Address[z + 1] = (uint8_t)((address >> ((AddrBytes - z - 1) * 8)) & 0xFF);
//--- Send the address ---
SPIInterface_Packet PacketDesc = SPI_INTERFACE_TX_DATA_DESC(&Address[0], (1 + AddrBytes), false);
return pSPI->fnSPI_Transfer(pSPI, &PacketDesc); // Transfer the address
}
//**********************************************************************************************************************************************************
//=============================================================================
// [STATIC] Read data from the SRAM23LCxxx device
//=============================================================================
eERRORRESULT __SRAM23LCxxx_ReadData(SRAM23LCxxx *pComp, const eSRAM23LCxxx_InstructionSet instruction, uint32_t address, uint8_t* data, size_t size)
{
#ifdef CHECK_NULL_PARAM
if ((pComp == NULL) || (data == NULL)) return ERR_GENERATE(ERR__PARAMETER_ERROR);
if (pComp->Conf == NULL) return ERR_GENERATE(ERR__PARAMETER_ERROR);
#endif
SPI_Interface* pSPI = GET_SPI_INTERFACE;
#if defined(CHECK_NULL_PARAM)
# if defined(USE_DYNAMIC_INTERFACE)
if (pSPI == NULL) return ERR_GENERATE(ERR__PARAMETER_ERROR);
# endif
if (pSPI->fnSPI_Transfer == NULL) return ERR_GENERATE(ERR__PARAMETER_ERROR);
#endif
if ((address + (uint32_t)size) > pComp->Conf->ArrayByteSize) return ERR_GENERATE(ERR__OUT_OF_MEMORY);
const eSRAM23LCxxx_IOmodes IOmode = SRAM23LCxxx_IO_MODE_GET(pComp->InternalConfig);
eERRORRESULT Error;
//--- Read data ---
Error = __SRAM23LCxxx_WriteAddress(pComp, instruction, address, (instruction == SRAM23LCxxx_RDSR)); // Start a write at address with the device
if (Error == ERR_NONE) // If there is no error while writing address then
{
SPIInterface_Packet PacketDesc = SPI_INTERFACE_RX_DATA_WITH_DUMMYBYTE_DESC(0x00, data, size, true);
if ((IOmode != SRAM23LCxxx_SPI) && (instruction != SRAM23LCxxx_RDSR)) // In SDI or SQI?
{
PacketDesc.DataSize = 1;
PacketDesc.Terminate = false;
Error = pSPI->fnSPI_Transfer(pSPI, &PacketDesc); // Continue the transfer by reading the dummy byte
if (Error != ERR_NONE) return Error; // If there is an error while calling fnSPI_Transfer() then return the error
PacketDesc.DataSize = size;
PacketDesc.Terminate = true;
}
Error = pSPI->fnSPI_Transfer(pSPI, &PacketDesc); // Continue the transfer by reading the data and stop transfer
}
return Error;
}
//=============================================================================
// Read SRAM data from the SRAM23LCxxx device
//=============================================================================
eERRORRESULT SRAM23LCxxx_ReadSRAMData(SRAM23LCxxx *pComp, uint32_t address, uint8_t* data, size_t size)
{
#ifdef CHECK_NULL_PARAM
if ((pComp == NULL) || (data == NULL)) return ERR_GENERATE(ERR__PARAMETER_ERROR);
if (pComp->Conf == NULL) return ERR_GENERATE(ERR__PARAMETER_ERROR);
#endif
const SRAM23LCxxx_Conf* const pConf = pComp->Conf;
const eSRAM23LCxxx_Modes SRAMmode = SRAM23LCxxx_MODE_GET(pComp->InternalConfig);
if ((address + (uint32_t)size) > pConf->ArrayByteSize) return ERR_GENERATE(ERR__OUT_OF_MEMORY);
uint8_t* pData = (uint8_t*)data;
eERRORRESULT Error;
//--- Cut data to write into pages/bytes ---
size_t PageRemData = (SRAMmode == SRAM23LCxxx_BYTE_MODE ? 1 : size);
while (size > 0)
{
if (SRAMmode == SRAM23LCxxx_PAGE_MODE) // Only in page mode
{
PageRemData = pConf->PageSize - (address & (pConf->PageSize - 1)); // Get how many bytes remain in the current page
PageRemData = (size < PageRemData ? size : PageRemData); // Get the least remaining bytes to read between remain size and remain in page
}
//--- Write data ---
Error = __SRAM23LCxxx_ReadData(pComp, SRAM23LCxxx_READ, address, pData, PageRemData); // Read data from a page/bytes
if (Error != ERR_NONE) return Error; // If there is an error while calling __SRAM23LCxxx_ReadData() then return the error
address += PageRemData;
pData += PageRemData;
size -= PageRemData;
}
return ERR_NONE;
}
//**********************************************************************************************************************************************************
//=============================================================================
// [STATIC] Write data to the SRAM23LCxxx device
//=============================================================================
eERRORRESULT __SRAM23LCxxx_WriteData(SRAM23LCxxx *pComp, const eSRAM23LCxxx_InstructionSet instruction, uint32_t address, const uint8_t* data, size_t size)
{
#ifdef CHECK_NULL_PARAM
if ((pComp == NULL) || (data == NULL)) return ERR_GENERATE(ERR__PARAMETER_ERROR);
if (pComp->Conf == NULL) return ERR_GENERATE(ERR__PARAMETER_ERROR);
#endif
SPI_Interface* pSPI = GET_SPI_INTERFACE;
#if defined(CHECK_NULL_PARAM)
# if defined(USE_DYNAMIC_INTERFACE)
if (pSPI == NULL) return ERR_GENERATE(ERR__PARAMETER_ERROR);
# endif
if (pSPI->fnSPI_Transfer == NULL) return ERR_GENERATE(ERR__PARAMETER_ERROR);
#endif
if ((address + (uint32_t)size) > pComp->Conf->ArrayByteSize) return ERR_GENERATE(ERR__OUT_OF_MEMORY);
uint8_t* pData = (uint8_t*)data;
eERRORRESULT Error;
//--- Write data ---
Error = __SRAM23LCxxx_WriteAddress(pComp, instruction, address, (instruction == SRAM23LCxxx_WRSR)); // Start a write at address with the device
if (Error == ERR_NONE) // If there is no error while writing address then
{
SPIInterface_Packet PacketDesc = SPI_INTERFACE_TX_DATA_DESC(pData, size, true);
Error = pSPI->fnSPI_Transfer(pSPI, &PacketDesc); // Continue the transfer by sending the data and stop transfer
}
return Error;
}
//=============================================================================
// Write SRAM data to the SRAM23LCxxx device
//=============================================================================
eERRORRESULT SRAM23LCxxx_WriteSRAMData(SRAM23LCxxx *pComp, uint32_t address, const uint8_t* data, size_t size)
{
#ifdef CHECK_NULL_PARAM
if ((pComp == NULL) || (data == NULL)) return ERR_GENERATE(ERR__PARAMETER_ERROR);
if (pComp->Conf == NULL) return ERR_GENERATE(ERR__PARAMETER_ERROR);
#endif
const SRAM23LCxxx_Conf* const pConf = pComp->Conf;
const eSRAM23LCxxx_Modes SRAMmode = SRAM23LCxxx_MODE_GET(pComp->InternalConfig);
if ((address + (uint32_t)size) > pConf->ArrayByteSize) return ERR_GENERATE(ERR__OUT_OF_MEMORY);
uint8_t* pData = (uint8_t*)data;
eERRORRESULT Error;
//--- Cut data to write into pages/bytes ---
size_t PageRemData = (SRAMmode == SRAM23LCxxx_BYTE_MODE ? 1 : size);
while (size > 0)
{
if (SRAMmode == SRAM23LCxxx_PAGE_MODE) // Only in page mode
{
PageRemData = pConf->PageSize - (address & (pConf->PageSize - 1)); // Get how many bytes remain in the current page
PageRemData = (size < PageRemData ? size : PageRemData); // Get the least remaining bytes to read between remain size and remain in page
}
//--- Write data ---
Error = __SRAM23LCxxx_WriteData(pComp, SRAM23LCxxx_WRITE, address, pData, PageRemData); // Write data to a page/bytes
if (Error != ERR_NONE) return Error; // If there is an error while calling __SRAM23LCxxx_WriteData() then return the error
address += PageRemData;
pData += PageRemData;
size -= PageRemData;
}
return ERR_NONE;
}
//=============================================================================
// Write an instruction to the SRAM23LCxxx device
//=============================================================================
eERRORRESULT SRAM23LCxxx_WriteInstruction(SRAM23LCxxx *pComp, const eSRAM23LCxxx_InstructionSet instruction)
{
#ifdef CHECK_NULL_PARAM
if (pComp == NULL) return ERR_GENERATE(ERR__PARAMETER_ERROR);
#endif
SPI_Interface* pSPI = GET_SPI_INTERFACE;
#if defined(CHECK_NULL_PARAM)
# if defined(USE_DYNAMIC_INTERFACE)
if (pSPI == NULL) return ERR_GENERATE(ERR__PARAMETER_ERROR);
# endif
if (pSPI->fnSPI_Transfer == NULL) return ERR_GENERATE(ERR__PARAMETER_ERROR);
#endif
uint8_t RegData = (uint8_t)instruction;
//--- Write instruction to SPI ---
SPIInterface_Packet PacketDesc = SPI_INTERFACE_TX_DATA_DESC(&RegData, sizeof(RegData), true);
return pSPI->fnSPI_Transfer(pSPI, &PacketDesc); // Start a read transfer, get the data and stop transfer
}
//**********************************************************************************************************************************************************
//=============================================================================
// Read Status register from the SRAM23LCxxx device
//=============================================================================
eERRORRESULT SRAM23LCxxx_GetStatus(SRAM23LCxxx *pComp, SRAM23LCxxx_StatusRegister* status)
{
#ifdef CHECK_NULL_PARAM
if (status == NULL) return ERR_GENERATE(ERR__PARAMETER_ERROR);
#endif
return __SRAM23LCxxx_ReadData(pComp, SRAM23LCxxx_RDSR, 0, &status->Status, sizeof(SRAM23LCxxx_StatusRegister));
}
//=============================================================================
// Write Status register to the SRAM23LCxxx device
//=============================================================================
eERRORRESULT SRAM23LCxxx_SetStatus(SRAM23LCxxx *pComp, const SRAM23LCxxx_StatusRegister status)
{
return __SRAM23LCxxx_WriteData(pComp, SRAM23LCxxx_WRSR, 0, &status.Status, sizeof(SRAM23LCxxx_StatusRegister));
}
//**********************************************************************************************************************************************************
//=============================================================================
// Set the I/O mode of the SRAM23LCxxx
//=============================================================================
eERRORRESULT SRAM23LCxxx_SetIOmode(SRAM23LCxxx *pComp, const eSRAM23LCxxx_IOmodes mode)
{
#ifdef CHECK_NULL_PARAM
if (pComp == NULL) return ERR_GENERATE(ERR__PARAMETER_ERROR);
if (pComp->Conf == NULL) return ERR_GENERATE(ERR__PARAMETER_ERROR);
#endif
SPI_Interface* pSPI = GET_SPI_INTERFACE;
#if defined(CHECK_NULL_PARAM)
# if defined(USE_DYNAMIC_INTERFACE)
if (pSPI == NULL) return ERR_GENERATE(ERR__PARAMETER_ERROR);
# endif
if (pSPI->fnSPI_Transfer == NULL) return ERR_GENERATE(ERR__PARAMETER_ERROR);
#endif
eERRORRESULT Error;
//--- Check SPI configuration ---
if (pComp->SPIclockSpeed > pComp->Conf->MaxSPIclockSpeed) return ERR_GENERATE(ERR__SPI_FREQUENCY_ERROR);
if ((mode == SRAM23LCxxx_SDI) && ((pComp->Conf->ModeSet & SRAM23LCxxx_SDI) == 0)) return ERR_GENERATE(ERR__SPI_CONFIG_ERROR);
if ((mode == SRAM23LCxxx_SQI) && ((pComp->Conf->ModeSet & SRAM23LCxxx_SQI) == 0)) return ERR_GENERATE(ERR__SPI_CONFIG_ERROR);
//--- Reset interface to SPI ---
if (SRAM23LCxxx_IO_MODE_GET(pComp->InternalConfig) != SRAM23LCxxx_SPI)
{
Error = SRAM23LCxxx_WriteInstruction(pComp, SRAM23LCxxx_RSTIO); // Return to SPI mode
if (Error != ERR_NONE) return Error; // If there is an error while calling SRAM23LCxxx_WriteInstruction() then return the error
if (mode != SRAM23LCxxx_SPI) // Reset interface to SPI only if the new mode will be other than SPI
{
Error = pSPI->fnSPI_Init(pSPI, pComp->SPIchipSelect, STD_SPI_MODE0, pComp->SPIclockSpeed); // Configure interface in SPI mode
if (Error != ERR_NONE) return Error; // If there is an error while calling fnSPI_Init() then return the error
}
}
//--- Configure SPI interface ---
eSPIInterface_Mode SxImode = STD_SPI_MODE0; // Default mode SPI
if (mode == SRAM23LCxxx_SDI) // SDI mode asked?
{
Error = SRAM23LCxxx_WriteInstruction(pComp, SRAM23LCxxx_EDIO); // Set SDI mode
if (Error != ERR_NONE) return Error; // If there is an error while calling SRAM23LCxxx_WriteInstruction() then return the error
SxImode = DUAL_SPI_MODE0;
}
if (mode == SRAM23LCxxx_SQI) // SQI mode asked?
{
Error = SRAM23LCxxx_WriteInstruction(pComp, SRAM23LCxxx_EQIO); // Set SQI mode
if (Error != ERR_NONE) return Error; // If there is an error while calling SRAM23LCxxx_WriteInstruction() then return the error
SxImode = QUAD_SPI_MODE0;
}
Error = pSPI->fnSPI_Init(pSPI, pComp->SPIchipSelect, SxImode, pComp->SPIclockSpeed);
if (Error != ERR_NONE) return Error; // If there is an error while calling fnSPI_Init() then return the error
pComp->InternalConfig &= ~SRAM23LCxxx_IO_MODE_Mask;
pComp->InternalConfig |= SRAM23LCxxx_IO_MODE_SET(mode); // Set the new I/O mode
return ERR_NONE;
}
//=============================================================================
// Set the SRAM operation mode of the SRAM23LCxxx
//=============================================================================
eERRORRESULT SRAM23LCxxx_SetOperationMode(SRAM23LCxxx *pComp, const eSRAM23LCxxx_Modes mode, const bool disableHold)
{
#ifdef CHECK_NULL_PARAM
if (pComp == NULL) return ERR_GENERATE(ERR__PARAMETER_ERROR);
if (pComp->Conf == NULL) return ERR_GENERATE(ERR__PARAMETER_ERROR);
#endif
eERRORRESULT Error;
//--- Configure the SRAM ---
SRAM23LCxxx_StatusRegister Reg;
Reg.Status = SRAM23LCxxx_HOLD_FEATURE_ENABLE | SRAM23LCxxx_MODE_SET(mode);
if (pComp->Conf->UseHold && disableHold) Reg.Status |= SRAM23LCxxx_HOLD_FEATURE_DISABLE;
//--- Set the new status ---
Error = SRAM23LCxxx_SetStatus(pComp, Reg);
if (Error != ERR_NONE) return Error; // If there is an error while calling SRAM23LCxxx_SetStatus() then return the error
pComp->InternalConfig &= 0xFF3E; // Clear status into the internal config
pComp->InternalConfig |= Reg.Status; // Set the new status into the internal config
return ERR_NONE;
}
//-----------------------------------------------------------------------------
#ifdef __cplusplus
}
#endif
//-----------------------------------------------------------------------------