本次工程是要同时实现SD卡读卡器和NAND Flash模拟U盘的功能。结合之前的两个工程,稍稍修改下就可以了。
既然要实现两个盘,当然在usb_prop.c中的Max_Lun变量赋值为1,在USB_User组中同时添加fsmc_nand.c和sdio_sdcard.c这两个文件,在外设库中挺尸添加stm32f10x_sdio.c和stm32f10x_fsmc.c两个文件。
添加完之后,可能会有些许的错误,解决完后,我们就要修改下mass_mal.c文件了,这个文件本次要同时实现SD卡和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;
uint16_t MAL_Init(uint8_t lun)
{
u16 status = MAL_OK;
switch (lun)
{
case 0:
Status = SD_Init(); //调用SD初始化函数
break;
case 1:
FlashInit();
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;
case 1:
FlashWriteOneSector(Memory_Offset,(u8*)Writebuff, Transfer_Length);
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); //等待sdio到sd卡传输结束
if ( Status != SD_OK )
{
return MAL_FAIL;
}
break;
case 1:
FlashReadOneSector(Memory_Offset, (u8*)Readbuff, Transfer_Length);
break;
default:
return MAL_FAIL;
}
return MAL_OK;
}
uint16_t MAL_GetStatus (uint8_t lun)
{
uint32_t DeviceSizeMul = 0, NumberOfBlocks = 0;
if (lun == 0)
{
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;
}
LED2_ON();
return MAL_OK;
}
else
{
LED2_OFF();
}
}
else
{
Mass_Block_Count[1] = FLASH_MAX_SECTOR_ADDR/FLASH_SECTOR_SIZE; //NAND_ZONE_SIZE * NAND_BLOCK_SIZE * NAND_MAX_ZONE ;
Mass_Block_Size[1] = FLASH_SECTOR_SIZE; //NAND_PAGE_SIZE;
Mass_Memory_Size[1] = (Mass_Block_Count[1] * Mass_Block_Size[1]);//这里的NAND要选择Mass_Block_Count[1]和Mass_Block_Size[1]
LED4_ON(); //因为SD的的是Mass_Block_Count[0]和Mass_Block_Size[0]
return MAL_OK;
}
return MAL_FAIL;
}
这样就可以在电脑上同时出现两个盘,分别是SD卡和NAND模拟出来的。
在调是这个代码之前,曾经粗心,把MAL_GetStatus()l里的NAND相关代码的下成如下:
Mass_Block_Count[0] = FLASH_MAX_SECTOR_ADDR/FLASH_SECTOR_SIZE;
Mass_Block_Size[0] = FLASH_SECTOR_SIZE;
Mass_Memory_Size[0] = (Mass_Block_Count[0] * Mass_Block_Size[0]);
数组元素都是0,与SD卡冲突,导致两个盘都显示不出来,但是你屏蔽掉SD相关的代码是可以的,或则屏蔽掉NAND相关代码也是可以的,偏偏两个代码一起的时候不行,检查了半天,才发现问题出在这里。
上一篇:STM32 USB SD读卡器(SDIO)
下一篇:STM32 USB NAND Flash模拟U盘无法格式化问题的解决
推荐阅读最新更新时间:2024-03-16 15:26