SAMV71 Xplained Ultra Software Package 1.5

main.c

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------- */
00002 /*                  Atmel Microcontroller Software Support                      */
00003 /*                       SAM Software Package License                           */
00004 /* ---------------------------------------------------------------------------- */
00005 /* Copyright (c) 2015, Atmel Corporation                                        */
00006 /*                                                                              */
00007 /* All rights reserved.                                                         */
00008 /*                                                                              */
00009 /* Redistribution and use in source and binary forms, with or without           */
00010 /* modification, are permitted provided that the following condition is met:    */
00011 /*                                                                              */
00012 /* - Redistributions of source code must retain the above copyright notice,     */
00013 /* this list of conditions and the disclaimer below.                            */
00014 /*                                                                              */
00015 /* Atmel's name may not be used to endorse or promote products derived from     */
00016 /* this software without specific prior written permission.                     */
00017 /*                                                                              */
00018 /* DISCLAIMER:  THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR   */
00019 /* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
00020 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE   */
00021 /* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,      */
00022 /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */
00023 /* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,  */
00024 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    */
00025 /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING         */
00026 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, */
00027 /* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                           */
00028 /* ---------------------------------------------------------------------------- */
00029 
00030 /**
00031  * \page hsmci_sdcard_fatfs Basic SD/MMC Card Example with fatfs file system
00032  *
00033  * \section Purpose
00034  *
00035  *  The hsmci_sdcard_fatfs will help you to get familiar with HSMCI interface
00036  * with fatfs support on
00037  *  SAM Microcontrollers. It can also help you to get familiar with the SD
00038  *  operation flow which can be used for fast implementation of your own SD
00039  *  drivers and other applications related.
00040  *
00041  *  \section Requirements
00042  *
00043  *  This package can be used with SAMV71 Xplained Ultra board or SAME70 Xplained board.
00044  *
00045  *  \section Description
00046  *
00047  *  The demonstration program detects, initialize the SD/MMC memory card
00048  *  inserted, and performs R/W test on it.
00049  *
00050  *  Open HyperTerminal before running this program, use SAM-BA to download
00051  *  this program to SRAM or Flash, make the program run, the HyperTerminal
00052  *  will give out the test results.
00053  *
00054  *  \section Usage
00055  *
00056  *  -# Build the program and download it inside the board.
00057  *     Please refer to the Getting Started with SAM V71/E70 Microcontrollers.pdf
00058  *  -# On the computer, open and configure a terminal application
00059  *     (e.g. HyperTerminal on Microsoft Windows) with these settings:
00060  *    - 115200 bauds
00061  *    - 8 bits of data
00062  *    - No parity
00063  *    - 1 stop bit
00064  *    - No flow control
00065  *  -# Start the application
00066  *  -# In HyperTerminal, it will show something like
00067  *      \code
00068  *      -- HSMCI SD/MMC Example xxx --
00069  *      -- SAMxxxxx-xx
00070  *      -- Compiled: xxx xx xxxx xx:xx:xx --
00071  *      -I- Please connect a SD card ...
00072  *      -I- SD card connection detected
00073  *      -I- Cannot check if SD card is write-protected
00074  *      -I- SD/MMC card initialization successful
00075  *      -I- Card size: *** MB
00076  *      -I- Block size: *** Bytes
00077  *      -I- Testing block [  *** -   ***] ..."
00078  *      \endcode
00079  *
00080  *  \section References
00081  *  - hsmci_sdcard_fatfs/main.c
00082  *  - hsmci.h
00083  *  - pio.h
00084  *
00085  */
00086 
00087 /**
00088  *  \file
00089  *
00090  *  \section Purpose
00091  *
00092  *  This file contains all the specific code for the hsmci_sdcard_fatfs example.
00093  *
00094  *----------------------------------------------------------------------------
00095  *         Headers
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  *         Local definitions
00111  *----------------------------------------------------------------------------*/
00112 
00113 
00114 /** Maximum number of blocks read once */
00115 #define NB_MULTI_BLOCKS     5
00116 
00117 /** Split R/W to 2, first R/W 4 blocks then remaining */
00118 #define NB_SPLIT_MULTI      4
00119 
00120 /** Test settings: start block address (0) */
00121 #define TEST_BLOCK_START    (0)
00122 
00123 /** Test settings: end block address (total SD/MMC) */
00124 #define TEST_BLOCK_END      SD_GetNumberBlocks(&sdDrv[bMciID])
00125 
00126 /** Test settings: skip size when "skip" key pressed */
00127 #define TEST_BLOCK_SKIP     (100 * 1024 * 2)    // 100M
00128 
00129 /**  Number of errors displayed */
00130 #define NB_ERRORS           5
00131 
00132 /** Bytes per cluster, FS format is necessary to make it effective*/
00133 #define AllOCSIZE        4096
00134 /*----------------------------------------------------------------------------
00135  *         Local variables
00136  *----------------------------------------------------------------------------*/
00137 
00138 /** Maximum number of LUNs which can be defined.*/
00139 /** (Logical drive = physical drive = medium number)*/
00140 #define MAX_LUNS        1
00141 
00142 /** Available media.*/
00143 extern sMedia medias[MAX_LUNS];
00144 
00145 /** DMA driver instance */
00146 static sXdmad dmaDrv;
00147 
00148 /** MCI driver instance. */
00149 static sMcid mciDrv[BOARD_NUM_MCI];
00150 
00151 /** SDCard driver instance. */
00152 extern  sSdCard sdDrv[BOARD_NUM_MCI];
00153 
00154 /** SD card pins instance. */
00155 static const Pin pinsSd[] = {BOARD_MCI_PINS_SLOTA, BOARD_MCI_PIN_CK};
00156 
00157 /** SD card detection pin instance. */
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 /** size of the file to write/read.minimum size 512 for erase operation*/
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 /** Test settings: Number of bytes to test performance */
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  *         Local macros
00199  *----------------------------------------------------------------------------*/
00200 
00201 /* Defined to test Multi-Block functions */
00202 
00203 /** \def READ_MULTI
00204  *  \brief Define to test multi-read (SD_Read())
00205  *         or
00206  *         single-read is used (SD_ReadBlocks()) */
00207 #define READ_MULTI
00208 /** \def WRITE_MULTI
00209  *  \brief Define to test multi-write (SD_Write())
00210  *         or
00211  *         single-write is used (SD_WriteBlocks()) */
00212 #define WRITE_MULTI
00213 
00214 /** \macro SDT_ReadFun
00215  * Function used for SD card test reading.
00216  * \param pSd  Pointer to a SD card driver instance.
00217  * \param address  Address of the block to read.
00218  * \param nbBlocks Number of blocks to be read.
00219  * \param pData    Data buffer whose size is at least the block size.
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 /** \macro SDT_WriteFun
00230  * Function used for SD card test writing.
00231  * \param pSd  Pointer to a SD card driver instance.
00232  * \param address  Address of the block to read.
00233  * \param nbBlocks Number of blocks to be read.
00234  * \param pData    Data buffer whose size is at least the block size.
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  *         Local functions
00246  *----------------------------------------------------------------------------*/
00247 
00248 /**
00249  * DMA interrupt handler.
00250  */
00251 void XDMAC_Handler(void)
00252 {
00253     XDMAD_Handler(&dmaDrv);
00254 }
00255 
00256 /**
00257  * MCI interrupt handler. Forwards the event to the MCI driver handlers.
00258  */
00259 void HSMCI_Handler(void)
00260 {
00261     MCID_Handler(&mciDrv[0]);
00262 
00263 }
00264 
00265 /*----------------------------------------------------------------------------
00266  *         Optional: SD card detection (connection, protection)
00267  *----------------------------------------------------------------------------*/
00268 
00269 /**
00270  * Configure for SD detect pin
00271  */
00272 static void CardDetectConfigure(void)
00273 {
00274     PIO_Configure(pinsCd, PIO_LISTSIZE(pinsCd));
00275     /* No protection detect pin */
00276 }
00277 
00278 /**
00279  * Return 1 if card is inserted.
00280  */
00281 static uint8_t CardIsConnected(uint8_t iMci)
00282 {
00283     return PIO_Get(&pinsCd[iMci]) ? 0 : 1;
00284 }
00285 
00286 /**
00287  * Return 1 if any card is inserted.
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  * Return 1 if card is protected.
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  * Delay some loop
00312  */
00313 static void LoopDelay(volatile unsigned int loop)
00314 {
00315     for (; loop > 0; loop--);
00316 }
00317 
00318 
00319 
00320 /**
00321  * Display: Dump Splitting row
00322  */
00323 static void DumpSeperator(void)
00324 {
00325     printf("\n\r==========================================\n\r");
00326 }
00327 
00328 
00329 /**
00330  * Dump card registers
00331  * \param slot Card slot (not used now).
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  * Run tests on the inserted card
00348  * \param slot Card slot (not used now).
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  * Initialize PIOs
00381  */
00382 static void _ConfigurePIOs(void)
00383 {
00384     /* Configure SDcard pins */
00385     PIO_Configure(pinsSd, PIO_LISTSIZE(pinsSd));
00386     /* Configure SD card detection */
00387     CardDetectConfigure();
00388     /* Check if card is write-protected (if supported) */
00389     CardIsProtected();
00390 }
00391 
00392 /**
00393  * Scan files under a certain path
00394  * \param path    folder path
00395  * return scan result, 1: success.
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     /** Format disk*/
00449     TRACE_INFO(" Please wait a moment. Formatting the disk...\n\r");
00450     res = f_mkfs(pDrv,    // Drv
00451                  0,    // FDISK partition
00452                  AllOCSIZE); // 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  * Do file system tests
00469  * \return test result, 1: success.
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     /** Mount disk*/
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     /** Clear file system object*/
00498     memset(&aligned_fs.fs, 0, sizeof(FATFS));
00499 
00500     /* Mount the drive */
00501     res = f_mount(&aligned_fs.fs, &Drv_Num, 1);
00502 
00503     if (res != FR_OK) {
00504         /* If No file system found, format it */
00505         if (res == FR_NO_FILESYSTEM) {
00506             TRACE_INFO("No file System found\n\r");
00507 
00508             if (formatdisk(&Drv_Num)) {
00509                 /* Try to mount again */
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     /** Test if the disk is formatted*/
00525     res = f_opendir (&dirs, STR_ROOT_DIRECTORY);
00526 
00527     if (res == FR_OK) {
00528         /** erase sdcard to re-format it ?*/
00529         TRACE_INFO("The disk is already formatted.\n\r");
00530 
00531         /** Display the file tree*/
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         /** Format disk*/
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     /** Create a new file*/
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     /** Write a checkerboard pattern in the buffer*/
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     /** Close the file*/
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     /** Open the file*/
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     /** Read file*/
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     /** Close the file*/
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     /** compare read data with the expected data*/
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     /** Create a new file*/
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     /** Close the file*/
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  * Initialize driver instances.
00703  */
00704 static void _ConfigureDrivers(void)
00705 {
00706     uint32_t i;
00707     /* Initialize the DMA driver */
00708     XDMAD_Initialize(&dmaDrv, 0);
00709 
00710     /* Enable XDMA interrupt and give it priority over any other peripheral interrupt */
00711     NVIC_ClearPendingIRQ(XDMAC_IRQn);
00712     NVIC_SetPriority(XDMAC_IRQn, 1);
00713     NVIC_EnableIRQ(XDMAC_IRQn);
00714 
00715     /* Initialize the HSMCI driver */
00716     MCID_Init(&mciDrv[0], HSMCI, ID_HSMCI, BOARD_MCK, &dmaDrv, 0);
00717 
00718     /* Enable MCI interrupt and give it priority lower than DMA*/
00719     NVIC_ClearPendingIRQ(HSMCI_IRQn);
00720     NVIC_SetPriority(HSMCI_IRQn, 3);
00721     NVIC_EnableIRQ(HSMCI_IRQn);
00722 
00723     /* Initialize SD driver */
00724     for (i = 0; i < BOARD_NUM_MCI; i++)
00725         SDD_InitializeSdmmcMode(&sdDrv[i], &mciDrv[i], 0);
00726 }
00727 
00728 /*----------------------------------------------------------------------------
00729  *         Global functions
00730  *----------------------------------------------------------------------------*/
00731 
00732 /**
00733  *  \brief hsmci_sdcard_fatfs Application entry point.
00734  *
00735  *  \return Unused (ANSI-C compatibility).
00736  */
00737 int main(void)
00738 {
00739     uint32_t i;
00740     uint8_t connected[BOARD_NUM_MCI];
00741 
00742     /* Disable watchdog */
00743     WDT_Disable(WDT);
00744 
00745     SCB_EnableICache();
00746     SCB_EnableDCache();
00747 
00748     /* Output example information */
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     /* Initialize PIO pins */
00755     _ConfigurePIOs();
00756 
00757     /* Initialize drivers */
00758     _ConfigureDrivers();
00759 
00760     /* Initialize connections */
00761     for (i = 0; i < BOARD_NUM_MCI; i++)
00762         connected[i] = 0;
00763 
00764     /* Check if any card is inserted */
00765     if (!AnyCardIsConnected()) {
00766         printf("-- Please insert a card\n\r");
00767 
00768         while (!AnyCardIsConnected());
00769     }
00770 
00771     /* Test all cards */
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 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines