cancel
Showing results for 
Search instead for 
Did you mean: 

FLASH_WaitForLastOperation CubeMx error

Posted on January 23, 2017 at 10:02

The latest Version of CubeMx (4.19.0) the 'FLASH_WaitForLastOperation' function causes errors if the HAL_GetTick() function is near its rollover point. The cause seems to be 'uint32_t timeout = HAL_GetTick() + Timeout', which causes an error of the comparison 'HAL_GetTick() >= timeout'. With a 1ms resolution the error happens after 49 days, which could cause a serious problem in real systems. It would be nice, if this could be corrected.

best regards

Thomas

#cubemx
6 REPLIES 6
Nesrine M_O
Lead II
Posted on January 23, 2017 at 10:32

Hi

Schlenger.Thomas

,

Could you precise inwhich HAL library you have found the issue. So, we can verify it.

-Nesrine-

Posted on January 23, 2017 at 11:30

Hi Nesrine,

it is in 'stm32l4xx_hal_flash.c' and starts in line 638. i.e. if HAL_GetTick() returns 0xffff8000 and Timeout is 50000 then timeout will be 17232, so the next compare with GetTick will initiate a timeout...

Posted on January 23, 2017 at 11:54

I changed the repository in this way, which seems to work.

Thanks a lot for the very fine work on the very helpfull cubemx package.

HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
{
 /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
 Even if the FLASH operation fails, the BUSY flag will be reset and an error�?�?�?�?
 flag will be set */
 
/*
 uint32_t timeout = HAL_GetTick() + Timeout;
 while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY)) 
 { 
 if(Timeout != HAL_MAX_DELAY)
 {
 if(HAL_GetTick() >= timeout)
 {
 return HAL_TIMEOUT;
 }
 } 
 }
*/
 // change ths 2017
 uint32_t timeout = HAL_GetTick();
 while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY)) 
 { 
 if(Timeout != HAL_MAX_DELAY)
 {
 if(HAL_GetTick()-timeout >= Timeout)
 {
 return HAL_TIMEOUT;
 }
 } 
 }�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

Posted on January 23, 2017 at 13:46

Hi

Schlenger.Thomas

,

Thank you very much for your focus on

STM32 related solutions.

I will check the issue related to

FLASH_WaitForLastOperation function

with our development team & come back to you.Sorry for the inconvenience it may bring.

-Nesrine-

zhivko zivkovic
Associate III
Posted on October 31, 2017 at 17:12

I have similar problem. I am using STM32F303CBT6TR and I get HAL_ERROR at

https://github.com/zhivko/EclipseStm32/blob/master/Src/eeprom_flash.cpp#L156

I just copied source code from CubeMX example located at:

.\STM32Cube\Repository\STM32Cube_FW_F3_V1.7.0\Projects\STM32303C_EVAL\Examples\FLASH\FLASH_WriteProtection\Src\main.c

It seems I an getting error only in case of debugging code.

If I run it without debugger it seems it runs OK. What is the reason that I am getting HAL_ERROR

 ? Is it because of 

FLASH_WaitForLastOperation contains HAL_GetTick  that doesnt work OK while debugging ?

Posted on October 31, 2017 at 16:55

Could you please post whole method body?

In my cubemx it is like this:

/**

* @brief Wait for a FLASH operation to complete.

* @param Timeout maximum flash operation timeout

* @retval HAL Status

*/

HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)

{

/* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.

Even if the FLASH operation fails, the BUSY flag will be reset and an error

flag will be set */

uint32_t tickstart = HAL_GetTick();

while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY))

{

if (Timeout != HAL_MAX_DELAY)

{

if((Timeout == 0U) || ((HAL_GetTick()-tickstart) > Timeout))

{

return HAL_TIMEOUT;

}

}

}

/* Check FLASH End of Operation flag */

if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))

{

/* Clear FLASH End of Operation pending bit */

__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);

}

if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) ||

__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR))

{

/*Save the error code*/

FLASH_SetErrorCode();

return HAL_ERROR;

}

/* There is no error flag set */

return HAL_OK;

}