SAMV71 Xplained Ultra Software Package 1.3

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 /*----------------------------------------------------------------------------
00132  *         Local variables
00133  *----------------------------------------------------------------------------*/
00134 
00135 /** Maximum number of LUNs which can be defined.*/
00136 /** (Logical drive = physical drive = medium number)*/
00137 #define MAX_LUNS        1
00138 
00139 /** Available media.*/
00140 extern sMedia medias[MAX_LUNS];
00141 
00142 /** DMA driver instance */
00143 static sXdmad dmaDrv;
00144 
00145 /** MCI driver instance. */
00146 static sMcid mciDrv[BOARD_NUM_MCI];
00147 
00148 /** SDCard driver instance. */
00149 extern  sSdCard sdDrv[BOARD_NUM_MCI];
00150 
00151 /** SD card pins instance. */
00152 static const Pin pinsSd[] = {BOARD_MCI_PINS_SLOTA, BOARD_MCI_PIN_CK};
00153 
00154 /** SD card detection pin instance. */
00155 static const Pin pinsCd[] = {BOARD_MCI_PIN_CD};
00156 
00157 
00158 #define ID_DRV DRV_MMC
00159 
00160 #if _FS_TINY == 0
00161 #define STR_ROOT_DIRECTORY "0:"
00162 #else
00163 #define STR_ROOT_DIRECTORY ""
00164 #endif
00165 
00166 const char* FileName = STR_ROOT_DIRECTORY "Basic.bin";
00167 const char* FileNameReadMe = STR_ROOT_DIRECTORY "ReadMe.txt";
00168 
00169 const char* ReadMeText = "Samv7 FatFS example: Done!!";
00170 
00171 
00172 /** size of the file to write/read.minimum size 512 for erase operation*/
00173 #define DATA_SIZE 2048
00174 
00175 uint8_t data[DATA_SIZE];
00176 
00177 /** Test settings: Number of bytes to test performance */
00178 #define TEST_PERFORMENCT_SIZE   (4*1024*1024)
00179 
00180 
00181 #define ASSERT(condition, ...)  { \
00182     if (!(condition)) { \
00183         printf("-F- ASSERT: "); \
00184         printf(__VA_ARGS__); \
00185     } \
00186 }
00187 /*----------------------------------------------------------------------------
00188  *         Local macros
00189  *----------------------------------------------------------------------------*/
00190 
00191 /* Defined to test Multi-Block functions */
00192 
00193 /** \def READ_MULTI
00194  *  \brief Define to test multi-read (SD_Read())
00195  *         or
00196  *         single-read is used (SD_ReadBlocks()) */
00197 #define READ_MULTI
00198 /** \def WRITE_MULTI
00199  *  \brief Define to test multi-write (SD_Write())
00200  *         or
00201  *         single-write is used (SD_WriteBlocks()) */
00202 #define WRITE_MULTI
00203 
00204 /** \macro SDT_ReadFun
00205  * Function used for SD card test reading.
00206  * \param pSd  Pointer to a SD card driver instance.
00207  * \param address  Address of the block to read.
00208  * \param nbBlocks Number of blocks to be read.
00209  * \param pData    Data buffer whose size is at least the block size.
00210  */
00211 #ifdef  READ_MULTI
00212 #define MMCT_ReadFun(pSd, blk, nbBlk, pData) \
00213     SD_Read(pSd, blk, pData, nbBlk, NULL, NULL)
00214 #else
00215 #define MMCT_ReadFun(pSd, blk, nbBlk, pData) \
00216     SD_ReadBlocks(pSd, blk, pData, nbBlk)
00217 #endif
00218 
00219 /** \macro SDT_WriteFun
00220  * Function used for SD card test writing.
00221  * \param pSd  Pointer to a SD card driver instance.
00222  * \param address  Address of the block to read.
00223  * \param nbBlocks Number of blocks to be read.
00224  * \param pData    Data buffer whose size is at least the block size.
00225  */
00226 #ifdef  WRITE_MULTI
00227 #define MMCT_WriteFun(pSd, blk, nbBlk, pData) \
00228     SD_Write(pSd, blk, pData, nbBlk, NULL, NULL)
00229 #else
00230 #define MMCT_WriteFun(pSd, blk, nbBlk, pData) \
00231     SD_WriteBlocks(pSd, blk, pData, nbBlk)
00232 #endif
00233 
00234 /*----------------------------------------------------------------------------
00235  *         Local functions
00236  *----------------------------------------------------------------------------*/
00237 
00238 /**
00239  * DMA interrupt handler.
00240  */
00241 void XDMAC_Handler(void)
00242 {
00243     XDMAD_Handler(&dmaDrv);
00244 }
00245 
00246 /**
00247  * MCI interrupt handler. Forwards the event to the MCI driver handlers.
00248  */
00249 void HSMCI_Handler(void)
00250 {
00251     
00252   MCID_Handler(&mciDrv[0]);
00253   
00254 }
00255 
00256 /*----------------------------------------------------------------------------
00257  *         Optional: SD card detection (connection, protection)
00258  *----------------------------------------------------------------------------*/
00259 
00260 /**
00261  * Configure for SD detect pin
00262  */
00263 static void CardDetectConfigure(void)
00264 {
00265     PIO_Configure(pinsCd, PIO_LISTSIZE(pinsCd));
00266     /* No protection detect pin */
00267 }
00268 
00269 /**
00270  * Return 1 if card is inserted.
00271  */
00272 static uint8_t CardIsConnected(uint8_t iMci)
00273 {
00274     return PIO_Get(&pinsCd[iMci]) ? 0 : 1;
00275 }
00276 
00277 /**
00278  * Return 1 if any card is inserted.
00279  */
00280 static uint8_t AnyCardIsConnected(void)
00281 {
00282     uint32_t i;
00283     for (i = 0; i < BOARD_NUM_MCI; i ++) {
00284         if ( CardIsConnected(i) ) {
00285             return 1;
00286         }
00287     }
00288     return 0;
00289 }
00290 
00291 /**
00292  * Return 1 if card is protected.
00293  */
00294 static uint8_t CardIsProtected(void)
00295 {
00296     TRACE_INFO("  Cannot check if SD card is write-protected\n\r");
00297     return 0;
00298 }
00299 
00300 /**
00301  * Delay some loop
00302  */
00303 static void LoopDelay(volatile unsigned int loop)
00304 {
00305     for(;loop > 0; loop --);
00306 }
00307 
00308 
00309 
00310 /**
00311  * Display: Dump Splitting row
00312  */
00313 static void DumpSeperator(void)
00314 {
00315     printf("\n\r==========================================\n\r");
00316 }
00317 
00318 
00319 /**
00320  * Dump card registers
00321  * \param slot Card slot (not used now).
00322  */
00323 static void DumpCardInfo(uint8_t iMci)
00324 {
00325     sSdCard *pSd = &sdDrv[iMci];
00326 
00327     if (SD_GetCardType(pSd) & CARD_TYPE_bmSDIO) {
00328         SDIO_DumpCardInformation(pSd);
00329     }
00330 
00331     if (SD_GetCardType(pSd) & CARD_TYPE_bmSDMMC) {
00332         SD_DumpCID(pSd->CID);
00333         SD_DumpCSD(pSd->CSD);
00334     }
00335 }
00336 
00337 /**
00338  * Run tests on the inserted card
00339  * \param slot Card slot (not used now).
00340  */
00341 static uint8_t CardInit(uint8_t iMci)
00342 {
00343     uint8_t error;
00344     uint8_t retry = 2;
00345 
00346     DumpSeperator();
00347     while(retry --) {
00348         error = SD_Init(&sdDrv[iMci]);
00349         if (error == SDMMC_OK) break;
00350     }
00351     if (error) {
00352         TRACE_ERROR("  SD/MMC card initialization failed: %d\n\r", error);
00353         return 1;
00354     }
00355     TRACE_INFO("  SD/MMC card initialization successful\n\r");
00356     TRACE_INFO("  Card size: %d MB", (int)SD_GetTotalSizeKB(&sdDrv[iMci])/1000);
00357     printf(", %d * %dB\n\r", 
00358         (int)SD_GetNumberBlocks(&sdDrv[iMci]), 
00359         (int)SD_GetBlockSize(&sdDrv[iMci]));
00360     DumpCardInfo(iMci);
00361     return 0;
00362 }
00363 
00364 
00365 
00366 /**
00367  * Initialize PIOs
00368  */
00369 static void _ConfigurePIOs(void)
00370 {
00371     /* Configure SDcard pins */
00372     PIO_Configure(pinsSd, PIO_LISTSIZE(pinsSd));
00373     /* Configure SD card detection */
00374     CardDetectConfigure();
00375     /* Check if card is write-protected (if supported) */
00376     CardIsProtected();
00377 }
00378 
00379 /**
00380  * Scan files under a certain path
00381  * \param path    folder path
00382  * return scan result, 1: success.
00383  */
00384 static FRESULT scan_files (char* path)
00385 {
00386     FRESULT res;
00387     FILINFO fno;
00388     DIR dir;
00389     int32_t i;
00390     char *fn;
00391 #if _USE_LFN
00392     static char lfn[_MAX_LFN * (_DF1S ? 2 : 1) + 1];
00393     fno.lfname = lfn;
00394     fno.lfsize = sizeof(lfn);
00395 #endif
00396 
00397 
00398     res = f_opendir(&dir, path);
00399     if (res == FR_OK) {
00400         i = strlen(path);
00401         for (;;) {
00402             res = f_readdir(&dir, &fno);
00403             if (res != FR_OK || fno.fname[0] == 0) break;
00404 #if _USE_LFN
00405             fn = *fno.lfname ? fno.lfname : fno.fname;
00406 #else
00407             fn = fno.fname;
00408 #endif
00409             if (*fn == '.') continue;
00410             if (fno.fattrib & AM_DIR) {
00411                 sprintf(&path[i], "/%s", fn);
00412                 res = scan_files(path);
00413                 if (res != FR_OK) break;
00414                 path[i] = 0;
00415             } else {
00416                 printf("%s/%s\n\r", path, fn);
00417             }
00418         }
00419     }
00420     return res;
00421 }
00422 
00423 static uint8_t formatdisk(const TCHAR *pDrv)
00424 {
00425     FRESULT res;
00426 #if _FS_TINY == 0
00427     /** Format disk*/
00428     TRACE_INFO(" Please wait a moment. Formatting the disk...\n\r");
00429     res = f_mkfs(pDrv,    // Drv
00430                0,    // FDISK partition
00431                512); // AllocSize
00432     TRACE_INFO("  Format disk finished !\n\r");
00433     if( res != FR_OK ) {
00434         TRACE_ERROR("  f_mkfs pb: 0x%X\n\r", res);
00435         return 0;
00436     }
00437     return 1;
00438 #else
00439     TRACE_INFO("  Please run Full version FAT FS test first\n\r");
00440     return 0;
00441 #endif
00442 }
00443 
00444 /**
00445  * Do file system tests
00446  * \return test result, 1: success.
00447  */
00448 static uint8_t RunFsTest(void)
00449 {
00450     uint32_t i;
00451     uint32_t ByteToRead;
00452     uint32_t ByteRead;
00453 #if _FS_TINY == 0
00454     uint32_t ByteWritten;
00455     char key;
00456 #endif
00457 
00458     const TCHAR Drv_Num = ID_DRV;
00459     FRESULT res;
00460     DIR dirs;
00461     FATFS fs;
00462     FIL FileObject;
00463     uint32_t tickStart, tickEnd, ticks, rwSpeed;
00464 
00465     gNbMedias = 1;
00466     /** Mount disk*/
00467     TRACE_INFO("Mount disk %d\n\r", ID_DRV);
00468     /** Clear file system object*/
00469     memset(&fs, 0, sizeof(FATFS));
00470     
00471     /* Mount the drive */
00472     res = f_mount(&fs, &Drv_Num, 1);
00473     if( res != FR_OK ) {
00474     /* If No file system found, format it */
00475         if(res == FR_NO_FILESYSTEM) {
00476             TRACE_INFO("No file System found\n\r");
00477             if(formatdisk(&Drv_Num)) {
00478             /* Try to mount again */
00479                 res = f_mount(&fs, &Drv_Num, 1);
00480                 if( res != FR_OK ) {
00481                     TRACE_ERROR("f_mount pb: 0x%X\n\r", res); 
00482                     return 0;
00483                 }
00484             } else {
00485                 return 0;
00486             }
00487         } else {
00488             TRACE_ERROR("f_mount pb: 0x%X\n\r", res);
00489             return 0;
00490         }
00491     }
00492 
00493     /** Test if the disk is formatted*/
00494     res = f_opendir (&dirs,STR_ROOT_DIRECTORY);
00495     if(res == FR_OK ){
00496         /** erase sdcard to re-format it ?*/
00497         TRACE_INFO("The disk is already formatted.\n\r");
00498 
00499         /** Display the file tree*/
00500         TRACE_INFO(" Display files contained on the SDcard :\n\r");
00501         DumpSeperator();
00502         scan_files(STR_ROOT_DIRECTORY);
00503         DumpSeperator();
00504 
00505 #if _FS_TINY == 0
00506         TRACE_INFO("Do you want to erase the sdcard to re-format disk ? (y/n)!\n\r");
00507         key = DBG_GetChar();
00508         if( (key == 'y') ||  (key == 'Y'))
00509         {
00510             for(i=0;i<100;i++) {
00511                 MEDSdcard_EraseBlock(&medias[ID_DRV], i);
00512             }
00513             TRACE_INFO(" Erase the first 100 blocks complete !\n\r");
00514             res = FR_NO_FILESYSTEM;
00515         }
00516 #endif
00517     }
00518 
00519     if( res == FR_NO_FILESYSTEM ) {
00520 
00521 #if _FS_TINY == 0
00522         /** Format disk*/
00523         TRACE_INFO("Format disk %d\n\r", ID_DRV);
00524         if(!(formatdisk(&Drv_Num)) ) {
00525           return 0;
00526         }
00527           
00528 #else
00529         TRACE_INFO("Please run Full version FAT FS test first\n\r");
00530         return 0;
00531 #endif
00532     }
00533 
00534 #if _FS_TINY == 0
00535     DumpSeperator();
00536     /** Create a new file*/
00537     TRACE_INFO("Create a file : \"%s\"\n\r", FileName);
00538     res = f_open(&FileObject, FileName, FA_CREATE_ALWAYS|FA_WRITE);
00539     if( res != FR_OK ) {
00540         TRACE_ERROR("f_open create pb: 0x%X\n\r", res);
00541         return 0;
00542     }
00543 
00544     /** Write a checkerboard pattern in the buffer*/
00545     for (i=0; i < sizeof(data); i++) {
00546         if ((i & 1) == 0) {
00547             data[i] = (i & 0x55);
00548         } else {
00549             data[i] = (i & 0xAA);
00550         }
00551     }
00552     TRACE_INFO("Writing to file\n\r");
00553     tickStart = GetTicks();
00554     for(i = 0; i< TEST_PERFORMENCT_SIZE;i+=DATA_SIZE) {
00555         res = f_write(&FileObject, data, DATA_SIZE, &ByteWritten);
00556         if( res != FR_OK ) {
00557             TRACE_ERROR("f_write pb: 0x%X\n\r", res);
00558             return 0;
00559         }
00560     }
00561     tickEnd = GetTicks();
00562     ticks = GetDelayInTicks(tickStart, tickEnd);
00563     rwSpeed = TEST_PERFORMENCT_SIZE / ticks;
00564     TRACE_INFO("Done, Bad %u, Speed %uK\n\r", (uint32_t)0, (uint32_t)rwSpeed);
00565     /** Close the file*/
00566     TRACE_INFO("Close file\n\r");
00567     res = f_close(&FileObject);
00568     if( res != FR_OK ) {
00569         TRACE_ERROR("f_close pb: 0x%X\n\r", res);
00570         return 0;
00571     }
00572 #endif
00573 
00574     DumpSeperator();
00575     /** Open the file*/
00576     TRACE_INFO("Open file to read: \"%s\"\n\r", FileName);
00577     res = f_open(&FileObject, FileName, FA_OPEN_EXISTING|FA_READ);
00578     if( res != FR_OK ) {
00579         TRACE_ERROR("f_open read pb: 0x%X\n\r", res);
00580         return 0;
00581     }
00582     /** Read file*/
00583     TRACE_INFO("Read file\n\r");
00584     memset(data, 0, DATA_SIZE);
00585     ByteToRead = FileObject.fsize;
00586     tickStart = GetTicks();
00587     for(i = 0; i< ByteToRead;i+=DATA_SIZE) {
00588         res = f_read(&FileObject, data, DATA_SIZE, &ByteRead);
00589         if(res != FR_OK) {
00590             TRACE_ERROR("f_read pb: 0x%X\n\r", res);
00591             return 0;
00592         }
00593     }
00594     tickEnd = GetTicks();
00595     ticks = GetDelayInTicks(tickStart, tickEnd);
00596     rwSpeed = ByteToRead / ticks;
00597     TRACE_INFO("Done, Bad %u, Speed %uK\n\r", (uint32_t)0, (uint32_t)rwSpeed);
00598 
00599     /** Close the file*/
00600     TRACE_INFO("Close file\n\r");
00601     res = f_close(&FileObject);
00602     if( res != FR_OK ) {
00603         TRACE_ERROR("f_close pb: 0x%X\n\r", res);
00604         return 0;
00605     }
00606 
00607     /** compare read data with the expected data*/
00608     for (i=0; i < sizeof(data); i++) {
00609         ASSERT((((i & 1) == 0) && (data[i] == (i & 0x55)))
00610                 || (data[i] == (i & 0xAA)),
00611                 "Invalid data at data[%u] (expected 0x%02X, read 0x%02X)\n\r",
00612                 i, ((i & 1) == 0) ? (i & 0x55) : (i & 0xAA), data[i]);
00613     }
00614     TRACE_INFO("File data OK !\n\r");
00615 
00616     DumpSeperator();
00617     /** Create a new file*/
00618     TRACE_INFO("Create a file : \"%s\"\n\r", FileNameReadMe);
00619     res = f_open(&FileObject, FileNameReadMe, FA_CREATE_ALWAYS|FA_WRITE);
00620     if( res != FR_OK ) {
00621         TRACE_ERROR("  f_open create pb: 0x%X\n\r", res);
00622         return 0;
00623     }
00624 
00625     TRACE_INFO("Write ReadMe file\n\r");
00626     tickStart = GetTicks();
00627     while(*ReadMeText != '\0')
00628         res = f_write(&FileObject, ReadMeText++, 1 , &ByteWritten);
00629     if( res != FR_OK ) {
00630         TRACE_ERROR("f_write pb: 0x%X\n\r", res);
00631         return 0;
00632     }
00633 
00634     tickEnd = GetTicks();
00635     ticks = GetDelayInTicks(tickStart, tickEnd);
00636     rwSpeed = TEST_PERFORMENCT_SIZE / ticks;
00637     TRACE_INFO("Done, Bad %u, Speed %uK\n\r", (uint32_t)0, (uint32_t)rwSpeed);
00638     /** Close the file*/
00639     TRACE_INFO("Close file\n\r");
00640     res = f_close(&FileObject);
00641     if( res != FR_OK ) {
00642         TRACE_ERROR("  f_close pb: 0x%X\n\r", res);
00643         return 0;
00644     }
00645 
00646     DumpSeperator();
00647     return 1;
00648 }
00649 /**
00650  * Initialize driver instances.
00651  */
00652 static void _ConfigureDrivers(void)
00653 {
00654     uint32_t i;
00655     /* Initialize the DMA driver */
00656     XDMAD_Initialize(&dmaDrv,0);
00657 
00658     /* Enable XDMA interrupt and give it priority over any other peripheral interrupt */
00659     NVIC_ClearPendingIRQ(XDMAC_IRQn);
00660     NVIC_SetPriority(XDMAC_IRQn, 1);
00661     NVIC_EnableIRQ( XDMAC_IRQn );
00662 
00663     /* Initialize the HSMCI driver */
00664     MCID_Init(&mciDrv[0], HSMCI, ID_HSMCI, BOARD_MCK, &dmaDrv, 0 ) ;
00665 
00666     /* Enable MCI interrupt and give it priority lower than DMA*/
00667     NVIC_ClearPendingIRQ(HSMCI_IRQn);
00668     NVIC_SetPriority(HSMCI_IRQn, 3);
00669     NVIC_EnableIRQ( HSMCI_IRQn );
00670 
00671     /* Initialize SD driver */
00672     for (i = 0; i < BOARD_NUM_MCI; i ++) {
00673         SDD_InitializeSdmmcMode(&sdDrv[i], &mciDrv[i], 0);
00674     }
00675 }
00676 
00677 /*----------------------------------------------------------------------------
00678  *         Global functions
00679  *----------------------------------------------------------------------------*/
00680 
00681 /**
00682  *  \brief hsmci_sdcard_fatfs Application entry point.
00683  *
00684  *  \return Unused (ANSI-C compatibility).
00685  */
00686 int main(void)
00687 {
00688     uint32_t i;
00689     uint8_t connected[BOARD_NUM_MCI];
00690 
00691     /* Disable watchdog */
00692     WDT_Disable( WDT ) ;
00693   
00694     SCB_EnableICache();
00695     SCB_EnableDCache();
00696 
00697     /* Output example information */
00698     printf("-- HSMCI SD/MMC FatFS Example %s --\n\r", SOFTPACK_VERSION);
00699     printf("-- %s\n\r", BOARD_NAME);
00700     printf( "-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__ , COMPILER_NAME) ;
00701 
00702     TimeTick_Configure();
00703     /* Initialize PIO pins */
00704     _ConfigurePIOs();
00705 
00706     /* Initialize drivers */
00707     _ConfigureDrivers();
00708     /* Initialize connections */
00709     for (i = 0; i < BOARD_NUM_MCI; i ++) {
00710         connected[i] = 0;
00711     }
00712 
00713     /* Check if any card is inserted */
00714     if (!AnyCardIsConnected()) {
00715         printf("-- Please insert a card\n\r");
00716         while(!AnyCardIsConnected());
00717     }
00718 
00719     /* Test all cards */
00720     for (i = 0; i < BOARD_NUM_MCI; i ++) {
00721         if (connected[i] == 0 && CardIsConnected(i)) {
00722             connected[i] = 1;
00723             LoopDelay(BOARD_MCK/1000/200);
00724             CardInit(i);
00725         }
00726 
00727         if (connected[i]) {
00728             if (RunFsTest()) {
00729                 TRACE_INFO("  Test passed !\n\r");
00730             } else {
00731                 printf("-F- Test Failed !\n\r");
00732             }
00733         }
00734     }
00735     while(1);
00736 }
00737 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines