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

📄 sdmmc.c

📁 IAR5.2下 AT91SAM9260 ARM 对 MCP2515 控制源化码
💻 C
📖 第 1 页 / 共 2 页
字号:
    pCommand->cmd = AT91C_ALL_SEND_CID_CMD;
    pCommand->resSize = 4;
    pCommand->pResp = pCid;
    // Set SD command state
    pSd->state = SD_STATE_STBY;

    // Send the command
    return SendCommand(pSd);
}

//------------------------------------------------------------------------------
/// Asks The card to publish a new relative address.
/// Returns the command transfer result (see SendCommand).
/// \param pSd  Pointer to a SD card driver instance.
//------------------------------------------------------------------------------
static unsigned char Cmd3(SdCard *pSd)
{
    MciCmd *pCommand = &(pSd->command);
    unsigned int cardAddress;
    unsigned char error;

    memset(pCommand, 0, sizeof(MciCmd));
    // Fill command information
    pCommand->cmd = AT91C_SET_RELATIVE_ADDR_CMD;
    // Assign relative address to MMC card
    if (pSd->cardType == CARD_MMC) {
        pCommand->arg = (0x1 << 16);
    }
    pCommand->resSize = 1;
    pCommand->pResp = &cardAddress;
    // Set SD command state
    pSd->state = SD_STATE_STBY;

    // Send command
    error = SendCommand(pSd);
    if (error) {

        return error;
    }

    // Save card address in driver
    if (pSd->cardType != CARD_MMC) {
        pSd->cardAddress = (cardAddress >> 16) & 0xFFFF;
    }
    else {
        // Default MMC RCA is 0x0001
        pSd->cardAddress = 1;
    }

    return 0;
}

//------------------------------------------------------------------------------
/// Toggles a card between the stand-by and the transfer states or between the
/// programming and disconnects states.
/// Returns the command transfer result (see SendCommand).
/// \param pSd  Pointer to a SD card driver instance.
/// \param address  Relative Card Address (0 deselects all).
//------------------------------------------------------------------------------
static unsigned char Cmd7(SdCard *pSd, unsigned int address)
{
    MciCmd *pCommand = &(pSd->command);

    memset(pCommand, 0, sizeof(MciCmd));
    // Fill command information
    pCommand->cmd = AT91C_SEL_DESEL_CARD_CMD;
    pCommand->arg = address << 16;
    // Set SD command state
    pSd->state = SD_STATE_STBY;

    // Send command
    return SendCommand(pSd);
}

//------------------------------------------------------------------------------
/// Voltage check.
/// Returns 0 if successful; otherwise returns SD_ERROR_NORESPONSE if the card did
/// not answer the command, or SD_ERROR_MCI.
/// \param pSd  Pointer to a SD card driver instance.
/// \param supplyVoltage  Expected supply voltage.
//------------------------------------------------------------------------------
static unsigned char Cmd8(SdCard *pSd, unsigned char supplyVoltage)
{
    MciCmd *pCommand = &(pSd->command);
    unsigned int response;
    unsigned char error;

    memset(pCommand, 0, sizeof(MciCmd));
    // Fill command information
    pCommand->cmd = AT91C_SEND_IF_COND;
    pCommand->arg = (supplyVoltage << 8) | (0xAA);
    pCommand->resSize    = 1;
    pCommand->pResp      = &response;
    // Set SD command state
    pSd->state = SD_STATE_STBY;

    // Send command
    error = SendCommand(pSd);

    // Check result
    if (error == MCI_STATUS_NORESPONSE) {

        return SD_ERROR_NORESPONSE;
    }
    else if (!error && (response == ((supplyVoltage << 8) | 0xAA))) {

        return 0;
    }
    else {

        return SD_ERROR_MCI;
    }
}

