Skip to content

Commit 324e53b

Browse files
Merge pull request #5697 from bcostm/dev_flash_f3
STM32: Add support of Flash API for STM32F3 devices
2 parents b12d0fb + 8203a8d commit 324e53b

File tree

3 files changed

+184
-2
lines changed

3 files changed

+184
-2
lines changed

targets/TARGET_STM/TARGET_STM32F3/common_objects.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,13 @@ struct can_s {
132132
};
133133
#endif
134134

135+
#if DEVICE_FLASH
136+
struct flash_s {
137+
/* nothing to be stored for now */
138+
uint32_t dummy;
139+
};
140+
#endif
141+
135142
#include "gpio_object.h"
136143

137144
#ifdef __cplusplus
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2017 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "flash_api.h"
18+
#include "mbed_critical.h"
19+
20+
#if DEVICE_FLASH
21+
#include "mbed_assert.h"
22+
#include "cmsis.h"
23+
24+
#ifndef FLASH_SIZE
25+
#define FLASH_SIZE (uint32_t)(*((uint16_t *)FLASHSIZE_BASE) * 1024U)
26+
#endif
27+
28+
// Minimum number of bytes to be programmed at a time
29+
#define MIN_PROG_SIZE (4U)
30+
31+
static int32_t flash_unlock(void)
32+
{
33+
/* Allow Access to Flash control registers and user Flash */
34+
if (HAL_FLASH_Unlock()) {
35+
return -1;
36+
} else {
37+
return 0;
38+
}
39+
}
40+
41+
static int32_t flash_lock(void)
42+
{
43+
/* Disable the Flash option control register access (recommended to protect
44+
the option Bytes against possible unwanted operations) */
45+
if (HAL_FLASH_Lock()) {
46+
return -1;
47+
} else {
48+
return 0;
49+
}
50+
}
51+
52+
int32_t flash_init(flash_t *obj)
53+
{
54+
return 0;
55+
}
56+
57+
int32_t flash_free(flash_t *obj)
58+
{
59+
return 0;
60+
}
61+
62+
int32_t flash_erase_sector(flash_t *obj, uint32_t address)
63+
{
64+
uint32_t PAGEError = 0;
65+
FLASH_EraseInitTypeDef EraseInitStruct;
66+
int32_t status = 0;
67+
68+
if (!(IS_FLASH_PROGRAM_ADDRESS(address))) {
69+
return -1;
70+
}
71+
72+
if (flash_unlock() != HAL_OK) {
73+
return -1;
74+
}
75+
76+
// Clear Flash status register's flags
77+
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGERR);
78+
79+
/* MBED HAL erases 1 sector at a time */
80+
/* Fill EraseInit structure*/
81+
EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
82+
EraseInitStruct.PageAddress = address;
83+
EraseInitStruct.NbPages = 1;
84+
85+
/* Note: If an erase operation in Flash memory also concerns data in the data or instruction cache,
86+
you have to make sure that these data are rewritten before they are accessed during code
87+
execution. If this cannot be done safely, it is recommended to flush the caches by setting the
88+
DCRST and ICRST bits in the FLASH_CR register. */
89+
90+
if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK) {
91+
status = -1;
92+
}
93+
94+
flash_lock();
95+
96+
return status;
97+
}
98+
99+
int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, uint32_t size)
100+
{
101+
uint32_t StartAddress = 0;
102+
int32_t status = 0;
103+
104+
if (!(IS_FLASH_PROGRAM_ADDRESS(address))) {
105+
return -1;
106+
}
107+
108+
if ((size % MIN_PROG_SIZE) != 0) {
109+
return -1;
110+
}
111+
112+
if (flash_unlock() != HAL_OK) {
113+
return -1;
114+
}
115+
116+
/* Program the user Flash area word by word */
117+
StartAddress = address;
118+
119+
/* HW needs an aligned address to program flash, which data parameter doesn't ensure */
120+
if ((uint32_t) data % 4 != 0) {
121+
122+
volatile uint32_t data32;
123+
while (address < (StartAddress + size) && (status == 0)) {
124+
for (uint8_t i = 0; i < MIN_PROG_SIZE; i++) {
125+
*(((uint8_t *) &data32) + i) = *(data + i);
126+
}
127+
128+
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, address, data32) == HAL_OK) {
129+
address = address + MIN_PROG_SIZE;
130+
data = data + MIN_PROG_SIZE;
131+
} else {
132+
status = -1;
133+
}
134+
}
135+
} else { /* case where data is aligned, so let's avoid any copy */
136+
while ((address < (StartAddress + size)) && (status == 0)) {
137+
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, address, *((uint32_t*) data)) == HAL_OK) {
138+
address = address + MIN_PROG_SIZE;
139+
data = data + MIN_PROG_SIZE;
140+
} else {
141+
status = -1;
142+
}
143+
}
144+
}
145+
146+
flash_lock();
147+
148+
return status;
149+
}
150+
151+
uint32_t flash_get_sector_size(const flash_t *obj, uint32_t address)
152+
{
153+
if (!(IS_FLASH_PROGRAM_ADDRESS(address))) {
154+
return MBED_FLASH_INVALID_SIZE;
155+
} else {
156+
return FLASH_PAGE_SIZE;
157+
}
158+
}
159+
160+
uint32_t flash_get_page_size(const flash_t *obj)
161+
{
162+
return MIN_PROG_SIZE;
163+
}
164+
165+
uint32_t flash_get_start_address(const flash_t *obj)
166+
{
167+
return FLASH_BASE;
168+
}
169+
170+
uint32_t flash_get_size(const flash_t *obj)
171+
{
172+
return FLASH_SIZE;
173+
}
174+
175+
#endif

targets/targets.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -959,7 +959,7 @@
959959
}
960960
},
961961
"detect_code": ["0745"],
962-
"device_has_add": ["ANALOGOUT", "CAN", "LOWPOWERTIMER", "SERIAL_ASYNCH", "SERIAL_FC"],
962+
"device_has_add": ["ANALOGOUT", "CAN", "LOWPOWERTIMER", "SERIAL_ASYNCH", "SERIAL_FC", "FLASH"],
963963
"release_versions": ["2", "5"],
964964
"device_name": "STM32F303RE"
965965
},
@@ -976,7 +976,7 @@
976976
}
977977
},
978978
"detect_code": ["0747"],
979-
"device_has_add": ["ANALOGOUT", "CAN", "LOWPOWERTIMER"],
979+
"device_has_add": ["ANALOGOUT", "CAN", "LOWPOWERTIMER", "FLASH"],
980980
"release_versions": ["2", "5"],
981981
"device_name": "STM32F303ZE"
982982
},

0 commit comments

Comments
 (0)