cancel
Showing results for 
Search instead for 
Did you mean: 

MSD Storage IF for STM32L073RZ

MikeAtETI
Associate III
Posted on November 29, 2017 at 14:41

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?

&sharpdefine EEPROM_START    0x8020000

&sharpdefine EEPROM_LENGTH 0x0010000

&sharpdefine STORAGE_LUN_NBR 1

&sharpdefine STORAGE_BLK_SIZ 0x200

&sharpdefine 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 */

}

#cubemx #msd
2 REPLIES 2
Posted on November 29, 2017 at 15:08

Do any of the functions throw error or status you might be ignoring?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on November 29, 2017 at 17:26

One bug I have noticed:

eraseInfo.PageAddress = EEPROM_START + blk_addr * (STORAGE_BLK_SIZ / FLASH_PAGE_SIZE);

should be:

eraseInfo.PageAddress = EEPROM_START + (blk_addr * STORAGE_BLK_SIZ);