title: Can´t erase data from flash memory (STM32)
body: <p>I´m trying to save some data to flash memory on my <strong>STM32F407</strong> board. Before I can save them I need to erase memory sector. I choosed 16 Kbytes <strong>Sector1</strong> starting with address 0x08004000 and choosed <strong>Voltage range 2.1-2.7 V</strong>. I'm using HAL library.</p>
  
  <p>Program stops responding after <strong>FLASH->CR |= FLASH_CR_STRT;</strong> line inside HAL_FLASHEx_Erase() -> FLASH_Erase_Sector() function.</p>
  
  <p>I'm pretty sure it's my fault but I can't find out what is wrong.</p>
  
  <pre><code>HAL_FLASH_Unlock();
  __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
                             FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
  FLASH_EraseInitTypeDef EraseInitStruct;
  EraseInitStruct.Sector = FLASH_SECTOR_1;
  EraseInitStruct.TypeErase = TYPEERASE_SECTORS;
  EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_2;
  EraseInitStruct.NbSectors = 1;
  uint32_t SectorError = 0;
  if (HAL_FLASHEx_Erase(&amp;EraseInitStruct, &amp;SectorError) != HAL_OK) {     
      HAL_FLASH_Lock();
      return;
  }
  
  uint16_t data = 300;
  //----------------------------write data
  if (HAL_FLASH_Program(TYPEPROGRAM_WORD, start_address, data) != HAL_OK) {
      HAL_FLASH_Lock();
      return;
  }
  HAL_FLASH_Lock();
  </code></pre>
  
  <p>Did I choosed wrong voltage range or number of sectors?</p>
  
  <p>Thanks for your answers.</p>