/* Waits for a Flash operation to complete or a TIMEOUT to occur.  */
FLASH_Status FLASH_WaitForLastOperation(u32 time_out)
/* Waits for a Flash operation to complete or a TIMEOUT to occur.  */
FLASH_Status FLASH_WaitForLastOperation(u32 time_out)
{
    u32          i;
    FLASH_Status ret;
    do {
        ret = FLASH_GetStatus();
        time_out--;
        for (i = 0xFF; i != 0; i--)
            ;
    } while ((ret == FLASH_BUSY) && (time_out != 0x00));
    FLASH->CR = 0;
    FLASH->SR = FLASH_SR_EOP | FLASH_SR_WRPRTERR | FLASH_SR_PGERR;
    return (FLASH_Status)((time_out == 0x00) ? FLASH_TIMEOUT : ret);
}