-
Notifications
You must be signed in to change notification settings - Fork 0
/
spi_flash_model_test.c
322 lines (294 loc) · 10.8 KB
/
spi_flash_model_test.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
/*************************************************************************
@author: Andreas Kaeberlein
@copyright: Copyright 2022
@credits: AKAE
@license: BSDv3
@maintainer: Andreas Kaeberlein
@email: andreas.kaeberlein@web.de
@file: spi_flash_model_test.c
@date: 2022-12-18
@see: https://github.com/akaeba/spi_flash_model
@brief: unit test
tests spi flash model
*************************************************************************/
/** Includes **/
/* Standard libs */
#include <stdlib.h> // EXIT codes, malloc
#include <stdio.h> // f.e. printf
#include <stdint.h> // defines fixed data types: int8_t...
#include <stddef.h> // various variable types and macros: size_t, offsetof, NULL, ...
#include <string.h> // string operation: memset, memcpy
#include <strings.h> // strcasecmp
/* Self */
#include "spi_flash_model.h" // function prototypes
/**
* Main
* ----
*/
int main ()
{
/** Variables **/
t_sfm spiFlash; // handle to SPI Flash
uint8_t spi[1024]; // spi buffer
uint32_t spiLen; // spiLen
FILE *fp; // file handle
size_t len = 0; // line length
char *line = NULL; // buffer line
/* entry message */
printf("INFO:%s: unit test started\n", __FUNCTION__);
/* sfm_init */
printf("INFO:%s: sfm_init\n", __FUNCTION__);
if ( 0 != sfm_init( &spiFlash, "W25q16JV" ) ) {
printf("ERROR:%s:sfm_init\n", __FUNCTION__);
goto ERO_END;
}
/* enable advanced output */
spiFlash.uint8MsgLevel = 1;
/* sfm_dump */
printf("INFO:%s: sfm_dump\n", __FUNCTION__);
if ( 0 != sfm_dump( &spiFlash, 0, 256 ) ) {
printf("ERROR:%s:sfm_dump\n", __FUNCTION__);
goto ERO_END;
}
/* sfm: Read Manufacturer / Device ID */
printf("INFO:%s:sfm: Read Manufacturer / Device ID\n", __FUNCTION__);
spiLen = 6;
memset(spi, 0, spiLen);
spi[0] = 0x90;
if ( 0 != sfm(&spiFlash, spi, spiLen) ) {
printf("ERROR:%s:sfm: Read Manufacturer / Device ID\n", __FUNCTION__);
goto ERO_END;
}
if ( (0xef != spi[4]) || (0x14 != spi[5]) ) {
printf("ERROR:%s:sfm: Wrong ID %02x%02x\n", __FUNCTION__, spi[4], spi[5]);
goto ERO_END;
}
/* sfm: Write Enable */
printf("INFO:%s:sfm: Write Enable\n", __FUNCTION__);
spiLen = 1;
memset(spi, 0, spiLen);
spi[0] = 0x06;
if ( 0 != sfm(&spiFlash, spi, spiLen) ) {
printf("ERROR:%s:sfm: Write Enable\n", __FUNCTION__);
goto ERO_END;
}
/* sfm: Write Disable */
printf("INFO:%s:sfm: Write Disable\n", __FUNCTION__);
spiLen = 1;
memset(spi, 0, spiLen);
spi[0] = 0x04;
if ( 0 != sfm(&spiFlash, spi, spiLen) ) {
printf("ERROR:%s:sfm: Write Disable\n", __FUNCTION__);
goto ERO_END;
}
/* sfm: Read Status Register */
printf("INFO:%s:sfm: Read Status Register\n", __FUNCTION__);
spiLen = 2;
spi[0] = 0x05;
if ( 0 != sfm(&spiFlash, spi, spiLen) ) {
printf("ERROR:%s:sfm: Read Status Register\n", __FUNCTION__);
goto ERO_END;
}
if ( 0 != spi[1] ) {
printf("ERROR:%s:sfm: Invalid Status Register value\n", __FUNCTION__);
goto ERO_END;
}
/* sfm: chip erase */
printf("INFO:%s:sfm: Chip erase\n", __FUNCTION__);
spiLen = 1;
spi[0] = 0x06;
if ( 0 != sfm(&spiFlash, spi, spiLen) ) {
printf("ERROR:%s:sfm: Write Enable\n", __FUNCTION__);
goto ERO_END;
}
spi[0] = 0xc7;
if ( 0 != sfm(&spiFlash, spi, spiLen) ) {
printf("ERROR:%s:sfm: chip erase\n", __FUNCTION__);
goto ERO_END;
}
// poll for WIP
for ( uint8_t i = 0; i < (SFM_WIP_RETRY_IDLE + 1); i++ ) {
spiLen = 2;
spi[0] = 0x05;
if ( 0 != sfm(&spiFlash, spi, spiLen) ) {
printf("ERROR:%s:sfm: Read Status Register\n", __FUNCTION__);
goto ERO_END;
}
printf("ERROR:%s:sfm: Status Register 1 = 0x%02x\n", __FUNCTION__, spi[1]);
if ( i < SFM_WIP_RETRY_IDLE ) { // WIP
if ( 0 == (spi[1] & 0x01) ) {
printf("ERROR:%s:sfm: Expected active WIP\n", __FUNCTION__);
goto ERO_END;
}
} else { // no WIP
if ( 0 != (spi[1] & 0x01) ) {
printf("ERROR:%s:sfm: Expected inactive WIP\n", __FUNCTION__);
goto ERO_END;
}
}
}
/* sfm: Sector erase */
printf("INFO:%s:sfm: Sector erase\n", __FUNCTION__);
spiLen = 1;
spi[0] = 0x06;
if ( 0 != sfm(&spiFlash, spi, spiLen) ) {
printf("ERROR:%s:sfm: Write Enable\n", __FUNCTION__);
goto ERO_END;
}
spiLen = 4;
spi[0] = 0x20;
spi[1] = 0x1F; // last sector in flash
spi[2] = 0xF0;
spi[3] = 0x10;
if ( 0 != sfm(&spiFlash, spi, spiLen) ) {
printf("ERROR:%s:sfm: sector erase\n", __FUNCTION__);
goto ERO_END;
}
// poll for WIP
for ( uint8_t i = 0; i < (SFM_WIP_RETRY_IDLE + 1); i++ ) {
spiLen = 2;
spi[0] = 0x05;
if ( 0 != sfm(&spiFlash, spi, spiLen) ) {
printf("ERROR:%s:sfm: Read Status Register\n", __FUNCTION__);
goto ERO_END;
}
printf("ERROR:%s:sfm: Status Register 1 = 0x%02x\n", __FUNCTION__, spi[1]);
if ( i < SFM_WIP_RETRY_IDLE ) { // WIP
if ( 0 == (spi[1] & 0x01) ) {
printf("ERROR:%s:sfm: Expected active WIP\n", __FUNCTION__);
goto ERO_END;
}
} else { // no WIP
if ( 0 != (spi[1] & 0x01) ) {
printf("ERROR:%s:sfm: Expected inactive WIP\n", __FUNCTION__);
goto ERO_END;
}
}
}
/* sfm: Read Data */
printf("INFO:%s:sfm: Read Data\n", __FUNCTION__);
spiLen = 6;
spi[0] = 0x03; // instruction
spi[1] = 0x0F; // address high byte
spi[2] = 0xFF; // address middle byte
spi[3] = 0x00; // address low byte
if ( 0 != sfm(&spiFlash, spi, spiLen) ) {
printf("ERROR:%s:sfm: Read Data\n", __FUNCTION__);
goto ERO_END;
}
if ( (0 != spi[0]) || (0 != spi[1]) || (0 != spi[2]) || (0 != spi[3]) || (0xff != spi[4]) || (0xff != spi[5]) ) {
printf("ERROR:%s:sfm: Invalid Read Data value\n", __FUNCTION__);
goto ERO_END;
}
/* sfm: Page Program */
printf("INFO:%s:sfm: Page Program\n", __FUNCTION__);
spiLen = 1;
spi[0] = 0x06;
if ( 0 != sfm(&spiFlash, spi, spiLen) ) {
printf("ERROR:%s:sfm: Write Enable\n", __FUNCTION__);
goto ERO_END;
}
spiLen = 8;
spi[0] = 0x02; // instruction
spi[1] = 0x00; // address high byte
spi[2] = 0x10; // address middle byte
spi[3] = 0x20; // address low byte
spi[4] = 0x01; // data
spi[5] = 0x23;
spi[6] = 0x45;
spi[7] = 0x67;
if ( 0 != sfm(&spiFlash, spi, spiLen) ) {
printf("ERROR:%s:sfm: Page Program\n", __FUNCTION__);
goto ERO_END;
}
if ( (0x01 != spiFlash.uint8PtrMem[0x1020]) ||
(0x23 != spiFlash.uint8PtrMem[0x1021]) ||
(0x45 != spiFlash.uint8PtrMem[0x1022]) ||
(0x67 != spiFlash.uint8PtrMem[0x1023])
) {
printf("ERROR:%s:sfm: Invalid Read Data value\n", __FUNCTION__);
goto ERO_END;
}
// poll for WIP
for ( uint8_t i = 0; i < (SFM_WIP_RETRY_IDLE + 1); i++ ) {
spiLen = 2;
spi[0] = 0x05;
if ( 0 != sfm(&spiFlash, spi, spiLen) ) {
printf("ERROR:%s:sfm: Read Status Register\n", __FUNCTION__);
goto ERO_END;
}
printf("ERROR:%s:sfm: Status Register 1 = 0x%02x\n", __FUNCTION__, spi[1]);
if ( i < SFM_WIP_RETRY_IDLE ) { // WIP
if ( 0 == (spi[1] & 0x01) ) {
printf("ERROR:%s:sfm: Expected active WIP\n", __FUNCTION__);
goto ERO_END;
}
} else { // no WIP
if ( 0 != (spi[1] & 0x01) ) {
printf("ERROR:%s:sfm: Expected inactive WIP\n", __FUNCTION__);
goto ERO_END;
}
}
}
// store to file
sfm_dump( &spiFlash, 0x1010, 0x1030 );
/* sfm_store */
printf("INFO:%s:sfm_store\n", __FUNCTION__);
if ( 0 != sfm_store(&spiFlash, "./flash.dif") ) {
printf("ERROR:%s:sfm_store: Failed to write file\n", __FUNCTION__);
goto ERO_END;
}
fp = fopen("./flash.dif", "r"); // open file for read
if ( NULL == fp ) {
printf("ERROR:%s:sfm_store: Failed to open file for read\n", __FUNCTION__);
goto ERO_END;
}
if ( getline(&line, &len, fp) ) {}; // read first line from file
if ( 0 != strcasecmp(line, "001020: 01 23 45 67 ff ff ff ff ff ff ff ff ff ff ff ff\n") ) {
printf("ERROR:%s:sfm_store: wrong values in file '%s'\n", __FUNCTION__, line);
goto ERO_END;
}
/* sfm_load */
printf("INFO:%s:sfm_load\n", __FUNCTION__);
if ( 0 != sfm_load(&spiFlash, "./test/flash_read.dif") ) {
printf("ERROR:%s:sfm_load: Failed to read file\n", __FUNCTION__);
goto ERO_END;
}
/* 00000: 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
00100: 00 10 20 30 40 50 60 70 80 90 A0 B0 C0 D0 E0 F0
*/
for ( uint8_t i = 0; i < 16; i++ ) {
if ( i != spiFlash.uint8PtrMem[i] ) {
printf("ERROR:%s:sfm_load: error byte=%x, is=%x, exp=%x\n", __FUNCTION__, i, spiFlash.uint8PtrMem[i], i);
sfm_dump( &spiFlash, 0x0, 0x10 );
goto ERO_END;
}
}
for ( uint8_t i = 0; i < 16; i++ ) {
if ( (i<<4) != spiFlash.uint8PtrMem[0x100+i] ) {
printf("ERROR:%s:sfm_load: error byte=%x, is=%x, exp=%x\n", __FUNCTION__, i, spiFlash.uint8PtrMem[i], i);
sfm_dump( &spiFlash, 0x90, 0x110 );
goto ERO_END;
}
}
/* sfm_cmp */
printf("INFO:%s:sfm_cmp\n", __FUNCTION__);
if ( 0 != sfm_cmp(&spiFlash, "./test/flash_read.dif") ) {
printf("ERROR:%s:sfm_cmp: Mismatch\n", __FUNCTION__);
goto ERO_END;
}
/* provoke compare error */
printf("INFO:%s:sfm_cmp: provoke error\n", __FUNCTION__);
spiFlash.uint8PtrMem[0x11] = 12;
if ( 0 == sfm_cmp(&spiFlash, "./test/flash_read.dif") ) {
printf("ERROR:%s:sfm_cmp: Mismatch expected\n", __FUNCTION__);
goto ERO_END;
}
/* graceful end */
printf("INFO:%s: Module test SUCCESSFUL :-)\n", __FUNCTION__);
exit(EXIT_SUCCESS);
/* abnormal end */
ERO_END:
printf("FAIL:%s: Module test FAILED :-(\n", __FUNCTION__);
exit(EXIT_FAILURE);
}