cancel
Showing results for 
Search instead for 
Did you mean: 

STM32G0 and FLASH_TYPEPROGRAM_FAST fail

JTred.2
Associate II

Hello,

I use a Nucleo-STM32G0B1 (512KB Flash).

I would like to flash same data in fast mode, but the HAL_FLASH_Program() function ending with error. The HAL_FLASH_GetError() = 0x2A0 (PGSERR and PGAERR).

Before writing I erase some flash pages.

If I clean only ONE page the HAL_FLASH_Program() works but if I clear more, it doesn't works.

This is my test code :

void write_test(void)
{
    FLASH_EraseInitTypeDef EraseInitStruct;
    uint32_t               PageError = 0;
    uint8_t                data[256];
    uint32_t               addrFlash = 0x8040100;
    uintptr_t              addrData  = (uintptr_t) data;
 
    // Init data
    for (int i = 0; i < 256; i++)
        data[i] = i;
 
    //====== ERASE
    if (HAL_FLASH_Unlock() != HAL_OK)
    {
        ERR("Flash unlock fail");
        return;
    }
    __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR);
    EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
    EraseInitStruct.Banks     = FLASH_BANK_2;
    EraseInitStruct.Page      = 0;
    EraseInitStruct.NbPages   = 3; // If I set it to 1, HAL_FLASH_Program() works.
    if (HAL_FLASHEx_Erase(&EraseInitStruct, &PageError) != HAL_OK)
    {
        ERR("Fail to erase data, error=%X", PageError);
        HAL_FLASH_Lock();
        return;
    }
    HAL_FLASH_Lock();
 
    //===== FLASH
    if (HAL_FLASH_Unlock() != HAL_OK)
    {
        ERR("Flash unlock fail");
        return;
    }
    __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR);
    if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_FAST, addrFlash, addrData) != HAL_OK)
    {
        ERR("FLash write fail 0x%X, error=%X", addrFlash, HAL_FLASH_GetError());
        HAL_FLASH_Lock();
        return;
    }
    HAL_FLASH_Lock();
}

6 REPLIES 6
JTred.2
Associate II

Same as this one (without official answer).

The workaround was to write in FAST mode only on the last erased page.

It also works if you perform Bank Erase.

KDJEM.1
ST Employee

Hello @Community member​ and welcome to the Community 🙂,

Can you please share the other discussion in same context and without official answer because you pointed to the same post that you shared.

I am investigating in this issue to reproduce the case on my side.

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.

JTred.2
Associate II

Hello @KDJEM.1​

I corrected the link.

You can use my code to reproduce the problem, or just erase a page and flash in FAST mode in another page.

KDJEM.1
ST Employee

Hello @Community member​,

Unfortunately, I don't have currently Nucleo-STM32G0B1.

For that, I tested your code with nucleo-G071RB, and I don't find any problem.

If you use the FLASH_TYPEPROGRAM_DOUBLEWORD, do you have the same issue?

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.

Antoine Odonne
ST Employee

Hello,

I am afraid fast mode programming only works after a page erase if you intend to write the last erased page. I provide a bit more informations from our design team and will make sure it ends up in our Reference Manual:

Because locations are not checked before being programmed, it is important to prevent SW from programming the same row multiple times.

This may be achieved by SW by either performing a mass-erase on the relevant bank(s) or by just erasing the page which is about to be programmed.

If a mass-erase was performed, then all flash rows may be programmed, with the only constraint that row address must always be incremented across different row programming operations.

If a page erased was performed, then only flash rows belonging to that page may be fast-programmed (always incrementing row addresses).

From our RM programming sequence we start with this line below, I will request to modify it in order to explicit the ocnstraint above.

"1.  Perform a mass or page erase. If not, PGSERR is set."

Thank you all and best regards,

Antoine

JTred.2
Associate II

Hello,

Thank you for the answer. I had changed my code to delete the pages as I used it.

And yes writing in FLASH_TYPEPROGRAM_DOUBLEWORD always works.

Regards,

Jerome