cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_FLASHEx_Erase() fails using FreeRTOS

Adriano Melis
Associate III
Posted on December 15, 2017 at 16:26

Hi all,

I am developing a bootloader + update system which needs FLASH pages manipulation (unlock/erase/program) from a FreeRTOS task.

The same unlock/erase code works fine if called

before

the os scheduler is launched, but fails when called from a task. What I am missing?

thanks,

Adriano

MCU: Cortex L4

Firmware version: STM32Cube_FW_L4_V1.8.1

code:

int erase_partition(const uint8_t *addr)

{

    int ret = HAL_OK;

    uint32_t PAGEError = 0;

    FLASH_EraseInitTypeDef EraseInitStruct;

    HAL_FLASH_Lock();

    /* Unlock the Flash to enable the flash control register access *************/

    ret = HAL_FLASH_Unlock();

    if (ret != HAL_OK) {

        return ret;

    }

    /* Clear OPTVERR bit set on virgin samples */

    __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR);

    EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;

    EraseInitStruct.Banks = FLASH_BANK_1;

    EraseInitStruct.Page = get_page((uint32_t)addr);

    EraseInitStruct.NbPages = APP_NUMPAGES;

    ret = HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError);

    if (ret != HAL_OK) {

        ret = HAL_FLASH_GetError();

        return ret;

    }

    return HAL_OK;

}

4 REPLIES 4
Doug Kehn
Senior
Posted on December 17, 2017 at 14:04

What error are you receiving?

Alan Chambers
Associate II
Posted on December 17, 2017 at 22:54

What happens if you disable interrupts for the duration of this function? It may be that a task switch occurs and that this is bad. Just guessing based on the behaviour you describe. Does it fail every time?

Posted on December 18, 2017 at 14:43

The peripheral uses interrupts so...

You can try changing the task's priority to be the highest priority so that task swaps don't occur. The L4 will automatically reset after some flash operations so beware of that. I follow the method described in

http://www.st.com/content/ccc/resource/technical/document/application_note/group0/ab/6a/0f/b7/1a/84/40/c3/DM00230416/files/DM00230416.pdf/jcr:content/translations/en.DM00230416.pdf

for L4 and haven't had any problems. Also, this

http://www.st.com/content/ccc/resource/training/technical/product_training/91/e3/aa/26/e6/69/4f/de/STM32L4_Memory_Flash.pdf/files/STM32L4_Memory_Flash.pdf/jcr:content/translations/en.STM32L4_Memory_Flash.pdf

 has some useful information.
Yeho S
Associate
Posted on February 18, 2018 at 09:34

Hey Adriano,

I'm facing quite the same problem. 

I'm running the simple ST FlashErase example on my L4 custom board, and it works great,

but when calling FreeRTOS' osKernelStart() routine and trying to use the example from within a task, it fails and generates the FLASH_FLAG_PGAERR and FLASH_FLAG_PGSERR inside the HAL_FLASHEx_Erase() routine.

I also read about working with this flash driver under FreeRTOS and tried disabling the interrupts before the flash activity and re-enabling them afterwards, but the result was the same.

Have you ever found an answer for this issue?

Thanks in advance,

Yeho.