AnsweredAssumed Answered

stm32f407 msc can not format

Question asked by yuanlong.xie on Jun 1, 2015
Latest reply on Jun 4, 2015 by yuanlong.xie
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);
}

Outcomes