-
Notifications
You must be signed in to change notification settings - Fork 0
/
sfm.c
300 lines (274 loc) · 9.32 KB
/
sfm.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
/*
* File: sfm.c
* Author: Peter Thornton
* Purpose: Functions test and use the integrated Serial Flash Memory (SFM)
* Created on: 13 May 2020
*/
// Note: the chip-select and write-protect pins are defined in spi.h
#include "xc.h"
#include "clock.h"
#include "spi.h" // low-level function prototypes and pin definitions for SPI3
#include "sfm.h"
// some basic SFM commands for AT25DF641
#define SFM_WRSR 0x01 // write status register
#define SFM_WRITE 0x02 // write command
#define SFM_READ 0x03 // read command
#define SFM_WDI 0x04 // write disable
#define SFM_RDSR 0x05 // read status register
#define SFM_WEN 0x06 // write enable
#define SFM_SECTUNP 0x39 // 64Kbyte sector unprotect (followed by 3-byte sector address)
#define SFM_SECTPRO 0x36 // 64Kbyte sector protect (followed by 3-byte sector address)
#define SFM_SECTRDR 0x3c // read the sector protect register for following address
#define SFM_ER4K 0x20 // Erase a 4 KByte block at following address
#define SFM_ER32K 0x52 // Erase a 32 Kbyte block at following address
#define SFM_ER64K 0xd8 // Erase a 64 Kbyte block at following address
int ReadSR( void)
{
// read the SFM status register and return its value
int srbyte1;
CS_SFM = 0;
write_spi3(SFM_RDSR);
srbyte1 = write_spi3(0);
CS_SFM = 1;
return srbyte1;
}
int sfm_read_1byte(int adr1, int adr2, int adr3)
{
// adr1, adr2, adr3 are the 24-bit address for the byte to read
int read_data;
CS_SFM = 0; // select SFM
write_spi3(SFM_READ);
write_spi3(adr1);
write_spi3(adr2);
write_spi3(adr3);
read_data = write_spi3(0);
CS_SFM = 1; // deselect SFM
return read_data;
}
void sfm_write_1byte(int adr1, int adr2, int adr3, int data)
{
// adr1, adr2, adr3 are the 24-bit address for the byte to write
// and data is the value to write
// write enable to change write protection
CS_SFM = 0; // select the SFM
write_spi3(SFM_WEN); // send write enable command
CS_SFM = 1; // deselect, terminate command
// turn off the write protect flag for sector
CS_SFM = 0;
write_spi3(SFM_SECTUNP);
write_spi3(adr1);
write_spi3(adr2);
write_spi3(adr3);
CS_SFM = 1;
// wait for the unprotect operation to complete by monitoring bit 0 of the SR
while (ReadSR() & 0x1);
// Erase the designated 4 KByte block (must be erased before writing)
// send write-enable command
CS_SFM = 0;
write_spi3(SFM_WEN);
CS_SFM = 1;
// send block erase command
CS_SFM = 0;
write_spi3(SFM_ER4K);
write_spi3(adr1);
write_spi3(adr2);
write_spi3(adr3);
CS_SFM = 1;
// wait for the erase operation to complete by monitoring bit 0 of the SR
while (ReadSR() & 0x1);
// write the data value to SFM
// send the write enable command
CS_SFM = 0;
write_spi3(SFM_WEN);
CS_SFM = 1;
// send the write command, address, and data
CS_SFM = 0;
write_spi3(SFM_WRITE);
write_spi3(adr1);
write_spi3(adr2);
write_spi3(adr3);
write_spi3(data);
CS_SFM = 1;
// wait for the write operation to complete by monitoring bit 0 of the SR
while (ReadSR() & 0x1);
}
void sfm_erase_4k(int adr1, int adr2, int adr3)
{
// adr1, adr2, adr3 are the 24-bit address within the 4k block to erase
// write enable to change write protection
CS_SFM = 0; // select the SFM
write_spi3(SFM_WEN); // send write enable command
CS_SFM = 1; // deselect, terminate command
// turn off the write protect flag for sector
CS_SFM = 0;
write_spi3(SFM_SECTUNP);
write_spi3(adr1);
write_spi3(adr2);
write_spi3(adr3);
CS_SFM = 1;
// wait for the unprotect operation to complete by monitoring bit 0 of the SR
while (ReadSR() & 0x1);
// Erase the designated 4 KByte block (must be erased before writing)
// send write-enable command
CS_SFM = 0;
write_spi3(SFM_WEN);
CS_SFM = 1;
// send block erase command
CS_SFM = 0;
write_spi3(SFM_ER4K);
write_spi3(adr1);
write_spi3(adr2);
write_spi3(adr3);
CS_SFM = 1;
// wait for the erase operation to complete by monitoring bit 0 of the SR
while (ReadSR() & 0x1);
}
void sfm_erase_64k(int sector)
{
// sector is the sector number (0-127) to be erased
// device addressing is 24-bit, so the 64k sector number translates
// to the MSB, with the other two bytes being 0.
// write enable to change write protection
CS_SFM = 0; // select the SFM
write_spi3(SFM_WEN); // send write enable command
CS_SFM = 1; // deselect, terminate command
// turn off the write protect flag for sector
CS_SFM = 0;
write_spi3(SFM_SECTUNP);
write_spi3(sector);
write_spi3(0);
write_spi3(0);
CS_SFM = 1;
// wait for the unprotect operation to complete by monitoring bit 0 of the SR
while (ReadSR() & 0x1);
// Erase the designated 64 KByte sector (must be erased before writing)
// send write-enable command
CS_SFM = 0;
write_spi3(SFM_WEN);
CS_SFM = 1;
// send sector erase command
CS_SFM = 0;
write_spi3(SFM_ER64K);
write_spi3(sector);
write_spi3(0);
write_spi3(0);
CS_SFM = 1;
// wait for the erase operation to complete by monitoring bit 0 of the SR
while (ReadSR() & 0x1);
}
void sfm_write_page(int sector, int page, char* data, int nbytes)
{
int i;
// write nbytes from the data array to specified sector and page of SFM
// send the write enable command
CS_SFM = 0;
write_spi3(SFM_WEN);
CS_SFM = 1;
// send the write command, address, and data
CS_SFM = 0;
write_spi3(SFM_WRITE);
write_spi3(sector);
write_spi3(page);
write_spi3(0);
// loop through data array and write all bytes
for (i=0 ; i<nbytes ; i++)
{
write_spi3((int)*data++);
}
CS_SFM = 1;
// wait for the write operation to complete by monitoring bit 0 of the SR
while (ReadSR() & 0x1);
}
void sfm_read_page(int sector, int page, int* data)
{
int i;
// assumes that data is a 256-byte long array
// and that the given page on the given sector is previously written and ready to read.
CS_SFM = 0;
write_spi3(SFM_READ);
write_spi3(sector);
write_spi3(page);
write_spi3(0);
// loop through data array and read all bytes
for (i=0 ; i<256 ; i++)
{
*data++ = write_spi3(0);
}
CS_SFM = 1;
}
int test_sfm(void)
{
// do a test write and read operation to the SFM
// return is_error = 0 (correct write/read), 1 (incorrect write/read)
// set the 24-bit start address for write/read test
int addr1 = 0x00;
int addr2 = 0x00;
int addr3 = 0x00;
// before any data can be written to the chip, the sector protection
// must be turned off for the target sector (turned on by default at power-up)
// The device determines the correct sector based on 24-bit address sent
// send write-enable command
CS_SFM = 0; // select the SFM
write_spi3(SFM_WEN); // send write enable command
CS_SFM = 1; // deselect, terminate command
// send sector unprotect command
CS_SFM = 0;
write_spi3(SFM_SECTUNP);
write_spi3(addr1);
write_spi3(addr2);
write_spi3(addr3);
CS_SFM = 1;
// Erase the first 4 KByte block in this sector, for write testing
// The device determines the correct block based on 24-bit address sent
// send write-enable command
CS_SFM = 0;
write_spi3(SFM_WEN);
CS_SFM = 1;
// send block erase command
CS_SFM = 0;
write_spi3(SFM_ER4K);
write_spi3(addr1);
write_spi3(addr2);
write_spi3(addr3);
CS_SFM = 1;
// wait for the write operation to complete by monitoring bit 0 of the SR
while (ReadSR() & 0x1);
// Write a few bytes of test data starting at defined address
// set test values for data bytes (use any non-zero values)
int write_data1 = 0xaa;
int write_data2 = 0xbb;
int write_data3 = 0xcc;
// send the write enable command
CS_SFM = 0;
write_spi3(SFM_WEN);
CS_SFM = 1;
// send the write command, address, and data
CS_SFM = 0;
write_spi3(SFM_WRITE);
write_spi3(addr1);
write_spi3(addr2);
write_spi3(addr3);
write_spi3(write_data1);
write_spi3(write_data2);
write_spi3(write_data3);
CS_SFM = 1;
// wait for the write operation to complete by monitoring bit 0 of the SR
while (ReadSR() & 0x1);
// Read back these bytes of data
int read_data1 = 0;
int read_data2 = 0;
int read_data3 = 0;
CS_SFM = 0;
write_spi3(SFM_READ);
write_spi3(addr1);
write_spi3(addr2);
write_spi3(addr3);
read_data1 = write_spi3(0);
read_data2 = write_spi3(0);
read_data3 = write_spi3(0);
CS_SFM = 1;
// compare the write data with the read data, and return
int is_error = (!(read_data1==write_data1 && read_data2==write_data2 &&
read_data3==write_data3));
return is_error;
}