diff --git a/include/zephyr/storage/stream_flash.h b/include/zephyr/storage/stream_flash.h index b1d78af90a9f..23f240594251 100644 --- a/include/zephyr/storage/stream_flash.h +++ b/include/zephyr/storage/stream_flash.h @@ -61,7 +61,9 @@ struct stream_flash_ctx { size_t bytes_written; /* Number of bytes written to flash */ size_t offset; /* Offset from base of flash device to write area */ size_t available; /* Available bytes in write area */ +#ifdef CONFIG_STREAM_FLASH_POST_WRITE_CALLBACK stream_flash_callback_t callback; /* Callback invoked after write op */ +#endif #ifdef CONFIG_STREAM_FLASH_ERASE off_t last_erased_page_start_offset; /* Last erased offset */ #endif @@ -82,6 +84,8 @@ struct stream_flash_ctx { * If this is '0', the size will be set to the total size * of the flash device minus the offset. * @param cb Callback to be invoked on completed flash write operations. + * Callback is supported when CONFIG_STREAM_FLASH_POST_WRITE_CALLBACK + * is enabled. * * @return non-negative on success, negative errno code on fail */ diff --git a/subsys/storage/stream/Kconfig b/subsys/storage/stream/Kconfig index 59a985feb124..25df2546196e 100644 --- a/subsys/storage/stream/Kconfig +++ b/subsys/storage/stream/Kconfig @@ -12,6 +12,14 @@ menuconfig STREAM_FLASH if STREAM_FLASH +config STREAM_FLASH_POST_WRITE_CALLBACK + bool "Write complete callback" + default y + help + Enable callback that will be invoked once data is synchronized from + stream to device. When callback is not used, disabling the option + allows to save some code storage and RAM. + config STREAM_FLASH_ERASE bool "Perform erase operations" depends on FLASH_HAS_EXPLICIT_ERASE diff --git a/subsys/storage/stream/stream_flash.c b/subsys/storage/stream/stream_flash.c index 63455af6d289..ae41f0b3ec95 100644 --- a/subsys/storage/stream/stream_flash.c +++ b/subsys/storage/stream/stream_flash.c @@ -165,6 +165,8 @@ static int flash_sync(struct stream_flash_ctx *ctx) return rc; } +#if defined(CONFIG_STREAM_FLASH_POST_WRITE_CALLBACK) + if (ctx->callback) { /* Invert to ensure that caller is able to discover a faulty * flash_read() even if no error code is returned. @@ -187,6 +189,8 @@ static int flash_sync(struct stream_flash_ctx *ctx) } } +#endif + ctx->bytes_written += ctx->buf_bytes; ctx->buf_bytes = 0U; @@ -317,7 +321,12 @@ int stream_flash_init(struct stream_flash_ctx *ctx, const struct device *fdev, ctx->offset = offset; ctx->available = (size == 0 ? inspect_flash_ctx.total_size - offset : size); + +#if !defined(CONFIG_STREAM_FLASH_POST_WRITE_CALLBACK) + ARG_UNUSED(cb); +#else ctx->callback = cb; +#endif #ifdef CONFIG_STREAM_FLASH_ERASE ctx->last_erased_page_start_offset = -1; diff --git a/tests/subsys/storage/stream/stream_flash/prj.conf b/tests/subsys/storage/stream/stream_flash/prj.conf index 99684390afa7..77b44389550c 100644 --- a/tests/subsys/storage/stream/stream_flash/prj.conf +++ b/tests/subsys/storage/stream/stream_flash/prj.conf @@ -13,3 +13,6 @@ CONFIG_SETTINGS=y CONFIG_STREAM_FLASH=y CONFIG_STREAM_FLASH_ERASE=y CONFIG_STREAM_FLASH_PROGRESS=y +# It case if this is no longer y by default in Kconfig as +# the tests are not ready to not test the feature. +CONFIG_STREAM_FLASH_POST_WRITE_CALLBACK=y