单片机
返回首页

野火STM32 Flash&sd卡模拟U盘

2020-10-16 来源:eefocus

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


 1 //mass_mal.c

 2 /* Includes ------------------------------------------------------------------*/

 3 #include '..Usersdcardbsp_sdio_sdcard.h'

 4 #include '..Userspi_flashfatfs_flash_spi.h'

 5 #include 'mass_mal.h'

 6 #include

 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 succseen');

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=%dn',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=%dn',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 }


进入单片机查看更多内容>>
相关视频
  • 【TI MSPM0 应用实战】智能小车+工业角度编码器+血氧仪+烟雾探测器!硬核参考设计详解!

  • 2022 Digi-Key KOL 系列: 你见过1GHz主频的单片机吗?Teensy 4.1开发板介绍

  • TI 新一代 C2000™ 微控制器:全方位助力伺服及马达驱动应用

  • MSP430电容触摸技术 - 防水Demo演示

  • 直播回放: Microchip Timberwolf™ 音频处理器在线研讨会

  • 基于灵动MM32W0系列MCU的指夹血氧仪控制及OTA升级应用方案分享

精选电路图
  • 1瓦线性调频增强器

  • 12V 转 28V DC-DC 变换器(基于 LM2585)

  • 红外遥控音量控制

  • LM317过压保护

  • 12V转110V/220V 500W逆变器

  • DS1669数字电位器

    相关电子头条文章