SAMV71 Xplained Ultra Software Package 1.4

main.c

Go to the documentation of this file.
00001 /* ----------------------------------------------------------------------------
00002  *         SAM Software Package License 
00003  * ----------------------------------------------------------------------------
00004  * Copyright (c) 2014, Atmel Corporation
00005  *
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *
00011  * - Redistributions of source code must retain the above copyright notice,
00012  * this list of conditions and the disclaimer below.
00013  *
00014  * Atmel's name may not be used to endorse or promote products derived from
00015  * this software without specific prior written permission.
00016  *
00017  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00019  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
00020  * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00022  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00023  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00024  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00025  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00026  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  * ----------------------------------------------------------------------------
00028  */
00029 /**
00030  * \page hsmci_sdcard_fatfs Basic SD/MMC Card Example with fatfs file system
00031  *
00032  * \section Purpose
00033  *
00034  *  The hsmci_sdcard_fatfs will help you to get familiar with HSMCI interface 
00035  * with fatfs support on
00036  *  SAM Microcontrollers. It can also help you to get familiar with the SD
00037  *  operation flow which can be used for fast implementation of your own SD
00038  *  drivers and other applications related.
00039  *
00040  *  \section Requirements
00041  *
00042  *  This package can be used with SAM V71 Xplained Ultra board.
00043  *
00044  *  \section Description
00045  *
00046  *  The demonstration program detects, initialize the SD/MMC memory card
00047  *  inserted, and performs R/W test on it.
00048  *
00049  *  Open HyperTerminal before running this program, use SAM-BA to download
00050  *  this program to SRAM or Flash, make the program run, the HyperTerminal
00051  *  will give out the test results.
00052  *
00053  *  \section Usage
00054  *
00055  *  -# Build the program and download it inside the SAM V71 Xplained Ultra board. 
00056  *     Please refer to the Getting Started with SAM V71 Microcontrollers.pdf
00057  *  -# On the computer, open and configure a terminal application
00058  *     (e.g. HyperTerminal on Microsoft Windows) with these settings:
00059  *    - 115200 bauds
00060  *    - 8 bits of data
00061  *    - No parity
00062  *    - 1 stop bit
00063  *    - No flow control
00064  *  -# Start the application
00065  *  -# In HyperTerminal, it will show something like
00066  *      \code
00067  *      -- HSMCI SD/MMC Example xxx --
00068  *      -- SAMxxxxx-xx
00069  *      -- Compiled: xxx xx xxxx xx:xx:xx --
00070  *      -I- Please connect a SD card ...
00071  *      -I- SD card connection detected
00072  *      -I- Cannot check if SD card is write-protected
00073  *      -I- SD/MMC card initialization successful
00074  *      -I- Card size: *** MB
00075  *      -I- Block size: *** Bytes
00076  *      -I- Testing block [  *** -   ***] ..."
00077  *      \endcode
00078  *
00079  *  \section References
00080  *  - hsmci_sdcard_fatfs/main.c
00081  *  - hsmci.h
00082  *  - pio.h
00083  *
00084  */
00085 
00086 /**
00087  *  \file
00088  *
00089  *  \section Purpose
00090  *
00091  *  This file contains all the specific code for the hsmci_sdcard_fatfs example.
00092  *
00093  *----------------------------------------------------------------------------
00094  *         Headers
00095  *----------------------------------------------------------------------------*/
00096 
00097 #include "board.h"
00098  
00099 #include "libsdmmc.h"
00100 #include "../fatfs_config.h"
00101 #include "Media.h"
00102 #include "MEDSdcard.h"
00103 #include <stdint.h>
00104 #include <stdio.h>
00105 #include <string.h>
00106 #include <assert.h>
00107 
00108 /*----------------------------------------------------------------------------
00109  *         Local definitions
00110  *----------------------------------------------------------------------------*/
00111 
00112 
00113 /** Maximum number of blocks read once */
00114 #define NB_MULTI_BLOCKS     5
00115 
00116 /** Split R/W to 2, first R/W 4 blocks then remaining */
00117 #define NB_SPLIT_MULTI      4
00118 
00119 /** Test settings: start block address (0) */
00120 #define TEST_BLOCK_START    (0)
00121 
00122 /** Test settings: end block address (total SD/MMC) */
00123 #define TEST_BLOCK_END      SD_GetNumberBlocks(&sdDrv[bMciID])
00124 
00125 /** Test settings: skip size when "skip" key pressed */
00126 #define TEST_BLOCK_SKIP     (100 * 1024 * 2)    // 100M
00127 
00128 /**  Number of errors displayed */
00129 #define NB_ERRORS           5
00130 
00131 /** Bytes per cluster, FS format is necessary to make it effective*/
00132 #define AllOCSIZE       512
00133 /*----------------------------------------------------------------------------
00134  *         Local variables
00135  *----------------------------------------------------------------------------*/
00136 
00137 /** Maximum number of LUNs which can be defined.*/
00138 /** (Logical drive = physical drive = medium number)*/
00139 #define MAX_LUNS        1
00140 
00141 /** Available media.*/
00142 extern sMedia medias[MAX_LUNS];
00143 
00144 /** DMA driver instance */
00145 static sXdmad dmaDrv;
00146 
00147 /** MCI driver instance. */
00148 static sMcid mciDrv[BOARD_NUM_MCI];
00149 
00150 /** SDCard driver instance. */
00151 extern  sSdCard sdDrv[BOARD_NUM_MCI];
00152 
00153 /** SD card pins instance. */
00154 static const Pin pinsSd[] = {BOARD_MCI_PINS_SLOTA, BOARD_MCI_PIN_CK};
00155 
00156 /** SD card detection pin instance. */
00157 static const Pin pinsCd[] = {BOARD_MCI_PIN_CD};
00158 
00159 
00160 #define ID_DRV DRV_MMC
00161 
00162 #if _FS_TINY == 0
00163 #define STR_ROOT_DIRECTORY "0:"
00164 #else
00165 #define STR_ROOT_DIRECTORY ""
00166 #endif
00167 
00168 const char* FileName = STR_ROOT_DIRECTORY "Basic.bin";
00169 const char* FileNameReadMe = STR_ROOT_DIRECTORY "ReadMe.txt";
00170 
00171 const char* ReadMeText = "Samv7 FatFS example: Done!!";
00172 
00173 
00174 /** size of the file to write/read.minimum size 512 for erase operation*/
00175 #define DATA_SIZE 2048
00176 
00177 uint8_t data[DATA_SIZE];
00178 
00179 /** Test settings: Number of bytes to test performance */
00180 #define TEST_PERFORMENCT_SIZE   (4*1024*1024)
00181 
00182 
00183 #define ASSERT(condition, ...)  { \
00184     if (!(condition)) { \
00185         printf("-F- ASSERT: "); \
00186         printf(__VA_ARGS__); \
00187     } \
00188 }
00189 /*----------------------------------------------------------------------------
00190  *         Local macros
00191  *----------------------------------------------------------------------------*/
00192 
00193 /* Defined to test Multi-Block functions */
00194 
00195 /** \def READ_MULTI
00196  *  \brief Define to test multi-read (SD_Read())
00197  *         or
00198  *         single-read is used (SD_ReadBlocks()) */
00199 #define READ_MULTI
00200 /** \def WRITE_MULTI
00201  *  \brief Define to test multi-write (SD_Write())
00202  *         or
00203  *         single-write is used (SD_WriteBlocks()) */
00204 #define WRITE_MULTI
00205 
00206 /** \macro SDT_ReadFun
00207  * Function used for SD card test reading.
00208  * \param pSd  Pointer to a SD card driver instance.
00209  * \param address  Address of the block to read.
00210  * \param nbBlocks Number of blocks to be read.
00211  * \param pData    Data buffer whose size is at least the block size.
00212  */
00213 #ifdef  READ_MULTI
00214 #define MMCT_ReadFun(pSd, blk, nbBlk, pData) \
00215     SD_Read(pSd, blk, pData, nbBlk, NULL, NULL)
00216 #else
00217 #define MMCT_ReadFun(pSd, blk, nbBlk, pData) \
00218     SD_ReadBlocks(pSd, blk, pData, nbBlk)
00219 #endif
00220 
00221 /** \macro SDT_WriteFun
00222  * Function used for SD card test writing.
00223  * \param pSd  Pointer to a SD card driver instance.
00224  * \param address  Address of the block to read.
00225  * \param nbBlocks Number of blocks to be read.
00226  * \param pData    Data buffer whose size is at least the block size.
00227  */
00228 #ifdef  WRITE_MULTI
00229 #define MMCT_WriteFun(pSd, blk, nbBlk, pData) \
00230     SD_Write(pSd, blk, pData, nbBlk, NULL, NULL)
00231 #else
00232 #define MMCT_WriteFun(pSd, blk, nbBlk, pData) \
00233     SD_WriteBlocks(pSd, blk, pData, nbBlk)
00234 #endif
00235 
00236 /*----------------------------------------------------------------------------
00237  *         Local functions
00238  *----------------------------------------------------------------------------*/
00239 
00240 /**
00241  * DMA interrupt handler.
00242  */
00243 void XDMAC_Handler(void)
00244 {
00245     XDMAD_Handler(&dmaDrv);
00246 }
00247 
00248 /**
00249  * MCI interrupt handler. Forwards the event to the MCI driver handlers.
00250  */
00251 void HSMCI_Handler(void)
00252 {
00253     
00254   MCID_Handler(&mciDrv[0]);
00255   
00256 }
00257 
00258 /*----------------------------------------------------------------------------
00259  *         Optional: SD card detection (connection, protection)
00260  *----------------------------------------------------------------------------*/
00261 
00262 /**
00263  * Configure for SD detect pin
00264  */
00265 static void CardDetectConfigure(void)
00266 {
00267     PIO_Configure(pinsCd, PIO_LISTSIZE(pinsCd));
00268     /* No protection detect pin */
00269 }
00270 
00271 /**
00272  * Return 1 if card is inserted.
00273  */
00274 static uint8_t CardIsConnected(uint8_t iMci)
00275 {
00276     return PIO_Get(&pinsCd[iMci]) ? 0 : 1;
00277 }
00278 
00279 /**
00280  * Return 1 if any card is inserted.
00281  */
00282 static uint8_t AnyCardIsConnected(void)
00283 {
00284     uint32_t i;
00285     for (i = 0; i < BOARD_NUM_MCI; i ++) {
00286         if ( CardIsConnected(i) ) {
00287             return 1;
00288         }
00289     }
00290     return 0;
00291 }
00292 
00293 /**
00294  * Return 1 if card is protected.
00295  */
00296 static uint8_t CardIsProtected(void)
00297 {
00298     TRACE_INFO("  Cannot check if SD card is write-protected\n\r");
00299     return 0;
00300 }
00301 
00302 /**
00303  * Delay some loop
00304  */
00305 static void LoopDelay(volatile unsigned int loop)
00306 {
00307     for(;loop > 0; loop --);
00308 }
00309 
00310 
00311 
00312 /**
00313  * Display: Dump Splitting row
00314  */
00315 static void DumpSeperator(void)
00316 {
00317     printf("\n\r==========================================\n\r");
00318 }
00319 
00320 
00321 /**
00322  * Dump card registers
00323  * \param slot Card slot (not used now).
00324  */
00325 static void DumpCardInfo(uint8_t iMci)
00326 {
00327     sSdCard *pSd = &sdDrv[iMci];
00328 
00329     if (SD_GetCardType(pSd) & CARD_TYPE_bmSDIO) {
00330         SDIO_DumpCardInformation(pSd);
00331     }
00332 
00333     if (SD_GetCardType(pSd) & CARD_TYPE_bmSDMMC) {
00334         SD_DumpCID(pSd->CID);
00335         SD_DumpCSD(pSd->CSD);
00336     }
00337 }
00338 
00339 /**
00340  * Run tests on the inserted card
00341  * \param slot Card slot (not used now).
00342  */
00343 static uint8_t CardInit(uint8_t iMci)
00344 {
00345     uint8_t error;
00346     uint8_t retry = 2;
00347 
00348     DumpSeperator();
00349     while(retry --) {
00350         error = SD_Init(&sdDrv[iMci]);
00351         if (error == SDMMC_OK) break;
00352     }
00353     if (error) {
00354         TRACE_ERROR("  SD/MMC card initialization failed: %d\n\r", error);
00355         return 1;
00356     }
00357     TRACE_INFO("  SD/MMC card initialization successful\n\r");
00358     TRACE_INFO("  Card size: %d MB", (int)SD_GetTotalSizeKB(&sdDrv[iMci])/1000);
00359     printf(", %d * %dB\n\r", 
00360         (int)SD_GetNumberBlocks(&sdDrv[iMci]), 
00361         (int)SD_GetBlockSize(&sdDrv[iMci]));
00362     DumpCardInfo(iMci);
00363     return 0;
00364 }
00365 
00366 
00367 
00368 /**
00369  * Initialize PIOs
00370  */
00371 static void _ConfigurePIOs(void)
00372 {
00373     /* Configure SDcard pins */
00374     PIO_Configure(pinsSd, PIO_LISTSIZE(pinsSd));
00375     /* Configure SD card detection */
00376     CardDetectConfigure();
00377     /* Check if card is write-protected (if supported) */
00378     CardIsProtected();
00379 }
00380 
00381 /**
00382  * Scan files under a certain path
00383  * \param path    folder path
00384  * return scan result, 1: success.
00385  */
00386 static FRESULT scan_files (char* path)
00387 {
00388     FRESULT res;
00389     FILINFO fno;
00390     DIR dir;
00391     int32_t i;
00392     char *fn;
00393 #if _USE_LFN
00394     static char lfn[_MAX_LFN * (_DF1S ? 2 : 1) + 1];
00395     fno.lfname = lfn;
00396     fno.lfsize = sizeof(lfn);
00397 #endif
00398 
00399 
00400     res = f_opendir(&dir, path);
00401     if (res == FR_OK) {
00402         i = strlen(path);
00403         for (;;) {
00404             res = f_readdir(&dir, &fno);
00405             if (res != FR_OK || fno.fname[0] == 0) break;
00406 #if _USE_LFN
00407             fn = *fno.lfname ? fno.lfname : fno.fname;
00408 #else
00409             fn = fno.fname;
00410 #endif
00411             if (*fn == '.') continue;
00412             if (fno.fattrib & AM_DIR) {
00413                 sprintf(&path[i], "/%s", fn);
00414                 res = scan_files(path);
00415                 if (res != FR_OK) break;
00416                 path[i] = 0;
00417             } else {
00418                 printf("%s/%s\n\r", path, fn);
00419             }
00420         }
00421     }
00422     return res;
00423 }
00424 
00425 static uint8_t formatdisk(const TCHAR *pDrv)
00426 {
00427     FRESULT res;
00428 #if _FS_TINY == 0
00429     /** Format disk*/
00430     TRACE_INFO(" Please wait a moment. Formatting the disk...\n\r");
00431     res = f_mkfs(pDrv,    // Drv
00432                0,    // FDISK partition
00433                AllOCSIZE); // AllocSize
00434     TRACE_INFO("  Format disk finished !\n\r");
00435     if( res != FR_OK ) {
00436         TRACE_ERROR("  f_mkfs pb: 0x%X\n\r", res);
00437         return 0;
00438     }
00439     return 1;
00440 #else
00441     TRACE_INFO("  Please run Full version FAT FS test first\n\r");
00442     return 0;
00443 #endif
00444 }
00445 
00446 /**
00447  * Do file system tests
00448  * \return test result, 1: success.
00449  */
00450 static uint8_t RunFsTest(void)
00451 {
00452     uint32_t i;
00453     uint32_t ByteToRead;
00454     uint32_t ByteRead;
00455     uint32_t len = 0;
00456 #if _FS_TINY == 0
00457     uint32_t ByteWritten;
00458     char key;
00459 #endif
00460 
00461     const TCHAR Drv_Num = ID_DRV;
00462     FRESULT res;
00463     DIR dirs;
00464     FATFS fs;
00465     FIL FileObject;
00466     uint32_t tickStart, tickEnd, ticks, rwSpeed;
00467 
00468     gNbMedias = 1;
00469     /** Mount disk*/
00470     TRACE_INFO("Mount disk %d\n\r", ID_DRV);
00471     /** Clear file system object*/
00472     memset(&fs, 0, sizeof(FATFS));
00473     
00474     /* Mount the drive */
00475     res = f_mount(&fs, &Drv_Num, 1);
00476     if( res != FR_OK ) {
00477     /* If No file system found, format it */
00478         if(res == FR_NO_FILESYSTEM) {
00479             TRACE_INFO("No file System found\n\r");
00480             if(formatdisk(&Drv_Num)) {
00481             /* Try to mount again */
00482                 res = f_mount(&fs, &Drv_Num, 1);
00483                 if( res != FR_OK ) {
00484                     TRACE_ERROR("f_mount pb: 0x%X\n\r", res); 
00485                     return 0;
00486                 }
00487             } else {
00488                 return 0;
00489             }
00490         } else {
00491             TRACE_ERROR("f_mount pb: 0x%X\n\r", res);
00492             return 0;
00493         }
00494     }
00495 
00496     /** Test if the disk is formatted*/
00497     res = f_opendir (&dirs,STR_ROOT_DIRECTORY);
00498     if(res == FR_OK ){
00499         /** erase sdcard to re-format it ?*/
00500         TRACE_INFO("The disk is already formatted.\n\r");
00501 
00502         /** Display the file tree*/
00503         TRACE_INFO(" Display files contained on the SDcard :\n\r");
00504         DumpSeperator();
00505         scan_files(STR_ROOT_DIRECTORY);
00506         DumpSeperator();
00507 
00508 #if _FS_TINY == 0
00509         TRACE_INFO("Do you want to erase the sdcard to re-format disk ? (y/n)!\n\r");
00510         key = DBG_GetChar();
00511         if( (key == 'y') ||  (key == 'Y'))
00512         {
00513             for(i=0;i<100;i++) {
00514                 MEDSdcard_EraseBlock(&medias[ID_DRV], i);
00515             }
00516             TRACE_INFO(" Erase the first 100 blocks complete !\n\r");
00517             res = FR_NO_FILESYSTEM;
00518         }
00519 #endif
00520     }
00521 
00522     if( res == FR_NO_FILESYSTEM ) {
00523 
00524 #if _FS_TINY == 0
00525         /** Format disk*/
00526         TRACE_INFO("Format disk %d\n\r", ID_DRV);
00527         if(!(formatdisk(&Drv_Num)) ) {
00528           return 0;
00529         }
00530           
00531 #else
00532         TRACE_INFO("Please run Full version FAT FS test first\n\r");
00533         return 0;
00534 #endif
00535     }
00536 
00537 #if _FS_TINY == 0
00538     DumpSeperator();
00539     /** Create a new file*/
00540     TRACE_INFO("Create a file : \"%s\"\n\r", FileName);
00541     res = f_open(&FileObject, FileName, FA_CREATE_ALWAYS|FA_WRITE);
00542     if( res != FR_OK ) {
00543         TRACE_ERROR("f_open create pb: 0x%X\n\r", res);
00544         return 0;
00545     }
00546 
00547     /** Write a checkerboard pattern in the buffer*/
00548     for (i=0; i < sizeof(data); i++) {
00549         if ((i & 1) == 0) {
00550             data[i] = (i & 0x55);
00551         } else {
00552             data[i] = (i & 0xAA);
00553         }
00554     }
00555     TRACE_INFO("Writing to file\n\r");
00556     tickStart = GetTicks();
00557     for(i = 0; i< TEST_PERFORMENCT_SIZE;i+=DATA_SIZE) {
00558         res = f_write(&FileObject, data, DATA_SIZE, &ByteWritten);
00559         if( res != FR_OK ) {
00560             TRACE_ERROR("f_write pb: 0x%X\n\r", res);
00561             return 0;
00562         }
00563     }
00564     tickEnd = GetTicks();
00565     ticks = GetDelayInTicks(tickStart, tickEnd);
00566     rwSpeed = TEST_PERFORMENCT_SIZE / ticks;
00567     TRACE_INFO("Done, Bad %u, Speed %uK\n\r", (uint32_t)0, (uint32_t)rwSpeed);
00568     /** Close the file*/
00569     TRACE_INFO("Close file\n\r");
00570     res = f_close(&FileObject);
00571     if( res != FR_OK ) {
00572         TRACE_ERROR("f_close pb: 0x%X\n\r", res);
00573         return 0;
00574     }
00575 #endif
00576 
00577     DumpSeperator();
00578     /** Open the file*/
00579     TRACE_INFO("Open file to read: \"%s\"\n\r", FileName);
00580     res = f_open(&FileObject, FileName, FA_OPEN_EXISTING|FA_READ);
00581     if( res != FR_OK ) {
00582         TRACE_ERROR("f_open read pb: 0x%X\n\r", res);
00583         return 0;
00584     }
00585     /** Read file*/
00586     TRACE_INFO("Read file\n\r");
00587     memset(data, 0, DATA_SIZE);
00588     ByteToRead = FileObject.fsize;
00589     tickStart = GetTicks();
00590     for(i = 0; i< ByteToRead;i+=DATA_SIZE) {
00591         res = f_read(&FileObject, data, DATA_SIZE, &ByteRead);
00592         if(res != FR_OK) {
00593             TRACE_ERROR("f_read pb: 0x%X\n\r", res);
00594             return 0;
00595         }
00596     }
00597     tickEnd = GetTicks();
00598     ticks = GetDelayInTicks(tickStart, tickEnd);
00599     rwSpeed = ByteToRead / ticks;
00600     TRACE_INFO("Done, Bad %u, Speed %uK\n\r", (uint32_t)0, (uint32_t)rwSpeed);
00601 
00602     /** Close the file*/
00603     TRACE_INFO("Close file\n\r");
00604     res = f_close(&FileObject);
00605     if( res != FR_OK ) {
00606         TRACE_ERROR("f_close pb: 0x%X\n\r", res);
00607         return 0;
00608     }
00609 
00610     /** compare read data with the expected data*/
00611     for (i=0; i < sizeof(data); i++) {
00612         ASSERT((((i & 1) == 0) && (data[i] == (i & 0x55)))
00613                 || (data[i] == (i & 0xAA)),
00614                 "Invalid data at data[%u] (expected 0x%02X, read 0x%02X)\n\r",
00615                 i, ((i & 1) == 0) ? (i & 0x55) : (i & 0xAA), data[i]);
00616     }
00617     TRACE_INFO("File data OK !\n\r");
00618 
00619     DumpSeperator();
00620     /** Create a new file*/
00621     TRACE_INFO("Create a file : \"%s\"\n\r", FileNameReadMe);
00622     res = f_open(&FileObject, FileNameReadMe, FA_CREATE_ALWAYS|FA_WRITE);
00623     if( res != FR_OK ) {
00624         TRACE_ERROR("  f_open create pb: 0x%X\n\r", res);
00625         return 0;
00626     }
00627 
00628     TRACE_INFO("Write ReadMe file\n\r");
00629     tickStart = GetTicks();
00630     while(*ReadMeText != '\0'){
00631         res = f_write(&FileObject, ReadMeText++, 1 , &ByteWritten);
00632         len++;
00633     }
00634     if( res != FR_OK ) {
00635         TRACE_ERROR("f_write pb: 0x%X\n\r", res);
00636         return 0;
00637     }
00638 
00639     tickEnd = GetTicks();
00640     ticks = GetDelayInTicks(tickStart, tickEnd);
00641     rwSpeed = len / ticks;
00642     TRACE_INFO("Done, Bad %u, Speed %uK\n\r", (uint32_t)0, (uint32_t)rwSpeed);
00643     /** Close the file*/
00644     TRACE_INFO("Close file\n\r");
00645     res = f_close(&FileObject);
00646     if( res != FR_OK ) {
00647         TRACE_ERROR("  f_close pb: 0x%X\n\r", res);
00648         return 0;
00649     }
00650 
00651     DumpSeperator();
00652     return 1;
00653 }
00654 /**
00655  * Initialize driver instances.
00656  */
00657 static void _ConfigureDrivers(void)
00658 {
00659     uint32_t i;
00660     /* Initialize the DMA driver */
00661     XDMAD_Initialize(&dmaDrv,0);
00662 
00663     /* Enable XDMA interrupt and give it priority over any other peripheral interrupt */
00664     NVIC_ClearPendingIRQ(XDMAC_IRQn);
00665     NVIC_SetPriority(XDMAC_IRQn, 1);
00666     NVIC_EnableIRQ( XDMAC_IRQn );
00667 
00668     /* Initialize the HSMCI driver */
00669     MCID_Init(&mciDrv[0], HSMCI, ID_HSMCI, BOARD_MCK, &dmaDrv, 0 ) ;
00670 
00671     /* Enable MCI interrupt and give it priority lower than DMA*/
00672     NVIC_ClearPendingIRQ(HSMCI_IRQn);
00673     NVIC_SetPriority(HSMCI_IRQn, 3);
00674     NVIC_EnableIRQ( HSMCI_IRQn );
00675 
00676     /* Initialize SD driver */
00677     for (i = 0; i < BOARD_NUM_MCI; i ++) {
00678         SDD_InitializeSdmmcMode(&sdDrv[i], &mciDrv[i], 0);
00679     }
00680 }
00681 
00682 /*----------------------------------------------------------------------------
00683  *         Global functions
00684  *----------------------------------------------------------------------------*/
00685 
00686 /**
00687  *  \brief hsmci_sdcard_fatfs Application entry point.
00688  *
00689  *  \return Unused (ANSI-C compatibility).
00690  */
00691 int main(void)
00692 {
00693     uint32_t i;
00694     uint8_t connected[BOARD_NUM_MCI];
00695 
00696     /* Disable watchdog */
00697     WDT_Disable( WDT ) ;
00698   
00699     SCB_EnableICache();
00700     SCB_EnableDCache();
00701 
00702     /* Output example information */
00703     printf("-- HSMCI SD/MMC FatFS Example %s --\n\r", SOFTPACK_VERSION);
00704     printf("-- %s\n\r", BOARD_NAME);
00705     printf( "-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__ , COMPILER_NAME) ;
00706 
00707     TimeTick_Configure();
00708     /* Initialize PIO pins */
00709     _ConfigurePIOs();
00710 
00711     /* Initialize drivers */
00712     _ConfigureDrivers();
00713     /* Initialize connections */
00714     for (i = 0; i < BOARD_NUM_MCI; i ++) {
00715         connected[i] = 0;
00716     }
00717 
00718     /* Check if any card is inserted */
00719     if (!AnyCardIsConnected()) {
00720         printf("-- Please insert a card\n\r");
00721         while(!AnyCardIsConnected());
00722     }
00723 
00724     /* Test all cards */
00725     for (i = 0; i < BOARD_NUM_MCI; i ++) {
00726         if (connected[i] == 0 && CardIsConnected(i)) {
00727             connected[i] = 1;
00728             LoopDelay(BOARD_MCK/1000/200);
00729             CardInit(i);
00730         }
00731 
00732         if (connected[i]) {
00733             if (RunFsTest()) {
00734                 TRACE_INFO("  Test passed !\n\r");
00735             } else {
00736                 printf("-F- Test Failed !\n\r");
00737             }
00738         }
00739     }
00740     while(1);
00741 }
00742 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines