Skip to content

Commit 2c6403e

Browse files
authored
Merge pull request #5787 from bcostm/dev_flash_f2
STM32: Add support of Flash API for STM32F2 devices
2 parents 2d83463 + 0d3e189 commit 2c6403e

File tree

4 files changed

+280
-1
lines changed

4 files changed

+280
-1
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/* mbed Microcontroller Library
2+
*******************************************************************************
3+
* Copyright (c) 2017, STMicroelectronics
4+
* All rights reserved.
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions are met:
8+
*
9+
* 1. Redistributions of source code must retain the above copyright notice,
10+
* this list of conditions and the following disclaimer.
11+
* 2. Redistributions in binary form must reproduce the above copyright notice,
12+
* this list of conditions and the following disclaimer in the documentation
13+
* and/or other materials provided with the distribution.
14+
* 3. Neither the name of STMicroelectronics nor the names of its contributors
15+
* may be used to endorse or promote products derived from this software
16+
* without specific prior written permission.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+
*******************************************************************************
29+
*/
30+
#ifndef MBED_FLASH_DATA_H
31+
#define MBED_FLASH_DATA_H
32+
33+
#include <stdint.h>
34+
35+
#if DEVICE_FLASH
36+
37+
#define FLASH_SIZE ((uint32_t)0x100000)
38+
39+
/* Base address of the Flash sectors */
40+
#define ADDR_FLASH_SECTOR_0 ((uint32_t)0x08000000) /* Base @ of Sector 0, 16 Kbytes */
41+
#define ADDR_FLASH_SECTOR_1 ((uint32_t)0x08004000) /* Base @ of Sector 1, 16 Kbytes */
42+
#define ADDR_FLASH_SECTOR_2 ((uint32_t)0x08008000) /* Base @ of Sector 2, 16 Kbytes */
43+
#define ADDR_FLASH_SECTOR_3 ((uint32_t)0x0800C000) /* Base @ of Sector 3, 16 Kbytes */
44+
#define ADDR_FLASH_SECTOR_4 ((uint32_t)0x08010000) /* Base @ of Sector 4, 64 Kbytes */
45+
#define ADDR_FLASH_SECTOR_5 ((uint32_t)0x08020000) /* Base @ of Sector 5, 128 Kbytes */
46+
#define ADDR_FLASH_SECTOR_6 ((uint32_t)0x08040000) /* Base @ of Sector 6, 128 Kbytes */
47+
#define ADDR_FLASH_SECTOR_7 ((uint32_t)0x08060000) /* Base @ of Sector 7, 128 Kbytes */
48+
#define ADDR_FLASH_SECTOR_8 ((uint32_t)0x08080000) /* Base @ of Sector 8, 128 Kbytes */
49+
#define ADDR_FLASH_SECTOR_9 ((uint32_t)0x080A0000) /* Base @ of Sector 9, 128 Kbytes */
50+
#define ADDR_FLASH_SECTOR_10 ((uint32_t)0x080C0000) /* Base @ of Sector 10, 128 Kbytes */
51+
#define ADDR_FLASH_SECTOR_11 ((uint32_t)0x080E0000) /* Base @ of Sector 11, 128 Kbytes */
52+
53+
#endif
54+
#endif
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
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+
#if DEVICE_FLASH
18+
19+
#include "flash_api.h"
20+
#include "flash_data.h"
21+
#include "platform/mbed_critical.h"
22+
23+
static uint32_t GetSector(uint32_t Address);
24+
static uint32_t GetSectorSize(uint32_t Sector);
25+
26+
static int32_t flash_unlock(void)
27+
{
28+
/* Allow Access to Flash control registers and user Falsh */
29+
if (HAL_FLASH_Unlock()) {
30+
return -1;
31+
} else {
32+
return 0;
33+
}
34+
}
35+
36+
static int32_t flash_lock(void)
37+
{
38+
/* Disable the Flash option control register access (recommended to protect
39+
the option Bytes against possible unwanted operations) */
40+
if (HAL_FLASH_Lock()) {
41+
return -1;
42+
} else {
43+
return 0;
44+
}
45+
}
46+
47+
int32_t flash_init(flash_t *obj)
48+
{
49+
return 0;
50+
}
51+
52+
int32_t flash_free(flash_t *obj)
53+
{
54+
return 0;
55+
}
56+
57+
int32_t flash_erase_sector(flash_t *obj, uint32_t address)
58+
{
59+
static FLASH_EraseInitTypeDef EraseInitStruct;
60+
uint32_t FirstSector;
61+
uint32_t SectorError = 0;
62+
int32_t status = 0;
63+
64+
if ((address >= (FLASH_BASE + FLASH_SIZE)) || (address < FLASH_BASE)) {
65+
return -1;
66+
}
67+
68+
if (flash_unlock() != HAL_OK) {
69+
return -1;
70+
}
71+
72+
/* Get the 1st sector to erase */
73+
FirstSector = GetSector(address);
74+
75+
/* Fill EraseInit structure*/
76+
EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
77+
EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
78+
EraseInitStruct.Sector = FirstSector;
79+
EraseInitStruct.NbSectors = 1;
80+
81+
if (HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK) {
82+
status = -1;
83+
}
84+
85+
flash_lock();
86+
87+
return status;
88+
}
89+
90+
int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, uint32_t size)
91+
{
92+
int32_t status = 0;
93+
94+
if ((address >= (FLASH_BASE + FLASH_SIZE)) || (address < FLASH_BASE)) {
95+
return -1;
96+
}
97+
98+
if (flash_unlock() != HAL_OK) {
99+
return -1;
100+
}
101+
102+
/* Note: If an erase operation in Flash memory also concerns data in the data or instruction cache,
103+
you have to make sure that these data are rewritten before they are accessed during code
104+
execution. If this cannot be done safely, it is recommended to flush the caches by setting the
105+
DCRST and ICRST bits in the FLASH_CR register. */
106+
__HAL_FLASH_DATA_CACHE_DISABLE();
107+
__HAL_FLASH_INSTRUCTION_CACHE_DISABLE();
108+
109+
__HAL_FLASH_DATA_CACHE_RESET();
110+
__HAL_FLASH_INSTRUCTION_CACHE_RESET();
111+
112+
__HAL_FLASH_INSTRUCTION_CACHE_ENABLE();
113+
__HAL_FLASH_DATA_CACHE_ENABLE();
114+
115+
while ((size > 0) && (status == 0)) {
116+
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, address, (uint64_t)*data) != HAL_OK) {
117+
status = -1;
118+
} else {
119+
size--;
120+
address++;
121+
data++;
122+
}
123+
}
124+
125+
flash_lock();
126+
127+
return status;
128+
}
129+
130+
uint32_t flash_get_sector_size(const flash_t *obj, uint32_t address)
131+
{
132+
if ((address >= (FLASH_BASE + FLASH_SIZE)) || (address < FLASH_BASE)) {
133+
return MBED_FLASH_INVALID_SIZE;
134+
} else {
135+
return (GetSectorSize(GetSector(address)));
136+
}
137+
}
138+
139+
uint32_t flash_get_page_size(const flash_t *obj)
140+
{
141+
// Flash of STM32F2 devices can be programed 1 byte at a time
142+
return 1;
143+
}
144+
145+
uint32_t flash_get_start_address(const flash_t *obj)
146+
{
147+
return FLASH_BASE;
148+
}
149+
150+
uint32_t flash_get_size(const flash_t *obj)
151+
{
152+
return FLASH_SIZE;
153+
}
154+
155+
/**
156+
* @brief Gets the sector of a given address
157+
* @param None
158+
* @retval The sector of a given address
159+
*/
160+
static uint32_t GetSector(uint32_t address)
161+
{
162+
uint32_t sector = 0;
163+
uint32_t tmp = address - ADDR_FLASH_SECTOR_0;
164+
/* This function supports 1Mb and 2Mb flash sizes */
165+
#if defined(ADDR_FLASH_SECTOR_16)
166+
if (address & 0x100000) { // handle 2nd bank
167+
/* Sector will be at least 12 */
168+
sector = FLASH_SECTOR_12;
169+
tmp -= 0x100000;
170+
address -= 0x100000;
171+
}
172+
#endif
173+
if (address < ADDR_FLASH_SECTOR_4) { // 16k sectorsize
174+
sector += tmp >>14;
175+
}
176+
#if defined(ADDR_FLASH_SECTOR_5)
177+
else if (address < ADDR_FLASH_SECTOR_5) { //64k sector size
178+
sector += FLASH_SECTOR_4;
179+
} else {
180+
sector += 4 + (tmp >>17);
181+
}
182+
#else
183+
// In case ADDR_FLASH_SECTOR_5 is not defined, sector 4 is the last one.
184+
else { //64k sector size
185+
sector += FLASH_SECTOR_4;
186+
}
187+
#endif
188+
return sector;
189+
}
190+
191+
/**
192+
* @brief Gets sector Size
193+
* @param None
194+
* @retval The size of a given sector
195+
*/
196+
static uint32_t GetSectorSize(uint32_t Sector)
197+
{
198+
uint32_t sectorsize = 0x00;
199+
#if defined(FLASH_SECTOR_16)
200+
if((Sector == FLASH_SECTOR_0) || (Sector == FLASH_SECTOR_1) || (Sector == FLASH_SECTOR_2) ||\
201+
(Sector == FLASH_SECTOR_3) || (Sector == FLASH_SECTOR_12) || (Sector == FLASH_SECTOR_13) ||\
202+
(Sector == FLASH_SECTOR_14) || (Sector == FLASH_SECTOR_15)) {
203+
sectorsize = 16 * 1024;
204+
} else if((Sector == FLASH_SECTOR_4) || (Sector == FLASH_SECTOR_16)) {
205+
#else
206+
if((Sector == FLASH_SECTOR_0) || (Sector == FLASH_SECTOR_1) || (Sector == FLASH_SECTOR_2) ||\
207+
(Sector == FLASH_SECTOR_3)) {
208+
sectorsize = 16 * 1024;
209+
} else if(Sector == FLASH_SECTOR_4) {
210+
#endif
211+
sectorsize = 64 * 1024;
212+
} else {
213+
sectorsize = 128 * 1024;
214+
}
215+
return sectorsize;
216+
}
217+
218+
#endif

targets/TARGET_STM/TARGET_STM32F2/objects.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,13 @@ struct can_s {
145145
};
146146
#endif
147147

148+
#if DEVICE_FLASH
149+
struct flash_s {
150+
/* nothing to be stored for now */
151+
uint32_t dummy;
152+
};
153+
#endif
154+
148155
#define GPIO_IP_WITHOUT_BRR
149156
#include "gpio_object.h"
150157

targets/targets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,7 @@
903903
},
904904
"detect_code": ["0835"],
905905
"macros_add": ["USBHOST_OTHER"],
906-
"device_has_add": ["ANALOGOUT", "CAN", "SERIAL_ASYNCH", "SERIAL_FC"],
906+
"device_has_add": ["ANALOGOUT", "CAN", "SERIAL_ASYNCH", "SERIAL_FC", "FLASH"],
907907
"features": ["LWIP"],
908908
"release_versions": ["2", "5"],
909909
"device_name": "STM32F207ZG"

0 commit comments

Comments
 (0)