Issue if tick rollover occurs while waiting for flash
I note in the post (from July 31, 2020 at 8:33 AM) entitled "FLASH_WaitForLastOperation CubeMx error" (here: https://community.st.com/s/question/0D53W00000EDrjiSAD/flashwaitforlastoperation-returns-error-in-stm32g070), that there is a mention of an error in the `FLASH_WaitForLastOperation()` function.
I was looking through my current code, trying to fix an error and found that it is still there, at least in the `Drivers\STM32G0xx_HAL_Driver\Src\stm32g0xx_hal_flash.c` file, within in my current project.
In function `FLASH_WaitForLastOperation()`, the timeout is checked in a way that will fail if the value returned by `HAL_GetTick()` rolls over during the operations. The relevant lines of code are these:
HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
{
...
uint32_t timeout = HAL_GetTick() + Timeout;
...
while ((FLASH->SR & error) != 0x00U)
{
if (HAL_GetTick() >= timeout)
{
return HAL_TIMEOUT;
}
}
...I believe this should instead be written more like:
HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
{
...
uint32_t wait_start_ms = HAL_GetTick();
...
while ((FLASH->SR & error) != 0x00U)
{
if ((HAL_GetTick() - wait_start_ms) >= Timeout)
{
return HAL_TIMEOUT;
}
}
...for it to not have a problem, if a rollover occurs just after line 4 (of the codes above), and the while loop exiting (when `(FLASH->SR & error)` becomes `0x00U`.
