所需工具
- CUBEMX5.6
- STM32F103ZET6开发板(正点原子战舰)
- LINK及其他线束
1、CUBEMX初始化
-
设置SYSTEM CORE下的SYS和RCC选项。
-
设置USB时钟
-
设置USB
- 设置USB_DEVICE
MSC_MEDIA_PACKET选择为512!!! -
修改堆栈大小
2、修改usbd_stroge_if.c
- 添加头文件及宏定义
#include "W25QXX.h"
#define SPI_FLASH_BLK_NBR ((12*1024*1024)/(SPI_FLASH_BLK_SIZ))
#define SPI_FLASH_BLK_SIZ 512
SPI_FLASH_BLK_NBR这里只使用了SPI_FLASH的前12M空间。如有需要可以自行修改。
- 添加W25Q128驱动文件
- 修改函数STORAGE_GetCapacity_FS
int8_t STORAGE_GetCapacity_FS(uint8_t lun, uint32_t *block_num, uint16_t *block_size)
{/* USER CODE BEGIN 3 */*block_num = SPI_FLASH_BLK_NBR;*block_size = SPI_FLASH_BLK_SIZ;return (USBD_OK);/* USER CODE END 3 */
}
- 修改函数STORAGE_Read_FS为如下所示
int8_t STORAGE_Read_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len)
{/* USER CODE BEGIN 6 *///读U盘函数BSP_W25QXX_Read(buf, blk_addr * SPI_FLASH_BLK_SIZ, blk_len * SPI_FLASH_BLK_SIZ);return (USBD_OK);/* USER CODE END 6 */
}
- 修改函数STORAGE_Write_FS为如下所示
int8_t STORAGE_Write_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len)
{/* USER CODE BEGIN 7 */BSP_W25QXX_Write(buf, blk_addr * SPI_FLASH_BLK_SIZ, blk_len * SPI_FLASH_BLK_SIZ);return (USBD_OK);/* USER CODE END 7 */
}