diff --git a/lib/flash_internal.c b/lib/flash_internal.c index 81eac5c..39d7632 100644 --- a/lib/flash_internal.c +++ b/lib/flash_internal.c @@ -72,7 +72,7 @@ bool flash_internal_read(uint32_t address, uint8_t *buffer, size_t size) return true; } -int8_t flash_internal_write(uint32_t address, const uint8_t *buffer, size_t size, bool preserve) +int32_t flash_internal_write(uint32_t address, const uint8_t *buffer, size_t size, bool preserve) { // sanity checks if (buffer == NULL || size == 0 || size % 2) { @@ -105,6 +105,7 @@ int8_t flash_internal_write(uint32_t address, const uint8_t *buffer, size_t size } } + uint32_t written = 0; // number of bytes written flash_unlock(); // unlock flash to be able to write it while (size) { // write page by page until all data has been written // verify of we need to erase the flash before writing it @@ -149,6 +150,7 @@ int8_t flash_internal_write(uint32_t address, const uint8_t *buffer, size_t size flash_lock(); // lock back flash to protect it return -8; } + written += 2; } } else { // simply copy data until end of page (or end of data) if (erase) { @@ -168,6 +170,7 @@ int8_t flash_internal_write(uint32_t address, const uint8_t *buffer, size_t size flash_lock(); // lock back flash to protect it return -11; } + written += 2; buffer += 2; address += 2; size -= 2; @@ -176,5 +179,5 @@ int8_t flash_internal_write(uint32_t address, const uint8_t *buffer, size_t size } flash_lock(); // lock back flash to protect it - return 0; + return written; } diff --git a/lib/flash_internal.h b/lib/flash_internal.h index ff804d0..247770a 100644 --- a/lib/flash_internal.h +++ b/lib/flash_internal.h @@ -32,7 +32,7 @@ bool flash_internal_read(uint32_t address, uint8_t *buffer, size_t size); * @param[in] buffer data to be written * @param[in] size how much data to write, in bytes * @param[in] preserve keep the rest of the page if data needs to be erased - * @return 0 on success, else see internal error code + * @return number of bytes written (including preserved data), or negative in case of error * @note the page will be erased if needed to write the data to the flash */ -int8_t flash_internal_write(uint32_t address, const uint8_t *buffer, size_t size, bool preserve); +int32_t flash_internal_write(uint32_t address, const uint8_t *buffer, size_t size, bool preserve);