2016-09-17 09:18 AM
Hi all,
When I used CubeMX to generate stm32L151c8t6 usb_mass_storage code and add some code to read or write the internal flash, I found that this u-disk cannot be formated and the blk_len allways equals to 0. The code I changed is as follow:#define STORAGE_LUN_NBR 1 #define STORAGE_BLK_NBR 159//32 #define STORAGE_BLK_SIZ 0x200 // 2k Bytes per page,space with only 2k per block can be formated/* USER CODE BEGIN PRIVATE_DEFINES */#define FLASH_DATA_END_ADDR 0x0801FFFF // Flash end address #define FLASH_DATA_START_ADDR (FLASH_DATA_END_ADDR - STORAGE_BLK_SIZ * STORAGE_BLK_NBR + 1)// Flash start address #define FLASH_DATA_PAGE_SIZE STORAGE_BLK_SIZ#define FLASH_DATA_SIZE (STORAGE_BLK_SIZ * STORAGE_BLK_NBR)// flash disk space#define FLASH_DATA_WAIT_TIMEOUT 100000 //timeout value#define FLASH_DATA_PAGES_PER_BLK 8int8_t STORAGE_Init_FS (uint8_t lun){ /* USER CODE BEGIN 2 */ HAL_FLASH_Unlock(); __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP|FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_SIZERR | FLASH_FLAG_OPTVERR); return (USBD_OK); /* USER CODE END 2 */ }int8_t STORAGE_Read_FS (uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len){ /* USER CODE BEGIN 6 */ uint16_t i; switch (lun) { case 0: for(i=0; i<blk_len*STORAGE_BLK_SIZ; i+=4) { *(uint32_t*)(&buf[i]) = ((volatile uint32_t*)(FLASH_DATA_START_ADDR + blk_addr*STORAGE_BLK_SIZ))[i]; } break; default: return USBD_FAIL; } return (USBD_OK); /* USER CODE END 6 */ }uint32_t PAGEError = 0;static FLASH_EraseInitTypeDef EraseInitStruct;int8_t STORAGE_Write_FS (uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len){ /* USER CODE BEGIN 7 */ uint32_t i; switch (lun) { case 0: // for (i=0; i<blk_len*STORAGE_BLK_SIZ; i+=STORAGE_BLK_SIZ)// {// if (FLASH_WaitForLastOperation(FLASH_DATA_WAIT_TIMEOUT) != FLASH_TIMEOUT)// {// FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);// }// FLASH_PageErase(FLASH_DATA_START_ADDR + blk_addr*STORAGE_BLK_SIZ + i);// } /* Fill EraseInit structure*/ EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES; EraseInitStruct.PageAddress = FLASH_DATA_START_ADDR + blk_addr*STORAGE_BLK_SIZ; EraseInitStruct.NbPages = blk_len * FLASH_DATA_PAGES_PER_BLK; HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError); for (i=0; i<blk_len*STORAGE_BLK_SIZ; i+=4) {// if(FLASH_WaitForLastOperation(FLASH_DATA_WAIT_TIMEOUT) != FLASH_TIMEOUT)// {// FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);// } HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, FLASH_DATA_START_ADDR + blk_addr*STORAGE_BLK_SIZ + i , *(uint32_t*)(&buf[i])); } break; default: return USBD_FAIL; } return (USBD_OK); /* USER CODE END 7 */ }I am not familiar with usb mass storage protocol. Could you give me some suggestion about how to debug this issue? The attached file is the modified generated project code.Thank you very much.2016-09-19 04:42 AM
1) 64KB FLASH device
stm32L151c8t6 mounts 64KB FLASH. #define FLASH_DATA_END_ADDR 0x0801FFFF // Flash end address <--- 0x0800FFFF 2) FLASH erase page size = 64 words #define FLASH_DATA_PAGE_SIZE STORAGE_BLK_SIZ <--- 0x100 #define FLASH_DATA_PAGES_PER_BLK 8 <--- 2 3) Number of storage blocks is too large for 64KB FLASH #define STORAGE_BLK_NBR 159//32 <--- too large 4) FLASH Unlock/lock HAL_FLASH routines should be enclosed in HAL_FLASH_Unlock()/HAL_FLASH_Lock() HAL_FLASH_Unlock(); HAL_FLASHEx_Erase(); for (...) HAL_FLASH_Program(); HAL_FLASH_Lock(); Tsuneo