cancel
Showing results for 
Search instead for 
Did you mean: 

FLASH_WaitForLastOperation() returning HAL_ERROR

eson
Associate

Hi..  I am using STM32L476 Nucleo board. 

When i tried to erase a flash page using HAL_FLASHEx_Erase(), the FLASH_WaitForLastOperation() returns a HAL_ERROR

I've seen similar issues in the community, but unfortunately, similar solutions just don't work.

And the error code is 0xA0 , how can I fix this

StatusDef FlashWr(uint32_t flash_addr, uint8_t *pData, uint16_t length)
{
  uint8_t *pBuf = (unsigned char *)flash_write_buf;
  uint16_t cnt; //  valid length in current sector
  uint32_t flash_addr_base;
  uint32_t addr_offset;
  bool cmp_result;
  uint16_t i;
  uint64_t val;
  uint16_t addr;

  StatusDef sta = STA_OK;

  while (length > 0)
  {
    flash_addr_base = (uint32_t)(flash_addr & ~FLASH_SECTOR_MASK);
    addr_offset = (uint32_t)(flash_addr & FLASH_SECTOR_MASK);

    cnt = FLASH_SECTOR_SIZE - addr_offset;
    if (length < cnt)
    {
      cnt = length;
    }
    FlashRd(flash_addr_base, pBuf, FLASH_SECTOR_SIZE);
    cmp_result = FlashCmp(pBuf + addr_offset, pData, cnt);
    if (false == cmp_result)
    {
      if (STA_OK != FlashPageErase(flash_addr_base))
      {
        return STA_ERROR;
      }

      Utils_MemCpy(pBuf + addr_offset, pData, cnt); // update the buffer
                                                    //  program the whole sector
      HAL_FLASH_Unlock();
      for (i = 0; i < FLASH_BUF_SIZE_64B; i++)
      {
        val = flash_write_buf[i];
        addr = flash_addr_base + i * 8;
        sta |= FlashPrg64bit(addr, val);
      }
      HAL_FLASH_Lock();
    }
    // next sector
    flash_addr += cnt;
    pData += cnt;
    length -= cnt;
  }

  return sta;
}
StatusDef FlashPageErase(uint32_t addr)
{
  StatusDef sta = STA_OK;
  uint32_t page_err;

  FLASH_EraseInitTypeDef EraseInitStruct = {0};
  uint32_t page_idx = (addr - FLASH_BASE) >> 11;
  if (FLASH_PAGE_NUM == 256)
  {
    if (page_idx > 256)
    {
      return STA_ERROR;
    }
    if (page_idx > 127)
    {
      page_idx = page_idx + 128;
    }
  }

  uint32_t banks_idx = page_idx < 256 ? FLASH_BANK_1 : FLASH_BANK_2;

  EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
  EraseInitStruct.Banks = banks_idx;
  EraseInitStruct.Page = page_idx;
  EraseInitStruct.NbPages = 1;

  HAL_FLASH_Unlock();
  if (HAL_FLASHEx_Erase(&EraseInitStruct, (uint32_t *)&page_err) != 0x00)
  {
    sta = STA_ERROR;
  }
  HAL_FLASH_Lock();
  return sta;
}

 

 

 

 

1 REPLY 1
KDJEM.1
ST Employee

Hello @eson and welcome to the community;

 

Are you testing FLASH_EraseProgram example? I recommend you to look at this example and check your project.

This project explains how to configure and use the FLASH HAL API to erase and program the internal Flash memory.

Before programming the desired addresses, an erase operation is performed using the flash erase page feature. The erase procedure is done by filling the erase init structure giving the starting erase page and the number of pages to erase. At this stage, all these pages will be erased one by one separately.

This example has been tested with NUCLEO-L476RG.

 

I hope this help you.

Thank you.

Kaouthar

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.