Skip to content

Commit ba37eb8

Browse files
committed
STM32 FLASH API : add critical sections
See PR #13802 (for F4 board) Concerned boards are STM32F0 STM32F1 STM32F2 STM32F3 STM32F4 STM32F7 STM32G0 STM32G4 STM32H7 STM32L0 STM32L1 STM32L4 STM32L5 Adding test of return code of HAL_FLASH_Lock() function Adding board STM32F4 Running AStyle
1 parent ac45aac commit ba37eb8

File tree

13 files changed

+197
-280
lines changed

13 files changed

+197
-280
lines changed

targets/TARGET_STM/TARGET_STM32F0/flash_api.c

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,6 @@
3030
// Minimum number of bytes to be programmed at a time
3131
#define MIN_PROG_SIZE (4U)
3232

33-
static int32_t flash_unlock(void)
34-
{
35-
/* Allow Access to Flash control registers and user Flash */
36-
if (HAL_FLASH_Unlock()) {
37-
return -1;
38-
} else {
39-
return 0;
40-
}
41-
}
42-
43-
static int32_t flash_lock(void)
44-
{
45-
/* Disable the Flash option control register access (recommended to protect
46-
the option Bytes against possible unwanted operations) */
47-
if (HAL_FLASH_Lock()) {
48-
return -1;
49-
} else {
50-
return 0;
51-
}
52-
}
53-
5433
int32_t flash_init(flash_t *obj)
5534
{
5635
return 0;
@@ -71,10 +50,12 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address)
7150
return -1;
7251
}
7352

74-
if (flash_unlock() != HAL_OK) {
53+
if (HAL_FLASH_Unlock() != HAL_OK) {
7554
return -1;
7655
}
7756

57+
core_util_critical_section_enter();
58+
7859
// Clear Flash status register's flags
7960
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGERR);
8061

@@ -93,7 +74,11 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address)
9374
status = -1;
9475
}
9576

96-
flash_lock();
77+
core_util_critical_section_exit();
78+
79+
if (HAL_FLASH_Lock() != HAL_OK) {
80+
return -1;
81+
}
9782

9883
return status;
9984
}
@@ -111,10 +96,12 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data,
11196
return -1;
11297
}
11398

114-
if (flash_unlock() != HAL_OK) {
99+
if (HAL_FLASH_Unlock() != HAL_OK) {
115100
return -1;
116101
}
117102

103+
core_util_critical_section_enter();
104+
118105
/* Program the user Flash area word by word */
119106
StartAddress = address;
120107

@@ -145,7 +132,11 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data,
145132
}
146133
}
147134

148-
flash_lock();
135+
core_util_critical_section_exit();
136+
137+
if (HAL_FLASH_Lock() != HAL_OK) {
138+
return -1;
139+
}
149140

150141
return status;
151142
}

targets/TARGET_STM/TARGET_STM32F1/flash_api.c

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,6 @@
3030
// Minimum number of bytes to be programmed at a time
3131
#define MIN_PROG_SIZE (4U)
3232

33-
static int32_t flash_unlock(void)
34-
{
35-
/* Allow Access to Flash control registers and user Flash */
36-
if (HAL_FLASH_Unlock()) {
37-
return -1;
38-
} else {
39-
return 0;
40-
}
41-
}
42-
43-
static int32_t flash_lock(void)
44-
{
45-
/* Disable the Flash option control register access (recommended to protect
46-
the option Bytes against possible unwanted operations) */
47-
if (HAL_FLASH_Lock()) {
48-
return -1;
49-
} else {
50-
return 0;
51-
}
52-
}
53-
5433
int32_t flash_init(flash_t *obj)
5534
{
5635
return 0;
@@ -71,10 +50,12 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address)
7150
return -1;
7251
}
7352

74-
if (flash_unlock() != HAL_OK) {
53+
if (HAL_FLASH_Unlock() != HAL_OK) {
7554
return -1;
7655
}
7756

57+
core_util_critical_section_enter();
58+
7859
// Clear Flash status register's flags
7960
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGERR | FLASH_FLAG_OPTVERR);
8061

@@ -93,7 +74,11 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address)
9374
status = -1;
9475
}
9576

96-
flash_lock();
77+
core_util_critical_section_exit();
78+
79+
if (HAL_FLASH_Lock() != HAL_OK) {
80+
return -1;
81+
}
9782

9883
return status;
9984
}
@@ -111,10 +96,12 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data,
11196
return -1;
11297
}
11398

114-
if (flash_unlock() != HAL_OK) {
99+
if (HAL_FLASH_Unlock() != HAL_OK) {
115100
return -1;
116101
}
117102

103+
core_util_critical_section_enter();
104+
118105
/* Program the user Flash area word by word */
119106
StartAddress = address;
120107

@@ -145,7 +132,11 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data,
145132
}
146133
}
147134

148-
flash_lock();
135+
core_util_critical_section_exit();
136+
137+
if (HAL_FLASH_Lock() != HAL_OK) {
138+
return -1;
139+
}
149140

150141
return status;
151142
}

targets/TARGET_STM/TARGET_STM32F2/flash_api.c

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,6 @@
2525
static uint32_t GetSector(uint32_t Address);
2626
static uint32_t GetSectorSize(uint32_t Sector);
2727

