Continuous Writes to NVS and Storage Space Management

We are currently working on a Zigbee-based network coordinator using the nRF52840. I have a function, dfu_multi_image_write, that writes data to NVS in chunks. The function writes continuously to NVS, and I'm unsure if this is acceptable or if the SDK automatically manages storage availability.

See the dfu_multi_image_write function:

int dfu_multi_image_write(size_t offset, const uint8_t *chunk, size_t chunk_size)
{
    int result;
    size_t chunk_offset = 0;

    if (offset > ctx.cur_offset) {
        /* Unexpected data gap */
        return -ESPIPE;
    }

    while (1) {
        /* Skip ahead to the current write offset */
        chunk_offset += (ctx.cur_offset - offset);

        if (chunk_offset >= chunk_size) {
            break;
        }

        result = process_current_item(chunk + chunk_offset, chunk_size - chunk_offset);

        if (result <= 0) {
            return result;
        }

        chunk_offset += (size_t)result;
        offset += (size_t)result;
    }

    return 0;
}


See the process_current_item function:

static int process_current_item(const uint8_t *chunk, size_t chunk_size)
{
	int err = 0;

	/* Process only remaining bytes of the current item (header or image) */
	chunk_size = MIN(chunk_size, ctx.cur_item_size - ctx.cur_item_offset);

	if (ctx.cur_image_no < 0) {
		memcpy(ctx.buffer + ctx.cur_item_offset, chunk, chunk_size);

		if (ctx.cur_item_offset + chunk_size == ctx.cur_item_size) {
			if (ctx.cur_image_no == IMAGE_NO_FIXED_HEADER) {
				err = parse_fixed_header();
			} else {
				err = parse_cbor_header();
			}
		}
	} else {
		/* Image data */
		const struct dfu_image_writer *writer = current_image_writer();

		if (!writer) {
			err = -ESPIPE;
		}

		if (!err && ctx.cur_item_offset == 0) {
			err = writer->open(writer->image_id,
					   ctx.header.images[ctx.cur_image_no].size);
		}

		if (!err) {
			err = writer->write(chunk, chunk_size);
		}

		if (!err && ctx.cur_item_offset + chunk_size == ctx.cur_item_size) {
			err = writer->close(true);
		}
	}

	if (err) {
		return err;
	}

	ctx.cur_offset += chunk_size;
	ctx.cur_item_offset += chunk_size;

	if (ctx.cur_item_offset == ctx.cur_item_size) {
		select_next_image();
	}

	return chunk_size;
}


Can you please clarify the following questions?

1. Is it acceptable to continuously write to NVS as shown in the code snippet? Are there any procedures to follow to avoid potential issues?
2. How can I check if the previous write was completed and verify the busy status of the writer?
3. What will happen if the writer is busy, and how is it handled?

Hardware: Custom hardware with nRF52840
SDK: nRF Connect SDK version 2.1.0

Thanks, and regards,
Kavitha

Related