flash_internal: use common page size and flash end address
This commit is contained in:
parent
c30b3ecb48
commit
b7e72bba67
|
@ -33,11 +33,30 @@
|
|||
|
||||
/** number of flash pages, located at the end of flash memory, to use for EEPROM functionality */
|
||||
static uint16_t flash_internal_eeprom_pages = 0;
|
||||
/** flash page size */
|
||||
static uint16_t flash_internal_page = 0;
|
||||
/** end address of flash */
|
||||
static uint32_t flash_internal_end = 0;
|
||||
/** start address of flash memory used for the emulated EEPROM */
|
||||
static uint32_t flash_internal_eeprom_start = 0;
|
||||
/** start address of emulated EEPROM */
|
||||
static uint32_t flash_internal_eeprom_address = 0;
|
||||
|
||||
/** find out page size and flash end address */
|
||||
static void flash_internal_init(void)
|
||||
{
|
||||
if (0 == flash_internal_page) {
|
||||
flash_internal_page_size(); // get page size
|
||||
}
|
||||
if (0 == flash_internal_end) {
|
||||
if ((uint32_t)&__flash_end >= FLASH_BASE) {
|
||||
flash_internal_end = (uint32_t)&__flash_end;
|
||||
} else {
|
||||
flash_internal_end = FLASH_BASE + DESIG_FLASH_SIZE * 1024;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** verify if the data is in the internal flash area
|
||||
* @param[in] address start address of the data to read
|
||||
* @param[in] size how much data to read or write, in bytes
|
||||
|
@ -45,20 +64,17 @@ static uint32_t flash_internal_eeprom_address = 0;
|
|||
*/
|
||||
static bool flash_internal_range(uint32_t address, size_t size)
|
||||
{
|
||||
if (0 == flash_internal_page || 0 == flash_internal_end) {
|
||||
flash_internal_init();
|
||||
}
|
||||
if (address > (UINT32_MAX - size)) { // on integer overflow will occur
|
||||
return false;
|
||||
}
|
||||
if (address < FLASH_BASE) { // start address is before the start of the internal flash
|
||||
return false;
|
||||
}
|
||||
if ((uint32_t)&__flash_end >= FLASH_BASE) { // check if the end for the internal flash is enforced by the linker script
|
||||
if ((address + size) > (uint32_t)&__flash_end) { // end address is after the end of the enforced internal flash
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if ((address + size) > (FLASH_BASE + DESIG_FLASH_SIZE * 1024)) { // end address is after the end of the advertised flash
|
||||
return false;
|
||||
}
|
||||
if ((address + size) > flash_internal_end) { // end address is after the end of the internal flash
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
@ -68,26 +84,21 @@ static bool flash_internal_range(uint32_t address, size_t size)
|
|||
*/
|
||||
uint16_t flash_internal_page_size(void)
|
||||
{
|
||||
static uint16_t page_size = 0; // remember page size
|
||||
|
||||
if (page_size) { // we already determined the size
|
||||
return page_size;
|
||||
}
|
||||
if (0 == page_size) { // we don't know the page size yet
|
||||
if (0 == flash_internal_page) { // we don't know the page size yet
|
||||
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;
|
||||
flash_internal_page = 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;
|
||||
flash_internal_page = 2048;
|
||||
} else { // unknown device type (or unreadable type, see errata), deduce page size from flash size
|
||||
if (DESIG_FLASH_SIZE < 256) {
|
||||
page_size = 1024;
|
||||
flash_internal_page = 1024;
|
||||
} else {
|
||||
page_size = 2048;
|
||||
flash_internal_page = 2048;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return page_size;
|
||||
return flash_internal_page;
|
||||
}
|
||||
|
||||
bool flash_internal_read(uint32_t address, uint8_t *buffer, size_t size)
|
||||
|
@ -113,33 +124,24 @@ int32_t flash_internal_write(uint32_t address, const uint8_t *buffer, size_t siz
|
|||
// sanity checks
|
||||
if (buffer == NULL || size == 0 || size % 2) {
|
||||
return -1;
|
||||
}
|
||||
if (!flash_internal_range(address, size)) {
|
||||
} else if (address < FLASH_BASE) {
|
||||
return -2;
|
||||
}
|
||||
|
||||
// verify if it's in the flash area
|
||||
if (address < FLASH_BASE) {
|
||||
} else if (!flash_internal_range(address, size)) {
|
||||
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;
|
||||
}
|
||||
|
||||
uint16_t page_size = flash_internal_page_size(); // get page size
|
||||
uint32_t written = 0; // number of bytes written
|
||||
flash_unlock(); // unlock flash to be able to write it
|
||||
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
|
||||
uint32_t page_start = address - (address % flash_internal_page); // get start of the current page
|
||||
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
|
||||
for (uint32_t i = 0; i < size && (address + i) < (page_start + flash_internal_page); i += 2) { // verify if any word in this page needs to be programmed
|
||||
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;
|
||||
// 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)
|
||||
// in practice the micro-controller will only 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
|
||||
|
@ -148,7 +150,7 @@ int32_t flash_internal_write(uint32_t address, const uint8_t *buffer, size_t siz
|
|||
}
|
||||
if (identical) { // no data needs to be changed
|
||||
// go to end of page, or size
|
||||
uint32_t remaining = (page_start + page_size) - address;
|
||||
uint32_t remaining = (page_start + flash_internal_page) - address;
|
||||
if (remaining > size) {
|
||||
remaining = size;
|
||||
}
|
||||
|
@ -157,21 +159,21 @@ int32_t flash_internal_write(uint32_t address, const uint8_t *buffer, size_t siz
|
|||
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
|
||||
uint8_t page_data[flash_internal_page]; // 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 + flash_internal_page) && page_i <flash_internal_page; 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 < flash_internal_page) {
|
||||
page_data[page_i++] = *buffer;
|
||||
buffer++;
|
||||
address++;
|
||||
size--;
|
||||
}
|
||||
// copy data after buffer until end of page
|
||||
while (page_i < page_size) {
|
||||
while (page_i < flash_internal_page) {
|
||||
page_data[page_i] = *(uint8_t*)(page_start + page_i);
|
||||
page_i++;
|
||||
}
|
||||
|
@ -180,7 +182,7 @@ int32_t flash_internal_write(uint32_t address, const uint8_t *buffer, size_t siz
|
|||
flash_lock(); // lock back flash to protect it
|
||||
return -6;
|
||||
}
|
||||
for (uint16_t i = 0; i < page_size; i += 2) { // write whole page
|
||||
for (uint16_t i = 0; i < flash_internal_page; i += 2) { // write whole page
|
||||
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
|
||||
|
@ -202,7 +204,7 @@ int32_t flash_internal_write(uint32_t address, const uint8_t *buffer, size_t siz
|
|||
return -9;
|
||||
}
|
||||
}
|
||||
while (size > 0 && address < (page_start + page_size)) {
|
||||
while (size > 0 && address < (page_start + flash_internal_page)) {
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue