storage/stream: elimination of usage of the flash API internals
Module was using flash driver implementation API call `page_layout()` which is part driver implementation interface API. This patch re-implement this part of code using `flash_page_foreach()` public API function. Signed-off-by: Andrzej Puzdrowski <andrzej.puzdrowski@nordicsemi.no>
This commit is contained in:
parent
607b390ba4
commit
450b82aeb7
1 changed files with 32 additions and 17 deletions
|
@ -181,6 +181,27 @@ size_t stream_flash_bytes_written(struct stream_flash_ctx *ctx)
|
||||||
return ctx->bytes_written;
|
return ctx->bytes_written;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct _inspect_flash {
|
||||||
|
size_t buf_len;
|
||||||
|
size_t total_size;
|
||||||
|
};
|
||||||
|
|
||||||
|
static bool find_flash_total_size(const struct flash_pages_info *info,
|
||||||
|
void *data)
|
||||||
|
{
|
||||||
|
struct _inspect_flash *ctx = (struct _inspect_flash *) data;
|
||||||
|
|
||||||
|
if (ctx->buf_len > info->size) {
|
||||||
|
LOG_ERR("Buffer size is bigger than page");
|
||||||
|
ctx->total_size = 0;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx->total_size += info->size;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
int stream_flash_init(struct stream_flash_ctx *ctx, const struct device *fdev,
|
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,
|
uint8_t *buf, size_t buf_len, size_t offset, size_t size,
|
||||||
stream_flash_callback_t cb)
|
stream_flash_callback_t cb)
|
||||||
|
@ -189,10 +210,10 @@ int stream_flash_init(struct stream_flash_ctx *ctx, const struct device *fdev,
|
||||||
return -EFAULT;
|
return -EFAULT;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t layout_size = 0;
|
struct _inspect_flash inspect_flash_ctx = {
|
||||||
size_t total_size = 0;
|
.buf_len = buf_len,
|
||||||
const struct flash_pages_layout *layout;
|
.total_size = 0
|
||||||
const struct flash_driver_api *api = fdev->api;
|
};
|
||||||
|
|
||||||
if (buf_len % flash_get_write_block_size(fdev)) {
|
if (buf_len % flash_get_write_block_size(fdev)) {
|
||||||
LOG_ERR("Buffer size is not aligned to minimal write-block-size");
|
LOG_ERR("Buffer size is not aligned to minimal write-block-size");
|
||||||
|
@ -200,21 +221,14 @@ int stream_flash_init(struct stream_flash_ctx *ctx, const struct device *fdev,
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Calculate the total size of the flash device */
|
/* Calculate the total size of the flash device */
|
||||||
api->page_layout(fdev, &layout, &layout_size);
|
flash_page_foreach(fdev, find_flash_total_size, &inspect_flash_ctx);
|
||||||
for (int i = 0; i < layout_size; i++) {
|
|
||||||
|
|
||||||
total_size += layout->pages_count * layout->pages_size;
|
|
||||||
|
|
||||||
if (buf_len > layout->pages_size) {
|
|
||||||
LOG_ERR("Buffer size is bigger than page");
|
|
||||||
return -EFAULT;
|
|
||||||
}
|
|
||||||
|
|
||||||
layout++;
|
|
||||||
|
|
||||||
|
/* The flash size counted should never be equal zero */
|
||||||
|
if (inspect_flash_ctx.total_size == 0) {
|
||||||
|
return -EFAULT;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((offset + size) > total_size ||
|
if ((offset + size) > inspect_flash_ctx.total_size ||
|
||||||
offset % flash_get_write_block_size(fdev)) {
|
offset % flash_get_write_block_size(fdev)) {
|
||||||
LOG_ERR("Incorrect parameter");
|
LOG_ERR("Incorrect parameter");
|
||||||
return -EFAULT;
|
return -EFAULT;
|
||||||
|
@ -226,7 +240,8 @@ int stream_flash_init(struct stream_flash_ctx *ctx, const struct device *fdev,
|
||||||
ctx->bytes_written = 0;
|
ctx->bytes_written = 0;
|
||||||
ctx->buf_bytes = 0U;
|
ctx->buf_bytes = 0U;
|
||||||
ctx->offset = offset;
|
ctx->offset = offset;
|
||||||
ctx->available = (size == 0 ? total_size - offset : size);
|
ctx->available = (size == 0 ? inspect_flash_ctx.total_size - offset :
|
||||||
|
size);
|
||||||
ctx->callback = cb;
|
ctx->callback = cb;
|
||||||
|
|
||||||
#ifdef CONFIG_STREAM_FLASH_ERASE
|
#ifdef CONFIG_STREAM_FLASH_ERASE
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue