⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 SDCard for ARM9 Atmel at91sam9260 mcu
💻 C
字号:
/* ----------------------------------------------------------------------------
 *         ATMEL Microcontroller Software Support  -  ROUSSET  -
 * ----------------------------------------------------------------------------
 * Copyright (c) 2007, Atmel Corporation

 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the disclaiimer below.
 *
 * - Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the disclaimer below in the documentation and/or
 * other materials provided with the distribution.
 *
 * Atmel's name may not be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * ----------------------------------------------------------------------------
 */

//------------------------------------------------------------------------------
//         Headers
//------------------------------------------------------------------------------

#include <board.h>
#include <aic/aic.h>
#include <pio/pio.h>
#include <dbgu/dbgu.h>
#include <mci/mci.h>
#include <utility/assert.h>
#include <components/sdmmc/sdmmc.h>

#include <string.h>


//------------------------------------------------------------------------------
//         Local variables
//------------------------------------------------------------------------------

/// MCI driver instance.
static Mci mciDrv;

/// SDCard driver instance.
static SdCard sdDrv;

/// SD card pins.
static const Pin pinsSd[] = {BOARD_SD_PINS};
/// DBGU pins.
static const Pin pinsDbgu[] = {PINS_DBGU};

//------------------------------------------------------------------------------
//         Local macros
//------------------------------------------------------------------------------
#define MULTIPLE_TEST

//------------------------------------------------------------------------------
//         Local functions
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
/// MCI0 interrupt handler. Forwards the event to the MCI driver handler.
//------------------------------------------------------------------------------
void ISR_Mci0(void)
{
    MCI_Handler(&mciDrv);
}

//------------------------------------------------------------------------------
/// Displays the content of the given buffer on the DBGU.
/// \param pBuffer  Pointer to the buffer to dump.
/// \param size  Buffer size in bytes.
//------------------------------------------------------------------------------
void Dump(const unsigned char *pBuffer, unsigned int size)
{
    unsigned int i, j;
    
    for (i=0; i < (size / 16); i++) {

        printf("0x%08X: ", i*16);
        for (j=0; j < 4; j++) {

            printf("%08X ", ((unsigned int *) pBuffer)[i*4+j]);
        }
        printf("\n\r");
    }
}

//------------------------------------------------------------------------------
//         Optional: SD card detection
//------------------------------------------------------------------------------

#ifdef BOARD_SD_PIN_CD

/// SD card detection pin instance.
static const Pin pinMciCardDetect = BOARD_SD_PIN_CD;

//------------------------------------------------------------------------------
/// Waits for a SD card to be connected.
//------------------------------------------------------------------------------
void WaitSdConn(void)
{
    PIO_Configure(&pinMciCardDetect, 1);
    printf("-I- Please connect a SD card ...\n\r");
    while (PIO_Get(&pinMciCardDetect) != 0);
    printf("-I- SD card connection detected\n\r");
}

#else

//------------------------------------------------------------------------------
/// Dummy function.
//------------------------------------------------------------------------------
void WaitSdConn(void)
{
    printf("-I- SD card detection not available, assuming card is present\n\r");
}

#endif

//------------------------------------------------------------------------------
//         Optional: Write protection status
//------------------------------------------------------------------------------

#ifdef BOARD_SD_PIN_WP

/// Write protection status pin instance.
static const Pin pinMciWriteProtect = BOARD_SD_PIN_WP;

//------------------------------------------------------------------------------
/// Checks if the device is write protected.
//------------------------------------------------------------------------------
void CheckProtection(void)
{
    PIO_Configure(&pinMciWriteProtect, 1);
    if (PIO_Get(&pinMciWriteProtect) != 0) {

        printf("-I- SD card is write-protected\n\r");
    }
    else {

        printf("-I- SD card is NOT write-protected.\n\r");
    }
}

#else

//------------------------------------------------------------------------------
/// Dummy implementation.
//------------------------------------------------------------------------------
void CheckProtection(void)
{
    printf("-I- Cannot check if SD card is write-protected\n\r");
}

#endif

//------------------------------------------------------------------------------
//         Global functions
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
/// Main function
//------------------------------------------------------------------------------
#define NB_MULTI_BLOCKS 5

