AnsweredAssumed Answered

USB MSC on internal FLASH and file deletion

Question asked by Denis Matteazzi on Jul 2, 2017

Hi to all.

I'm using STM32L072 family MCU. I configured it to use last portion of internal flash (64kB) as USB MSC. It seems to work (more or less). Operative System can see it as removable drive. I can't format it but after I copied on flash an image of a 64KB FAT it starts to work. I can copy files on it and I can read them. The problem appears when I want to delete them. If I try to erase a file it is apparently deleted, but if I press F5 on explorer, file reappears with 0 bytes dimension, and I can't overwrite it. I checked what Windows wants to write when I try to erase a file. I thought it change first character of the filename, but it isn't so, it only set to 0 the dimension, and I can't understand the reason.

 

This is the code:

 

#define STORAGE_BLK_SIZ                  0x200

#define FLASH_PAGE_SIZE            ((uint32_t)128U)

 

const volatile uint8_t *fileRawMemory = (uint8_t *)0x08020000;

 

int8_t flashFatFsRead(uint8_t *buf, uint32_t blk_addr, uint16_t blk_len)
{
   memcpy(buf, fileRawMemory + blk_addr * STORAGE_BLK_SIZ, blk_len * STORAGE_BLK_SIZ);
   return 0;
}

 

int8_t flashFatFsWrite(uint8_t *buf, uint32_t blk_addr, uint16_t blk_len)
{
   FLASH_EraseInitTypeDef EraseInitStruct;
   uint32_t PAGEError = 0;
   uint32_t pageAddress;
   uint32_t currPageAddress;
   uint32_t data;
   uint32_t buffPointer = 0;
   uint8_t i = 0;
   uint8_t err;

 

   /* Unlock the Flash to enable the flash control register access *************/
   HAL_FLASH_Unlock();

   /* Erase the user Flash area
     (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/

      pageAddress = (uint32_t)(fileRawMemory) + blk_addr * STORAGE_BLK_SIZ;

   /* Fill EraseInit structure*/
   EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
   EraseInitStruct.PageAddress = pageAddress;
   EraseInitStruct.NbPages = (STORAGE_BLK_SIZ / FLASH_PAGE_SIZE) * blk_len;

   err = HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError);
   if ( err != HAL_OK )
   {
      /*
      Error occurred while page erase.
      User can add here some code to deal with this error.
      PAGEError will contain the faulty page and then to know the code error on this page,
      user can call function 'HAL_FLASH_GetError()'
      */
      HAL_FLASH_Lock();
      return 2;
   }

   /* Program the user Flash area word by word
   (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/


   currPageAddress = pageAddress;
   while (currPageAddress < pageAddress + blk_len * STORAGE_BLK_SIZ)
   {
      memcpy(&data, buf + buffPointer, 4);

      if ((err = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, currPageAddress, data)) == HAL_OK)
      {
         currPageAddress = currPageAddress + 4;
         buffPointer = buffPointer + 4;
      }
      else
      {
         HAL_FLASH_Lock();
         return 2;
      }
   }


   HAL_FLASH_Lock();

   return 0;
}

 

If it is necessary I can attach the FAT image or the USB initialization.

 

Thanks in advance.

 

Denis.

Outcomes