I'm trying to erase sectors 11,12 in the STM32F779II, I have been running into that problem for one week without a solution.
So Right now I'm trying to check if there is ANY write protection on sectors 11,12. But There is NO write Protection error.
However when I erase them, I get WriteProtectionError Flag!. That's very strange, I don't know what I'm really doing wrong here.
#define FLASH_STATUS_ALL_FLAGS \
(FLASH_SR_RDERR | FLASH_SR_PGPERR | FLASH_SR_PGAERR | \
FLASH_SR_WRPERR | FLASH_SR_EOP | FLASH_SR_BSY)
secbool flash_unlock_write(void) {
HAL_FLASH_Unlock();
FLASH->SR |= FLASH_STATUS_ALL_FLAGS; // clear all status flags
return sectrue;
}
and for flash erasing and write protection detection:
#define FLASH_WRP_SECTORS (OB_WRP_DB_SECTOR_11 | OB_WRP_DB_SECTOR_12) /* sectors 6 and 7 */
secbool flash_erase_sectors(const uint8_t *sectors, int len,
void (*progress)(int pos, int len)) {
ensure(flash_unlock_write(), NULL);
// HAL_FLASH_OB_Unlock();
FLASH_OBProgramInitTypeDef OBInit;
__IO uint32_t SectorsWRPStatus = 0xFFF;
HAL_FLASHEx_OBGetConfig(&OBInit);
SectorsWRPStatus = OBInit.WRPSector & FLASH_WRP_SECTORS;
if (SectorsWRPStatus == 0) {
/* If FLASH_WRP_SECTORS are write protected, disable the write protection
*/
/* Allow Access to option bytes sector */
HAL_FLASH_OB_Unlock();
/* Allow Access to Flash control registers and user Flash */
HAL_FLASH_Unlock();
/* Disable FLASH_WRP_SECTORS write protection */
OBInit.OptionType = OPTIONBYTE_WRP;
OBInit.WRPState = OB_WRPSTATE_DISABLE;
OBInit.WRPSector = FLASH_WRP_SECTORS;
HAL_FLASHEx_OBProgram(&OBInit);
/* Start the Option Bytes programming process */
if (HAL_FLASH_OB_Launch() != HAL_OK) {
/* User can add here some code to deal with this error */
while (1) {
}
}
/* Prevent Access to option bytes sector */
HAL_FLASH_OB_Lock();
/* Disable the Flash option control register access (recommended to
protect the option Bytes against possible unwanted operations) */
HAL_FLASH_Lock();
/* Get FLASH_WRP_SECTORS write protection status */
HAL_FLASHEx_OBGetConfig(&OBInit);
SectorsWRPStatus = OBInit.WRPSector & FLASH_WRP_SECTORS;
/* Check if FLASH_WRP_SECTORS write protection is disabled */
if (SectorsWRPStatus == FLASH_WRP_SECTORS) {
printf("flash");
} else {
printf("flaa");
/* Set the LCD Text Color */
}
}
FLASH_EraseInitTypeDef EraseInitStruct;
EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
EraseInitStruct.NbSectors = 1;
if (progress) {
progress(0, len);
}
for (int i = 0; i < len; i++) {
EraseInitStruct.Sector = sectors[i];
uint32_t SectorError;
if (HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK) {
ensure(flash_lock_write(), NULL);
return secfalse;
}
// check whether the sector was really deleted (contains only 0xFF)
const uint32_t addr_start = FLASH_SECTOR_TABLE[sectors[i]],
addr_end = FLASH_SECTOR_TABLE[sectors[i] + 1];
for (uint32_t addr = addr_start; addr < addr_end; addr += 4) {
if (*((const uint32_t *)addr) != 0xFFFFFFFF) {
ensure(flash_lock_write(), NULL);
return secfalse;
}
}
if (progress) {
progress(i + 1, len);
}
}
ensure(flash_lock_write(), NULL);
return sectrue;
}
User contributions licensed under CC BY-SA 3.0