28-
static int32_t flash_unlock(void)
29-
{
30-
/* Allow Access to Flash control registers and user Falsh */
31-
if (HAL_FLASH_Unlock()) {
32-
return -1;
33-
} else {
34-
return 0;
35-
}
36-
}
37-
38-
static int32_t flash_lock(void)
39-
{
40-
/* Disable the Flash option control register access (recommended to protect
41-
the option Bytes against possible unwanted operations) */
42-
if (HAL_FLASH_Lock()) {
43-
return -1;
44-
} else {
45-
return 0;
46-
}
47-
}
48-
4928
int32_t flash_init(flash_t *obj)
5029
{
5130
return 0;
@@ -67,10 +46,12 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address)
6746
return -1;
6847
}
6948

70-
if (flash_unlock() != HAL_OK) {
49+
if (HAL_FLASH_Unlock() != HAL_OK) {
7150
return -1;
7251
}
7352

53+
core_util_critical_section_enter();
54+
7455
/* Get the 1st sector to erase */
7556
FirstSector = GetSector(address);
7657

@@ -84,7 +65,11 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address)
8465
status = -1;
8566
}
8667

87-
flash_lock();
68+
core_util_critical_section_exit();
69+
70+
if (HAL_FLASH_Lock() != HAL_OK) {
71+
return -1;
72+
}
8873

8974
return status;
9075
}
@@ -97,10 +82,12 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data,
9782
return -1;
9883
}
9984

100-
if (flash_unlock() != HAL_OK) {
85+
if (HAL_FLASH_Unlock() != HAL_OK) {
10186
return -1;
10287
}
10388

89+
core_util_critical_section_enter();
90+
10491
/* Note: If an erase operation in Flash memory also concerns data in the data or instruction cache,
10592
you have to make sure that these data are rewritten before they are accessed during code
10693
execution. If this cannot be done safely, it is recommended to flush the caches by setting the
@@ -124,7 +111,11 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data,
124111
}
125112
}
126113

127-
flash_lock();
114+
core_util_critical_section_exit();
115+
116+
if (HAL_FLASH_Lock() != HAL_OK) {
117+
return -1;
118+
}
128119

129120
return status;
130121
}

targets/TARGET_STM/TARGET_STM32F3/flash_api.c

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,6 @@
3030
// Minimum number of bytes to be programmed at a time
3131
#define MIN_PROG_SIZE (4U)
3232

33-
static int32_t flash_unlock(void)
34-
{
35-
/* Allow Access to Flash control registers and user Flash */
36-
if (HAL_FLASH_Unlock()) {
37-
return -1;
38-
} else {
39-
return 0;
40-
}
41-
}
42-
43-
static int32_t flash_lock(void)
44-
{
45-
/* Disable the Flash option control register access (recommended to protect
46-
the option Bytes against possible unwanted operations) */
47-
if (HAL_FLASH_Lock()) {
48-
return -1;
49-
} else {
50-
return 0;
51-
}
52-
}
53-
5433
int32_t flash_init(flash_t *obj)
5534
{
5635
return 0;
@@ -71,10 +50,12 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address)
7150
return -1;
7251
}
7352

74-
if (flash_unlock() != HAL_OK) {
53+
if (HAL_FLASH_Unlock() != HAL_OK) {
7554
return -1;
7655
}
7756

57+
core_util_critical_section_enter();
58+
7859
// Clear Flash status register's flags
7960
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGERR);
8061

@@ -93,7 +74,11 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address)
9374
status = -1;
9475
}
9576

96-
flash_lock();
77+
core_util_critical_section_exit();
78+
79+
if (HAL_FLASH_Lock() != HAL_OK) {
80+
return -1;
81+
}
9782

9883
return status;
9984
}
@@ -111,10 +96,12 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data,
11196
return -1;
11297
}
11398

114-
if (flash_unlock() != HAL_OK) {
99+
if (HAL_FLASH_Unlock() != HAL_OK) {
115100
return -1;
116101
}
117102

103+
core_util_critical_section_enter();
104+
118105
/* Program the user Flash area word by word */
119106
StartAddress = address;
120107

@@ -145,7 +132,11 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data,
145132
}
146133
}
147134

148-
flash_lock();
135+
core_util_critical_section_exit();
136+
137+
if (HAL_FLASH_Lock() != HAL_OK) {
138+
return -1;
139+
}
149140

150141
return status;
151142
}

targets/TARGET_STM/TARGET_STM32F4/flash_api.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address)
6565

6666
core_util_critical_section_exit();
6767

68-
HAL_FLASH_Lock();
68+
if (HAL_FLASH_Lock() != HAL_OK) {
69+
return -1;
70+
}
6971

7072
return status;
7173
}
@@ -83,6 +85,7 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data,
8385
}
8486

8587
core_util_critical_section_enter();
88+
8689
/* Note: If an erase operation in Flash memory also concerns data in the data or instruction cache,
8790
you have to make sure that these data are rewritten before they are accessed during code
8891
execution. If this cannot be done safely, it is recommended to flush the caches by setting the
@@ -108,7 +111,9 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data,
108111

109112
core_util_critical_section_exit();
110113

111-
HAL_FLASH_Lock();
114+
if (HAL_FLASH_Lock() != HAL_OK) {
115+
return -1;
116+
}
112117

113118
return status;
114119
}

0 commit comments

Comments
 (0)