這次是做一個SD卡的USB讀卡器的功能,我們就在上次NAND Flash模擬出的U盤的工程上修改了,這樣的話只要修改一小部分了。
工程的絕大部分不需要修改,只要將fsmc_nand.c文件移除工程,添加上官方的關(guān)于SDIO的SD卡的驅(qū)動代碼文件stm32_eval_sdio_sd.c,我討厭出現(xiàn)eval,所以將該文件直接改為sdio_sdcard.c。同時,既然我們使用SDIO來驅(qū)動SD卡,所以在外設庫文件組里要添加stm32_sdio.c文件,否則會出現(xiàn)很多未定錯誤。
我們接下去修改的就只有mass_mal.c這個文件了。更NAND Flash工程一樣,我們改成簡潔點:
uint32_t Mass_Memory_Size[2];
uint32_t Mass_Block_Size[2];
uint32_t Mass_Block_Count[2];
__IO uint32_t Status = 0;
#ifdef USE_STM3210E_EVAL
SD_CardInfo SDCardInfo;
#endif
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/*******************************************************************************
* Function Name : MAL_Init
* Description : 初始化STM32的媒體設備
* Input : lun
* Output : None
* Return : None
*******************************************************************************/
uint16_t MAL_Init(uint8_t lun)
{
uint16_t status = MAL_OK;
switch (lun)
{
case 0: //SD卡
Status = SD_Init(); //調(diào)用SD初始化函數(shù)
break;
default:
return MAL_FAIL; //都不是
}
return status;
}
uint16_t MAL_Write(uint8_t lun, uint32_t Memory_Offset, uint32_t *Writebuff, uint16_t Transfer_Length)
{
switch (lun)
{
case 0:
Status = SD_WriteBlock((uint8_t*)Writebuff, Memory_Offset, Transfer_Length);
SD_WaitWriteOperation(); //等待dma傳輸結(jié)束
while(SD_GetStatus() != SD_TRANSFER_OK); //等待sdio到sd卡傳輸結(jié)束
if ( Status != SD_OK )
{
return MAL_FAIL;
}
break;
default:
return MAL_FAIL;
}
return MAL_OK;
}
uint16_t MAL_Read(uint8_t lun, uint32_t Memory_Offset, uint32_t *Readbuff, uint16_t Transfer_Length)
{
switch (lun)
{
case 0:
Status = SD_ReadBlock((uint8_t*)Readbuff, Memory_Offset, Transfer_Length);
SD_WaitReadOperation(); //循環(huán)查詢dma傳輸是否結(jié)束
while(SD_GetStatus() != SD_TRANSFER_OK);
if ( Status != SD_OK )
{
return MAL_FAIL;
}
break;
default:
return MAL_FAIL;
}
return MAL_OK;
}
uint16_t MAL_GetStatus (uint8_t lun)
{
uint32_t temp_block_mul = 0;
uint32_t DeviceSizeMul = 0;
if (lun == 0)
{
#ifdef USE_STM3210E_EVAL
if (SD_Init() == SD_OK)
{
SD_GetCardInfo(&SDCardInfo);
SD_SelectDeselect((uint32_t) (SDCardInfo.RCA << 16));
DeviceSizeMul = (SDCardInfo.SD_csd.DeviceSizeMul + 2);
if(SDCardInfo.CardType == SDIO_HIGH_CAPACITY_SD_CARD)
{
Mass_Block_Count[0] = (SDCardInfo.SD_csd.DeviceSize + 1) * 1024;
}
else
{
NumberOfBlocks = ((1 << (SDCardInfo.SD_csd.RdBlockLen)) / 512);
Mass_Block_Count[0] = ((SDCardInfo.SD_csd.DeviceSize + 1) * (1 << DeviceSizeMul) << (NumberOfBlocks/2));
}
Mass_Block_Size[0] = 512;
Status = SD_SelectDeselect((uint32_t) (SDCardInfo.RCA << 16));
Status = SD_EnableWideBusOperation(SDIO_BusWide_4b);
if ( Status != SD_OK )
{
return MAL_FAIL;
}
Mass_Memory_Size[0] = Mass_Block_Count[0] * Mass_Block_Size[0];
LED2_ON();
return MAL_OK;
}
else
{
LED2_OFF();
}
}
return MAL_FAIL;
}
這樣SDIO接口的SD卡讀卡器就修改完畢了。我使用8G的SD卡測試的,不知道為什么寫這么慢,才60KB/秒左右,讀的速度為230KB/秒左右。
現(xiàn)在把成功后的截圖貼上來看看:
可以正常讀寫,也可以格式化,一切都OK,噢耶。