flash_internal: only flash relevant bits

This commit is contained in:
King Kévin 2020-01-02 13:16:24 +01:00
parent a6fb25b6c6
commit 822b66ea8f
1 changed files with 34 additions and 14 deletions

View File

@ -111,13 +111,29 @@ int32_t flash_internal_write(uint32_t address, const uint8_t *buffer, size_t siz
while (size) { // write page by page until all data has been written
// verify of we need to erase the flash before writing it
uint32_t page_start = address - (address % page_size); // get start of the current page
bool erase = false; // verify if the flash to write is erased of if we need to erase the page
for (uint32_t i = 0; i < size && (address + i) < (page_start + page_size); i += 2) { // verify if no bit need to be flipped to 1 again
if (*(uint16_t*)(buffer + i) != 0x0000 && (*(uint16_t*)(address + i)) != 0xffff ) { // to write the flashed, it needs to be erased, or the data needs to be 0
erase = true; // we need to erase the flash to flip the bit back to 1
bool erase = false; // verify if we need to erase the page
bool identical = true; // verify if we actually need to write data, or if the data to be written is the identical to the one already if flash
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
}
}
if (erase && preserve) { // erase before
if (identical) { // no data needs to be changed
// go to end of page, or size
uint32_t remaining = (page_start + page_size) - address;
if (remaining > size) {
remaining = size;
}
written += remaining;
buffer += remaining;
address += remaining;
size -= remaining;
} else if (erase && preserve) { // erase before
uint8_t page_data[page_size]; // a copy of the complete page before the erase it
uint16_t page_i = 0; // index for page data
// copy page before address
@ -133,7 +149,7 @@ int32_t flash_internal_write(uint32_t address, const uint8_t *buffer, size_t siz
}
// copy data after buffer until end of page
while (page_i < page_size) {
page_data[page_i] = *(uint8_t*)(page_start+page_i);
page_data[page_i] = *(uint8_t*)(page_start + page_i);
page_i++;
}
flash_erase_page(page_start); // erase current page
@ -142,10 +158,12 @@ int32_t flash_internal_write(uint32_t address, const uint8_t *buffer, size_t siz
return -6;
}
for (uint16_t i = 0; i < page_size; i += 2) { // write whole page
flash_program_half_word(page_start + i, *((uint16_t*)(page_data + i)));
if (flash_get_status_flags() != FLASH_SR_EOP) { // operation went wrong
flash_lock(); // lock back flash to protect it
return -7;
if (*((uint16_t*)(page_data + i)) != 0xffff) { // after an erase the bits are set to one, no need to program them
flash_program_half_word(page_start + i, *((uint16_t*)(page_data + i)));
if (flash_get_status_flags() != FLASH_SR_EOP) { // operation went wrong
flash_lock(); // lock back flash to protect it
return -7;
}
}
if (*((uint16_t*)(page_data + i)) != *((uint16_t*)(page_start + i))) { // verify the programmed data is right
flash_lock(); // lock back flash to protect it
@ -162,10 +180,12 @@ int32_t flash_internal_write(uint32_t address, const uint8_t *buffer, size_t siz
}
}
while (size > 0 && address < (page_start + page_size)) {
flash_program_half_word(address, *((uint16_t*)(buffer)));
if (flash_get_status_flags() != FLASH_SR_EOP) { // operation went wrong
flash_lock(); // lock back flash to protect it
return -10;
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 (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