-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
472 lines (426 loc) · 14.7 KB
/
main.cpp
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
#include <cstdio>
#include <cstdlib>
#include <cstdint>
#include <cctype>
#include <array>
#include <time.h>
#include <SDCard_info.h>
#include "SDPolicies.h"
#include "SDCard.hpp"
//#undef _WIN32
#include "FatLib/source/ff.h"
#include "FatLib/source/diskio.h"
SPIDriver spiTester;
std::string SpiDriverPort;
sd::SpiCard<SPIShim, sd::noCRC> sdcard;
int test_diskio (
BYTE pdrv, /* Physical drive number to be checked (all data on the drive will be lost) */
UINT ncyc, /* Number of test cycles */
DWORD* buff, /* Pointer to the working buffer */
UINT sz_buff /* Size of the working buffer in unit of byte */
);
/* Pseudo random number generator */
/* 0:Initialize, !0:Read */
static DWORD pn ( DWORD pns);
int main(int argc, char *argv[])
{
if (argc < 2) {
printf("Usage: spicl <PORTNAME>\n");
exit(1);
}
SpiDriverPort = std::string(argv[1]);
int rc;
DWORD buff[FF_MAX_SS]; /* Working buffer (4 sector in size) */
sdcard.begin();
auto ocr = sdcard.readOCR();
printf("ocr: 0x%02X %02X %02X %02X\n", ocr->raw[0], ocr->raw[1], ocr->raw[2], ocr->raw[3]);
auto cid = sdcard.readCID();
printf("cid: 0x");
for(const auto &c : cid->raw ) {
printf("%02X", c);
}
printf("\n");
auto csd = sdcard.readCSD();
printf("csd: 0x");
for(const auto &c : csd->raw ) {
printf("%02X", c);
}
printf("\n");
printf("Block Count: %d\n", csd->blockCount());
printf("CardSize: %d\n", csd->cardCapacity());
/* Check function/compatibility of the physical drive #0 */
rc = test_diskio(0, 3, buff, sizeof buff);
if (rc) {
printf("Sorry the function/compatibility test failed. (rc=%d)\nFatFs will not work with this disk driver.\n", rc);
}
else {
printf("Congratulations! The disk driver works well.\n");
}
// BYTE Buff[4096]; /* Working buffer */
// FATFS FatFs; /* FatFs work area needed for each volume */
// FIL Fil; /* File object needed for each open file */
// UINT bw;
//
// f_mount(&FatFs, "", 0); /* Give a work area to the default drive */
//
// auto status = f_open(&Fil, "newfile.txt", FA_WRITE | FA_CREATE_ALWAYS);
//
// if(status == FR_NO_FILESYSTEM) {
// status = f_mkfs("", FM_FAT32, 0, Buff, 4096);
// status = f_open(&Fil, "newfile.txt", FA_WRITE | FA_CREATE_ALWAYS);
// }
//
// if (status == FR_OK) { /* Create a file */
//
// f_write(&Fil, "It works!\r\n", 11, &bw); /* Write data to the file */
//
// f_close(&Fil); /* Close the file */
//
// }
return 0;
}
static DSTATUS stat = STA_NOINIT;
DSTATUS disk_status ( BYTE pdrv ) {
(void)pdrv;
return stat;
}
DSTATUS disk_initialize ( BYTE pdrv ) {
(void)pdrv;
if(!sdcard.begin()) {
stat = STA_NODISK;
}
else {
stat = 0;
}
return stat;
}
DRESULT disk_read (
BYTE pdrv, /* [IN] Physical drive number */
BYTE* buff, /* [OUT] Pointer to the read data buffer */
DWORD sector, /* [IN] Start sector number */
UINT count /* [IN] Number of sectros to read */
)
{
(void)pdrv;
const ssize_t err = sdcard.readBlocks(sector, buff, count);
return err == count ? RES_OK : RES_ERROR;
}
DRESULT disk_write (
BYTE pdrv, /* [IN] Physical drive number */
const BYTE* buff, /* [IN] Pointer to the data to be written */
DWORD sector, /* [IN] Sector number to write from */
UINT count /* [IN] Number of sectors to write */
)
{
(void)pdrv;
const ssize_t err = sdcard.writeBlocks(sector, buff, count);
return err == count ? RES_OK : RES_ERROR;
}
DRESULT disk_ioctl (
BYTE pdrv, /* [IN] Drive number */
BYTE cmd, /* [IN] Control command code */
void* buff /* [I/O] Parameter and data buffer */
)
{
(void)pdrv;
switch(cmd) {
case CTRL_SYNC :
break;
case GET_SECTOR_COUNT :
{
if(auto cap = sdcard.cardCapacity(); cap.has_value()) {
*((DWORD*)buff) = *cap;
}
else {
*((DWORD*)buff) = 0;
}
break;
}
case GET_SECTOR_SIZE :
*((DWORD*)buff) = 512;
break;
case GET_BLOCK_SIZE :
*((DWORD*)buff) = 1;
break;
case CTRL_TRIM :
break;
default: break;
}
return RES_OK;
}
DWORD get_fattime (void) {
time_t rawtime;
struct tm * timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
DWORD fatTime = 0;
fatTime |= ( (timeinfo->tm_year - 80)<<25 );
fatTime |= ( (timeinfo->tm_mon + 1)<<21 );
fatTime |= ( (timeinfo->tm_mday)<<16 );
fatTime |= ( (timeinfo->tm_hour)<<11 );
fatTime |= ( (timeinfo->tm_min)<<5 );
fatTime |= ( (timeinfo->tm_sec / 2)<<0 );
return fatTime;
}
/* Pseudo random number generator */
static DWORD pn (
DWORD pns /* 0:Initialize, !0:Read */
)
{
static DWORD lfsr;
UINT n;
if (pns) {
lfsr = pns;
for (n = 0; n < 32; n++) pn(0);
}
if (lfsr & 1) {
lfsr >>= 1;
lfsr ^= 0x80200003;
} else {
lfsr >>= 1;
}
return lfsr;
}
#define FAT_TEST_PRINT(...) do { fprintf(stdout, __VA_ARGS__); fflush(stdout); } while(0)
int test_diskio (
BYTE pdrv, /* Physical drive number to be checked (all data on the drive will be lost) */
UINT ncyc, /* Number of test cycles */
DWORD* buff, /* Pointer to the working buffer */
UINT sz_buff /* Size of the working buffer in unit of byte */
)
{
UINT n, cc, ns;
DWORD sz_drv, lba, lba2, sz_eblk, pns = 1;
WORD sz_sect;
BYTE *pbuff = (BYTE*)buff;
DSTATUS ds;
DRESULT dr;
FAT_TEST_PRINT("test_diskio(%u, %u, 0x%08X, 0x%08X)\n", pdrv, ncyc, (uint64_t)buff, sz_buff);
if (sz_buff < FF_MAX_SS + 4) {
FAT_TEST_PRINT("Insufficient work area to run program.\n");
return 1;
}
for (cc = 1; cc <= ncyc; cc++) {
FAT_TEST_PRINT("**** Test cycle %u of %u start ****\n", cc, ncyc);
FAT_TEST_PRINT(" disk_initalize(%u)", pdrv);
ds = disk_initialize(pdrv);
if (ds & STA_NOINIT) {
FAT_TEST_PRINT(" - failed.\n");
return 2;
} else {
FAT_TEST_PRINT(" - ok.\n");
}
FAT_TEST_PRINT("**** Get drive size ****\n");
FAT_TEST_PRINT(" disk_ioctl(%u, GET_SECTOR_COUNT, 0x%08X)", pdrv, (uint64_t)&sz_drv);
sz_drv = 0;
dr = disk_ioctl(pdrv, GET_SECTOR_COUNT, &sz_drv);
if (dr == RES_OK) {
FAT_TEST_PRINT(" - ok.\n");
} else {
FAT_TEST_PRINT(" - failed.\n");
return 3;
}
if (sz_drv < 128) {
FAT_TEST_PRINT("Failed: Insufficient drive size to test.\n");
return 4;
}
FAT_TEST_PRINT(" Number of sectors on the drive %u is %lu.\n", pdrv, sz_drv);
#if FF_MAX_SS != FF_MIN_SS
FAT_TEST_PRINT("**** Get sector size ****\n");
FAT_TEST_PRINT(" disk_ioctl(%u, GET_SECTOR_SIZE, 0x%X)", pdrv, (uint64_t)&sz_sect);
sz_sect = 0;
dr = disk_ioctl(pdrv, GET_SECTOR_SIZE, &sz_sect);
if (dr == RES_OK) {
FAT_TEST_PRINT(" - ok.\n");
} else {
FAT_TEST_PRINT(" - failed.\n");
return 5;
}
FAT_TEST_PRINT(" Size of sector is %u bytes.\n", sz_sect);
#else
sz_sect = FF_MAX_SS;
#endif
FAT_TEST_PRINT("**** Get block size ****\n");
FAT_TEST_PRINT(" disk_ioctl(%u, GET_BLOCK_SIZE, 0x%X)", pdrv, (uint64_t)&sz_eblk);
sz_eblk = 0;
dr = disk_ioctl(pdrv, GET_BLOCK_SIZE, &sz_eblk);
if (dr == RES_OK) {
FAT_TEST_PRINT(" - ok.\n");
} else {
FAT_TEST_PRINT(" - failed.\n");
}
if (dr == RES_OK || sz_eblk >= 2) {
FAT_TEST_PRINT(" Size of the erase block is %lu sectors.\n", sz_eblk);
} else {
FAT_TEST_PRINT(" Size of the erase block is unknown.\n");
}
/* Single sector write test */
FAT_TEST_PRINT("**** Single sector write test 1 ****\n");
lba = 0;
for (n = 0, pn(pns); n < sz_sect; n++) pbuff[n] = (BYTE)pn(0);
FAT_TEST_PRINT(" disk_write(%u, 0x%X, %lu, 1)", pdrv, (uint64_t)pbuff, lba);
dr = disk_write(pdrv, pbuff, lba, 1);
if (dr == RES_OK) {
FAT_TEST_PRINT(" - ok.\n");
} else {
FAT_TEST_PRINT(" - failed.\n");
return 6;
}
FAT_TEST_PRINT(" disk_ioctl(%u, CTRL_SYNC, NULL)", pdrv);
dr = disk_ioctl(pdrv, CTRL_SYNC, 0);
if (dr == RES_OK) {
FAT_TEST_PRINT(" - ok.\n");
} else {
FAT_TEST_PRINT(" - failed.\n");
return 7;
}
memset(pbuff, 0, sz_sect);
FAT_TEST_PRINT(" disk_read(%u, 0x%X, %lu, 1)", pdrv, (uint64_t)pbuff, lba);
dr = disk_read(pdrv, pbuff, lba, 1);
if (dr == RES_OK) {
FAT_TEST_PRINT(" - ok.\n");
} else {
FAT_TEST_PRINT(" - failed.\n");
return 8;
}
for (n = 0, pn(pns); n < sz_sect && pbuff[n] == (BYTE)pn(0); n++) ;
if (n == sz_sect) {
FAT_TEST_PRINT(" Data matched.\n");
} else {
FAT_TEST_PRINT("Failed: Read data differs from the data written.\n");
return 10;
}
pns++;
FAT_TEST_PRINT("**** Multiple sector write test ****\n");
lba = 1; ns = sz_buff / sz_sect;
if (ns > 4) ns = 4;
for (n = 0, pn(pns); n < (uint64_t)(sz_sect * ns); n++) pbuff[n] = (BYTE)pn(0);
FAT_TEST_PRINT(" disk_write(%u, 0x%X, %lu, %u)", pdrv, (uint64_t)pbuff, lba, ns);
dr = disk_write(pdrv, pbuff, lba, ns);
if (dr == RES_OK) {
FAT_TEST_PRINT(" - ok.\n");
} else {
FAT_TEST_PRINT(" - failed.\n");
return 11;
}
FAT_TEST_PRINT(" disk_ioctl(%u, CTRL_SYNC, NULL)", pdrv);
dr = disk_ioctl(pdrv, CTRL_SYNC, 0);
if (dr == RES_OK) {
FAT_TEST_PRINT(" - ok.\n");
} else {
FAT_TEST_PRINT(" - failed.\n");
return 12;
}
memset(pbuff, 0, sz_sect * ns);
FAT_TEST_PRINT(" disk_read(%u, 0x%X, %lu, %u)", pdrv, (uint64_t)pbuff, lba, ns);
dr = disk_read(pdrv, pbuff, lba, ns);
if (dr == RES_OK) {
FAT_TEST_PRINT(" - ok.\n");
} else {
FAT_TEST_PRINT(" - failed.\n");
return 13;
}
for (n = 0, pn(pns); n < (uint64_t)(sz_sect * ns) && pbuff[n] == (BYTE)pn(0); n++) ;
if (n == (uint64_t)(sz_sect * ns)) {
FAT_TEST_PRINT(" Data matched.\n");
} else {
FAT_TEST_PRINT("Failed: Read data differs from the data written.\n");
return 14;
}
pns++;
FAT_TEST_PRINT("**** Single sector write test (misaligned address) ****\n");
lba = 5;
for (n = 0, pn(pns); n < sz_sect; n++) pbuff[n+3] = (BYTE)pn(0);
FAT_TEST_PRINT(" disk_write(%u, 0x%X, %lu, 1)", pdrv, (uint64_t)(pbuff+3), lba);
dr = disk_write(pdrv, pbuff+3, lba, 1);
if (dr == RES_OK) {
FAT_TEST_PRINT(" - ok.\n");
} else {
FAT_TEST_PRINT(" - failed.\n");
return 15;
}
FAT_TEST_PRINT(" disk_ioctl(%u, CTRL_SYNC, NULL)", pdrv);
dr = disk_ioctl(pdrv, CTRL_SYNC, 0);
if (dr == RES_OK) {
FAT_TEST_PRINT(" - ok.\n");
} else {
FAT_TEST_PRINT(" - failed.\n");
return 16;
}
memset(pbuff+5, 0, sz_sect);
FAT_TEST_PRINT(" disk_read(%u, 0x%X, %lu, 1)", pdrv, (uint64_t)(pbuff+5), lba);
dr = disk_read(pdrv, pbuff+5, lba, 1);
if (dr == RES_OK) {
FAT_TEST_PRINT(" - ok.\n");
} else {
FAT_TEST_PRINT(" - failed.\n");
return 17;
}
for (n = 0, pn(pns); n < sz_sect && pbuff[n+5] == (BYTE)pn(0); n++) ;
if (n == sz_sect) {
FAT_TEST_PRINT(" Data matched.\n");
} else {
FAT_TEST_PRINT("Failed: Read data differs from the data written.\n");
return 18;
}
pns++;
FAT_TEST_PRINT("**** 4GB barrier test ****\n");
if (sz_drv >= 128 + 0x80000000 / (sz_sect / 2)) {
lba = 6; lba2 = lba + 0x80000000 / (sz_sect / 2);
for (n = 0, pn(pns); n < (uint64_t)(sz_sect * 2); n++) pbuff[n] = (BYTE)pn(0);
FAT_TEST_PRINT(" disk_write(%u, 0x%X, %lu, 1)", pdrv, (uint64_t)pbuff, lba);
dr = disk_write(pdrv, pbuff, lba, 1);
if (dr == RES_OK) {
FAT_TEST_PRINT(" - ok.\n");
} else {
FAT_TEST_PRINT(" - failed.\n");
return 19;
}
FAT_TEST_PRINT(" disk_write(%u, 0x%X, %lu, 1)", pdrv, (uint64_t)(pbuff+sz_sect), lba2);
dr = disk_write(pdrv, pbuff+sz_sect, lba2, 1);
if (dr == RES_OK) {
FAT_TEST_PRINT(" - ok.\n");
} else {
FAT_TEST_PRINT(" - failed.\n");
return 20;
}
FAT_TEST_PRINT(" disk_ioctl(%u, CTRL_SYNC, NULL)", pdrv);
dr = disk_ioctl(pdrv, CTRL_SYNC, 0);
if (dr == RES_OK) {
FAT_TEST_PRINT(" - ok.\n");
} else {
FAT_TEST_PRINT(" - failed.\n");
return 21;
}
memset(pbuff, 0, sz_sect * 2);
FAT_TEST_PRINT(" disk_read(%u, 0x%X, %lu, 1)", pdrv, (uint64_t)pbuff, lba);
dr = disk_read(pdrv, pbuff, lba, 1);
if (dr == RES_OK) {
FAT_TEST_PRINT(" - ok.\n");
} else {
FAT_TEST_PRINT(" - failed.\n");
return 22;
}
FAT_TEST_PRINT(" disk_read(%u, 0x%X, %lu, 1)", pdrv, (uint64_t)(pbuff+sz_sect), lba2);
dr = disk_read(pdrv, pbuff+sz_sect, lba2, 1);
if (dr == RES_OK) {
FAT_TEST_PRINT(" - ok.\n");
} else {
FAT_TEST_PRINT(" - failed.\n");
return 23;
}
for (n = 0, pn(pns); pbuff[n] == (BYTE)pn(0) && n < (uint64_t)(sz_sect * 2); n++) ;
if (n == (uint64_t)(sz_sect * 2)) {
FAT_TEST_PRINT(" Data matched.\n");
} else {
FAT_TEST_PRINT("Failed: Read data differs from the data written.\n");
return 24;
}
} else {
FAT_TEST_PRINT(" Test skipped.\n");
}
pns++;
FAT_TEST_PRINT("**** Test cycle %u of %u completed ****\n\n", cc, ncyc);
}
return 0;
}