在USB库文件mass_mal.c中添加对flash和sd读写的函数,USB库调用这些函数从而实现模拟U盘的功能
//mass_mal.c
/* Includes ------------------------------------------------------------------*/
#include "..\User\sdcard\bsp_sdio_sdcard.h"
#include "..\User\spi_flash\fatfs_flash_spi.h"
#include "mass_mal.h"
#include <stdio.h>
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
uint32_t Mass_Memory_Size[];
uint32_t Mass_Block_Size[];
uint32_t Mass_Block_Count[];
__IO uint32_t Status = ;
//#define sFLASH_ID 0xEF3015 //W25X16
//#define sFLASH_ID 0xEF4015 //W25Q16
#define sFLASH_ID 0XEF4017 //W25Q64
extern SD_CardInfo SDCardInfo; //ÓÃÓÚ´æ´¢¿¨µÄÐÅÏ¢
/*******************************************************************************
* Function Name : MAL_Init
* Description : Initializes the Media on the STM32
* Input : None
* Output : None
* Return : None
*******************************************************************************/
uint16_t MAL_Init(uint8_t lun)
{
uint16_t status = MAL_OK; switch (lun)
{
case :
FLASH_SPI_disk_initialize();
if(SPI_FLASH_ReadID()==sFLASH_ID)
{
printf("flash init succsee\n");
Status = MAL_OK;
}
else
Status = MAL_FAIL;
break;
case :
Status = SD_Init();
break;
default:
return MAL_FAIL;
}
return status;
}
/*******************************************************************************
* Function Name : MAL_Write
* Description : Write sectors
* Input : None
* Output : None
* Return : None
*******************************************************************************/
uint16_t MAL_Write(uint8_t lun, uint32_t Memory_Offset, uint32_t *Writebuff, uint16_t Transfer_Length)
{
switch (lun)
{
case :
Memory_Offset+=(*);//扇区偏移,外部flash文件系统空间放在外部flash 6M空间 //printf("write add =%d.length=%d\n",Memory_Offset/4096,Transfer_Length/4096);
SPI_FLASH_SectorErase(Memory_Offset);
SPI_FLASH_BufferWrite((uint8_t *)Writebuff,Memory_Offset,Transfer_Length);
break;
case :
Status = SD_WriteBlock((uint8_t*)Writebuff, Memory_Offset, Transfer_Length);
Status = SD_WaitWriteOperation();
while(SD_GetStatus() != SD_TRANSFER_OK);
if ( Status != SD_OK )
{
return MAL_FAIL;
}
break;
default:
return MAL_FAIL;
}
return MAL_OK;
}
/*******************************************************************************
* Function Name : MAL_Read
* Description : Read sectors
* Input : None
* Output : None
* Return : Buffer pointer
*******************************************************************************/
uint16_t MAL_Read(uint8_t lun, uint32_t Memory_Offset, uint32_t *Readbuff, uint16_t Transfer_Length)
{ switch (lun)
{
case :
Memory_Offset+=(*);//扇区偏移 //printf("read add =%d.length=%d\n",Memory_Offset/4096,Transfer_Length/4096);
SPI_FLASH_BufferRead((uint8_t *)Readbuff, Memory_Offset, Transfer_Length);
break; case :
SD_ReadBlock((uint8_t*)Readbuff, Memory_Offset, Transfer_Length);
Status = SD_WaitReadOperation();
while(SD_GetStatus() != SD_TRANSFER_OK)
{
}
if ( Status != SD_OK )
{
return MAL_FAIL;
}
break;
default:
return MAL_FAIL;
}
return MAL_OK;
}
/*******************************************************************************
* Function Name : MAL_GetStatus
* Description : Get status
* Input : None
* Output : None
* Return : None
*******************************************************************************/
uint16_t MAL_GetStatus (uint8_t lun)
{
uint32_t DeviceSizeMul = , NumberOfBlocks = ;
switch (lun)
{
case :
{
FLASH_SPI_disk_initialize();
if(SPI_FLASH_ReadID()==sFLASH_ID)
{
Mass_Block_Size[] =;
Mass_Block_Count[] =;
Mass_Memory_Size[] =Mass_Block_Size[]*Mass_Block_Count[];
return MAL_OK;
}
}
case :
if (SD_Init() == SD_OK)
{
SD_GetCardInfo(&SDCardInfo);
SD_SelectDeselect((uint32_t) (SDCardInfo.RCA << ));
DeviceSizeMul = (SDCardInfo.SD_csd.DeviceSizeMul + ); if(SDCardInfo.CardType == SDIO_HIGH_CAPACITY_SD_CARD)
{
Mass_Block_Count[] = (SDCardInfo.SD_csd.DeviceSize + ) * ;
}
else
{
NumberOfBlocks = (( << (SDCardInfo.SD_csd.RdBlockLen)) / );
Mass_Block_Count[] = ((SDCardInfo.SD_csd.DeviceSize + ) * ( << DeviceSizeMul) << (NumberOfBlocks/));
}
Mass_Block_Size[] = ; Status = SD_SelectDeselect((uint32_t) (SDCardInfo.RCA << ));
Status = SD_EnableWideBusOperation(SDIO_BusWide_4b);
if ( Status != SD_OK )
{
return MAL_FAIL;
} Mass_Memory_Size[] = Mass_Block_Count[] * Mass_Block_Size[];
return MAL_OK;
}
default:break;
}
return MAL_FAIL;
}