//------------------------------------------------------------------------------
/// Addressed card sends its card specific data.
/// Returns the command transfer result (see SendCommand).
/// \param pSd  Pointer to a SD card driver instance.
//------------------------------------------------------------------------------
static unsigned char Cmd9(SdCard *pSd)
{
    MciCmd *pCommand = &(pSd->command);

    memset(pCommand, 0, sizeof(MciCmd));
    // Fill command information
    pCommand->cmd = AT91C_SEND_CSD_CMD;
    pCommand->arg = pSd->cardAddress << 16;
    pCommand->resSize = 4;
    pCommand->pResp = pSd->csd;
    // Set SD command state
    pSd->state = SD_STATE_STBY;

    // Send command
    return SendCommand(pSd);
}

//------------------------------------------------------------------------------
/// Forces the card to stop transmission.
/// \param pSd  Pointer to a SD card driver instance.
/// \param pStatus  Pointer to a status variable.
//------------------------------------------------------------------------------
static unsigned char Cmd12(SdCard *pSd)
{
    MciCmd *pCommand = &(pSd->command);

    memset(pCommand, 0, sizeof(MciCmd));
    // Fill command information
    pCommand->cmd = AT91C_STOP_TRANSMISSION_CMD;
    pCommand->conTrans = MCI_NEW_TRANSFER;
    // Set SD command state
    pSd->state = SD_STATE_STBY;

    // Send command
    return SendCommand(pSd);
}

//------------------------------------------------------------------------------
/// Addressed card sends its status register.
/// Returns the command transfer result (see SendCommand).
/// \param pSd  Pointer to a SD card driver instance.
/// \param pStatus  Pointer to a status variable.
//------------------------------------------------------------------------------
static unsigned char Cmd13(SdCard *pSd, unsigned int *pStatus)
{
    MciCmd *pCommand = &(pSd->command);

    memset(pCommand, 0, sizeof(MciCmd));
    // Fill command information
    pCommand->cmd = AT91C_SEND_STATUS_CMD;
    pCommand->arg = pSd->cardAddress << 16;
    pCommand->resSize = 1;
    pCommand->pResp = pStatus;
    // Set SD command state
    pSd->state = SD_STATE_STBY;

    // Send command
    return SendCommand(pSd);
}

//------------------------------------------------------------------------------
/// In the case of a Standard Capacity SD Memory Card, this command sets the
/// block length (in bytes) for all following block commands (read, write, lock).
/// Default block length is fixed to 512 Bytes.
/// Set length is valid for memory access commands only if partial block read
/// operation are allowed in CSD.
/// In the case of a High Capacity SD Memory Card, block length set by CMD16
/// command does not affect the memory read and write commands. Always 512
/// Bytes fixed block length is used. This command is effective for LOCK_UNLOCK command.
/// In both cases, if block length is set larger than 512Bytes, the card sets the
/// BLOCK_LEN_ERROR bit.
/// \param pSd  Pointer to a SD card driver instance.
/// \param blockLength  Block length in bytes.
//------------------------------------------------------------------------------
static unsigned char Cmd16(SdCard *pSd, unsigned short blockLength)
{
    MciCmd *pCommand = &(pSd->command);

    memset(pCommand, 0, sizeof(MciCmd));
    // Fill command information
    pCommand->cmd = AT91C_SET_BLOCKLEN_CMD;
    pCommand->arg = blockLength;
    // Set SD command state
    pSd->state = SD_STATE_STBY;

    // Send command
    return SendCommand(pSd);
}

//------------------------------------------------------------------------------
/// In the case of a Standard Capacity SD Memory Card, this command reads a block
/// of the size selected by the SET_BLOCKLEN.
/// In the case of a high capacity card, block length is fixed 512 bytes.
/// \param pSd  Pointer to a SD card driver instance.
/// \param blockSize  Block size (shall be set to 512 in case of high capacity).
/// \param pData  Pointer to the application buffer to be filled.
/// \param address  SD card address.
//------------------------------------------------------------------------------
static unsigned char Cmd17(
    SdCard *pSd,
    unsigned char *pData,
    unsigned int address)
{
    MciCmd *pCommand = &(pSd->command);

    memset(pCommand, 0, sizeof(MciCmd));
    // Fill command information
    pCommand->cmd = AT91C_READ_SINGLE_BLOCK_CMD;
    pCommand->arg = address;
    pCommand->blockSize = SD_BLOCK_SIZE;
    pCommand->pData = pData;
    pCommand->isRead = 1;
    // Set SD command state
    pSd->state = SD_STATE_STBY;

    // Send command
    return SendCommand(pSd);
}

