cancel
Showing results for 
Search instead for 
Did you mean: 

I am facing USB MSD Device format issue using STM32F411E Discovery kit

PANKAJ KHARCHE
Associate II

I am developing the application of USB Mass storage device using internal flash. I have used the ST CubeMx tool to generate the USB MSD code. I am using the last sector-7 (128KB) of MCU to create the Drive & i have modified the STORAGE_Write_FS() & STORAGE_Read_FS() functions to read and write from internal flash.

When i plugged In USB Device to PC, it created the flash drive in PC & the PC ask for formatting the drive first , but it is not able to format.

I have attached my code here, please check the code & let me know where i am wrong... 0693W000001ch8wQAA.jpg

1 ACCEPTED SOLUTION

Accepted Solutions

Problem is you can't just write the 512-bytes without destroying the 64 or 128 KB surrounding it. You'd need to preserve the data on either side of the sector you write. ie hold in RAM, insert new data, erase/write larger flash block with new content

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

6 REPLIES 6
TDK
Guru

> but it is not able to format.

Explain this better. Why not? What sort of error messages are you getting? What part of your code encounters these errors? Don't make me guess.

If you feel a post has answered your question, please click "Accept as Solution".

Probably because it expects the read/write to work properly at a 512 byte sector level, and you've got a 128 KB erase block, which is also very slow.

Test your READ/WRITE sector functionality.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Hi,

Thanks for the quick reply, as you said i have tested read ,write and erase functions it is working fine and also i have verified the data at flash memory locations it is also correct, i have used random data for testing. Previously i have used byte (8bit) read and write logic. Now i have changed it to WORD (32Bit) read & write logic and i am using sector 4 of flash to create drive, it is of 64KB. Below are the testing results...

0693W000001pLEjQAM.png0693W000001pLF8QAM.png

0693W000001pLFDQA2.png 

Below are my flash read & write functions.....

//*******************************************************************

int8_t STORAGE_Read_FS (uint8_t lun,

            uint8_t *buf,

            uint32_t blk_addr,

            uint16_t blk_len)

{

 uint32_t Flash_End_Addr = 0;

switch(lun)

{

case 0:

  blk_addr = blk_addr + FLASH_USER_START_ADDR;

  Flash_End_Addr = blk_addr + (blk_len*STORAGE_BLK_SIZ);

  while (blk_addr < Flash_End_Addr)

  {

    data32 = *(__IO uint32_t*)blk_addr;

    buf[4] = (data32 >> 24) & 0xFF;

    buf[3] = (data32 >> 16) & 0xFF;

    buf[2] = (data32 >> 8) & 0xFF;

    buf[1] = data32 & 0xFF;

   buf = buf + 4;

   blk_addr = blk_addr + 4;

  }

break;

case 1:

break;

default:

return (USBD_FAIL);

}

return (USBD_OK);

}

//***********************************************************

int8_t STORAGE_Write_FS (uint8_t lun,

             uint8_t *buf,

             uint32_t blk_addr,

             uint16_t blk_len)

{

 /* USER CODE BEGIN 7 */

 HAL_StatusTypeDef status = HAL_ERROR;

 uint32_t Flash_End_Addr = 0;

 uint32_t Data_32Bit =0;

switch(lun)

{

case 0:

   /* Unlock the Flash to enable the flash control register access *************/

   HAL_FLASH_Unlock();

status = HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError);

   /* Note: If an erase operation in Flash memory also concerns data in the data or instruction cache,

   you have to make sure that these data are rewritten before they are accessed during code

   execution. If this cannot be done safely, it is recommended to flush the caches by setting the

   DCRST and ICRST bits in the FLASH_CR register. */

   __HAL_FLASH_DATA_CACHE_DISABLE();

   __HAL_FLASH_INSTRUCTION_CACHE_DISABLE();

   __HAL_FLASH_DATA_CACHE_RESET();

   __HAL_FLASH_INSTRUCTION_CACHE_RESET();

   __HAL_FLASH_INSTRUCTION_CACHE_ENABLE();

   __HAL_FLASH_DATA_CACHE_ENABLE();

   /* Program the user Flash area word by word

    (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/

   blk_addr = blk_addr + FLASH_USER_START_ADDR;

   Flash_End_Addr = blk_addr + (blk_len*STORAGE_BLK_SIZ);

   while (blk_addr < Flash_End_Addr)

   {

    Data_32Bit = buf[0] | buf[1] << 8 | buf[2] << 16 | buf[3] << 24;

    if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, blk_addr,(uint32_t)Data_32Bit) == HAL_OK)

    {

     buf = buf + 4;

     blk_addr = blk_addr + 4;

    }

    else

    {

     /* Error occurred while writing data in Flash memory.

      User can add here some code to deal with this error */

     /*

      FLASH_ErrorTypeDef errorcode = HAL_FLASH_GetError();

     */

     Error_Handler();

    }

   }

   /* Lock the Flash to disable the flash control register access (recommended

   to protect the FLASH memory against possible unwanted operation) *********/

   HAL_FLASH_Lock();

break;

case 1:

break;

default:

return (USBD_FAIL);

}

return (status);

 /* USER CODE END 7 */

}

Below is the format error screenshot...

 0693W000001pLFIQA2.jpg

I have also tested this code using RAM memory it is working fine. But when i am using flash memory facing format issue...i am bit confused what to do now.

I have attached my code here, pls guide me where i am wrong....

Regards,

Pankaj

Problem is you can't just write the 512-bytes without destroying the 64 or 128 KB surrounding it. You'd need to preserve the data on either side of the sector you write. ie hold in RAM, insert new data, erase/write larger flash block with new content

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Thank you for your support. I did the changes you suggested now its working.

GBiav.1
Associate

Hello, mr. Pankaj Kharche, can you gently post the source code corrected and working

Thanks