/
SST25VF032B.c
381 lines (319 loc) · 12.9 KB
/
SST25VF032B.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
#include "MainDemo.h"
/************************************************************************
* Function: SST25Init
*
* Overview: this function setup SPI and IOs connected to SST25
*
* Input: none
*
* Output: none
*
************************************************************************/
void SST25Init(void)
{
// Initialize SPI2
/*
SPI2STAT = 0;
SPI2CON1 = 0x001b;
SPI2CON1bits.MSTEN = 1;
SPI2CON2 = 0;
SPI2CON1bits.MODE16 = 0;
SPI2CON1bits.CKE = 0;
SPI2CON1bits.CKP = 1;
SPI2CON1bits.SMP = 1;
SPI2STATbits.SPIEN = 1;
*/
// Set IOs directions for EEPROM SPI
SST25_SS_TRIS = 0;
SST25_SS_PORT = 1;
Nop();
Nop();
Nop();
EEPROM_SCK_TRIS = 0;
Nop();
EEPROM_SDO_TRIS = 0;
Nop();
Nop();
EEPROM_SDI_TRIS = 1;
SST25ResetWriteProtection();
}
/************************************************************************
* Macro: EEPROMSSLow()
*
* Preconditions: SS IO must be configured as output
*
* Overview: this macro pulls down SS line
*
* Input: none
*
* Output: none
*
************************************************************************/
#define SST25SSLow() SST25_SS_PORT=0;
/************************************************************************
* Macro: EEPROMSSHigh()
*
* Preconditions: SS IO must be configured as output
*
* Overview: this macro set SS line to high level
*
* Input: none
*
* Output: none
*
************************************************************************/
#define SST25SSHigh() SST25_SS_PORT=1;
/************************************************************************
* Function: void SST25WriteByte(BYTE data, DWORD address)
*
* Overview: this function writes a byte to the address specified
*
* Input: data to be written and address
*
* Output: none
*
************************************************************************/
void SST25WriteByte(BYTE data, DWORD address)
{
// SST25SectorErase(address);
//DelayMs(100);
SST25WriteEnable();
SST25SSLow();
SPIPut(SST25_CMD_WRITE);
SPIGet();
SPIPut(((DWORD_VAL) address).v[2]);
SPIGet();
SPIPut(((DWORD_VAL) address).v[1]);
SPIGet();
SPIPut(((DWORD_VAL) address).v[0]);
SPIGet();
SPIPut(data);
SPIGet();
SST25SSHigh();
// Wait for write end
while(SST25IsWriteBusy());
}
/************************************************************************
* Function: BYTE SST25ReadByte(DWORD address)
*
* Overview: this function reads a byte from the address specified
*
* Input: address
*
* Output: data read
*
************************************************************************/
BYTE SST25ReadByte(DWORD address)
{
BYTE temp;
SST25SSLow();
SPIPut(SST25_CMD_READ);
SPIGet();
SPIPut(((DWORD_VAL) address).v[2]);
SPIGet();
SPIPut(((DWORD_VAL) address).v[1]);
SPIGet();
SPIPut(((DWORD_VAL) address).v[0]);
SPIGet();
SPIPut(0);
temp = SPIGet();
SST25SSHigh();
return (temp);
}
/************************************************************************
* Function: void SST25WriteWord(WODR data, DWORD address)
*
* Overview: this function writes a 16-bit word to the address specified
*
* Input: data to be written and address
*
* Output: none
*
************************************************************************/
void SST25WriteWord(WORD data, DWORD address)
{
SST25WriteByte(((WORD_VAL) data).v[0], address);
SST25WriteByte(((WORD_VAL) data).v[1], address + 1);
}
/************************************************************************
* Function: WORD SST25ReadWord(DWORD address)
*
* Overview: this function reads a 16-bit word from the address specified
*
* Input: address
*
* Output: data read
*
************************************************************************/
WORD SST25ReadWord(DWORD address)
{
WORD_VAL temp;
temp.v[0] = SST25ReadByte(address);
temp.v[1] = SST25ReadByte(address + 1);
return (temp.Val);
}
/************************************************************************
* Function: SST25WriteEnable()
*
* Overview: this function allows write/erase SST25. Must be called
* before every write/erase command.
*
* Input: none
*
* Output: none
*
************************************************************************/
void SST25WriteEnable(void)
{
SST25SSLow();
SPIPut(SST25_CMD_WREN);
SPIGet();
SST25SSHigh();
}
/************************************************************************
* Function: BYTE SST25IsWriteBusy(void)
*
* Overview: this function reads status register and chek BUSY bit for write operation
*
* Input: none
*
* Output: non zero if busy
*
************************************************************************/
BYTE SST25IsWriteBusy(void)
{
BYTE temp;
SST25SSLow();
SPIPut(SST25_CMD_RDSR);
SPIGet();
SPIPut(0);
temp = SPIGet();
SST25SSHigh();
return (temp & 0x01);
}
/************************************************************************
* Function: BYTE SST25WriteArray(DWORD address, BYTE* pData, nCount)
*
* Overview: this function writes a data array at the address specified
*
* Input: flash memory address, pointer to the data array, data number
*
* Output: return 1 if the operation was successfull
*
************************************************************************/
BYTE SST25WriteArray(DWORD address, BYTE *pData, WORD nCount)
{
DWORD addr;
BYTE *pD;
WORD counter;
addr = address;
pD = pData;
// WRITE
for(counter = 0; counter < nCount; counter++)
{
SST25WriteByte(*pD++, addr++);
}
// VERIFY
for(counter = 0; counter < nCount; counter++)
{
if(*pData != SST25ReadByte(address))
return (0);
pData++;
address++;
}
return (1);
}
/************************************************************************
* Function: void SST25ReadArray(DWORD address, BYTE* pData, nCount)
*
* Overview: this function reads data into buffer specified
*
* Input: flash memory address, pointer to the data buffer, data number
*
************************************************************************/
void SST25ReadArray(DWORD address, BYTE *pData, WORD nCount)
{
SST25SSLow();
SPIPut(SST25_CMD_READ);
SPIGet();
SPIPut(((DWORD_VAL) address).v[2]);
SPIGet();
SPIPut(((DWORD_VAL) address).v[1]);
SPIGet();
SPIPut(((DWORD_VAL) address).v[0]);
SPIGet();
while(nCount--)
{
SPIPut(0);
*pData++ = SPIGet();
}
SST25SSHigh();
}
/************************************************************************
* Function: void SST25ChipErase(void)
*
* Overview: chip erase
*
* Input: none
*
************************************************************************/
void SST25ChipErase(void)
{
SST25WriteEnable();
SST25SSLow();
SPIPut(SST25_CMD_ERASE);
SPIGet();
SST25SSHigh();
// Wait for write end
while(SST25IsWriteBusy());
}
/************************************************************************
* Function: void SST25ResetWriteProtection()
*
* Overview: this function reset write protection bits
*
* Input: none
*
* Output: none
*
************************************************************************/
void SST25ResetWriteProtection(void)
{
SST25SSLow();
SPIPut(SST25_CMD_EWSR);
SPIGet();
SST25SSHigh();
SST25SSLow();
SPIPut(SST25_CMD_WRSR);
SPIGet();
SPIPut(0);
SPIGet();
SST25SSHigh();
}
/************************************************************************
* Function: void SST25SectorErase(DWORD address)
*
* Overview: this function erases a 4Kb sector
*
* Input: address within sector to be erased
*
* Output: none
*
************************************************************************/
void SST25SectorErase(DWORD address)
{
SST25WriteEnable();
SST25SSLow();
SPIPut(SST25_CMD_SER);
SPIGet();
SPIPut(((DWORD_VAL) address).v[2]);
SPIGet();
SPIPut(((DWORD_VAL) address).v[1]);
SPIGet();
SPIPut(((DWORD_VAL) address).v[0]);
SPIGet();
SST25SSHigh();
// Wait for write end
DelayMs(100);
while(SST25IsWriteBusy());
}