00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098 #include "board.h"
00099
00100 #include "libsdmmc.h"
00101 #include "../fatfs_config.h"
00102 #include "Media.h"
00103 #include "MEDSdcard.h"
00104 #include <stdint.h>
00105 #include <stdio.h>
00106 #include <string.h>
00107 #include <assert.h>
00108
00109
00110
00111
00112
00113
00114
00115 #define NB_MULTI_BLOCKS 5
00116
00117
00118 #define NB_SPLIT_MULTI 4
00119
00120
00121 #define TEST_BLOCK_START (0)
00122
00123
00124 #define TEST_BLOCK_END SD_GetNumberBlocks(&sdDrv[bMciID])
00125
00126
00127 #define TEST_BLOCK_SKIP (100 * 1024 * 2) // 100M
00128
00129
00130 #define NB_ERRORS 5
00131
00132
00133 #define AllOCSIZE 4096
00134
00135
00136
00137
00138
00139
00140 #define MAX_LUNS 1
00141
00142
00143 extern sMedia medias[MAX_LUNS];
00144
00145
00146 static sXdmad dmaDrv;
00147
00148
00149 static sMcid mciDrv[BOARD_NUM_MCI];
00150
00151
00152 extern sSdCard sdDrv[BOARD_NUM_MCI];
00153
00154
00155 static const Pin pinsSd[] = {BOARD_MCI_PINS_SLOTA, BOARD_MCI_PIN_CK};
00156
00157
00158 static const Pin pinsCd[] = {BOARD_MCI_PIN_CD};
00159
00160
00161 #define ID_DRV DRV_MMC
00162
00163 #if _FS_TINY == 0
00164 #define STR_ROOT_DIRECTORY "0:"
00165 #else
00166 #define STR_ROOT_DIRECTORY ""
00167 #endif
00168
00169 const char *FileName = STR_ROOT_DIRECTORY "Basic.bin";
00170 const char *FileNameReadMe = STR_ROOT_DIRECTORY "ReadMe.txt";
00171
00172 const char *ReadMeText = "Samv7 FatFS example: Done!!";
00173
00174
00175
00176 #define DATA_SIZE 4096
00177
00178 uint8_t data[DATA_SIZE];
00179
00180 typedef struct _ALIGN_FATFS {
00181 uint8_t padding[16];
00182 FATFS fs;
00183 } ALIGN_FATFS;
00184
00185 COMPILER_ALIGNED(32) ALIGN_FATFS aligned_fs;
00186
00187
00188 #define TEST_PERFORMENCT_SIZE (4*1024*1024)
00189
00190
00191 #define ASSERT(condition, ...) { \
00192 if (!(condition)) { \
00193 printf("-F- ASSERT: "); \
00194 printf(__VA_ARGS__); \
00195 } \
00196 }
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207 #define READ_MULTI
00208
00209
00210
00211
00212 #define WRITE_MULTI
00213
00214
00215
00216
00217
00218
00219
00220
00221 #ifdef READ_MULTI
00222 #define MMCT_ReadFun(pSd, blk, nbBlk, pData) \
00223 SD_Read(pSd, blk, pData, nbBlk, NULL, NULL)
00224 #else
00225 #define MMCT_ReadFun(pSd, blk, nbBlk, pData) \
00226 SD_ReadBlocks(pSd, blk, pData, nbBlk)
00227 #endif
00228
00229
00230
00231
00232
00233
00234
00235
00236 #ifdef WRITE_MULTI
00237 #define MMCT_WriteFun(pSd, blk, nbBlk, pData) \
00238 SD_Write(pSd, blk, pData, nbBlk, NULL, NULL)
00239 #else
00240 #define MMCT_WriteFun(pSd, blk, nbBlk, pData) \
00241 SD_WriteBlocks(pSd, blk, pData, nbBlk)
00242 #endif
00243
00244
00245
00246
00247
00248
00249
00250
00251 void XDMAC_Handler(void)
00252 {
00253 XDMAD_Handler(&dmaDrv);
00254 }
00255
00256
00257
00258
00259 void HSMCI_Handler(void)
00260 {
00261 MCID_Handler(&mciDrv[0]);
00262
00263 }
00264
00265
00266
00267
00268
00269
00270
00271
00272 static void CardDetectConfigure(void)
00273 {
00274 PIO_Configure(pinsCd, PIO_LISTSIZE(pinsCd));
00275
00276 }
00277
00278
00279
00280
00281 static uint8_t CardIsConnected(uint8_t iMci)
00282 {
00283 return PIO_Get(&pinsCd[iMci]) ? 0 : 1;
00284 }
00285
00286
00287
00288
00289 static uint8_t AnyCardIsConnected(void)
00290 {
00291 uint32_t i;
00292
00293 for (i = 0; i < BOARD_NUM_MCI; i++) {
00294 if (CardIsConnected(i))
00295 return 1;
00296 }
00297
00298 return 0;
00299 }
00300
00301
00302
00303
00304 static uint8_t CardIsProtected(void)
00305 {
00306 TRACE_INFO(" Cannot check if SD card is write-protected\n\r");
00307 return 0;
00308 }
00309
00310
00311
00312
00313 static void LoopDelay(volatile unsigned int loop)
00314 {
00315 for (; loop > 0; loop--);
00316 }
00317
00318
00319
00320
00321
00322
00323 static void DumpSeperator(void)
00324 {
00325 printf("\n\r==========================================\n\r");
00326 }
00327
00328
00329
00330
00331
00332
00333 static void DumpCardInfo(uint8_t iMci)
00334 {
00335 sSdCard *pSd = &sdDrv[iMci];
00336
00337 if (SD_GetCardType(pSd) & CARD_TYPE_bmSDIO)
00338 SDIO_DumpCardInformation(pSd);
00339
00340 if (SD_GetCardType(pSd) & CARD_TYPE_bmSDMMC) {
00341 SD_DumpCID(pSd->CID);
00342 SD_DumpCSD(pSd->CSD);
00343 }
00344 }
00345
00346
00347
00348
00349
00350 static uint8_t CardInit(uint8_t iMci)
00351 {
00352 uint8_t error;
00353 uint8_t retry = 2;
00354
00355 DumpSeperator();
00356
00357 while (retry--) {
00358 error = SD_Init(&sdDrv[iMci]);
00359
00360 if (error == SDMMC_OK) break;
00361 }
00362
00363 if (error) {
00364 TRACE_ERROR(" SD/MMC card initialization failed: %d\n\r", error);
00365 return 1;
00366 }
00367
00368 TRACE_INFO(" SD/MMC card initialization successful\n\r");
00369 TRACE_INFO(" Card size: %d MB", (int)SD_GetTotalSizeKB(&sdDrv[iMci]) / 1000);
00370 printf(", %d * %dB\n\r",
00371 (int)SD_GetNumberBlocks(&sdDrv[iMci]),
00372 (int)SD_GetBlockSize(&sdDrv[iMci]));
00373 DumpCardInfo(iMci);
00374 return 0;
00375 }
00376
00377
00378
00379
00380
00381
00382 static void _ConfigurePIOs(void)
00383 {
00384
00385 PIO_Configure(pinsSd, PIO_LISTSIZE(pinsSd));
00386
00387 CardDetectConfigure();
00388
00389 CardIsProtected();
00390 }
00391
00392
00393
00394
00395
00396
00397 static FRESULT scan_files (char *path)
00398 {
00399 FRESULT res;
00400 FILINFO fno;
00401 DIR dir;
00402 int32_t i;
00403 char *fn;
00404 #if _USE_LFN
00405 static char lfn[_MAX_LFN * (_DF1S ? 2 : 1) + 1];
00406 fno.lfname = lfn;
00407 fno.lfsize = sizeof(lfn);
00408 #endif
00409
00410
00411 res = f_opendir(&dir, path);
00412
00413 if (res == FR_OK) {
00414 i = strlen(path);
00415
00416 for (;; ) {
00417 res = f_readdir(&dir, &fno);
00418
00419 if (res != FR_OK || fno.fname[0] == 0) break;
00420
00421 #if _USE_LFN
00422 fn = *fno.lfname ? fno.lfname : fno.fname;
00423 #else
00424 fn = fno.fname;
00425 #endif
00426
00427 if (*fn == '.') continue;
00428
00429 if (fno.fattrib & AM_DIR) {
00430 sprintf(&path[i], "/%s", fn);
00431 res = scan_files(path);
00432
00433 if (res != FR_OK) break;
00434
00435 path[i] = 0;
00436 } else
00437 printf("%s/%s\n\r", path, fn);
00438 }
00439 }
00440
00441 return res;
00442 }
00443
00444 static uint8_t formatdisk(const TCHAR *pDrv)
00445 {
00446 FRESULT res;
00447 #if _FS_TINY == 0
00448
00449 TRACE_INFO(" Please wait a moment. Formatting the disk...\n\r");
00450 res = f_mkfs(pDrv,
00451 0,
00452 AllOCSIZE);
00453 TRACE_INFO(" Format disk finished !\n\r");
00454
00455 if (res != FR_OK) {
00456 TRACE_ERROR(" f_mkfs pb: 0x%X\n\r", res);
00457 return 0;
00458 }
00459
00460 return 1;
00461 #else
00462 TRACE_INFO(" Please run Full version FAT FS test first\n\r");
00463 return 0;
00464 #endif
00465 }
00466
00467
00468
00469
00470
00471 static uint8_t RunFsTest(void)
00472 {
00473 uint32_t i;
00474 uint32_t ByteToRead;
00475 uint32_t ByteRead;
00476 uint32_t len = 0;
00477 #if _FS_TINY == 0
00478 uint32_t ByteWritten;
00479 char key;
00480 #endif
00481
00482 const TCHAR Drv_Num = ID_DRV;
00483 FRESULT res;
00484 DIR dirs;
00485 FIL FileObject;
00486 uint32_t tickStart, tickEnd, ticks, rwSpeed;
00487
00488 gNbMedias = 1;
00489
00490 TRACE_INFO("Mount disk %d\n\r", ID_DRV);
00491
00492 if (0 != ( ((uint32_t)&aligned_fs.fs.win) & (32 - 1) )) {
00493 TRACE_ERROR("field win in FATFS should aligned to cache line!\n\r");
00494 return 0;
00495 }
00496
00497
00498 memset(&aligned_fs.fs, 0, sizeof(FATFS));
00499
00500
00501 res = f_mount(&aligned_fs.fs, &Drv_Num, 1);
00502
00503 if (res != FR_OK) {
00504
00505 if (res == FR_NO_FILESYSTEM) {
00506 TRACE_INFO("No file System found\n\r");
00507
00508 if (formatdisk(&Drv_Num)) {
00509
00510 res = f_mount(&aligned_fs.fs, &Drv_Num, 1);
00511
00512 if (res != FR_OK) {
00513 TRACE_ERROR("f_mount pb: 0x%X\n\r", res);
00514 return 0;
00515 }
00516 } else
00517 return 0;
00518 } else {
00519 TRACE_ERROR("f_mount pb: 0x%X\n\r", res);
00520 return 0;
00521 }
00522 }
00523
00524
00525 res = f_opendir (&dirs, STR_ROOT_DIRECTORY);
00526
00527 if (res == FR_OK) {
00528
00529 TRACE_INFO("The disk is already formatted.\n\r");
00530
00531
00532 TRACE_INFO(" Display files contained on the SDcard :\n\r");
00533 DumpSeperator();
00534 scan_files((char *)STR_ROOT_DIRECTORY);
00535 DumpSeperator();
00536
00537 #if _FS_TINY == 0
00538 TRACE_INFO("Do you want to erase the sdcard to re-format disk ? (y/n)!\n\r");
00539 key = DBG_GetChar();
00540
00541 if ((key == 'y') || (key == 'Y')) {
00542 for (i = 0; i < 100; i++)
00543 MEDSdcard_EraseBlock(&medias[ID_DRV], i);
00544
00545 TRACE_INFO(" Erase the first 100 blocks complete !\n\r");
00546 res = FR_NO_FILESYSTEM;
00547 }
00548
00549 #endif
00550 }
00551
00552 if (res == FR_NO_FILESYSTEM) {
00553
00554 #if _FS_TINY == 0
00555
00556 TRACE_INFO("Format disk %d\n\r", ID_DRV);
00557
00558 if (!(formatdisk(&Drv_Num)))
00559 return 0;
00560
00561 #else
00562 TRACE_INFO("Please run Full version FAT FS test first\n\r");
00563 return 0;
00564 #endif
00565 }
00566
00567 #if _FS_TINY == 0
00568 DumpSeperator();
00569
00570 TRACE_INFO("Create a file : \"%s\"\n\r", FileName);
00571 res = f_open(&FileObject, FileName, FA_CREATE_ALWAYS | FA_WRITE);
00572
00573 if (res != FR_OK) {
00574 TRACE_ERROR("f_open create pb: 0x%X\n\r", res);
00575 return 0;
00576 }
00577
00578
00579 for (i = 0; i < sizeof(data); i++) {
00580 if ((i & 1) == 0)
00581 data[i] = (i & 0x55);
00582 else
00583 data[i] = (i & 0xAA);
00584 }
00585
00586 TRACE_INFO("Writing to file\n\r");
00587 tickStart = GetTicks();
00588
00589 for (i = 0; i < TEST_PERFORMENCT_SIZE; i += DATA_SIZE) {
00590 res = f_write(&FileObject, data, DATA_SIZE, (UINT*)&ByteWritten);
00591
00592 if (res != FR_OK) {
00593 TRACE_ERROR("f_write pb: 0x%X\n\r", res);
00594 return 0;
00595 }
00596 }
00597
00598 tickEnd = GetTicks();
00599 ticks = GetDelayInTicks(tickStart, tickEnd);
00600 rwSpeed = TEST_PERFORMENCT_SIZE / ticks;
00601 TRACE_INFO("Done, Bad %u, Speed %uK\n\r", (unsigned int)0, (unsigned int)rwSpeed);
00602
00603 TRACE_INFO("Close file\n\r");
00604 res = f_close(&FileObject);
00605
00606 if (res != FR_OK) {
00607 TRACE_ERROR("f_close pb: 0x%X\n\r", res);
00608 return 0;
00609 }
00610
00611 #endif
00612
00613 DumpSeperator();
00614
00615 TRACE_INFO("Open file to read: \"%s\"\n\r", FileName);
00616 res = f_open(&FileObject, FileName, FA_OPEN_EXISTING | FA_READ);
00617
00618 if (res != FR_OK) {
00619 TRACE_ERROR("f_open read pb: 0x%X\n\r", res);
00620 return 0;
00621 }
00622
00623
00624 TRACE_INFO("Read file\n\r");
00625 memset(data, 0, DATA_SIZE);
00626 ByteToRead = FileObject.fsize;
00627 tickStart = GetTicks();
00628
00629 for (i = 0; i < ByteToRead; i += DATA_SIZE) {
00630 res = f_read(&FileObject, data, DATA_SIZE, (UINT*)&ByteRead);
00631
00632 if (res != FR_OK) {
00633 TRACE_ERROR("f_read pb: 0x%X\n\r", res);
00634 return 0;
00635 }
00636 }
00637
00638 tickEnd = GetTicks();
00639 ticks = GetDelayInTicks(tickStart, tickEnd);
00640 rwSpeed = ByteToRead / ticks;
00641 TRACE_INFO("Done, Bad %u, Speed %uK\n\r", (unsigned int)0, (unsigned int)rwSpeed);
00642
00643
00644 TRACE_INFO("Close file\n\r");
00645 res = f_close(&FileObject);
00646
00647 if (res != FR_OK) {
00648 TRACE_ERROR("f_close pb: 0x%X\n\r", res);
00649 return 0;
00650 }
00651
00652
00653 for (i = 0; i < sizeof(data); i++) {
00654 ASSERT((((i & 1) == 0) && (data[i] == (i & 0x55)))
00655 || (data[i] == (i & 0xAA)),
00656 "Invalid data at data[%u] (expected 0x%02X, read 0x%02X)\n\r",
00657 (unsigned int)i, (unsigned int)(((i & 1) == 0) ? (i & 0x55) : (i & 0xAA)), data[i]);
00658 }
00659
00660 TRACE_INFO("File data OK !\n\r");
00661
00662 DumpSeperator();
00663
00664 TRACE_INFO("Create a file : \"%s\"\n\r", FileNameReadMe);
00665 res = f_open(&FileObject, FileNameReadMe, FA_CREATE_ALWAYS | FA_WRITE);
00666
00667 if (res != FR_OK) {
00668 TRACE_ERROR(" f_open create pb: 0x%X\n\r", res);
00669 return 0;
00670 }
00671
00672 TRACE_INFO("Write ReadMe file\n\r");
00673 tickStart = GetTicks();
00674
00675 while (*ReadMeText != '\0') {
00676 res = f_write(&FileObject, ReadMeText++, 1 , (UINT*)&ByteWritten);
00677 len++;
00678 }
00679
00680 if (res != FR_OK) {
00681 TRACE_ERROR("f_write pb: 0x%X\n\r", res);
00682 return 0;
00683 }
00684
00685 tickEnd = GetTicks();
00686 ticks = GetDelayInTicks(tickStart, tickEnd);
00687 rwSpeed = len / ticks;
00688 TRACE_INFO("Done, Bad %u, Speed %uK\n\r", (unsigned int)0, (unsigned int)rwSpeed);
00689
00690 TRACE_INFO("Close file\n\r");
00691 res = f_close(&FileObject);
00692
00693 if (res != FR_OK) {
00694 TRACE_ERROR(" f_close pb: 0x%X\n\r", res);
00695 return 0;
00696 }
00697
00698 DumpSeperator();
00699 return 1;
00700 }
00701
00702
00703
00704 static void _ConfigureDrivers(void)
00705 {
00706 uint32_t i;
00707
00708 XDMAD_Initialize(&dmaDrv, 0);
00709
00710
00711 NVIC_ClearPendingIRQ(XDMAC_IRQn);
00712 NVIC_SetPriority(XDMAC_IRQn, 1);
00713 NVIC_EnableIRQ(XDMAC_IRQn);
00714
00715
00716 MCID_Init(&mciDrv[0], HSMCI, ID_HSMCI, BOARD_MCK, &dmaDrv, 0);
00717
00718
00719 NVIC_ClearPendingIRQ(HSMCI_IRQn);
00720 NVIC_SetPriority(HSMCI_IRQn, 3);
00721 NVIC_EnableIRQ(HSMCI_IRQn);
00722
00723
00724 for (i = 0; i < BOARD_NUM_MCI; i++)
00725 SDD_InitializeSdmmcMode(&sdDrv[i], &mciDrv[i], 0);
00726 }
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737 int main(void)
00738 {
00739 uint32_t i;
00740 uint8_t connected[BOARD_NUM_MCI];
00741
00742
00743 WDT_Disable(WDT);
00744
00745 SCB_EnableICache();
00746 SCB_EnableDCache();
00747
00748
00749 printf("-- HSMCI SD/MMC FatFS Example %s --\n\r", SOFTPACK_VERSION);
00750 printf("-- %s\n\r", BOARD_NAME);
00751 printf("-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__ , COMPILER_NAME);
00752
00753 TimeTick_Configure();
00754
00755 _ConfigurePIOs();
00756
00757
00758 _ConfigureDrivers();
00759
00760
00761 for (i = 0; i < BOARD_NUM_MCI; i++)
00762 connected[i] = 0;
00763
00764
00765 if (!AnyCardIsConnected()) {
00766 printf("-- Please insert a card\n\r");
00767
00768 while (!AnyCardIsConnected());
00769 }
00770
00771
00772 for (i = 0; i < BOARD_NUM_MCI; i++) {
00773 if (connected[i] == 0 && CardIsConnected(i)) {
00774 connected[i] = 1;
00775 LoopDelay(BOARD_MCK / 1000 / 200);
00776 CardInit(i);
00777 }
00778
00779 if (connected[i]) {
00780 if (RunFsTest()) {
00781 TRACE_INFO(" Test passed !\n\r");
00782
00783 } else
00784 printf("-F- Test Failed !\n\r");
00785 }
00786 }
00787
00788 while (1);
00789 }
00790