Skip to content

Commit dfd0cf6

Browse files
committed
Replase CMSIS flash driver by SDK flash driver
1 parent 567dbf7 commit dfd0cf6

File tree

4 files changed

+340
-63
lines changed

4 files changed

+340
-63
lines changed
Lines changed: 262 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
11
/* mbed Microcontroller Library
2-
* Copyright (c) 2017 ARM Limited
2+
*******************************************************************************
3+
* Copyright (c) 2017, STMicroelectronics
4+
* All rights reserved.
35
*
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
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions are met:
78
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
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.
917
*
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.
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+
*******************************************************************************
1529
*/
1630

1731
#include "flash_api.h"
@@ -22,57 +36,245 @@
2236

2337
#if DEVICE_FLASH
2438

25-
// This is a flash algo binary blob. It is PIC (position independent code) that should be stored in RAM
26-
static uint32_t FLASH_ALGO[] = {
27-
0xf3c04601, 0x28203007, 0x2204bf24, 0x1050eb02, 0x2810d205, 0x2203bf26, 0x1010eb02, 0xf4110880,
28-
0xbf181f80, 0x0010f040, 0x48714770, 0x6001496f, 0x60014970, 0x68014870, 0x01f0f041, 0x486f6001,
29-
0xf0106800, 0xd1080f20, 0xf245486d, 0x60015155, 0x60412106, 0x71fff640, 0x20006081, 0x49694770,
30-
0xf4206808, 0x600a52f8, 0x48676008, 0xf0416801, 0x60014100, 0x47702000, 0xc18cf8df, 0x0000f8dc,
31-
0x0004f040, 0x0000f8cc, 0x0000f8dc, 0x4000f440, 0x0000f8cc, 0x0000f8dc, 0x3080f440, 0x0000f8cc,
32-
0x0004f1ac, 0xf4116801, 0xbf1c3f80, 0x21aaf64a, 0xd0044a53, 0x68036011, 0x3f80f413, 0xf8dcd1fa,
33-
0xf0200000, 0xf8cc0004, 0xf8dc0000, 0xf4200000, 0xf8cc4000, 0x20000000, 0xf3c04770, 0x29203107,
34-
0x2204bf24, 0x1151eb02, 0x2910d205, 0x2203bf26, 0x1111eb02, 0xf4100889, 0xbf181f80, 0x0110f041,
35-
0x6802483d, 0x02f0f042, 0xf1006002, 0x22020c04, 0x2000f8cc, 0x2000f8dc, 0xea0323f8, 0x431101c1,
36-
0x1000f8cc, 0x1000f8dc, 0x3180f441, 0x1000f8cc, 0xf4116801, 0xbf1c3f80, 0x21aaf64a, 0xd0044a30,
37-
0x68036011, 0x3f80f413, 0xf8dcd1fa, 0xf0211000, 0xf8cc0102, 0x68011000, 0x0ff0f011, 0x2000bf04,
38-
0x68014770, 0x01f0f041, 0x20016001, 0x4b224770, 0x1cc9b430, 0xc000f8d3, 0x0103f031, 0x0cf0f04c,
39-
0xc000f8c3, 0x0404f103, 0x0c00f04f, 0xc000f8c4, 0xf240bf18, 0xd0252501, 0xc000f8d4, 0x0c05ea4c,
40-
0xc000f8c4, 0xc000f8d2, 0xc000f8c0, 0xc000f8d3, 0x3f80f41c, 0xf8d4d1fa, 0xf02cc000, 0xf8c40c01,
41-
0xf8d3c000, 0xf01cc000, 0xd0060ff0, 0xf0406818, 0x601800f0, 0x2001bc30, 0x1d004770, 0xf1021f09,
42-
0xd1d90204, 0x2000bc30, 0x00004770, 0x45670123, 0x40023c04, 0xcdef89ab, 0x40023c0c, 0x40023c14,
43-
0x40003000, 0x40023c00, 0x40023c10, 0x00000000
44-
};
45-
46-
static const flash_algo_t flash_algo_config = {
47-
.init = 0x2b,
48-
.uninit = 0x5f,
49-
.erase_sector = 0xdb,
50-
.program_page = 0x16f,
51-
.static_base = 0x20c,
52-
.algo_blob = FLASH_ALGO
53-
};
54-
55-
static const sector_info_t sectors_info[] = {
56-
{0x8000000, 0x4000},
57-
{0x8010000, 0x10000},
58-
{0x8020000, 0x20000},
59-
{0x8100000, 0x4000},
60-
{0x8110000, 0x10000},
61-
{0x8120000, 0x20000},
62-
};
63-
64-
static const flash_target_config_t flash_target_config = {
65-
.page_size = 0x400,
66-
.flash_start = 0x8000000,
67-
.flash_size = 0x200000,
68-
.sectors = sectors_info,
69-
.sector_info_count = sizeof(sectors_info) / sizeof(sector_info_t)
70-
};
71-
72-
void flash_set_target_config(flash_t *obj)
39+
#if defined (STM32F429xx) || defined (STM32F439xx)
40+
#define FLASH_SIZE (uint32_t) 0x200000
41+
#endif
42+
43+
static uint32_t GetSector(uint32_t Address);
44+
static uint32_t GetSectorSize(uint32_t Sector);
45+
46+
int32_t flash_init(flash_t *obj)
47+
{
48+
/* Allow Access to Flash control registers and user Falsh */
49+
if (HAL_FLASH_Unlock()) {
50+
return -1;
51+
} else {
52+
return 0;
53+
}
54+
}
55+
int32_t flash_free(flash_t *obj)
56+
{
57+
/* Disable the Flash option control register access (recommended to protect
58+
the option Bytes against possible unwanted operations) */
59+
if (HAL_FLASH_Lock()) {
60+
return -1;
61+
} else {
62+
return 0;
63+
}
64+
}
65+
int32_t flash_erase_sector(flash_t *obj, uint32_t address)
66+
{
67+
/*Variable used for Erase procedure*/
68+
static FLASH_EraseInitTypeDef EraseInitStruct;
69+
uint32_t FirstSector;
70+
uint32_t SectorError = 0;
71+
72+
if ((address >= (FLASH_BASE + FLASH_SIZE)) || (address < FLASH_BASE)) {
73+
74+
return -1;
75+
}
76+
77+
/* Get the 1st sector to erase */
78+
FirstSector = GetSector(address);
79+
80+
/* Fill EraseInit structure*/
81+
EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
82+
EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
83+
EraseInitStruct.Sector = FirstSector;
84+
EraseInitStruct.NbSectors = 1;
85+
if(HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK){
86+
return -1;
87+
} else {
88+
return 0;
89+
}
90+
}
91+
92+
int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, uint32_t size)
93+
{
94+
95+
if ((address >= (FLASH_BASE + FLASH_SIZE)) || (address < FLASH_BASE)) {
96+
return -1;
97+
}
98+
99+
/* Note: If an erase operation in Flash memory also concerns data in the data or instruction cache,
100+
you have to make sure that these data are rewritten before they are accessed during code
101+
execution. If this cannot be done safely, it is recommended to flush the caches by setting the
102+
DCRST and ICRST bits in the FLASH_CR register. */
103+
__HAL_FLASH_DATA_CACHE_DISABLE();
104+
__HAL_FLASH_INSTRUCTION_CACHE_DISABLE();
105+
106+
__HAL_FLASH_DATA_CACHE_RESET();
107+
__HAL_FLASH_INSTRUCTION_CACHE_RESET();
108+
109+
__HAL_FLASH_INSTRUCTION_CACHE_ENABLE();
110+
__HAL_FLASH_DATA_CACHE_ENABLE();
111+
112+
while (size > 0) {
113+
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, address, (uint64_t)*data) != HAL_OK) {
114+
return -1;
115+
} else {
116+
size--; address++;data++;
117+
}
118+
}
119+
return 0;
120+
}
121+
122+
uint32_t flash_get_sector_size(const flash_t *obj, uint32_t address)
123+
{
124+
125+
if ((address >= (FLASH_BASE + FLASH_SIZE)) || (address < FLASH_BASE)) {
126+
return MBED_FLASH_INVALID_SIZE;
127+
}
128+
129+
return (GetSectorSize(GetSector(address)));
130+
}
131+
132+
uint32_t flash_get_page_size(const flash_t *obj)
133+
{
134+
// not applicable for STM32F4
135+
return (0x4000); // minimum sector size
136+
}
137+
uint32_t flash_get_start_address(const flash_t *obj)
138+
{
139+
return FLASH_BASE;
140+
}
141+
uint32_t flash_get_size(const flash_t *obj)
142+
{
143+
return FLASH_SIZE;
144+
}
145+
146+
/**
147+
* @brief Gets the sector of a given address
148+
* @param None
149+
* @retval The sector of a given address
150+
*/
151+
static uint32_t GetSector(uint32_t address)
152+
{
153+
uint32_t sector = 0;
154+
155+
if((address < ADDR_FLASH_SECTOR_1) && (address >= ADDR_FLASH_SECTOR_0))
156+
{
157+
sector = FLASH_SECTOR_0;
158+
}
159+
else if((address < ADDR_FLASH_SECTOR_2) && (address >= ADDR_FLASH_SECTOR_1))
160+
{
161+
sector = FLASH_SECTOR_1;
162+
}
163+
else if((address < ADDR_FLASH_SECTOR_3) && (address >= ADDR_FLASH_SECTOR_2))
164+
{
165+
sector = FLASH_SECTOR_2;
166+
}
167+
else if((address < ADDR_FLASH_SECTOR_4) && (address >= ADDR_FLASH_SECTOR_3))
168+
{
169+
sector = FLASH_SECTOR_3;
170+
}
171+
else if((address < ADDR_FLASH_SECTOR_5) && (address >= ADDR_FLASH_SECTOR_4))
172+
{
173+
sector = FLASH_SECTOR_4;
174+
}
175+
else if((address < ADDR_FLASH_SECTOR_6) && (address >= ADDR_FLASH_SECTOR_5))
176+
{
177+
sector = FLASH_SECTOR_5;
178+
}
179+
else if((address < ADDR_FLASH_SECTOR_7) && (address >= ADDR_FLASH_SECTOR_6))
180+
{
181+
sector = FLASH_SECTOR_6;
182+
}
183+
else if((address < ADDR_FLASH_SECTOR_8) && (address >= ADDR_FLASH_SECTOR_7))
184+
{
185+
sector = FLASH_SECTOR_7;
186+
}
187+
else if((address < ADDR_FLASH_SECTOR_9) && (address >= ADDR_FLASH_SECTOR_8))
188+
{
189+
sector = FLASH_SECTOR_8;
190+
}
191+
else if((address < ADDR_FLASH_SECTOR_10) && (address >= ADDR_FLASH_SECTOR_9))
192+
{
193+
sector = FLASH_SECTOR_9;
194+
}
195+
else if((address < ADDR_FLASH_SECTOR_11) && (address >= ADDR_FLASH_SECTOR_10))
196+
{
197+
sector = FLASH_SECTOR_10;
198+
}
199+
else if((address < ADDR_FLASH_SECTOR_12) && (address >= ADDR_FLASH_SECTOR_11))
200+
{
201+
sector = FLASH_SECTOR_11;
202+
}
203+
else if((address < ADDR_FLASH_SECTOR_13) && (address >= ADDR_FLASH_SECTOR_12))
204+
{
205+
sector = FLASH_SECTOR_12;
206+
}
207+
else if((address < ADDR_FLASH_SECTOR_14) && (address >= ADDR_FLASH_SECTOR_13))
208+
{
209+
sector = FLASH_SECTOR_13;
210+
}
211+
else if((address < ADDR_FLASH_SECTOR_15) && (address >= ADDR_FLASH_SECTOR_14))
212+
{
213+
sector = FLASH_SECTOR_14;
214+
}
215+
else if((address < ADDR_FLASH_SECTOR_16) && (address >= ADDR_FLASH_SECTOR_15))
216+
{
217+
sector = FLASH_SECTOR_15;
218+
}
219+
else if((address < ADDR_FLASH_SECTOR_17) && (address >= ADDR_FLASH_SECTOR_16))
220+
{
221+
sector = FLASH_SECTOR_16;
222+
}
223+
else if((address < ADDR_FLASH_SECTOR_18) && (address >= ADDR_FLASH_SECTOR_17))
224+
{
225+
sector = FLASH_SECTOR_17;
226+
}
227+
else if((address < ADDR_FLASH_SECTOR_19) && (address >= ADDR_FLASH_SECTOR_18))
228+
{
229+
sector = FLASH_SECTOR_18;
230+
}
231+
else if((address < ADDR_FLASH_SECTOR_20) && (address >= ADDR_FLASH_SECTOR_19))
232+
{
233+
sector = FLASH_SECTOR_19;
234+
}
235+
else if((address < ADDR_FLASH_SECTOR_21) && (address >= ADDR_FLASH_SECTOR_20))
236+
{
237+
sector = FLASH_SECTOR_20;
238+
}
239+
else if((address < ADDR_FLASH_SECTOR_22) && (address >= ADDR_FLASH_SECTOR_21))
240+
{
241+
sector = FLASH_SECTOR_21;
242+
}
243+
else if((address < ADDR_FLASH_SECTOR_23) && (address >= ADDR_FLASH_SECTOR_22))
244+
{
245+
sector = FLASH_SECTOR_22;
246+
}
247+
else/*(address < FLASH_END_ADDR) && (address >= ADDR_FLASH_SECTOR_23))*/
248+
{
249+
sector = FLASH_SECTOR_23;
250+
}
251+
252+
return sector;
253+
}
254+
255+
/**
256+
* @brief Gets sector Size
257+
* @param None
258+
* @retval The size of a given sector
259+
*/
260+
static uint32_t GetSectorSize(uint32_t Sector)
73261
{
74-
obj->flash_algo = &flash_algo_config;
75-
obj->target_config = &flash_target_config;
262+
uint32_t sectorsize = 0x00;
263+
if((Sector == FLASH_SECTOR_0) || (Sector == FLASH_SECTOR_1) || (Sector == FLASH_SECTOR_2) ||\
264+
(Sector == FLASH_SECTOR_3) || (Sector == FLASH_SECTOR_12) || (Sector == FLASH_SECTOR_13) ||\
265+
(Sector == FLASH_SECTOR_14) || (Sector == FLASH_SECTOR_15))
266+
{
267+
sectorsize = 16 * 1024;
268+
}
269+
else if((Sector == FLASH_SECTOR_4) || (Sector == FLASH_SECTOR_16))
270+
{
271+
sectorsize = 64 * 1024;
272+
}
273+
else
274+
{
275+
sectorsize = 128 * 1024;
276+
}
277+
return sectorsize;
76278
}
77279

78280
#endif

0 commit comments

Comments
 (0)