flash_internal: return number of bytes written

This commit is contained in:
King Kévin 2018-10-29 12:29:47 +01:00
parent 6c829a51cf
commit 5c976064b0
2 changed files with 7 additions and 4 deletions

View File

@ -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;
}

View File

@ -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);