int main(void)
{
    unsigned char pBuffer[SD_BLOCK_SIZE * NB_MULTI_BLOCKS];
    unsigned int block;
    unsigned int i;
    unsigned short multiBlock;
    unsigned char error;

    /// Configure DBGU
    PIO_Configure(pinsDbgu, PIO_LISTSIZE(pinsDbgu));
    DBGU_Configure(DBGU_STANDARD, 115200, BOARD_MCK);
    printf("-- Basic SD-Card Project 1.3 --\n\r");

    // Configure SDcard pins
    PIO_Configure(pinsSd, PIO_LISTSIZE(pinsSd));

    // Wait for SD card connection (if supported)
    WaitSdConn();

    // Check if card is write-protected (if supported)
    CheckProtection();

    // Initialize the MCI driver
    AIC_ConfigureIT(BOARD_SD_MCI_ID,  AT91C_AIC_PRIOR_LOWEST, ISR_Mci0);
    MCI_Init(&mciDrv, BOARD_SD_MCI_BASE, BOARD_SD_MCI_ID, BOARD_SD_SLOT);
    AIC_EnableIT(BOARD_SD_MCI_ID);

    // Initialize the SD card driver
    if (SD_Init(&sdDrv, &mciDrv)) {
    
        printf("-E- SD/MMC card initialization failed\n\r");
    }
    else {
    
        printf("-I- SD/MMC card initialization successful\n\r");
        printf("-I- Card size: %u MB\n\r", SD_TOTAL_SIZE(&sdDrv)/(1024*1024));
        printf("-I- Block size: %d Bytes\n\r", SD_CSD_BLOCK_LEN(&sdDrv));
    }
    MCI_SetSpeed(&mciDrv, 10000000);

    // Perform tests on each block
    multiBlock = 1;
    for (block=0; block < (SD_TOTAL_BLOCK(&sdDrv)-multiBlock); ) {
        // Perform single block or multi block transfer
        printf("\r-I- Testing block [%6u - %6u] ...", block, (block + multiBlock -1));

        // Clear the block
        memset(pBuffer, 0, SD_BLOCK_SIZE * multiBlock);
        for (i=0; i < SD_BLOCK_SIZE * multiBlock; i++) {

            ASSERT(pBuffer[i] == 0,
                   "\n\r-F- Invalid data at pBuffer[%u] before write ! (expected 0x00, read 0x%02X)\n\r",
                   i, pBuffer[i]);
        }
        error = SD_WriteBlock(&sdDrv, block, multiBlock, pBuffer);
        ASSERT(!error, "\n\r-F- Failed to write block (%d) #%u\n\r", error, block);

        // Read back the data to check the write operation
        memset(pBuffer, 0xFF, SD_BLOCK_SIZE * multiBlock);
        error = SD_ReadBlock(&sdDrv, block, multiBlock, pBuffer);
        ASSERT(!error, "\n\r-F- Failed to read block (%d) #%u\n\r", error, block);
        for (i=0; i < SD_BLOCK_SIZE * multiBlock; i++) {

            ASSERT(pBuffer[i] == 0,
                   "\n\r-F- Invalid data at pBuffer[%u] (expected 0x00, read 0x%02X)\n\r",
                   i, pBuffer[i]);
        }

        // Write a checkerboard pattern on the block
        for (i=0; i < SD_BLOCK_SIZE * multiBlock; i++) {

            if ((i & 1) == 0) {

                pBuffer[i] = (i & 0x55);
            }
            else {

                pBuffer[i] = (i & 0xAA);
            }
        }
        error = SD_WriteBlock(&sdDrv, block, multiBlock, pBuffer);
        ASSERT(!error, "\n\r-F- Failed to write block #%u\n\r", block);

        // Read back the data to check the write operation
        memset(pBuffer, 0, SD_BLOCK_SIZE * multiBlock);
        error = SD_ReadBlock(&sdDrv, block, multiBlock, pBuffer);
        ASSERT(!error, "\n\r-F- Failed to read block #%u\n\r", block);
        for (i=0; i < SD_BLOCK_SIZE * multiBlock; i++) {

            ASSERT((((i & 1) == 0) && (pBuffer[i] == (i & 0x55)))
                   || (pBuffer[i] == (i & 0xAA)),
                   "\n\r-F- Invalid data at pBuffer[%u] (expected 0x%02X, read 0x%02X)\n\r",
                   i, ((i & 1) == 0) ? (i & 0x55) : (i & 0xAA), pBuffer[i]);
        }

        block += multiBlock;

        // Perform different single or multiple bloc operations
        if (++multiBlock > NB_MULTI_BLOCKS)
            multiBlock = 1;
    }

    #ifdef MULTIPLE_TEST
    MCI_SetSpeed(&mciDrv, 1000000);
    block=0;
    for (; block < SD_TOTAL_BLOCK(&sdDrv)-NB_MULTI_BLOCKS; ) {

        for (multiBlock = 0; multiBlock < NB_MULTI_BLOCKS; multiBlock++) {
            for (i=0; i < SD_BLOCK_SIZE; i++) {
                    pBuffer[i] = ((block + multiBlock) & 0xFF);
            }
            printf("\r-I- Writing block [%6u] ...             ", (block + multiBlock));
            error = SD_WriteBlock(&sdDrv, (block + multiBlock), 1, pBuffer);
            ASSERT(!error, "\n\r-F- Failed to write block #%u\n\r", (block + multiBlock));
        }

        printf("\n\r");

        for (multiBlock = 0; multiBlock < NB_MULTI_BLOCKS; multiBlock++) {
            printf("\r-I- Reading block [%6u] ... ", (block + multiBlock));
            error = SD_ReadBlock(&sdDrv, (block + multiBlock), 1, pBuffer);
            // Dump(pBuffer, SD_BLOCK_SIZE);
            ASSERT(!error, "\n\r-F- Failed to read block #%u\n\r", (block + multiBlock));
            for (i=0; i < SD_BLOCK_SIZE; i++) {

                ASSERT(pBuffer[i] == ((block + multiBlock) & 0xFF),
                       "\n\r-F- Invalid data at pBuffer[%u] (expected 0x%02X, read 0x%02X)\n\r",
                       i, ((block + multiBlock) & 0xFF), pBuffer[i]);
            }
        }
        printf("\n\r");

        block+= NB_MULTI_BLOCKS;
    }
    #endif

    error = SD_Stop(&sdDrv, &mciDrv);
    ASSERT(!error, "\n\r-F- Failed to stop SD card!\n\r");

    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -