2015-06-01 02:29 AM
Hello!I am using stm32f407 flash to make a msc.It can be identified by my pc.But I can not format it.Can somebody help me?Thanks a lot!Here are my code on usbd_storage.c.
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define STORAGE_LUN_NBR 1
#define STORAGE_BLK_NBR 0x00300
#define STORAGE_BLK_SIZ 0x200
#define USER_FLASH_START 0x8060000 /* Flash start address 56K */
#define USER_FLASH_END ( 0x8060000 + ( 0x60000 ) )
#define USER_FLASH_SIZE ( USER_FLASH_END - USER_FLASH_START )
static uint32_t user_flash_StartAddr,user_flash_EndAddr;
int8_t STORAGE_Init(uint8_t lun)
{
static uint32_t SectorError = 0;
static uint32_t FirstSector,NbOfSectors;
user_flash_EndAddr = USER_FLASH_END;
user_flash_StartAddr = USER_FLASH_START;
/* Unlock the Flash to enable the flash control register access *************/
HAL_FLASH_Unlock();
/* Get the 1st sector to erase */
FirstSector = GetSector(user_flash_StartAddr);
/* Get the number of sector to erase from 1st sector*/
NbOfSectors = GetSector(user_flash_EndAddr) - FirstSector + 1;
/* Fill EraseInit structure*/
EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
EraseInitStruct.Sector = FirstSector;
EraseInitStruct.NbSectors = NbOfSectors;
/* 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_FLASHEx_Erase(&EraseInitStruct, &SectorError);
return 0;
}
/**
* @brief Returns the medium capacity.
* @param lun: Logical unit number
* @param block_num: Number of total block number
* @param block_size: Block size
* @retval Status (0: Ok / -1: Error)
*/
int8_t STORAGE_GetCapacity(uint8_t lun, uint32_t *block_num, uint16_t *block_size)
{
*block_num = STORAGE_BLK_NBR;
*block_size = STORAGE_BLK_SIZ ;
return (0);
}
/**
* @brief Checks whether the medium is ready.
* @param lun: Logical unit number
* @retval Status (0: Ok / -1: Error)
*/
int8_t STORAGE_IsReady(uint8_t lun)
{
return 0;
}
/**
* @brief Checks whether the medium is write protected.
* @param lun: Logical unit number
* @retval Status (0: write enabled / -1: otherwise)
*/
int8_t STORAGE_IsWriteProtected(uint8_t lun)
{
return 0;
}
/**
* @brief Reads data from the medium.
* @param lun: Logical unit number
* @param blk_addr: Logical block address
* @param blk_len: Blocks number
* @retval Status (0: Ok / -1: Error)
*/
int8_t STORAGE_Read(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len)
{
static uint32_t i = 0,k =0;
uint8_t * firmware ;
firmware = (uint8_t*)USER_FLASH_START;
for (k=0;k<blk_len;k++)
for ( i = 0; i<512; i++)
{
buf[i] = *(firmware + blk_addr*512/8);
}
return 0;
}
/**
* @brief Writes data into the medium.
* @param lun: Logical unit number
* @param blk_addr: Logical block address
* @param blk_len: Blocks number
* @retval Status (0 : Ok / -1 : Error)
*/
int8_t STORAGE_Write(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len)
{
static uint32_t Address;
static uint16_t cnt_number1 = 0x00;
static uint16_t cnt_number2 = 0x00;
Address = user_flash_StartAddr;
for(cnt_number1 = 0;cnt_number1 < blk_len ;cnt_number1 ++)
for(cnt_number2 = 0;cnt_number2 < 8 ;cnt_number2 ++)
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, Address+blk_addr*512/8, (u64)(*buf)) == HAL_OK)
{
buf = buf +8;
}
return 0;
}
/**
* @brief Returns the Max Supported LUNs.
* @param None
* @retval Lun(s) number
*/
int8_t STORAGE_GetMaxLun(void)
{
return(STORAGE_LUN_NBR - 1);
}
#stm32f407-msc-format
2015-06-01 06:42 AM
Going to have a problem here the second time any sector is written.
The erase block size of the flash is likely to be a challenge here.2015-06-01 06:20 PM
Hello!Thank you!
I dodified function of storage_write.But it still doesn't work.Should I make file system for internal flash for making a mass storage class?Or there are other problems in my code?
static uint32_t FirstSector,FirstSector_temp;
int8_t STORAGE_Write(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len)
{
static uint16_t cnt_num = 0x00;
static uint32_t SectorError = 0;
/* Unlock the Flash to enable the flash control register access *************/
HAL_FLASH_Unlock();
/* Get the 1st sector to erase */
FirstSector = GetSector(user_flash_StartAddr+blk_addr*512);
/* Fill EraseInit structure*/
EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
EraseInitStruct.Sector = FirstSector;
if(FirstSector_temp != FirstSector)
{
FirstSector_temp = FirstSector;
/* 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_FLASHEx_Erase(&EraseInitStruct, &SectorError);
}
for(cnt_num = 0;cnt_num < 8 ;cnt_num ++)
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, user_flash_StartAddr+blk_addr*512+cnt_num*8, (u64)(*buf)) == HAL_OK)
{
buf = buf + 8;
}
return 0;
}
2015-06-01 06:56 PM
Not really sure you're grasping the scope of the issue here.
The Mass Storage device expects to be able to randomly read/write 512 byte sectors throughout the media. You can only write to flash that's been erased. The flash sectors (erase block) can be up to 128KB. You need to write, or over-write, 512 byte sectors without destroying the content in any of the other surrounding sectors.2015-06-01 07:42 PM
Hi!I know what you are talking about.Erasing one sector is operating by a block of 128K.I use ''FirstSector_temp'' to cache the sector that has been erased in case of been erased again.Must ''STORAGE_BLK_SIZ'' in my code be 512 byte?Or I can just put it up to 128k?
2015-06-03 08:41 PM
The problem has been soved.The flash can not be erased successfully if I do not clear pending flags.So ,just put a line ''__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |\
FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR| FLASH_FLAG_PGSERR);'' and everything goes well!Thank you all!