aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Austein <sra@hactrn.net>2016-11-02 19:02:58 -0400
committerRob Austein <sra@hactrn.net>2016-11-02 19:02:58 -0400
commit66348b6986cf38d9d0fe707eca7bbfd7be3def18 (patch)
treef527150782aa8d8bb6b07d9fa0343664f59d5354
parent42d98519724319a3fad92fc40cca5aefa30276d7 (diff)
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.
-rw-r--r--spiflash_n25q128.c29
1 files 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)
{