From 66348b6986cf38d9d0fe707eca7bbfd7be3def18 Mon Sep 17 00:00:00 2001 From: Rob Austein Date: Wed, 2 Nov 2016 19:02:58 -0400 Subject: Wait for WIP to clear before returning from erase operations too. Wrong-block-type race condition errors went away after adding the WIP check after flash write operations, then came back once (isolated incident) while running a series of tests which had written enough flash blocks that ks_flash may have finally had to erase something rather than just zeroing. Code inspection confirmed that the erase code was not waiting for WIP to clear before exiting. Difficult to prove that this was the cause of an unreproducible failure, but seems like a likely candidate given previous behavior and change should be harmless, so adding it. Timeout for this flag check is 2000 ms, which is what other erase-related WIP flag checks were already using. --- spiflash_n25q128.c | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/spiflash_n25q128.c b/spiflash_n25q128.c index 0c2abd7..c23f244 100644 --- a/spiflash_n25q128.c +++ b/spiflash_n25q128.c @@ -216,6 +216,18 @@ int n25q128_get_wip_flag(struct spiflash_ctx *ctx) return (spi_rx[1] & 1); } +/* Wait until the flash memory is done writing (wip = Write In Progress) */ +inline int _wait_while_wip(struct spiflash_ctx *ctx, uint32_t timeout) +{ + int i; + while (timeout--) { + i = n25q128_get_wip_flag(ctx); + if (i < 0) return 0; + if (! i) break; + HAL_Delay(10); + } + return 1; +} static int n25q128_erase_something(struct spiflash_ctx *ctx, uint8_t command, uint32_t byte_offset) { @@ -259,6 +271,10 @@ static int n25q128_erase_something(struct spiflash_ctx *ctx, uint8_t command, ui // check if (ok != HAL_OK) return 0; + // wait for erase to finish + + if (! _wait_while_wip(ctx, 2000)) return 0; + // done return 1; } @@ -303,19 +319,6 @@ int _n25q128_get_wel_flag(struct spiflash_ctx *ctx) return ((spi_rx[1] >> 1) & 1); } -/* Wait until the flash memory is done writing (wip = Write In Progress) */ -inline int _wait_while_wip(struct spiflash_ctx *ctx, uint32_t timeout) -{ - int i; - while (timeout--) { - i = n25q128_get_wip_flag(ctx); - if (i < 0) return 0; - if (! i) break; - HAL_Delay(10); - } - return 1; -} - /* This function performs erasure if needed, and then writing of a number of pages to the flash memory */ int n25q128_write_data(struct spiflash_ctx *ctx, uint32_t offset, const uint8_t *buf, const uint32_t len, const int auto_erase) { -- cgit v1.2.3