野火STM32 Flash&sd卡模拟U盘

 在USB库文件mass_mal.c中添加对flash和sd读写的函数,USB库调用这些函数从而实现模拟U盘的功能

 1 //mass_mal.c
 2 /* Includes ------------------------------------------------------------------*/
 3 #include "..Usersdcardsp_sdio_sdcard.h"
 4 #include "..Userspi_flashfatfs_flash_spi.h"
 5 #include "mass_mal.h"
 6 #include <stdio.h>
 7 /* Private typedef -----------------------------------------------------------*/
 8 /* Private define ------------------------------------------------------------*/
 9 /* Private macro -------------------------------------------------------------*/
10 /* Private variables ---------------------------------------------------------*/
11 uint32_t Mass_Memory_Size[2];
12 uint32_t Mass_Block_Size[2];
13 uint32_t Mass_Block_Count[2];
14 __IO uint32_t Status = 0;
15 //#define  sFLASH_ID              0xEF3015     //W25X16
16 //#define  sFLASH_ID              0xEF4015     //W25Q16
17 #define  sFLASH_ID              0XEF4017    //W25Q64
18 extern SD_CardInfo SDCardInfo;      //ÓÃÓÚ´æ´¢¿¨µÄÐÅÏ¢
 1 /*******************************************************************************
 2 * Function Name  : MAL_Init
 3 * Description    : Initializes the Media on the STM32
 4 * Input          : None
 5 * Output         : None
 6 * Return         : None
 7 *******************************************************************************/
 8 uint16_t MAL_Init(uint8_t lun)
 9 {
10   uint16_t status = MAL_OK;
11 
12   switch (lun)
13   {
14     case 0:
15             FLASH_SPI_disk_initialize();
16             if(SPI_FLASH_ReadID()==sFLASH_ID)
17             {
18                 printf("flash init succsee
");
19                 Status = MAL_OK;
20             }
21             else
22                 Status = MAL_FAIL;
23       break;
24         case 1:
25       Status = SD_Init();
26       break;
27     default:
28       return MAL_FAIL;
29   }
30   return status;
31 }
 1 /*******************************************************************************
 2 * Function Name  : MAL_Write
 3 * Description    : Write sectors
 4 * Input          : None
 5 * Output         : None
 6 * Return         : None
 7 *******************************************************************************/
 8 uint16_t MAL_Write(uint8_t lun, uint32_t Memory_Offset, uint32_t *Writebuff, uint16_t Transfer_Length)
 9 {
10   switch (lun)
11   {
12     case 0:
13             Memory_Offset+=(512*4096);//扇区偏移,外部flash文件系统空间放在外部flash 6M空间14             //printf("write add =%d.length=%d
",Memory_Offset/4096,Transfer_Length/4096);
15             SPI_FLASH_SectorErase(Memory_Offset);
16             SPI_FLASH_BufferWrite((uint8_t *)Writebuff,Memory_Offset,Transfer_Length);
17       break;
18         case 1:
19             Status =    SD_WriteBlock((uint8_t*)Writebuff, Memory_Offset, Transfer_Length);
20             Status = SD_WaitWriteOperation();  
21             while(SD_GetStatus() != SD_TRANSFER_OK);
22       if ( Status != SD_OK )
23       {
24         return MAL_FAIL;
25       }      
26       break;
27     default:
28       return MAL_FAIL;
29   }
30   return MAL_OK;
31 }
 1 /*******************************************************************************
 2 * Function Name  : MAL_Read
 3 * Description    : Read sectors
 4 * Input          : None
 5 * Output         : None
 6 * Return         : Buffer pointer
 7 *******************************************************************************/
 8 uint16_t MAL_Read(uint8_t lun, uint32_t Memory_Offset, uint32_t *Readbuff, uint16_t Transfer_Length)
 9 {
10 
11   switch (lun)
12   {
13     case 0:
14             Memory_Offset+=(512*4096);//扇区偏移15             //printf("read add =%d.length=%d
",Memory_Offset/4096,Transfer_Length/4096);
16             SPI_FLASH_BufferRead((uint8_t *)Readbuff, Memory_Offset, Transfer_Length);
17       break;
18         
19         case 1:
20       SD_ReadBlock((uint8_t*)Readbuff, Memory_Offset, Transfer_Length);
21             Status = SD_WaitReadOperation();
22       while(SD_GetStatus() != SD_TRANSFER_OK)
23       {
24       }
25       if ( Status != SD_OK )
26       {
27         return MAL_FAIL;
28       }
29       break;
30     default:
31       return MAL_FAIL;
32   }
33   return MAL_OK;
34 }
 1 /*******************************************************************************
 2 * Function Name  : MAL_GetStatus
 3 * Description    : Get status
 4 * Input          : None
 5 * Output         : None
 6 * Return         : None
 7 *******************************************************************************/
 8 uint16_t MAL_GetStatus (uint8_t lun)
 9 {
10   uint32_t DeviceSizeMul = 0, NumberOfBlocks = 0;
11     switch (lun)
12   {
13     case 0:
14         {
15             FLASH_SPI_disk_initialize();
16             if(SPI_FLASH_ReadID()==sFLASH_ID)
17             {
18                 Mass_Block_Size[0]  =4096;
19                 Mass_Block_Count[0] =1536;
20                 Mass_Memory_Size[0] =Mass_Block_Size[0]*Mass_Block_Count[0];
21                 return MAL_OK;
22             }
23         }
24         case 1:
25             if (SD_Init() == SD_OK)
26             {
27                 SD_GetCardInfo(&SDCardInfo);
28                 SD_SelectDeselect((uint32_t) (SDCardInfo.RCA << 16));
29                 DeviceSizeMul = (SDCardInfo.SD_csd.DeviceSizeMul + 2);
30 
31                 if(SDCardInfo.CardType == SDIO_HIGH_CAPACITY_SD_CARD)
32                 {
33                     Mass_Block_Count[1] = (SDCardInfo.SD_csd.DeviceSize + 1) * 1024;
34                 }
35                 else
36                 {
37                     NumberOfBlocks  = ((1 << (SDCardInfo.SD_csd.RdBlockLen)) / 512);
38                     Mass_Block_Count[1] = ((SDCardInfo.SD_csd.DeviceSize + 1) * (1 << DeviceSizeMul) << (NumberOfBlocks/2));
39                 }
40                 Mass_Block_Size[1]  = 512;
41 
42                 Status = SD_SelectDeselect((uint32_t) (SDCardInfo.RCA << 16)); 
43                 Status = SD_EnableWideBusOperation(SDIO_BusWide_4b); 
44                 if ( Status != SD_OK )
45                 {
46                     return MAL_FAIL;
47                 }          
48 
49                 Mass_Memory_Size[1] = Mass_Block_Count[1] * Mass_Block_Size[1];
50                 return MAL_OK;
51             }
52         default:break;
53     }
54   return MAL_FAIL;
55 }
原文地址:https://www.cnblogs.com/prayer521/p/5823248.html