Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/stlink-lib/calculate.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,29 @@
#include "read_write.h"


uint32_t calculate_F4_sectornum(uint32_t flashaddr) {
uint32_t offset = 0;
flashaddr &= ~STM32_FLASH_BASE; // page now holding the actual flash address
uint32_t calculate_F4_sectornum(stlink_t *sl, uint32_t flashaddr) {
uint32_t offset = 0;
flashaddr &= ~STM32_FLASH_BASE; // page now holding the actual flash address

if(flashaddr >= 0x100000) {
/* The F413 and F423 chips have a single bank flash */
if (sl->chip_id != STM32_CHIPID_F413) {
/* Dual bank flash of 1MB have a discontinuity in their sector
numbering. Sector 7 is the last from the first bank and sector 12 is
the first from the second bank. */
if (sl->flash_size <= 1024*1024) {
if (flashaddr >= 0x080000) {
offset = 12;
flashaddr -= 0x080000;
}
}
/* Dual bank flash of 2MB */
else {
if (flashaddr >= 0x100000) {
offset = 12;
flashaddr -= 0x100000;
}
}
}

if(flashaddr < 0x4000) {
return (offset + 0);
Expand Down
2 changes: 1 addition & 1 deletion src/stlink-lib/calculate.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* @param flashaddr address in flash
* @return the sector number; sectors in the second MiB (second bank) are numbered from 12
*/
uint32_t calculate_F4_sectornum(uint32_t flashaddr);
uint32_t calculate_F4_sectornum(stlink_t *sl, uint32_t flashaddr);

/**
* Sector number of a flash address on STM32F7 (sectors of 32, 128 and 256 KiB).
Expand Down
20 changes: 7 additions & 13 deletions src/stlink-lib/common_flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -1304,23 +1304,17 @@ int32_t stlink_erase_flash_page(stlink_t *sl, stm32_addr_t flashaddr) {
stlink_calculate_pagesize(sl, flashaddr));
write_flash_cr_snb(sl, sector, BANK_1);

// STM32F2
// STM32F4
} else {
// calculate the actual page from the address
uint32_t sector = calculate_F4_sectornum(flashaddr);
// STM32F2
// STM32F4
} else {
// calculate the actual page from the address
uint32_t sector = calculate_F4_sectornum(sl, flashaddr);

DLOG("EraseFlash - Sector:0x%x Size:0x%x\n", sector,
stlink_calculate_pagesize(sl, flashaddr));

// the SNB values for flash sectors in the second bank do not directly
// follow the values for the first bank on 2mb devices...
if(sector >= 12) {
sector += 4;
}

write_flash_cr_snb(sl, sector, BANK_1);
}
write_flash_cr_snb(sl, sector, BANK_1);
}

set_flash_cr_strt(sl, BANK_1); // start erase operation
wait_flash_busy(sl); // wait for completion
Expand Down
9 changes: 6 additions & 3 deletions src/stlink-lib/common_legacy.c
Original file line number Diff line number Diff line change
Expand Up @@ -996,10 +996,13 @@ uint32_t stlink_calculate_pagesize(stlink_t *sl, uint32_t flashaddr) {
(sl->chip_id == STM32_CHIPID_F412) ||
(sl->chip_id == STM32_CHIPID_F410) ||
(sl->chip_id == STM32_CHIPID_F413)) {
uint32_t sector = calculate_F4_sectornum(flashaddr);
uint32_t sector = calculate_F4_sectornum(sl, flashaddr);

if(sector >= 12) {
sector -= 12;
/* Correct sector for dual bank flash memories only */
if (sl->chip_id != STM32_CHIPID_F413) {
if(sector >= 12) {
sector -= 12;
}
}

if(sector < 4) {
Expand Down
Loading