Skip to content

Commit c6911e5

Browse files
committed
FLASH API UPDATE
See PR #13802 (for F4 board) Concerned boards are STM32F0 STM32F1 STM32F2 STM32F3 STM32F7 STM32G0 STM32G4 STM32H7 STM32L0 STM32L1 STM32L4 STM32L5
1 parent 6068428 commit c6911e5

File tree

12 files changed

+143
-277
lines changed

12 files changed

+143
-277
lines changed

targets/TARGET_STM/TARGET_STM32F0/flash_api.c

Lines changed: 12 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,9 @@ 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+
HAL_FLASH_Lock();
9780

9881
return status;
9982
}
@@ -111,10 +94,12 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data,
11194
return -1;
11295
}
11396

114-
if (flash_unlock() != HAL_OK) {
97+
if (HAL_FLASH_Unlock() != HAL_OK) {
11598
return -1;
11699
}
117100

101+
core_util_critical_section_enter();
102+
118103
/* Program the user Flash area word by word */
119104
StartAddress = address;
120105

@@ -145,7 +130,9 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data,
145130
}
146131
}
147132

148-
flash_lock();
133+
core_util_critical_section_exit();
134+
135+
HAL_FLASH_Lock();
149136

150137
return status;
151138
}

targets/TARGET_STM/TARGET_STM32F1/flash_api.c

Lines changed: 12 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,9 @@ 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+
HAL_FLASH_Lock();
9780

9881
return status;
9982
}
@@ -111,10 +94,12 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data,
11194
return -1;
11295
}
11396

114-
if (flash_unlock() != HAL_OK) {
97+
if (HAL_FLASH_Unlock() != HAL_OK) {
11598
return -1;
11699
}
117100

101+
core_util_critical_section_enter();
102+
118103
/* Program the user Flash area word by word */
119104
StartAddress = address;
120105

@@ -145,7 +130,9 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data,
145130
}
146131
}
147132

148-
flash_lock();
133+
core_util_critical_section_exit();
134+
135+
HAL_FLASH_Lock();
149136

150137
return status;
151138
}

targets/TARGET_STM/TARGET_STM32F2/flash_api.c

Lines changed: 12 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,9 @@ 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+
HAL_FLASH_Lock();
8871

8972
return status;
9073
}
@@ -97,10 +80,12 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data,
9780
return -1;
9881
}
9982

100-
if (flash_unlock() != HAL_OK) {
83+
if (HAL_FLASH_Unlock() != HAL_OK) {
10184
return -1;
10285
}
10386

87+
core_util_critical_section_enter();
88+
10489
/* Note: If an erase operation in Flash memory also concerns data in the data or instruction cache,
10590
you have to make sure that these data are rewritten before they are accessed during code
10691
execution. If this cannot be done safely, it is recommended to flush the caches by setting the
@@ -124,7 +109,9 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data,
124109
}
125110
}
126111

127-
flash_lock();
112+
core_util_critical_section_exit();
113+
114+
HAL_FLASH_Lock();
128115

129116
return status;
130117
}

targets/TARGET_STM/TARGET_STM32F3/flash_api.c

Lines changed: 12 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,9 @@ 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+
HAL_FLASH_Lock();
9780

9881
return status;
9982
}
@@ -111,10 +94,12 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data,
11194
return -1;
11295
}
11396

114-
if (flash_unlock() != HAL_OK) {
97+
if (HAL_FLASH_Unlock() != HAL_OK) {
11598
return -1;
11699
}
117100

101+
core_util_critical_section_enter();
102+
118103
/* Program the user Flash area word by word */
119104
StartAddress = address;
120105

@@ -145,7 +130,9 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data,
145130
}
146131
}
147132

148-
flash_lock();
133+
core_util_critical_section_exit();
134+
135+
HAL_FLASH_Lock();
149136

150137
return status;
151138
}

targets/TARGET_STM/TARGET_STM32F7/flash_api.c

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -61,27 +61,6 @@ int32_t flash_free(flash_t *obj)
6161
return 0;
6262
}
6363

64-
static int32_t flash_unlock(void)
65-
{
66-
/* Allow Access to Flash control registers and user Falsh */
67-
if (HAL_FLASH_Unlock()) {
68-
return -1;
69-
} else {
70-
return 0;
71-
}
72-
}
73-
74-
static int32_t flash_lock(void)
75-
{
76-
/* Disable the Flash option control register access (recommended to protect
77-
the option Bytes against possible unwanted operations) */
78-
if (HAL_FLASH_Lock()) {
79-
return -1;
80-
} else {
81-
return 0;
82-
}
83-
}
84-
8564
int32_t flash_erase_sector(flash_t *obj, uint32_t address)
8665
{
8766
/* Variable used for Erase procedure */
@@ -94,10 +73,12 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address)
9473
return -1;
9574
}
9675

97-
if (flash_unlock() != HAL_OK) {
76+
if (HAL_FLASH_Unlock() != HAL_OK) {
9877
return -1;
9978
}
10079

80+
core_util_critical_section_enter();
81+
10182
/* Note: If an erase operation in Flash memory also concerns data in the data or instruction cache,
10283
you have to make sure that these data are rewritten before they are accessed during code
10384
execution. If this cannot be done safely, it is recommended to flush the caches by setting the
@@ -122,7 +103,9 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address)
122103
SCB_CleanInvalidateDCache_by_Addr((uint32_t *)GetSectorBase(SectorId), GetSectorSize(SectorId));
123104
SCB_InvalidateICache();
124105

125-
flash_lock();
106+
core_util_critical_section_exit();
107+
108+
HAL_FLASH_Lock();
126109

127110
return status;
128111
}
@@ -138,16 +121,18 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data,
138121
return -1;
139122
}
140123

141-
if (flash_unlock() != HAL_OK) {
124+
if (HAL_FLASH_Unlock() != HAL_OK) {
142125
return -1;
143126
}
144127

128+
core_util_critical_section_enter();
129+
145130
/* Note: If an erase operation in Flash memory also concerns data in the data or instruction cache,
146131
you have to make sure that these data are rewritten before they are accessed during code
147132
execution. If this cannot be done safely, it is recommended to flush the caches by setting the
148133
DCRST and ICRST bits in the FLASH_CR register. */
149134
__HAL_FLASH_ART_DISABLE();
150-
__HAL_FLASH_ART_RESET();
135+
__HAL_FLASH_ART_RESET();q
151136
__HAL_FLASH_ART_ENABLE();
152137

153138
while ((size > 0) && (status == 0)) {
@@ -164,7 +149,9 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data,
164149
SCB_CleanInvalidateDCache_by_Addr((uint32_t *)StartAddress, FullSize);
165150
SCB_InvalidateICache();
166151

167-
flash_lock();
152+
core_util_critical_section_exit();
153+
154+
HAL_FLASH_Lock();
168155

169156
return status;
170157
}

0 commit comments

Comments
 (0)