Skip to content

Commit

Permalink
storeage/stream_flash: Cache write_block_size to ctx on init
Browse files Browse the repository at this point in the history
The commit caches write_block_size and erase_value to stream flash
context, at init, to avoid calling Flash API multiple times
to get these values at various stages of code exectuion,
at run-time.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
  • Loading branch information
de-nordic authored and henrikbrixandersen committed May 29, 2024
1 parent 9baf77d commit 25138ff
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
2 changes: 2 additions & 0 deletions include/zephyr/storage/stream_flash.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ struct stream_flash_ctx {
#ifdef CONFIG_STREAM_FLASH_ERASE
off_t last_erased_page_start_offset; /* Last erased offset */
#endif
uint8_t erase_value;
uint8_t write_block_size; /* Offset/size device write alignment */
};

/**
Expand Down
14 changes: 10 additions & 4 deletions subsys/storage/stream/stream_flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@ static int flash_sync(struct stream_flash_ctx *ctx)
}
}

fill_length = flash_get_write_block_size(ctx->fdev);
fill_length = ctx->write_block_size;
if (ctx->buf_bytes % fill_length) {
fill_length -= ctx->buf_bytes % fill_length;
filler = flash_get_parameters(ctx->fdev)->erase_value;
filler = ctx->erase_value;

memset(ctx->buf + ctx->buf_bytes, filler, fill_length);
} else {
Expand Down Expand Up @@ -249,6 +249,7 @@ int stream_flash_init(struct stream_flash_ctx *ctx, const struct device *fdev,
uint8_t *buf, size_t buf_len, size_t offset, size_t size,
stream_flash_callback_t cb)
{
const struct flash_parameters *params;
if (!ctx || !fdev || !buf) {
return -EFAULT;
}
Expand All @@ -267,7 +268,10 @@ int stream_flash_init(struct stream_flash_ctx *ctx, const struct device *fdev,
.total_size = 0
};

if (buf_len % flash_get_write_block_size(fdev)) {
params = flash_get_parameters(fdev);
ctx->write_block_size = params->write_block_size;

if (buf_len % ctx->write_block_size) {
LOG_ERR("Buffer size is not aligned to minimal write-block-size");
return -EFAULT;
}
Expand All @@ -281,11 +285,12 @@ int stream_flash_init(struct stream_flash_ctx *ctx, const struct device *fdev,
}

if ((offset + size) > inspect_flash_ctx.total_size ||
offset % flash_get_write_block_size(fdev)) {
offset % ctx->write_block_size) {
LOG_ERR("Incorrect parameter");
return -EFAULT;
}


ctx->fdev = fdev;
ctx->buf = buf;
ctx->buf_len = buf_len;
Expand All @@ -299,6 +304,7 @@ int stream_flash_init(struct stream_flash_ctx *ctx, const struct device *fdev,
#ifdef CONFIG_STREAM_FLASH_ERASE
ctx->last_erased_page_start_offset = -1;
#endif
ctx->erase_value = params->erase_value;

return 0;
}
Expand Down

0 comments on commit 25138ff

Please sign in to comment.