flash: get page size from device instead of hardcoded value
This commit is contained in:
parent
f05ab20021
commit
074546f66e
|
@ -23,15 +23,13 @@
|
|||
#include <stdlib.h> // general utilities
|
||||
|
||||
/* STM32 (including CM3) libraries */
|
||||
#include <libopencm3/stm32/desig.h> // device signature utilities
|
||||
#include <libopencm3/stm32/flash.h> // flash utilities
|
||||
#include <libopencm3/stm32/desig.h> // device signature definitions
|
||||
#include <libopencm3/stm32/dbgmcu.h> // debug definitions
|
||||
|
||||
#include "flash_internal.h" // flash storage library API
|
||||
#include "global.h" // global definitions
|
||||
|
||||
/** the flash page size (medium-density devices have 1KiB page size) */
|
||||
#define PAGE_SIZE 1024
|
||||
|
||||
bool flash_internal_read(uint32_t address, uint8_t *buffer, size_t size)
|
||||
{
|
||||
// sanity checks
|
||||
|
@ -74,31 +72,45 @@ bool flash_internal_write(uint32_t address, uint8_t *buffer, size_t size)
|
|||
return false;
|
||||
}
|
||||
|
||||
// get page size
|
||||
uint16_t page_size = 0;
|
||||
if ((0x412==(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
|
||||
page_size = 2048;
|
||||
} else { // unknown device type (or unreadable type, see errata), deduce page size from flash size
|
||||
if (DESIG_FLASH_SIZE<256) {
|
||||
page_size = 1024;
|
||||
} else {
|
||||
page_size = 2048;
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
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
|
||||
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
|
||||
}
|
||||
}
|
||||
if (erase) { // make copy of the page to erase and erase it
|
||||
uint8_t page_data[PAGE_SIZE]; // a copy of the complete page before the erase it
|
||||
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<sizeof(page_data); 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<sizeof(page_data)) {
|
||||
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<sizeof(page_data)) {
|
||||
while (page_i<page_size) {
|
||||
page_data[page_i] = *(uint8_t*)(page_start+page_i);
|
||||
page_i++;
|
||||
}
|
||||
|
@ -107,7 +119,7 @@ bool flash_internal_write(uint32_t address, uint8_t *buffer, size_t size)
|
|||
flash_lock(); // lock back flash to protect it
|
||||
return false;
|
||||
}
|
||||
for (uint16_t i=0; i<PAGE_SIZE/2; i++) { // write whole page
|
||||
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
|
||||
flash_lock(); // lock back flash to protect it
|
||||
|
@ -119,7 +131,7 @@ bool flash_internal_write(uint32_t address, uint8_t *buffer, size_t size)
|
|||
}
|
||||
}
|
||||
} else { // simply data until end of page
|
||||
while (size>0 && address<(page_start+PAGE_SIZE)) {
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue