Skip to content
Permalink
Browse files

drivers/flash/flash_simulator: fix flash size calculation

Flash size calculation was don with assumption that flash page size
is always 1 KB and flash size was parameterized with such granularity.

This patch correct this bug.

Flash pages number under statistic can't be calculated via preprocessor
anymore - thus are parameterized via Kconfig.

Signed-off-by: Andrzej Puzdrowski <andrzej.puzdrowski@nordicsemi.no>
  • Loading branch information...
nvlsianpu authored and andrewboie committed Jul 4, 2019
1 parent 891ccdc commit 428c281bc66b4abaa9e34a36e4d8d366e0c9bd6f
Showing with 25 additions and 11 deletions.
  1. +13 −0 drivers/flash/Kconfig.simulator
  2. +10 −8 drivers/flash/flash_simulator.c
  3. +2 −3 tests/drivers/flash_simulator/src/main.c
@@ -44,6 +44,19 @@ config FLASH_SIMULATOR_SIMULATE_TIMING
bool
prompt "Enable hardware timing simulation"

config FLASH_SIMULATOR_STAT_PAGE_COUNT
int "Pages under statistic"
range 1 256
default 256
help
Only up to this number of beginning pages will be tracked
while catching dedicated flash operations and thresholds.
This number is not automatic because implementation uses
UNTIL_REPEAT() macro, which is limited to take explicitly
number of iterations.
This is why it's not possible to calculate the number of pages with
preprocessor using DT properties.

if FLASH_SIMULATOR_SIMULATE_TIMING

config FLASH_SIMULATOR_MIN_READ_TIME_US
@@ -17,16 +17,17 @@
#define FLASH_SIMULATOR_BASE_OFFSET DT_FLASH_SIM_BASE_ADDRESS
#define FLASH_SIMULATOR_ERASE_UNIT DT_FLASH_SIM_ERASE_BLOCK_SIZE
#define FLASH_SIMULATOR_PROG_UNIT DT_FLASH_SIM_WRITE_BLOCK_SIZE
#define FLASH_SIMULATOR_FLASH_SIZE (DT_FLASH_SIM_SIZE / 1024)
#define FLASH_SIMULATOR_FLASH_SIZE DT_FLASH_SIM_SIZE

#define FLASH_SIMULATOR_PAGE_COUNT (FLASH_SIMULATOR_FLASH_SIZE / \
FLASH_SIMULATOR_ERASE_UNIT)

#if (FLASH_SIMULATOR_ERASE_UNIT % FLASH_SIMULATOR_PROG_UNIT)
#error "Erase unit must be a multiple of program unit"
#endif

#define FLASH(addr) (mock_flash + (addr) - FLASH_SIMULATOR_BASE_OFFSET)

#define FLASH_SIZE (FLASH_SIMULATOR_FLASH_SIZE * FLASH_SIMULATOR_ERASE_UNIT)

/* maximum number of pages that can be tracked by the stats module */
#define STATS_PAGE_COUNT_THRESHOLD 256

@@ -45,13 +46,13 @@
} while (0)

#if (defined(CONFIG_STATS) && \
(FLASH_SIMULATOR_FLASH_SIZE > STATS_PAGE_COUNT_THRESHOLD))
(CONFIG_FLASH_SIMULATOR_STAT_PAGE_COUNT > STATS_PAGE_COUNT_THRESHOLD))
/* Limitation above is caused by used UTIL_REPEAT */
/* Using FLASH_SIMULATOR_FLASH_PAGE_COUNT allows to avoid terrible */
/* error logg at the output and work with the stats module partially */
#define FLASH_SIMULATOR_FLASH_PAGE_COUNT STATS_PAGE_COUNT_THRESHOLD
#else
#define FLASH_SIMULATOR_FLASH_PAGE_COUNT FLASH_SIMULATOR_FLASH_SIZE
#define FLASH_SIMULATOR_FLASH_PAGE_COUNT CONFIG_FLASH_SIMULATOR_STAT_PAGE_COUNT
#endif

/* simulator statistcs */
@@ -101,15 +102,16 @@ STATS_NAME(flash_sim_thresholds, max_erase_calls)
STATS_NAME(flash_sim_thresholds, max_len)
STATS_NAME_END(flash_sim_thresholds);

static u8_t mock_flash[FLASH_SIZE];
static u8_t mock_flash[FLASH_SIMULATOR_FLASH_SIZE];
static bool write_protection;

static const struct flash_driver_api flash_sim_api;

static int flash_range_is_valid(struct device *dev, off_t offset, size_t len)
{
ARG_UNUSED(dev);
if ((offset + len > FLASH_SIZE + FLASH_SIMULATOR_BASE_OFFSET) ||
if ((offset + len > FLASH_SIMULATOR_FLASH_SIZE +
FLASH_SIMULATOR_BASE_OFFSET) ||
(offset < FLASH_SIMULATOR_BASE_OFFSET)) {
return 0;
}
@@ -296,7 +298,7 @@ static int flash_sim_erase(struct device *dev, const off_t offset,

#ifdef CONFIG_FLASH_PAGE_LAYOUT
static const struct flash_pages_layout flash_sim_pages_layout = {
.pages_count = FLASH_SIMULATOR_FLASH_SIZE,
.pages_count = FLASH_SIMULATOR_PAGE_COUNT,
.pages_size = FLASH_SIMULATOR_ERASE_UNIT,
};

@@ -12,11 +12,10 @@
#define FLASH_SIMULATOR_BASE_OFFSET DT_FLASH_SIM_BASE_ADDRESS
#define FLASH_SIMULATOR_ERASE_UNIT DT_FLASH_SIM_ERASE_BLOCK_SIZE
#define FLASH_SIMULATOR_PROG_UNIT DT_FLASH_SIM_WRITE_BLOCK_SIZE
#define FLASH_SIMULATOR_FLASH_SIZE (DT_FLASH_SIM_SIZE / 1024)
#define FLASH_SIMULATOR_FLASH_SIZE DT_FLASH_SIM_SIZE

/* Offset between pages */
#define TEST_SIM_FLASH_SIZE (FLASH_SIMULATOR_ERASE_UNIT *\
FLASH_SIMULATOR_FLASH_SIZE)
#define TEST_SIM_FLASH_SIZE FLASH_SIMULATOR_FLASH_SIZE

#define TEST_SIM_FLASH_END (TEST_SIM_FLASH_SIZE +\
FLASH_SIMULATOR_BASE_OFFSET)

0 comments on commit 428c281

Please sign in to comment.
You can’t perform that action at this time.