-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheeprom.c
More file actions
422 lines (377 loc) · 11.8 KB
/
eeprom.c
File metadata and controls
422 lines (377 loc) · 11.8 KB
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
/*
eeprom.c - Library for the SPI Serial EEPROM chip
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "common_macros.h"
#include "common_types.h"
#include "eeprom.h"
#include "spidev_lib.h"
uint32_t EEPROM_open(struct eeprom *pDev, int fdSpiDev, uint32_t model)
{
// ATMEL
if (model == 25010) {
pDev->_totalBytes = 128;
pDev->_addrBits = 7;
pDev->_pageSize = 8;
pDev->_lastPage = (pDev->_totalBytes/pDev->_pageSize)-1;
strcpy(pDev->_name, "AT25010");
} else if (model == 25020) {
pDev->_totalBytes = 256;
pDev->_addrBits = 8;
pDev->_pageSize = 8;
pDev->_lastPage = (pDev->_totalBytes/pDev->_pageSize)-1;
strcpy(pDev->_name, "AT25020");
} else if (model == 25040) {
pDev->_totalBytes = 512;
pDev->_addrBits = 9;
pDev->_pageSize = 8;
pDev->_lastPage = (pDev->_totalBytes/pDev->_pageSize)-1;
strcpy(pDev->_name, "AT25040");
} else if (model == 25080) {
pDev->_totalBytes = 1024;
pDev->_addrBits = 10;
pDev->_pageSize = 32;
pDev->_lastPage = (pDev->_totalBytes/pDev->_pageSize)-1;
strcpy(pDev->_name, "AT25080");
} else if (model == 25160) {
pDev->_totalBytes = 2048;
pDev->_addrBits = 11;
pDev->_pageSize = 32;
pDev->_lastPage = (pDev->_totalBytes/pDev->_pageSize)-1;
strcpy(pDev->_name, "AT25160");
} else if (model == 25320) {
pDev->_totalBytes = 4096;
pDev->_addrBits = 12;
pDev->_pageSize = 32;
pDev->_lastPage = (pDev->_totalBytes/pDev->_pageSize)-1;
strcpy(pDev->_name, "AT25320");
} else if (model == 25640) {
pDev->_totalBytes = 8192;
pDev->_addrBits = 13;
pDev->_pageSize = 32;
pDev->_lastPage = (pDev->_totalBytes/pDev->_pageSize)-1;
strcpy(pDev->_name, "AT25640");
} else if (model == 25128) {
pDev->_totalBytes = 16384;
pDev->_addrBits = 14;
pDev->_pageSize = 64;
pDev->_lastPage = (pDev->_totalBytes/pDev->_pageSize)-1;
strcpy(pDev->_name, "AT25128");
} else if (model == 25256) {
pDev->_totalBytes = 32768;
pDev->_addrBits = 15;
pDev->_pageSize = 64;
pDev->_lastPage = (pDev->_totalBytes/pDev->_pageSize)-1;
strcpy(pDev->_name, "AT25256");
} else if (model == 25512) {
pDev->_totalBytes = 65536;
pDev->_addrBits = 16;
pDev->_pageSize = 128;
pDev->_lastPage = (pDev->_totalBytes/pDev->_pageSize)-1;
strcpy(pDev->_name, "AT25512");
}
// ST Micro
if (model == 95010) {
pDev->_totalBytes = 128;
pDev->_addrBits = 7;
pDev->_pageSize = 16;
pDev->_lastPage = (pDev->_totalBytes/pDev->_pageSize)-1;
strcpy(pDev->_name, "M95010");
} else if (model == 95020) {
pDev->_totalBytes = 256;
pDev->_addrBits = 8;
pDev->_pageSize = 16;
pDev->_lastPage = (pDev->_totalBytes/pDev->_pageSize)-1;
strcpy(pDev->_name, "M95020");
} else if (model == 95040) {
pDev->_totalBytes = 512;
pDev->_addrBits = 9;
pDev->_pageSize = 16;
pDev->_lastPage = (pDev->_totalBytes/pDev->_pageSize)-1;
strcpy(pDev->_name, "M95040");
} else if (model == 95080) {
pDev->_totalBytes = 1024;
pDev->_addrBits = 10;
pDev->_pageSize = 32;
pDev->_lastPage = (pDev->_totalBytes/pDev->_pageSize)-1;
strcpy(pDev->_name, "M95080");
} else if (model == 95160) {
pDev->_totalBytes = 2048;
pDev->_addrBits = 11;
pDev->_pageSize = 32;
pDev->_lastPage = (pDev->_totalBytes/pDev->_pageSize)-1;
strcpy(pDev->_name, "M95160");
} else if (model == 95320) {
pDev->_totalBytes = 4096;
pDev->_addrBits = 12;
pDev->_pageSize = 32;
pDev->_lastPage = (pDev->_totalBytes/pDev->_pageSize)-1;
strcpy(pDev->_name, "M95320");
} else if (model == 95640) {
pDev->_totalBytes = 8192;
pDev->_addrBits = 13;
pDev->_pageSize = 32;
pDev->_lastPage = (pDev->_totalBytes/pDev->_pageSize)-1;
strcpy(pDev->_name, "M95640");
} else if (model == 95128) {
pDev->_totalBytes = 16384;
pDev->_addrBits = 14;
pDev->_pageSize = 64;
pDev->_lastPage = (pDev->_totalBytes/pDev->_pageSize)-1;
strcpy(pDev->_name, "M95128");
} else if (model == 95256) {
pDev->_totalBytes = 32768;
pDev->_addrBits = 15;
pDev->_pageSize = 64;
pDev->_lastPage = (pDev->_totalBytes/pDev->_pageSize)-1;
strcpy(pDev->_name, "M95256");
}
pDev->_fdSpiDev = fdSpiDev;
pDev->_model = model;
return pDev->_totalBytes;
}
e_ReturnValue EEPROM_waitReady(struct eeprom *pDev)
{
e_ReturnValue l_nResult = e_RETURNVALUE_Success;
//Wait until device is READY.
uint8_t data[2];
uint32_t l_nCounter = 0;
int l_nIntResult;
#define RDY_MAXRETRIES 100000
while(++l_nCounter < RDY_MAXRETRIES)
{
data[0] = EEPROM_RDSR_CMD;
data[1] = 0x0;
l_nIntResult=spi_xfer( pDev->_fdSpiDev, data, 2, data, 2 );
if ((data[1] & 0x01) == 0) break;
usleep(DELAY_WAIT);
}
if ( RDY_MAXRETRIES <= l_nCounter )
{
printf("%s: Timeout waiting for rdy\r\n", __FUNCTION__);
l_nResult = e_RETURNVALUE_Failure;
}
return l_nResult;
}
e_ReturnValue EEPROM_writeEnable(struct eeprom *pDev)
{
e_ReturnValue l_nResult = e_RETURNVALUE_Success;
//printf("EEPROM_writeEnable start\n");
//Wait until device is WRITE ENABLED.
uint8_t data[2];
int l_nIntResult;
uint32_t l_nCounter = 0;
#define WE_MAXRETRIES 100000
while(++l_nCounter < WE_MAXRETRIES)
{
data[0] = EEPROM_WREN_CMD;
l_nIntResult=spi_xfer( pDev->_fdSpiDev, data, 1, data, 1 );
if (l_nIntResult < 0)
{
printf("%s: Error WREN_CMD: %d\r\n", __FUNCTION__, l_nIntResult);
l_nResult = e_RETURNVALUE_Failure;
goto lbl_err;
}
usleep(DELAY_WAIT);
data[0] = EEPROM_RDSR_CMD;
data[1] = 0x0;
l_nIntResult=spi_xfer( pDev->_fdSpiDev, data, 2, data, 2 );
if (l_nIntResult < 0)
{
printf("%s: Error EEPROM_RDSR_CMD: %d\r\n", __FUNCTION__, l_nIntResult);
l_nResult = e_RETURNVALUE_Failure;
goto lbl_err;
}
//printf("EEPROM_writeEnable data[1]=%x\n",data[1]);
//if ((data[1] & 0x02) == 0x02) break;
if (data[1] == 0x02) break;
usleep(DELAY_WAIT);
}
if ( WE_MAXRETRIES <= l_nCounter )
{
printf("%s: Failed to set WE\r\n", __FUNCTION__);
l_nResult = e_RETURNVALUE_Failure;
}
lbl_err:
return l_nResult;
}
e_ReturnValue EEPROM_writeByte(struct eeprom *pDev, uint16_t address, uint8_t data)
{
e_ReturnValue l_nResult = e_RETURNVALUE_Success;
e_ReturnValue l_nIntermediateResult;
int l_nIntResult;
uint8_t index;
uint8_t work[4];
l_nResult = EEPROM_writeEnable(pDev);
if (l_nResult != e_RETURNVALUE_Success)
{
printf("%s: Error setting WE: %d\r\n", __FUNCTION__, l_nResult);
goto lbl_err;
}
work[0] = EEPROM_WRITE_CMD;
if (pDev->_addrBits == 9 && address > 0xff) work[0] = work[0] | 0x8;
if(DEBUG)printf("[write_byte] address=0x%x work[0]=0x%x\n",address, work[0]);
if (pDev->_addrBits < 10)
{
work[1] = (address & 0xFF); //send LSByte address
work[2] = data;
int l_nIntResult=spi_xfer( pDev->_fdSpiDev, work, 3, work, 3 );
if (l_nIntResult < 0)
{
printf("%s: Error writing byte (short address): %d\r\n", __FUNCTION__, l_nIntResult);
l_nResult = e_RETURNVALUE_Failure;
goto lbl_err;
}
}
else
{
work[1] = (address & 0x0FF00) >> 8; //send MSByte address first
work[2] = (address & 0x0FF); //send LSByte address
work[3] = data;
int l_nIntResult=spi_xfer( pDev->_fdSpiDev, work, 4, work, 4 );
if (l_nIntResult < 0)
{
printf("%s: Error writing byte (long address): %d\r\n", __FUNCTION__, l_nIntResult);
l_nResult = e_RETURNVALUE_Failure;
goto lbl_err;
}
}
lbl_err:
l_nIntermediateResult = EEPROM_waitReady(pDev);
if (l_nIntermediateResult != e_RETURNVALUE_Success)
{
printf("%s: Error waiting for flash ready : %d\r\n", __FUNCTION__, l_nIntermediateResult);
if ( e_RETURNVALUE_Success == l_nResult )
{
l_nResult = l_nIntermediateResult;
}
}
return l_nResult;
}
e_ReturnValue EEPROM_writePage(struct eeprom *pDev, uint16_t nPageNb, uint8_t *pData)
{
e_ReturnValue l_nResult = e_RETURNVALUE_Success;
e_ReturnValue l_nIntermediateResult;
int l_nIntResult;
uint8_t index;
uint8_t *work = (uint8_t *)malloc(128);
uint16_t address = nPageNb * pDev->_pageSize;
if ( NULL == work )
{
printf("%s: Error allocating page memory\r\n", __FUNCTION__);
l_nResult = e_RETURNVALUE_NoMem;
goto lbl_err;
}
l_nResult = EEPROM_writeEnable(pDev);
if (l_nResult != e_RETURNVALUE_Success)
{
printf("%s: Error setting WE: %d\r\n", __FUNCTION__, l_nResult);
goto lbl_err;
}
work[0] = EEPROM_WRITE_CMD;
if (pDev->_addrBits == 9 && address > 0xff) work[0] = work[0] | 0x8;
if(DEBUG)printf("[write_pageSize] address=%x work[0]=%x\n",address, work[0]);
if (pDev->_addrBits < 10)
{
work[1] = (address & 0xFF); //send LSByte address
index = 2;
}
else
{
work[1] = (address & 0x0FF00) >> 8; //send MSByte address first
work[2] = (address & 0x0FF); //send LSByte address
index = 3;
}
if(DEBUG)printf("[write_pageSize] _pageSize=%d\n", pDev->_pageSize);
for (int i=0; i<pDev->_pageSize; i++)
work[index+i] = pData[i];
l_nIntResult=spi_write (pDev->_fdSpiDev, work, index+pDev->_pageSize);
if (l_nIntResult < 0)
{
printf("%s: Error writing page: %d\r\n", __FUNCTION__, l_nIntResult);
l_nResult = e_RETURNVALUE_Failure;
goto lbl_free;
}
lbl_free:
free (work);
lbl_err:
l_nIntermediateResult = EEPROM_waitReady(pDev);
if (l_nIntermediateResult != e_RETURNVALUE_Success)
{
printf("%s: Error waiting for flash ready : %d\r\n", __FUNCTION__, l_nIntermediateResult);
if ( e_RETURNVALUE_Success == l_nResult )
{
l_nResult = l_nIntermediateResult;
}
}
return l_nResult;
}
e_ReturnValue EEPROM_readByte(struct eeprom *pDev, uint16_t address, uint8_t *pData)
{
e_ReturnValue l_nResult = e_RETURNVALUE_Success;
int l_nIntResult;
uint8_t work[4];
if (( NULL == pDev) || (NULL == pData) )
{
printf("%s: Null argument(s)\r\n", __FUNCTION__);
l_nResult = e_RETURNVALUE_IllegalArgument;
goto lbl_err;
}
work[0] = EEPROM_READ_CMD;
if ( (pDev->_addrBits == 9) && (address > 0xff) )
{
work[0] = work[0] | 0x8;
}
if(DEBUG)printf("[read_byte] address=0x%x work[0]=0x%x\n",address, work[0]);
uint8_t data;
if (pDev->_addrBits < 10)
{
work[1] = (address & 0xFF); //send LSByte address
work[2] = 0;
l_nIntResult=spi_xfer( pDev->_fdSpiDev, work, 3, work, 3 );
if (l_nIntResult < 0)
{
printf("%s: Error sending short address: %d\r\n", __FUNCTION__, l_nIntResult);
l_nResult = e_RETURNVALUE_Failure;
goto lbl_err;
}
data = work[2];
}
else
{
work[1] = (address & 0x0FF00) >> 8; //send MSByte address first
work[2] = (address & 0x0FF); //send LSByte address
work[3] = 0;
l_nIntResult=spi_xfer( pDev->_fdSpiDev, work, 4, work, 4 );
if (l_nIntResult < 0)
{
printf("%s: Error sending long address: %d\r\n", __FUNCTION__, l_nIntResult);
l_nResult = e_RETURNVALUE_Failure;
goto lbl_err;
}
data = work[3];
}
*pData= (data);
lbl_err:
return l_nResult;
}
uint32_t EEPROM_totalBytes(struct eeprom *pDev)
{
return pDev->_totalBytes;
}
int16_t EEPROM_lastPage(struct eeprom *pDev)
{
return pDev->_lastPage;
}
uint16_t EEPROM_pageSize(struct eeprom *pDev)
{
return pDev->_pageSize;
}
char * EEPROM_name(struct eeprom *pDev)
{
return pDev->_name;
}