Skip to content

Commit 67d6abc

Browse files
committed
STM32F4 FLASH update
Add critical section in flash_erase_sector
1 parent 8ef0a43 commit 67d6abc

File tree

1 file changed

+8
-29
lines changed

1 file changed

+8
-29
lines changed

targets/TARGET_STM/TARGET_STM32F4/flash_api.c

Lines changed: 8 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+
HAL_FLASH_Lock();
67+
68+
core_util_critical_section_exit();
9069

9170
return status;
9271
}
@@ -99,7 +78,7 @@ 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

@@ -126,7 +105,7 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data,
126105
}
127106
}
128107

129-
flash_lock();
108+
HAL_FLASH_Lock();
130109

131110
return status;
132111
}

0 commit comments

Comments
 (0)