AnsweredAssumed Answered

MSD Storage IF for STM32L073RZ

Question asked by hudgell.michael on Nov 29, 2017
Latest reply on Nov 29, 2017 by hudgell.michael

I'm trying to create a MSD for the STM32L073RZT6, and want to use 64K of the flash memory as storage.

 

I've tried to get the erase/write correct, but Windows says the format fails...

Can anyone spot the obvious mistake?

 

 

#define EEPROM_START    0x8020000

#define EEPROM_LENGTH 0x0010000

 

#define STORAGE_LUN_NBR 1

#define STORAGE_BLK_SIZ 0x200

#define STORAGE_BLK_NBR (EEPROM_LENGTH / STORAGE_BLK_SIZ)

 

int8_t STORAGE_Read_FS (uint8_t lun,
uint8_t *buf,
uint32_t blk_addr,
uint16_t blk_len)
{
/* USER CODE BEGIN 6 */
uint32_t newAddr = (STORAGE_BLK_SIZ * blk_addr) +EEPROM_START;
memcpy(buf, (void *) newAddr, blk_len * STORAGE_BLK_SIZ);
return (USBD_OK);
/* 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 */
FLASH_EraseInitTypeDef eraseInfo;
uint32_t PageError;
HAL_FLASH_Unlock();
// 128 byte pages in our flash, so 4 pages per storage block - erase the blocks
eraseInfo.TypeErase = FLASH_TYPEERASE_PAGES;
eraseInfo.PageAddress = EEPROM_START + blk_addr * (STORAGE_BLK_SIZ / FLASH_PAGE_SIZE);
eraseInfo.NbPages = blk_len * (STORAGE_BLK_SIZ / FLASH_PAGE_SIZE);
HAL_FLASHEx_Erase(&eraseInfo, &PageError);

for (int a = 0; a < (blk_len * STORAGE_BLK_SIZ); a += 4)
{
HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, EEPROM_START + (blk_addr * STORAGE_BLK_SIZ) +a , *((uint32_t *)&buf[a]));
}

HAL_FLASH_Lock();
return (USBD_OK);
/* USER CODE END 7 */
}

Outcomes