本帖最后由 hpdell 于 2016-12-7 09:49 编辑
请教下,最近在折腾sd卡模拟u盘读写 ,设备管理器里面可以识别到 ,电脑磁盘盘符也可以识别到(不能够识别sd卡容量),但就是不能够读写,
设备管理器识别:
最下面的 STM SDIO Flash USB Device 就是使用sd卡模拟出来的,
程序会不停的进入 STORAGE_Init_HS 、 STORAGE_Read_HS 这两个函数里面 ????????????????
这个函数识别 sd卡容量是正确的
/*******************************************************************************
* Function Name : STORAGE_GetCapacity_HS
* Description :
* Input : None.
* Output : None.
* Return : None.
*******************************************************************************/
int8_t STORAGE_GetCapacity_HS (uint8_t lun, uint32_t *block_num, uint16_t *block_size)
{
HAL_SD_Get_CardInfo(&hsd2, &SDCardInfo2);
*block_num = SDCardInfo2.CardCapacity/512;
*block_size = STORAGE_BLK_SIZ;
return (USBD_OK);
}
const int8_t STORAGE_Inquirydata_HS[] = {/* 36 */
/* LUN 0 */
0x00,
0x80,
0x02,
0x02,
(STANDARD_INQUIRY_DATA_LEN - 5),
0x00,
0x00,
0x00,
'S', 'T', 'M', ' ', ' ', ' ', ' ', ' ', /* Manufacturer : 8 bytes */
'S', 'D', 'I', 'O', ' ', 'F', 'l', 'a', /* Product : 16 Bytes */
's', 'h', ' ', ' ', ' ', ' ', ' ', ' ',
'0', '.', '0' ,'1', /* Version : 4 Bytes */
};
USBD_StorageTypeDef USBD_Storage_Interface_fops_HS =
{
STORAGE_Init_HS,
STORAGE_GetCapacity_HS,
STORAGE_IsReady_HS,
STORAGE_IsWriteProtected_HS,
STORAGE_Read_HS,
STORAGE_Write_HS,
STORAGE_GetMaxLun_HS,
(int8_t *)STORAGE_Inquirydata_HS,
};
int8_t STORAGE_Init_HS (uint8_t lun)
{
/* USER CODE BEGIN 9 */
static uint8_t i = 0;
uint8_t res=0;
res = BSP_SD_Init();
if(res != 0)
{
printf("BSP SDIO Init Error
");
return USBD_FAIL;
}
printf("BSP SDIO Init HS OK %d
", i++);
return res; // (USBD_OK);
/* USER CODE END 9 */
}
int8_t STORAGE_Read_HS (uint8_t lun,
uint8_t *buf,
uint32_t blk_addr,
uint16_t blk_len)
{
uint16_t n;
int8_t res=0;
long long lsector = blk_addr;
if(SDCardInfo2.CardType != STD_CAPACITY_SD_CARD_V1_1)
lsector <<= 9;
if( ((uint32_t )buf % 4) != 0)
{
for(n=0; n < blk_len; n++)
{
res = HAL_SD_ReadBlocks_DMA(&hsd2, (uint32_t *)sdio_buff, lsector + (512 * n), 512, 1);//通过DMA读取SD卡一个扇区
if(res == 0)//读取成功
{
//等待读取完成
res = HAL_SD_CheckReadOperation(&hsd2,(uint32_t)100000000);
}
memcpy(buf,sdio_buff, 512);
buf += 512;
}
}
else
{
res = HAL_SD_ReadBlocks_DMA(&hsd2, (uint32_t *)buf, lsector , 512, blk_len);//通过DMA读取SD卡一个扇区
if(res == 0)//读取成功
{
//等待读取完成
res = HAL_SD_CheckReadOperation(&hsd2,(uint32_t)100000000);
}
}
if(res)
{
USB_STATUS_REG|=0X08;//读错误!
}
return (USBD_StatusTypeDef)res;
}
int8_t STORAGE_Write_HS (uint8_t lun,
uint8_t *buf,
uint32_t blk_addr,
uint16_t blk_len)
{
uint16_t n;
int8_t res=0;
long long lsector = blk_addr;
{
if(SDCardInfo2.CardType != STD_CAPACITY_SD_CARD_V1_1)
lsector <<= 9;
if( ((uint32_t )buf % 4) != 0)
{
for(n=0; n < blk_len; n++)
{
memcpy(sdio_buff, buf, 512);
res = HAL_SD_WriteBlocks_DMA(&hsd2, (uint32_t *)sdio_buff, lsector + (512 * n), 512, 1);//单个sector的写操作
if(res == 0)//读取成功
{
//等待读取完成
res = HAL_SD_CheckWriteOperation(&hsd2,(uint32_t)100000000);
}
buf += 512;
}
}
else
{
res = HAL_SD_WriteBlocks_DMA(&hsd2, (uint32_t *)buf, lsector , 512, blk_len);//通过DMA读取SD卡一个扇区
if(res == 0)//读取成功
{
//等待读取完成
res = HAL_SD_CheckWriteOperation(&hsd2,(uint32_t)100000000);
}
}
}
if(res)
{
USB_STATUS_REG |= 0X04;//写错误!
}
return (USBD_StatusTypeDef)res;
}
多谢多谢了啊,终于搞定了,原来是 usb的速度配置错了啊
一周热门 更多>