Skip to content

Commit 66054f4

Browse files
authored
Merge pull request #5455 from LMESTM/flash_2MB_GetSector_fix
STM32 Flash 2MB GetSector fix
2 parents 82933bf + 48a49b4 commit 66054f4

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

targets/TARGET_STM/TARGET_STM32F4/flash_api.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data,
145145

146146
uint32_t flash_get_sector_size(const flash_t *obj, uint32_t address)
147147
{
148-
149148
if ((address >= (FLASH_BASE + FLASH_SIZE)) || (address < FLASH_BASE)) {
150149
return MBED_FLASH_INVALID_SIZE;
151150
}
@@ -165,7 +164,7 @@ uint32_t flash_get_start_address(const flash_t *obj)
165164
}
166165
uint32_t flash_get_size(const flash_t *obj)
167166
{
168-
return FLASH_SIZE;
167+
return FLASH_SIZE;
169168
}
170169

171170
/**
@@ -175,28 +174,30 @@ uint32_t flash_get_size(const flash_t *obj)
175174
*/
176175
static uint32_t GetSector(uint32_t address)
177176
{
178-
uint32_t sector = 0;
177+
uint32_t sector = 0;
179178
uint32_t tmp = address - ADDR_FLASH_SECTOR_0;
180179
/* This function supports 1Mb and 2Mb flash sizes */
181180
#if defined(ADDR_FLASH_SECTOR_16)
182181
if (address & 0x100000) { // handle 2nd bank
182+
/* Sector will be at least 12 */
183183
sector = FLASH_SECTOR_12;
184-
tmp = address - ADDR_FLASH_SECTOR_12;
184+
tmp -= 0x100000;
185+
address -= 0x100000;
185186
}
186187
#endif
187188
if (address < ADDR_FLASH_SECTOR_4) { // 16k sectorsize
188189
sector += tmp >>14;
189190
}
190191
#if defined(ADDR_FLASH_SECTOR_5)
191192
else if (address < ADDR_FLASH_SECTOR_5) { //64k sector size
192-
sector += FLASH_SECTOR_4;
193+
sector += FLASH_SECTOR_4;
193194
} else {
194195
sector += 4 + (tmp >>17);
195196
}
196197
#else
197198
// In case ADDR_FLASH_SECTOR_5 is not defined, sector 4 is the last one.
198199
else { //64k sector size
199-
sector += FLASH_SECTOR_4;
200+
sector += FLASH_SECTOR_4;
200201
}
201202
#endif
202203
return sector;
@@ -225,7 +226,7 @@ if((Sector == FLASH_SECTOR_0) || (Sector == FLASH_SECTOR_1) || (Sector == FLASH_
225226
sectorsize = 64 * 1024;
226227
} else {
227228
sectorsize = 128 * 1024;
228-
}
229+
}
229230
return sectorsize;
230231
}
231232

0 commit comments

Comments
 (0)