From e9011a1ae42333fdcbebf9b85ced6ea942f3547c Mon Sep 17 00:00:00 2001 From: Paul Selkirk Date: Sun, 19 Feb 2017 19:23:18 -0500 Subject: Remove unnecessary delays in flash code. --- projects/board-test/keystore-perf.c | 2 +- projects/board-test/spiflash-perf.c | 19 +-------------- spiflash_n25q128.c | 48 +++++++++++++------------------------ spiflash_n25q128.h | 1 - stm-fpgacfg.c | 9 ------- stm-keystore.c | 16 +++++-------- stm-keystore.h | 1 + 7 files changed, 25 insertions(+), 71 deletions(-) diff --git a/projects/board-test/keystore-perf.c b/projects/board-test/keystore-perf.c index 7af4893..75b4e3f 100644 --- a/projects/board-test/keystore-perf.c +++ b/projects/board-test/keystore-perf.c @@ -187,8 +187,8 @@ int main(void) uart_send_string("Starting...\r\n"); time_check("read data ", test_read_data(), KEYSTORE_NUM_SUBSECTORS); - time_check("erase sector ", test_erase_sector(), KEYSTORE_NUM_SECTORS); time_check("erase subsector ", test_erase_subsector(), KEYSTORE_NUM_SUBSECTORS); + time_check("erase sector ", test_erase_sector(), KEYSTORE_NUM_SECTORS); time_check("verify erase ", test_verify_erase(), KEYSTORE_NUM_SUBSECTORS); time_check("write data ", test_write_data(), KEYSTORE_NUM_SUBSECTORS); time_check("verify write ", test_verify_write(), KEYSTORE_NUM_SUBSECTORS); diff --git a/projects/board-test/spiflash-perf.c b/projects/board-test/spiflash-perf.c index 52d3c0f..1abcd7b 100644 --- a/projects/board-test/spiflash-perf.c +++ b/projects/board-test/spiflash-perf.c @@ -137,21 +137,6 @@ static void test_verify_erase(void) _read_verify(vrfy_buf); } -/* - * Borrowed from spiflash_n25q128.c, since n25q128_write_page doesn't call it. - */ -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; -} - /* * 3a. Write the entire flash with a pattern. */ @@ -166,8 +151,6 @@ static void test_write_page(void) for (i = 0; i < N25Q128_NUM_PAGES; ++i) { err = n25q128_write_page(ctx, i, write_buf); - if (err == 1) - err = _wait_while_wip(ctx, 1000); if (err != 1) { uart_send_string("ERROR: n25q128_write_page returned "); uart_send_integer(err, 0); @@ -234,8 +217,8 @@ int main(void) uart_send_string("Starting...\r\n"); time_check("read page ", test_read_page(), N25Q128_NUM_PAGES); - time_check("erase sector ", test_erase_sector(), N25Q128_NUM_SECTORS); time_check("erase subsector ", test_erase_subsector(), N25Q128_NUM_SUBSECTORS); + time_check("erase sector ", test_erase_sector(), N25Q128_NUM_SECTORS); time_check("erase bulk ", test_erase_bulk(), 1); time_check("verify erase ", test_verify_erase(), N25Q128_NUM_PAGES); time_check("write page ", test_write_page(), N25Q128_NUM_PAGES); diff --git a/spiflash_n25q128.c b/spiflash_n25q128.c index 371bef2..35a2fcf 100644 --- a/spiflash_n25q128.c +++ b/spiflash_n25q128.c @@ -49,7 +49,8 @@ #endif -int _n25q128_get_wel_flag(struct spiflash_ctx *ctx); +static int _n25q128_get_wel_flag(struct spiflash_ctx *ctx); +static int _wait_while_wip(struct spiflash_ctx *ctx, uint32_t timeout); int n25q128_check_id(struct spiflash_ctx *ctx) @@ -67,7 +68,6 @@ int n25q128_check_id(struct spiflash_ctx *ctx) // select, send command & read response, deselect _n25q128_select(ctx); ok = HAL_SPI_TransmitReceive(ctx->hspi, spi_tx, spi_rx, 4, N25Q128_SPI_TIMEOUT); - HAL_Delay(1); _n25q128_deselect(ctx); // check @@ -108,7 +108,6 @@ int n25q128_read_page(struct spiflash_ctx *ctx, uint32_t page_offset, uint8_t *p // activate, send command _n25q128_select(ctx); ok = HAL_SPI_Transmit(ctx->hspi, spi_tx, 4, N25Q128_SPI_TIMEOUT); - HAL_Delay(1); // check if (ok != HAL_OK) { @@ -118,7 +117,6 @@ int n25q128_read_page(struct spiflash_ctx *ctx, uint32_t page_offset, uint8_t *p // read response, deselect ok = HAL_SPI_Receive(ctx->hspi, page_buffer, N25Q128_PAGE_SIZE, N25Q128_SPI_TIMEOUT); - HAL_Delay(1); _n25q128_deselect(ctx); // check @@ -146,7 +144,6 @@ int n25q128_write_page(struct spiflash_ctx *ctx, uint32_t page_offset, const uin // activate, send command, deselect _n25q128_select(ctx); ok = HAL_SPI_Transmit(ctx->hspi, spi_tx, 1, N25Q128_SPI_TIMEOUT); - HAL_Delay(1); _n25q128_deselect(ctx); // check @@ -168,7 +165,6 @@ int n25q128_write_page(struct spiflash_ctx *ctx, uint32_t page_offset, const uin // activate, send command _n25q128_select(ctx); ok = HAL_SPI_Transmit(ctx->hspi, spi_tx, 4, N25Q128_SPI_TIMEOUT); - HAL_Delay(1); // check if (ok != HAL_OK) { @@ -178,18 +174,20 @@ int n25q128_write_page(struct spiflash_ctx *ctx, uint32_t page_offset, const uin // send data, deselect ok = HAL_SPI_Transmit(ctx->hspi, (uint8_t *) page_buffer, N25Q128_PAGE_SIZE, N25Q128_SPI_TIMEOUT); - HAL_Delay(1); _n25q128_deselect(ctx); // check if (ok != HAL_OK) return 0; + // wait until write finishes + if (! _wait_while_wip(ctx, 1000)) return 0; + // done return 1; } -int n25q128_get_wip_flag(struct spiflash_ctx *ctx) +static int n25q128_get_wip_flag(struct spiflash_ctx *ctx) { // tx, rx buffers uint8_t spi_tx[2]; @@ -204,7 +202,6 @@ int n25q128_get_wip_flag(struct spiflash_ctx *ctx) // send command, read response, deselect _n25q128_select(ctx); ok = HAL_SPI_TransmitReceive(ctx->hspi, spi_tx, spi_rx, 2, N25Q128_SPI_TIMEOUT); - HAL_Delay(1); _n25q128_deselect(ctx); // check @@ -215,16 +212,18 @@ int n25q128_get_wip_flag(struct spiflash_ctx *ctx) } /* Wait until the flash memory is done writing (wip = Write In Progress) */ -inline int _wait_while_wip(struct spiflash_ctx *ctx, uint32_t timeout) +static int _wait_while_wip(struct spiflash_ctx *ctx, uint32_t timeout) { + uint32_t tick_end = HAL_GetTick() + timeout; int i; - while (timeout--) { + + do { i = n25q128_get_wip_flag(ctx); if (i < 0) return 0; - if (! i) break; - HAL_Delay(10); - } - return 1; + if (! i) return 1; + } while (HAL_GetTick() < tick_end); + + return 0; } static int n25q128_erase_something(struct spiflash_ctx *ctx, uint8_t command, uint32_t byte_offset) @@ -244,7 +243,6 @@ static int n25q128_erase_something(struct spiflash_ctx *ctx, uint8_t command, ui // select, send command, deselect _n25q128_select(ctx); ok = HAL_SPI_Transmit(ctx->hspi, spi_tx, 1, N25Q128_SPI_TIMEOUT); - HAL_Delay(1); _n25q128_deselect(ctx); // check @@ -263,7 +261,6 @@ static int n25q128_erase_something(struct spiflash_ctx *ctx, uint8_t command, ui // activate, send command, deselect _n25q128_select(ctx); ok = HAL_SPI_Transmit(ctx->hspi, spi_tx, 4, N25Q128_SPI_TIMEOUT); - HAL_Delay(1); _n25q128_deselect(ctx); // check @@ -271,7 +268,7 @@ static int n25q128_erase_something(struct spiflash_ctx *ctx, uint8_t command, ui // wait for erase to finish - if (! _wait_while_wip(ctx, 2000)) return 0; + if (! _wait_while_wip(ctx, 1000)) return 0; // done return 1; @@ -306,7 +303,6 @@ int n25q128_erase_bulk(struct spiflash_ctx *ctx) // select, send command, deselect _n25q128_select(ctx); ok = HAL_SPI_Transmit(ctx->hspi, spi_tx, 1, N25Q128_SPI_TIMEOUT); - HAL_Delay(1); _n25q128_deselect(ctx); // check @@ -322,7 +318,6 @@ int n25q128_erase_bulk(struct spiflash_ctx *ctx) // activate, send command, deselect _n25q128_select(ctx); ok = HAL_SPI_Transmit(ctx->hspi, spi_tx, 1, N25Q128_SPI_TIMEOUT); - HAL_Delay(1); _n25q128_deselect(ctx); // check @@ -340,7 +335,7 @@ int n25q128_erase_bulk(struct spiflash_ctx *ctx) /* * Read the Write Enable Latch bit in the status register. */ -int _n25q128_get_wel_flag(struct spiflash_ctx *ctx) +static int _n25q128_get_wel_flag(struct spiflash_ctx *ctx) { // tx, rx buffers uint8_t spi_tx[2]; @@ -355,7 +350,6 @@ int _n25q128_get_wel_flag(struct spiflash_ctx *ctx) // send command, read response, deselect _n25q128_select(ctx); ok = HAL_SPI_TransmitReceive(ctx->hspi, spi_tx, spi_rx, 2, N25Q128_SPI_TIMEOUT); - HAL_Delay(1); _n25q128_deselect(ctx); // check @@ -385,16 +379,12 @@ int n25q128_write_data(struct spiflash_ctx *ctx, uint32_t offset, const uint8_t * sectors. */ - if (! _wait_while_wip(ctx, 1000)) return -3; - if (! n25q128_erase_sector(ctx, offset / N25Q128_SECTOR_SIZE)) { return -4; } } for (page = 0; page < len / N25Q128_PAGE_SIZE; page++) { - if (! _wait_while_wip(ctx, 1000)) return -5; - if (! n25q128_write_page(ctx, offset / N25Q128_PAGE_SIZE, buf)) { return -6; } @@ -406,12 +396,6 @@ int n25q128_write_data(struct spiflash_ctx *ctx, uint32_t offset, const uint8_t */ } - /* - * Wait until last write finishes. - */ - - if (! _wait_while_wip(ctx, 1000)) return -7; - return 1; } diff --git a/spiflash_n25q128.h b/spiflash_n25q128.h index 41b6101..c01629d 100644 --- a/spiflash_n25q128.h +++ b/spiflash_n25q128.h @@ -71,7 +71,6 @@ struct spiflash_ctx { }; extern int n25q128_check_id(struct spiflash_ctx *ctx); -extern int n25q128_get_wip_flag(struct spiflash_ctx *ctx); extern int n25q128_read_page(struct spiflash_ctx *ctx, uint32_t page_offset, uint8_t *page_buffer); extern int n25q128_write_page(struct spiflash_ctx *ctx, uint32_t page_offset, const uint8_t *page_buffer); extern int n25q128_erase_sector(struct spiflash_ctx *ctx, uint32_t sector_offset); diff --git a/stm-fpgacfg.c b/stm-fpgacfg.c index 1f15f88..d3a9aec 100644 --- a/stm-fpgacfg.c +++ b/stm-fpgacfg.c @@ -96,15 +96,6 @@ int fpgacfg_erase_sectors(int num) { if (num > N25Q128_NUM_SECTORS || num < 0) num = N25Q128_NUM_SECTORS; while (num) { - int timeout = 200; /* times 10ms = 2 seconds timeout */ - while (timeout--) { - int i = n25q128_get_wip_flag(&fpgacfg_ctx); - if (i < 0) return 0; - if (! i) break; - HAL_Delay(10); - } - if (! timeout) return 0; - if (! n25q128_erase_sector(&fpgacfg_ctx, num - 1)) { return -1; } diff --git a/stm-keystore.c b/stm-keystore.c index d5d8adb..633d554 100644 --- a/stm-keystore.c +++ b/stm-keystore.c @@ -40,7 +40,7 @@ SPI_HandleTypeDef hspi_keystore; struct spiflash_ctx keystore_ctx = {&hspi_keystore, KSM_PROM_CS_N_GPIO_Port, KSM_PROM_CS_N_Pin}; -int keystore_check_id() +int keystore_check_id(void) { return n25q128_check_id(&keystore_ctx); } @@ -64,15 +64,6 @@ static int keystore_erase_something(uint32_t start, uint32_t stop, uint32_t limi if (stop > limit || stop < start) return -3; for (something = start; something <= stop; something++) { - int timeout = 200; /* times 10ms = 2 seconds timeout */ - while (timeout--) { - int i = n25q128_get_wip_flag(&keystore_ctx); - if (i < 0) return 0; - if (! i) break; - HAL_Delay(10); - } - if (! timeout) return 0; - if (! eraser(&keystore_ctx, something)) { return -1; } @@ -91,3 +82,8 @@ int keystore_erase_subsectors(uint32_t start, uint32_t stop) return keystore_erase_something(start, stop, N25Q128_NUM_SUBSECTORS, n25q128_erase_subsector); } + +int keystore_erase_bulk(void) +{ + return n25q128_erase_bulk(&keystore_ctx); +} diff --git a/stm-keystore.h b/stm-keystore.h index 437cdf6..f27687a 100644 --- a/stm-keystore.h +++ b/stm-keystore.h @@ -62,5 +62,6 @@ extern int keystore_read_data(uint32_t offset, uint8_t *buf, const uint32_t len) extern int keystore_write_data(uint32_t offset, const uint8_t *buf, const uint32_t len); extern int keystore_erase_sectors(uint32_t start, uint32_t stop); extern int keystore_erase_subsectors(uint32_t start, uint32_t stop); +extern int keystore_erase_bulk(void); #endif /* __STM32_KEYSTORE_H */ -- cgit v1.2.3