From 756e5852cb931633889366afdf04f41edfc267ed Mon Sep 17 00:00:00 2001 From: "S.J.R. van Schaik" Date: Thu, 27 Jul 2017 16:53:01 +0200 Subject: [PATCH] flash: implement functions to check if pages are all zero or one --- include/flash.h | 3 ++- source/core/flash.c | 16 +++++++++++++--- source/drivers/spi_flash.c | 2 +- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/include/flash.h b/include/flash.h index 79b44a5..5d1002b 100644 --- a/include/flash.h +++ b/include/flash.h @@ -24,7 +24,8 @@ struct flash_dev { uint8_t log2_block_size; }; -int default_flash_is_erased(struct flash_dev *dev, uint32_t addr); +int default_flash_is_zero(struct flash_dev *dev, uint32_t addr); +int default_flash_is_one(struct flash_dev *dev, uint32_t addr); size_t default_flash_copy(struct flash_dev *dev, uint32_t dst, uint32_t src, size_t len); diff --git a/source/core/flash.c b/source/core/flash.c index 7357e93..412754c 100644 --- a/source/core/flash.c +++ b/source/core/flash.c @@ -5,9 +5,9 @@ #include #include -int default_flash_is_erased(struct flash_dev *dev, uint32_t addr) +static int default_flash_is_val(struct flash_dev *dev, uint32_t addr, char val) { - uint8_t data[32]; + char data[32]; size_t i, nbytes, len = 1 << dev->log2_block_size; addr <<= dev->log2_block_size; @@ -19,7 +19,7 @@ int default_flash_is_erased(struct flash_dev *dev, uint32_t addr) return 0; for (i = 0; i < sizeof(data); ++i) { - if (data[i] != 0xff) + if (data[i] != val) return 0; } @@ -30,6 +30,16 @@ int default_flash_is_erased(struct flash_dev *dev, uint32_t addr) return 1; } +int default_flash_is_zero(struct flash_dev *dev, uint32_t addr) +{ + return default_flash_is_val(dev, addr, 0); +} + +int default_flash_is_one(struct flash_dev *dev, uint32_t addr) +{ + return default_flash_is_val(dev, addr, 0xff); +} + size_t default_flash_copy(struct flash_dev *dev, uint32_t dst, uint32_t src, size_t len) { diff --git a/source/drivers/spi_flash.c b/source/drivers/spi_flash.c index c888436..1bee04d 100644 --- a/source/drivers/spi_flash.c +++ b/source/drivers/spi_flash.c @@ -24,7 +24,7 @@ static struct flash_ops spi_flash_ops = { .read = spi_flash_read, .write = spi_flash_write, .copy = default_flash_copy, - .is_erased = default_flash_is_erased, + .is_erased = default_flash_is_one, .erase = spi_flash_erase, };