flash: only erase and preserve flash when required or requested

This commit is contained in:
King Kévin 2018-10-28 22:50:51 +01:00
parent 42ed03fa67
commit 6c829a51cf
3 changed files with 50 additions and 40 deletions

View File

@ -72,33 +72,33 @@ bool flash_internal_read(uint32_t address, uint8_t *buffer, size_t size)
return true;
}
bool flash_internal_write(uint32_t address, uint8_t *buffer, size_t size)
int8_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) {
return false;
if (buffer == NULL || size == 0 || size % 2) {
return -1;
}
if (!flash_internal_range(address, size)) {
return false;
return -2;
}
// verify if it's in the flash area
if (address<FLASH_BASE) {
return false;
} else if ((uint32_t)&__flash_end>=FLASH_BASE && (address+size)>(uint32_t)&__flash_end) {
return false;
} else if ((uint32_t)&__flash_end<FLASH_BASE && (address+size)>(FLASH_BASE+DESIG_FLASH_SIZE*1024)) {
return false;
if (address < FLASH_BASE) {
return -3;
} else if ((uint32_t)&__flash_end >= FLASH_BASE && (address + size) > (uint32_t)&__flash_end) {
return 4;
} else if ((uint32_t)&__flash_end < FLASH_BASE && (address + size) > (FLASH_BASE+DESIG_FLASH_SIZE * 1024)) {
return -5;
}
// get page size
uint16_t page_size = 0;
if ((0x410==(DBGMCU_IDCODE&DBGMCU_IDCODE_DEV_ID_MASK)) || (0x412==(DBGMCU_IDCODE&DBGMCU_IDCODE_DEV_ID_MASK))) { // low-density (16-32 KB flash) and medium-density (64-128 KB flash) devices have 1 KB flash pages
if ((0x410 == (DBGMCU_IDCODE&DBGMCU_IDCODE_DEV_ID_MASK)) || (0x412 == (DBGMCU_IDCODE&DBGMCU_IDCODE_DEV_ID_MASK))) { // low-density (16-32 KB flash) and medium-density (64-128 KB flash) devices have 1 KB flash pages
page_size = 1024;
} else if ((0x414==(DBGMCU_IDCODE&DBGMCU_IDCODE_DEV_ID_MASK)) || (0x430==(DBGMCU_IDCODE&DBGMCU_IDCODE_DEV_ID_MASK)) || (0x418==(DBGMCU_IDCODE&DBGMCU_IDCODE_DEV_ID_MASK))) { // high-density (256-512 KB flash), XL-density (768-1024 KB flash) devices and connectivity line have 2 KB flash pages
} else if ((0x414 == (DBGMCU_IDCODE&DBGMCU_IDCODE_DEV_ID_MASK)) || (0x430 == (DBGMCU_IDCODE&DBGMCU_IDCODE_DEV_ID_MASK)) || (0x418==(DBGMCU_IDCODE&DBGMCU_IDCODE_DEV_ID_MASK))) { // high-density (256-512 KB flash), XL-density (768-1024 KB flash) devices and connectivity line have 2 KB flash pages
page_size = 2048;
} else { // unknown device type (or unreadable type, see errata), deduce page size from flash size
if (DESIG_FLASH_SIZE<256) {
if (DESIG_FLASH_SIZE < 256) {
page_size = 1024;
} else {
page_size = 2048;
@ -107,58 +107,66 @@ bool flash_internal_write(uint32_t address, uint8_t *buffer, size_t size)
flash_unlock(); // unlock flash to be able to write it
while (size) { // write page by page until all data has been written
uint32_t page_start = address-(address%page_size); // get start of the current page
// 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 flash=address; flash<(address+size) && flash<(page_start+page_size); flash += 2) { // go through page
if (*(uint16_t*)(flash)!=0xffff) { // is flash not erased
erase = true; // the erase flash
for (uint32_t i = 0; i < size && (address + i) < (page_start + page_size); i += 2) { // verify if not 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
}
}
if (erase) { // make copy of the page to erase and erase it
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
for (uint32_t flash=page_start; flash<address && flash<(page_start+page_size) && page_i<page_size; flash++) {
for (uint32_t flash = page_start; flash < address && flash < (page_start + page_size) && page_i < page_size; flash++) {
page_data[page_i++] = *(uint8_t*)(flash);
}
// copy data starting at address
while (size>0 && page_i<page_size) {
while (size > 0 && page_i < page_size) {
page_data[page_i++] = *buffer;
buffer++;
address++;
size--;
}
// copy data after buffer until end of page
while (page_i<page_size) {
while (page_i < page_size) {
page_data[page_i] = *(uint8_t*)(page_start+page_i);
page_i++;
}
flash_erase_page(page_start); // erase current page
if (flash_get_status_flags()!=FLASH_SR_EOP) { // operation went wrong
if (flash_get_status_flags() != FLASH_SR_EOP) { // operation went wrong
flash_lock(); // lock back flash to protect it
return false;
return -6;
}
for (uint16_t i=0; i<page_size/2; i++) { // write whole page
flash_program_half_word(page_start+i*2, *((uint16_t*)(page_data+i*2)));
if (flash_get_status_flags()!=FLASH_SR_EOP) { // operation went wrong
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 false;
return -7;
}
if (*((uint16_t*)(page_data+i*2))!=*((uint16_t*)(page_start+i*2))) { // verify the programmed data is right
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
return false;
return -8;
}
}
} else { // simply data until end of page
while (size>0 && address<(page_start+page_size)) {
} else { // simply copy data until end of page (or end of data)
if (erase) {
flash_erase_page(page_start); // erase current page
if (flash_get_status_flags() != FLASH_SR_EOP) { // operation went wrong
flash_lock(); // lock back flash to protect it
return -9;
}
}
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
if (flash_get_status_flags() != FLASH_SR_EOP) { // operation went wrong
flash_lock(); // lock back flash to protect it
return false;
return -10;
}
if (*((uint16_t*)address)!=*((uint16_t*)buffer)) { // verify the programmed data is right
if (*((uint16_t*)address) != *((uint16_t*)buffer)) { // verify the programmed data is right
flash_lock(); // lock back flash to protect it
return false;
return -11;
}
buffer += 2;
address += 2;
@ -168,5 +176,5 @@ bool flash_internal_write(uint32_t address, uint8_t *buffer, size_t size)
}
flash_lock(); // lock back flash to protect it
return true;
return 0;
}

View File

@ -15,7 +15,7 @@
/** library to read/write internal flash (API)
* @file flash_internal.h
* @author King Kévin <kingkevin@cuvoodoo.info>
* @date 2016-2017
* @date 2016-2018
* @note peripherals used: none
*/
#pragma once
@ -31,6 +31,8 @@ bool flash_internal_read(uint32_t address, uint8_t *buffer, size_t size);
* @param[in] address start address where to write data to
* @param[in] buffer data to be written
* @param[in] size how much data to write, in bytes
* @return if write succeeded
* @param[in] preserve keep the rest of the page if data needs to be erased
* @return 0 on success, else see internal error code
* @note the page will be erased if needed to write the data to the flash
*/
bool flash_internal_write(uint32_t address, uint8_t *buffer, size_t size);
int8_t flash_internal_write(uint32_t address, const uint8_t *buffer, size_t size, bool preserve);

View File

@ -157,7 +157,7 @@ static void usb_dfu_flash(usbd_device *usbd_dev, struct usb_setup_data *req)
(void)usbd_dev; // variable not used
(void)req; // variable not used
led_off(); // indicate we are processing
if (flash_internal_write(flash_pointer, download_data, download_length)) { // write downloaded data
if (0 != flash_internal_write(flash_pointer, download_data, download_length, true)) { // write downloaded data
flash_pointer += download_length; // go to next segment
usb_dfu_state = STATE_DFU_DNLOAD_IDLE; // go back to idle stat to wait for next segment
} else { // warn about writing error