//------------------------------------------------------------------------------
/// Continously transfers datablocks from card to host until interrupted by a
/// STOP_TRANSMISSION command.
/// \param pSd  Pointer to a SD card driver instance.
/// \param blockSize  Block size (shall be set to 512 in case of high capacity).
/// \param pData  Pointer to the application buffer to be filled.
/// \param address  SD card address.
//------------------------------------------------------------------------------
static unsigned char Cmd18(
    SdCard *pSd,
    unsigned short nbBlock,
    unsigned char *pData,
    unsigned int address)
{
    MciCmd *pCommand = &(pSd->command);

    memset(pCommand, 0, sizeof(MciCmd));
    // Fill command information
    pCommand->cmd = AT91C_READ_MULTIPLE_BLOCK_CMD;
    pCommand->arg = address;
    pCommand->blockSize = SD_BLOCK_SIZE;
    pCommand->nbBlock = nbBlock;
    pCommand->pData = pData;
    pCommand->isRead = 1;
    pCommand->conTrans = MCI_NEW_TRANSFER;
    // Set SD command state
    pSd->state = SD_STATE_DATA;

    // Send command
    return SendCommand(pSd);
}

//------------------------------------------------------------------------------
/// Write block command
/// \param pSd  Pointer to a SD card driver instance.
/// \param blockSize  Block size (shall be set to 512 in case of high capacity).
/// \param pData  Pointer to the application buffer to be filled.
/// \param address  SD card address.
//------------------------------------------------------------------------------
static unsigned char Cmd24(
    SdCard *pSd,
    unsigned short blockSize,
    const unsigned char *pData,
    unsigned int address)
{
    MciCmd *pCommand = &(pSd->command);

    memset(pCommand, 0, sizeof(MciCmd));
    // Fill command information
    pCommand->cmd = AT91C_WRITE_BLOCK_CMD;
    pCommand->arg = address;
    pCommand->blockSize = blockSize;
    pCommand->pData = (unsigned char *) pData;
    // Set SD command state
    pSd->state = SD_STATE_STBY;

    // Send command
    return SendCommand(pSd);
}

//------------------------------------------------------------------------------
/// Write block command
/// \param pSd  Pointer to a SD card driver instance.
/// \param blockSize  Block size (shall be set to 512 in case of high capacity).
/// \param pData  Pointer to the application buffer to be filled.
/// \param address  SD card address.
//------------------------------------------------------------------------------
static unsigned char Cmd25(
    SdCard *pSd,
    unsigned short nbBlock,
    unsigned char *pData,
    unsigned int address)
{
    MciCmd *pCommand = &(pSd->command);

    memset(pCommand, 0, sizeof(MciCmd));
    // Fill command information
    pCommand->cmd = AT91C_WRITE_MULTIPLE_BLOCK_CMD;
    pCommand->arg = address;
    pCommand->blockSize = SD_BLOCK_SIZE;
    pCommand->nbBlock = nbBlock;
    pCommand->pData = (unsigned char *) pData;
    pCommand->conTrans = MCI_NEW_TRANSFER;
    // Set SD command state
    pSd->state = SD_STATE_RCV;

    // Send command
    return SendCommand(pSd);
}


//------------------------------------------------------------------------------
/// Initialization delay: The maximum of 1 msec, 74 clock cycles and supply
/// ramp up time.
/// Returns the command transfer result (see SendCommand).
/// \param pSd  Pointer to a SD card driver instance.
//------------------------------------------------------------------------------
static unsigned char Cmd55(SdCard *pSd)
{
    MciCmd *pCommand = &(pSd->command);

    memset(pCommand, 0, sizeof(MciCmd));
    // Fill command information
    pCommand->cmd = AT91C_APP_CMD;
    pCommand->arg = (pSd->cardAddress << 16);
    // Set SD command state
    pSd->state = SD_STATE_STBY;

    // Send command
    return SendCommand(pSd);
}

//------------------------------------------------------------------------------
/// Defines the data bus width (

⌨️ 快捷键说明

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