本文主要是介绍STM32 USB SD读卡器(SDIO),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
这次是做一个SD卡的USB读卡器的功能,我们就在上次NAND Flash模拟出的U盘的工程上修改了,这样的话只要修改一小部分了。这样SDIO接口的SD卡读卡器就修改完毕了。我使用8G的SD卡测试的,不知道为什么写这么慢,才60KB/秒左右,读的速度为230KB/秒左右。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(); //调用SD初始化函数 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传输结束 while(SD_GetStatus() != SD_TRANSFER_OK); //等待sdio到sd卡传输结束 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(); //循环查询dma传输是否结束
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; }
现在把成功后的截图贴上来看看:
可以正常读写,也可以格式化,一切都OK,噢耶。
这篇关于STM32 USB SD读卡器(SDIO)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!