Skip to content

Commit a3929c3

Browse files
0xc0170c1728p9
authored andcommitted
flash: add flash algo common layer
K64F and KL46Z addition to use this common layer. This implementation uses flash algo blob that are generated via scripts. If target supports flash algo, it can get common HAL implementation, providing feature "HAL_FLASH_CMSIS_ALGO". This simplifies target code, and having one implementation that can be shared by many targets. Be careful with flash cmsis algo, in some cases they execute code that can affect the system. For instance, it can disable cache, or affect some system clocks. Therefore this feature should be well tested.
1 parent 714bff4 commit a3929c3

File tree

6 files changed

+451
-9
lines changed

6 files changed

+451
-9
lines changed
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2016 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 "flash_data.h"
19+
#include "critical.h"
20+
21+
#define MBED_FLASH_ALGO_ERASE 1UL
22+
#define MBED_FLASH_ALGO_PROGRAM 2UL
23+
24+
extern uint32_t SystemCoreClock;
25+
26+
/*
27+
This binary blob (thumb code) sets r9 (static base) as the code we are jumping to
28+
is PIC (position independent code).
29+
30+
These are the instructions (r0 is a pointer to arg_t):
31+
push {r5, lr, r4}
32+
mov r5, r9
33+
push {r5}
34+
ldr r5, [r0, #20]
35+
ldr r3, [r0, #16]
36+
mov r9, r3
37+
ldr r3, [r0, #12]
38+
ldr r2, [r0, #8]
39+
ldr r1, [r0, #4]
40+
ldr r0, [r0, #0]
41+
blx r5
42+
pop {r5}
43+
mov r9, r5
44+
pop {r4-r5, pc}
45+
bx r14
46+
*/
47+
static uint32_t jump_to_flash_algo[] = {
48+
0x464DB530,
49+
0x6945B420,
50+
0x46996903,
51+
0x688268C3,
52+
0x68006841,
53+
0xBC2047A8,
54+
0xBD3046A9
55+
};
56+
57+
static uint32_t get_sector_index(flash_t *obj, uint32_t address)
58+
{
59+
// check where address belongs to
60+
size_t sector_index = 0;
61+
size_t sectors_count = sizeof(obj->target_config->sectors) / sizeof(sector_info_t);
62+
for (; sector_index < sectors_count; sector_index++) {
63+
if ((address > obj->target_config->sectors[sector_index].start) &&
64+
(address < (obj->target_config->sectors[sector_index].start +obj->target_config->sectors[sector_index].size))) {
65+
break;
66+
}
67+
}
68+
return sector_index;
69+
}
70+
71+
// should be called within critical section
72+
static int32_t flash_algo_init(flash_t *obj, uint32_t address, uint32_t function)
73+
{
74+
args_t arguments = {
75+
.r0 = address,
76+
.r1 = SystemCoreClock,
77+
.r2 = function,
78+
.r3 = 0,
79+
.r9 = (uint32_t)obj->flash_algo->algo_blob + obj->flash_algo->static_base,
80+
.pc = (uint32_t)obj->flash_algo->algo_blob + obj->flash_algo->init
81+
};
82+
return ((flash_algo_jump_t)(((uint32_t)&jump_to_flash_algo) | 1))(&arguments);
83+
}
84+
85+
// should be called within critical section
86+
static int32_t flash_algo_uninit(flash_t *obj, uint32_t address, uint32_t function)
87+
{
88+
args_t arguments = {
89+
.r0 = address,
90+
.r1 = SystemCoreClock,
91+
.r2 = function,
92+
.r3 = 0,
93+
.r9 = (uint32_t)obj->flash_algo->algo_blob + obj->flash_algo->static_base,
94+
.pc = (uint32_t)obj->flash_algo->algo_blob + obj->flash_algo->uninit
95+
};
96+
return ((flash_algo_jump_t)(((uint32_t)&jump_to_flash_algo) | 1))(&arguments);
97+
}
98+
99+
100+
int32_t flash_init(flash_t *obj)
101+
{
102+
flash_set_target_config(obj);
103+
return 0;
104+
}
105+
106+
int32_t flash_free(flash_t *obj)
107+
{
108+
return 0;
109+
}
110+
111+
int32_t flash_erase_sector(flash_t *obj, uint32_t address)
112+
{
113+
size_t sector_index = get_sector_index(obj, address);
114+
115+
// erase sector boundary
116+
if ((address % obj->target_config->sectors[sector_index].size) != 0) {
117+
return -1;
118+
}
119+
120+
core_util_critical_section_enter();
121+
flash_algo_init(obj, address, MBED_FLASH_ALGO_ERASE);
122+
123+
args_t arguments = {
124+
.r0 = address,
125+
.r1 = 0,
126+
.r2 = 0,
127+
.r3 = 0,
128+
.r9 = (uint32_t)obj->flash_algo->algo_blob + obj->flash_algo->static_base,
129+
.pc = (uint32_t)obj->flash_algo->algo_blob + obj->flash_algo->erase_sector
130+
};
131+
int32_t ret = ((flash_algo_jump_t)(((uint32_t)&jump_to_flash_algo) | 1))(&arguments);
132+
133+
flash_algo_uninit(obj, address, MBED_FLASH_ALGO_ERASE);
134+
core_util_critical_section_exit();
135+
return ret ? -1 : 0;
136+
}
137+
138+
139+
int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, uint32_t size)
140+
{
141+
// write size boundary
142+
if ((address % obj->target_config->page_size) != 0) {
143+
return -1; // TODO return types
144+
}
145+
// size multiple of sectors
146+
if ((size < obj->target_config->page_size) || (size % obj->target_config->page_size != 0)) {
147+
return -1;
148+
}
149+
150+
uint32_t sector_index = get_sector_index(obj, address);
151+
// should not cross sector boundary
152+
if (((address % obj->target_config->sectors[sector_index].size) + size) > obj->target_config->sectors[sector_index].size) {
153+
return -1;
154+
}
155+
156+
core_util_critical_section_enter();
157+
flash_algo_init(obj, address, MBED_FLASH_ALGO_PROGRAM);
158+
159+
args_t arguments = {
160+
.r0 = address,
161+
.r1 = size,
162+
.r2 = (uint32_t)data,
163+
.r3 = 0,
164+
.r9 = (uint32_t)obj->flash_algo->algo_blob + obj->flash_algo->static_base,
165+
.pc = (uint32_t)obj->flash_algo->algo_blob + obj->flash_algo->program_page
166+
};
167+
int32_t ret = ((flash_algo_jump_t)(((uint32_t)&jump_to_flash_algo) | 1))(&arguments);
168+
169+
flash_algo_uninit(obj, address, MBED_FLASH_ALGO_PROGRAM);
170+
core_util_critical_section_exit();
171+
return ret ? -1 : 0;
172+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2016 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+
#ifndef MBED_FLASH_DATA_H
17+
#define MBED_FLASH_DATA_H
18+
19+
// This file is automagically generated from flash-algo repository
20+
21+
#include <stdint.h>
22+
23+
// Target flash algorithm structure
24+
typedef struct {
25+
const uint32_t init;
26+
const uint32_t uninit;
27+
const uint32_t erase_sector;
28+
const uint32_t program_page;
29+
const uint32_t static_base;
30+
uint32_t *algo_blob;
31+
} flash_algo_t;
32+
33+
typedef struct {
34+
const uint32_t start;
35+
const uint32_t size;
36+
} sector_info_t;
37+
38+
typedef struct {
39+
const uint32_t page_size;
40+
const uint32_t flash_start;
41+
const uint32_t flash_size;
42+
const sector_info_t *sectors;
43+
} flash_target_config_t;
44+
45+
// Target flash configuration
46+
struct flash_s {
47+
const flash_target_config_t *target_config;
48+
const flash_algo_t *flash_algo;
49+
};
50+
51+
typedef struct {
52+
uint32_t r0;
53+
uint32_t r1;
54+
uint32_t r2;
55+
uint32_t r3;
56+
uint32_t r9;
57+
uint32_t pc;
58+
} args_t;
59+
60+
typedef int32_t (*flash_algo_jump_t)(args_t*);
61+
62+
// prototypes for flash algo CMSIS API
63+
64+
typedef int (*CMSIS_Algo_Function_Init)(unsigned long adr, unsigned long clk, unsigned long fnc);
65+
typedef int (*CMSIS_Algo_Function_UnInit)(unsigned long fnc);
66+
typedef int (*CMSIS_Algo_Function_EraseSector)(unsigned long adr);
67+
typedef int (*CMSIS_Algo_Function_EraseChip)(void);
68+
typedef int (*CMSIS_Algo_Function_ProgramPage)(unsigned long adr, unsigned long sz, unsigned char *buf);
69+
typedef unsigned long (*CMSIS_Algo_Function_Verify)(unsigned long adr, unsigned long sz, unsigned char *buf);
70+
71+
#ifdef __cplusplus
72+
extern "C" {
73+
#endif
74+
75+
/* Set target configuration
76+
*/
77+
void flash_set_target_config(flash_t *obj);
78+
79+
#ifdef __cplusplus
80+
};
81+
#endif
82+
83+
84+
#endif

hal/flash_api.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#define MBED_FLASH_API_H
2121

2222
#include "device.h"
23-
#include "stdint.h"
23+
#include <stdint.h>
2424

2525
#if DEVICE_FLASH
2626

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2016 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 "flash_data.h"
19+
#include "critical.h"
20+
21+
// This file is automagically generated
22+
23+
// This is a flash algo binary blob. It is PIC (position independent code) that should be stored in RAM
24+
static uint32_t FLASH_ALGO[] = {
25+
0xb5104935, 0x60084449, 0x21004834, 0x48356001, 0x44484a33, 0x22016002, 0x04926041, 0x02926082,
26+
0x220560c2, 0x61420692, 0x03122201, 0x46026182, 0x70113220, 0x62411e49, 0xf939f000, 0xd0002800,
27+
0xbd102001, 0x47702000, 0xb5084a27, 0x0349447a, 0x0c0a9200, 0x48234601, 0x44482300, 0xf9c4f000,
28+
0xd0002800, 0xbd082001, 0x491fb508, 0x481d4479, 0x44483920, 0xf8a1f000, 0xd10e2800, 0x4478481a,
29+
0x38324b1a, 0x9000447b, 0x22044816, 0x44484918, 0xf967f000, 0xd0002800, 0xbd082001, 0x4b12b510,
30+
0x4601447b, 0x2201480f, 0x02923b54, 0xf0004448, 0xbd10f8b6, 0x460cb538, 0x4479490b, 0x9100396e,
31+
0x48084601, 0x46224613, 0xf0004448, 0x2800f94a, 0x2001d000, 0x0000bd38, 0x00000004, 0x40048100,
32+
0x40020000, 0x00000008, 0x000000a5, 0x0000055c, 0x0000040c, 0x4604b570, 0x25006800, 0x061b7803,
33+
0x2370d5fc, 0x20007003, 0x0003e03a, 0xfa5df000, 0x0f0b070c, 0x1f1b1713, 0x2f2b2723, 0x68263633,
34+
0x71f37813, 0x6826e02a, 0x71b37853, 0x6826e026, 0x71737893, 0x6826e022, 0x713378d3, 0x6826e01e,
35+
0x72f37913, 0x6826e01a, 0x72b37953, 0x6826e016, 0x72737993, 0x6826e012, 0x723379d3, 0x6826e00e,
36+
0x73f37a13, 0x6826e00a, 0x73b37a53, 0x6826e006, 0x73737a93, 0x6826e002, 0x73337ad3, 0xb2c01c40,
37+
0xd9c24288, 0x20806821, 0xe0037008, 0x1c416a60, 0x4780d000, 0x78006820, 0xd5f70600, 0x78006820,
38+
0xd5010681, 0xe0062504, 0xd50106c1, 0xe0022508, 0xd00007c0, 0x46282510, 0xb508bd70, 0x2244460b,
39+
0x700a4669, 0x2100466a, 0xbd084798, 0x4614b538, 0xd002078a, 0x300120ff, 0x6842bd38, 0xd803428a,
40+
0x18d36883, 0xd80d428b, 0x428b68c3, 0x6902d803, 0x428a189a, 0x2002d801, 0x2201bd38, 0x05d21ac9,
41+
0xe0001889, 0x22081a89, 0x701a466b, 0x705a0c0a, 0x709a0a0a, 0x466a70d9, 0x47a02103, 0xb5ffbd38,
42+
0x4615b081, 0x27019a01, 0x26006852, 0x02bf1948, 0xd804428a, 0x689b9b01, 0x428318d3, 0x9a01d20f,
43+
0x428a68d2, 0x9b01d804, 0x18d3691b, 0xd2014283, 0xe0292602, 0x21011a88, 0x184405c9, 0x1a8ce000,
44+
0x46204639, 0xf904f000, 0xd0022900, 0x360126ff, 0x4639e01a, 0xf0004628, 0x2900f8fb, 0x2601d012,
45+
0x2009e012, 0x70084669, 0x70480c20, 0x70880a20, 0x9b0470cc, 0x2103466a, 0x47989801, 0xd1030006,
46+
0x19e41bed, 0xd1ec2d00, 0xb0054630, 0xb5f0bdf0, 0x24006801, 0x0612780a, 0x2270d5fc, 0x6802700a,
47+
0x71d12103, 0x22806801, 0x6803718a, 0x71592100, 0x23fc6805, 0x6803712b, 0x680373d9, 0x6802701a,
48+
0x061b7813, 0x7a55d5fc, 0x07177a12, 0x0f3f2201, 0x10560412, 0xf000003b, 0x0910f968, 0x0b0d0b0b,
49+
0x0b0b0b0b, 0x090d0b0b, 0x0e090b0b, 0xe0026102, 0xe0006101, 0x072a6106, 0x00130f12, 0xf955f000,
50+
0x0c090910, 0x1815120f, 0x091f1d1b, 0x09090909, 0x61c10a09, 0xbdf04620, 0x03092101, 0x2101e7f9,
51+
0xe7f602c9, 0x02892101, 0x2101e7f3, 0xe7f00249, 0x310121ff, 0x2180e7ed, 0x2140e7eb, 0x2120e7e9,
52+
0xb5fee7e7, 0x46074616, 0x2000461d, 0x078b198a, 0x20ffd002, 0xbdfe3001, 0xd00107b3, 0xbdfe2001,
53+
0x428b687b, 0x68bcd803, 0x4294191c, 0x68fbd20d, 0xd803428b, 0x191c693c, 0xd2014294, 0xbdfe2002,
54+
0x1ac92201, 0x188c05d2, 0x1acce01b, 0x2006e019, 0x70084669, 0x70480c20, 0x70880a20, 0x78e870cc,
55+
0x78a87108, 0x78687148, 0x78287188, 0x9b0871c8, 0x2107466a, 0x47984638, 0xd1e02800, 0x1f361d24,
56+
0x2e001d2d, 0xbdfed1e3, 0x4615b5fe, 0x68424604, 0x184000a8, 0x428a461e, 0x68a3d803, 0x428b18d3,
57+
0x68e3d808, 0xd803428b, 0x19db6927, 0xd801428b, 0xbdfe2002, 0xd8054282, 0x18d368a3, 0xd3014283,
58+
0xe00a1a8f, 0x428268e2, 0x6923d903, 0x428318d3, 0x1a88d3ee, 0x05c92101, 0x21041847, 0xf0004638,
59+
0x2900f817, 0x20ffd002, 0xbdfe3001, 0x46692001, 0x0c387008, 0x0a387048, 0x70cf7088, 0x71080a28,
60+
0x718e714d, 0x466a9b08, 0x46202106, 0xbdfe4798, 0x09032200, 0xd32c428b, 0x428b0a03, 0x2300d311,
61+
0xe04e469c, 0x430b4603, 0x2200d43c, 0x428b0843, 0x0903d331, 0xd31c428b, 0x428b0a03, 0x4694d301,
62+
0x09c3e03f, 0xd301428b, 0x1ac001cb, 0x09834152, 0xd301428b, 0x1ac0018b, 0x09434152, 0xd301428b,
63+
0x1ac0014b, 0x09034152, 0xd301428b, 0x1ac0010b, 0x08c34152, 0xd301428b, 0x1ac000cb, 0x08834152,
64+
0xd301428b, 0x1ac0008b, 0x08434152, 0xd301428b, 0x1ac0004b, 0x1a414152, 0x4601d200, 0x46104152,
65+
0xe05d4770, 0xd0000fca, 0x10034249, 0x4240d300, 0x22004053, 0x0903469c, 0xd32d428b, 0x428b0a03,
66+
0x22fcd312, 0xba120189, 0x428b0a03, 0x0189d30c, 0x428b1192, 0x0189d308, 0x428b1192, 0x0189d304,
67+
0x1192d03a, 0x0989e000, 0x428b09c3, 0x01cbd301, 0x41521ac0, 0x428b0983, 0x018bd301, 0x41521ac0,
68+
0x428b0943, 0x014bd301, 0x41521ac0, 0x428b0903, 0x010bd301, 0x41521ac0, 0x428b08c3, 0x00cbd301,
69+
0x41521ac0, 0x428b0883, 0x008bd301, 0x41521ac0, 0x0843d2d9, 0xd301428b, 0x1ac0004b, 0x1a414152,
70+
0x4601d200, 0x41524663, 0x4610105b, 0x4240d301, 0xd5002b00, 0x47704249, 0x105b4663, 0x4240d300,
71+
0x2000b501, 0x46c046c0, 0xb430bd02, 0x1e644674, 0x1c647825, 0xd20042ab, 0x5d63461d, 0x18e3005b,
72+
0x4718bc30, 0xfffffffe, 0xffffffff, 0xfffffffe, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
73+
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000
74+
};
75+
76+
static const flash_algo_t flash_algo_config = {
77+
.init = 0x1,
78+
.uninit = 0x45,
79+
.erase_sector = 0x9d,
80+
.program_page = 0xb5,
81+
.static_base = 0x5f0,
82+
.algo_blob = FLASH_ALGO,
83+
};
84+
85+
static const sector_info_t sectors_info[] = {
86+
{0x0, 0x400},
87+
};
88+
89+
static const flash_target_config_t flash_target_config = {
90+
.page_size = 0x100,
91+
.flash_start = 0x0,
92+
.flash_size = 0x40000,
93+
.sectors = sectors_info,
94+
};
95+
96+
void flash_set_target_config(flash_t *obj)
97+
{
98+
obj->flash_algo = &flash_algo_config;
99+
obj->target_config = &flash_target_config;
100+
}

0 commit comments

Comments
 (0)