STM32cube直接生成内部FLASH做U盘的程序注意事项

  1. 使用STM32cube配置好相关引脚,开启USB的Mass Storage Class功能
  2. USB_DEVICE中的MSC_MEDIA_PACKET可以设置大一点,增大缓冲区可以提高速度,对于外部spi-flash来说,要大于4096bytes
  3. 增加堆栈大小,否则USB无法正常工作
  4. usbd_storage_if.c添加代码:
#define     FLASH_START_ADDR        0x08010000    // 
#define     FLASH_PAGE_NBR              90       // 30K


int8_t STORAGE_GetCapacity_FS(uint8_t lun, uint32_t *block_num, uint16_t *block_size)
{
  /* USER CODE BEGIN 3 */
 if(lun == 0)
 {
  *block_num  = FLASH_PAGE_NBR;
  *block_size = FLASH_PAGE_SIZE;
  }
  return (USBD_OK);
  /* USER CODE END 3 */
}

int8_t STORAGE_Read_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len)
{
  /* USER CODE BEGIN 6 */
 if(lun == 0)
 {
    memcpy(buf,(u8 *)(FLASH_START_ADDR + blk_addr*FLASH_PAGE_SIZE),blk_len*FLASH_PAGE_SIZE);
    return USBD_OK;
  }
  return USBD_FAIL;
  /* USER CODE END 6 */
}

int8_t STORAGE_Write_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len)
{
  /* USER CODE BEGIN 7 */
  if(lun == 0)
  {
    uint16_t i;
	HAL_FLASH_Unlock();

	FLASH_EraseInitTypeDef f;
	f.TypeErase = FLASH_TYPEERASE_PAGES;
	f.PageAddress = FLASH_START_ADDR + blk_addr*FLASH_PAGE_SIZE ;
	f.NbPages = blk_len;
	uint32_t PageError = 0;
	HAL_FLASHEx_Erase(&f, &PageError);

  for(i=0;i

 

对于USB D+有上拉电阻控制的方案,要USB初始化中加入控制

 

 

你可能感兴趣的:(STM32)