摘要:本文主要向大家介绍了Flash基础入门之野火STM32 Flash&sd卡模拟U盘,通过具体的内容向大家展现,希望对大家学习Flash基础入门有所帮助。
本文主要向大家介绍了Flash基础入门之野火STM32 Flash&sd卡模拟U盘,通过具体的内容向大家展现,希望对大家学习Flash基础入门有所帮助。
在USB库文件mass_mal.c中添加对flash和sd读写的函数,USB库调用这些函数从而实现模拟U盘的功能
1 //mass_mal.c
2 /* Includes ------------------------------------------------------------------*/
3 #include "..\User\sdcard\bsp_sdio_sdcard.h"
4 #include "..\User\spi_flash\fatfs_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 succsee\n");
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\n",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\n",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 }
以上就介绍了Flash的相关知识,希望对Flash有兴趣的朋友有所帮助。了解更多内容,请关注职坐标常用软件Flash频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号