flash_internal: fix erase for any bit write

This commit is contained in:
King Kévin 2020-01-06 12:59:57 +01:00
parent 32948f9e8d
commit 99bf7157b2
1 changed files with 6 additions and 5 deletions

View File

@ -138,11 +138,12 @@ int32_t flash_internal_write(uint32_t address, const uint8_t *buffer, size_t siz
for (uint32_t i = 0; i < size && (address + i) < (page_start + page_size); i += 2) { // verify if no bit needs to be flipped to 1 again for (uint32_t i = 0; i < size && (address + i) < (page_start + page_size); i += 2) { // verify if no bit needs to be flipped to 1 again
if (*(uint16_t*)(buffer + i) != (*(uint16_t*)(address + i))) { // verify if the data to be written is identical to the one already written if (*(uint16_t*)(buffer + i) != (*(uint16_t*)(address + i))) { // verify if the data to be written is identical to the one already written
identical = false; identical = false;
} // in theory writing flash is only about flipping (individual) bits from 1 (erase state) to 0
if ((*(uint16_t*)(buffer + i) & *(uint16_t*)(address + i)) != *(uint16_t*)(buffer + i)) { // check if a bit needs to be flipped from 0 back to 1 (through an erase) // in practice the micro-controller won't allow to flip individual bits if the whole half-word is erased (set to 0xffff)
erase = true; if (*(uint16_t*)(address + i) != 0xffff) { // flash is not erased
identical = false; // should already have been set, but I want to be sure erase = true; // we need to erase it for it to be written
break; // no need to check further break; // no need to check further
}
} }
} }
if (identical) { // no data needs to be changed if (identical) { // no data needs to be changed