diff --git a/lib/flash_internal.c b/lib/flash_internal.c index 3d27a3d..167dc4e 100644 --- a/lib/flash_internal.c +++ b/lib/flash_internal.c @@ -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 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; - } - 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) - erase = true; - identical = false; // should already have been set, but I want to be sure - break; // no need to check further + // in theory writing flash is only about flipping (individual) bits from 1 (erase state) to 0 + // in practice the micro-controller won't allow to flip individual bits if the whole half-word is erased (set to 0xffff) + if (*(uint16_t*)(address + i) != 0xffff) { // flash is not erased + erase = true; // we need to erase it for it to be written + break; // no need to check further + } } } if (identical) { // no data needs to be changed