diff --git a/lib/flash_internal.c b/lib/flash_internal.c index 2a1a093..3d27a3d 100644 --- a/lib/flash_internal.c +++ b/lib/flash_internal.c @@ -202,16 +202,16 @@ int32_t flash_internal_write(uint32_t address, const uint8_t *buffer, size_t siz } } while (size > 0 && address < (page_start + page_size)) { - if (*((uint16_t*)(buffer)) != 0xffff) { // after an erase the bits are set to one, no need to program them - flash_program_half_word(address, *((uint16_t*)(buffer))); + if (*((uint16_t*)(buffer)) != *((uint16_t*)(address)) && *((uint16_t*)(buffer)) != 0xffff) { // only program when data is different and bits need to be set + flash_program_half_word(address, *((uint16_t*)(buffer))); // program the data if (flash_get_status_flags() != FLASH_SR_EOP) { // operation went wrong flash_lock(); // lock back flash to protect it return -10; } - } - if (*((uint16_t*)address) != *((uint16_t*)buffer)) { // verify the programmed data is right - flash_lock(); // lock back flash to protect it - return -11; + if (*((uint16_t*)address) != *((uint16_t*)buffer)) { // verify the programmed data is right + flash_lock(); // lock back flash to protect it + return -11; + } } written += 2; buffer += 2;