Skip to content

Commit 5cd3533

Browse files
committed
STM32F4 FLASH update
Add critical section in - flash_erase_sector and - flash_program_page to make FLASH erase procedure interrupt safe (can occur with Ethernet)
1 parent 77638f9 commit 5cd3533

File tree

1 file changed

+11
-29
lines changed

1 file changed

+11
-29
lines changed

targets/TARGET_STM/TARGET_STM32F4/flash_api.c

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@
2121
#include "flash_data.h"
2222
#include "platform/mbed_critical.h"
2323

24-
// This file is automatically generated
25-
26-
2724
static uint32_t GetSector(uint32_t Address);
2825
static uint32_t GetSectorSize(uint32_t Sector);
2926

@@ -37,30 +34,8 @@ int32_t flash_free(flash_t *obj)
3734
return 0;
3835
}
3936

40-
static int32_t flash_unlock(void)
41-
{
42-
/* Allow Access to Flash control registers and user Falsh */
43-
if (HAL_FLASH_Unlock()) {
44-
return -1;
45-
} else {
46-
return 0;
47-
}
48-
}
49-
50-
static int32_t flash_lock(void)
51-
{
52-
/* Disable the Flash option control register access (recommended to protect
53-
the option Bytes against possible unwanted operations) */
54-
if (HAL_FLASH_Lock()) {
55-
return -1;
56-
} else {
57-
return 0;
58-
}
59-
}
60-
6137
int32_t flash_erase_sector(flash_t *obj, uint32_t address)
6238
{
63-
/*Variable used for Erase procedure*/
6439
static FLASH_EraseInitTypeDef EraseInitStruct;
6540
uint32_t FirstSector;
6641
uint32_t SectorError = 0;
@@ -70,10 +45,12 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address)
7045
return -1;
7146
}
7247

73-
if (flash_unlock() != HAL_OK) {
48+
if (HAL_FLASH_Unlock() != HAL_OK) {
7449
return -1;
7550
}
7651

52+
core_util_critical_section_enter();
53+
7754
/* Get the 1st sector to erase */
7855
FirstSector = GetSector(address);
7956

@@ -86,7 +63,9 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address)
8663
status = -1;
8764
}
8865

89-
flash_lock();
66+
core_util_critical_section_exit();
67+
68+
HAL_FLASH_Lock();
9069

9170
return status;
9271
}
@@ -99,10 +78,11 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data,
9978
return -1;
10079
}
10180

102-
if (flash_unlock() != HAL_OK) {
81+
if (HAL_FLASH_Unlock() != HAL_OK) {
10382
return -1;
10483
}
10584

85+
core_util_critical_section_enter();
10686
/* Note: If an erase operation in Flash memory also concerns data in the data or instruction cache,
10787
you have to make sure that these data are rewritten before they are accessed during code
10888
execution. If this cannot be done safely, it is recommended to flush the caches by setting the
@@ -126,7 +106,9 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data,
126106
}
127107
}
128108

129-
flash_lock();
109+
core_util_critical_section_exit();
110+
111+
HAL_FLASH_Lock();
130112

131113
return status;
132114
}

0 commit comments

Comments
 (0)