STM32移植USB从机(读卡器) 从官方例程中移植以下文件
然后主要修改,注意在主函数中初始化SD卡后就不要在这个文件中初始化了
#include "usbd_msc_mem.h"
#include "AppLib.h"
#define STORAGE_LUN_NBR 2
const int8_t STORAGE_Inquirydata[] = {
0x00,
0x80,
0x02,
0x02,
(USBD_STD_INQUIRY_LENGTH- 5),
0x00,
0x00,
0x00,
'S','T', 'M', ' ', ' ', ' ', ' ', ' ',
'm','i', 'c', 'r', 'o', 'S', 'D', ' ',
'F','l', 'a', 's', 'h', ' ', ' ', ' ',
'1','.', '0' ,'0',
0x00,
0x80,
0x02,
0x02,
(USBD_STD_INQUIRY_LENGTH- 5),
0x00,
0x00,
0x00,
'S','T', 'M', ' ', ' ', ' ', ' ', ' ',
'N','a', 'n', 'd', ' ', ' ', ' ', ' ',
'F','l', 'a', 's', 'h', ' ', ' ', ' ',
'1','.', '0' ,'0',
};
int8_t STORAGE_Init(uint8_t lun);
int8_t STORAGE_GetCapacity(uint8_t lun,
uint32_t * block_num, uint32_t * block_size);
int8_t STORAGE_IsReady(uint8_t lun);
int8_t STORAGE_IsWriteProtected(uint8_t lun);
int8_t STORAGE_Read(uint8_t lun,
uint8_t * buf, uint32_t blk_addr, uint16_t blk_len);
int8_t STORAGE_Write(uint8_t lun,
uint8_t * buf, uint32_t blk_addr, uint16_t blk_len);
int8_t STORAGE_GetMaxLun(void);
USBD_STORAGE_cb_TypeDef USBD_MICRO_SDIO_fops = {
STORAGE_Init,
STORAGE_GetCapacity,
STORAGE_IsReady,
STORAGE_IsWriteProtected,
STORAGE_Read,
STORAGE_Write,
STORAGE_GetMaxLun,
(int8_t *) STORAGE_Inquirydata,
};
USBD_STORAGE_cb_TypeDef *USBD_STORAGE_fops = &USBD_MICRO_SDIO_fops;
int8_t STORAGE_Init(uint8_t lun)
{
return (0);
}
int8_t STORAGE_GetCapacity(uint8_t lun, uint32_t * block_num,
uint32_t * block_size)
{
SD_CardInfo SDCardInfo;
SD_GetCardInfo(&SDCardInfo);
if (SD_GetStatus() != 0)
{
return (-1);
}
*block_size = 512;
*block_num = SDCardInfo.CardCapacity / 512;
return (0);
}
int8_t STORAGE_IsReady(uint8_t lun)
{
if (SD_GetStatus() != 0)
{
return (-1);
}
return (0);
}
int8_t STORAGE_IsWriteProtected(uint8_t lun)
{
return 0;
}
int8_t STORAGE_Read(uint8_t lun,
uint8_t * buf, uint32_t blk_addr, uint16_t blk_len)
{
if(lun==0)
{
if (SD_ReadMultiBlocks(buf, blk_addr * 512, 512, blk_len) != 0)
{
return -1;
}
SD_WaitReadOperation();
while (SD_GetStatus() != SD_TRANSFER_OK);
}
if(lun==1)
{
if (SD_ReadMultiBlocks_SPI(buf, blk_addr * 512, 512, blk_len) != 0)
{
return -1;
}
}
return 0;
}
int8_t STORAGE_Write(uint8_t lun,
uint8_t * buf, uint32_t blk_addr, uint16_t blk_len)
{
if(lun==0)
{
if (SD_WriteMultiBlocks(buf, blk_addr * 512, 512, blk_len) != 0)
{
return -1;
}
SD_WaitWriteOperation();
while (SD_GetStatus() != SD_TRANSFER_OK);
}
if(lun==1)
{
if (SD_WriteMultiBlocks_SPI(buf, blk_addr * 512, 512, blk_len) != 0)
{
return -1;
}
}
return (0);
}
int8_t STORAGE_GetMaxLun(void)
{
return (STORAGE_LUN_NBR - 1);
}
|