Skip to content

Commit 70f3252

Browse files
committed
Check in algo generation code
Check in scripts which are able to generate flash algos for supported targets. To initially download all packs the following command should be run: "python extract.py --rebuild_all" After that all supported targets can be rebuilt by running: "python extract.py" Finally, to rebuild an individual target you can used its pack name: "python extract.py --target STM32F302R8"
1 parent 75f6f2d commit 70f3252

File tree

3 files changed

+522
-0
lines changed

3 files changed

+522
-0
lines changed

tools/flash_algo/c_blob_mbed.tmpl

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 "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+
{{algo.format_algo_data(4, 8, "c")}}
26+
};
27+
28+
static const flash_algo_t flash_algo_config = {
29+
.init = {{'0x%x' % algo.symbols['Init']}},
30+
.uninit = {{'0x%x' % algo.symbols['UnInit']}},
31+
.erase_sector = {{'0x%x' % algo.symbols['EraseSector']}},
32+
.program_page = {{'0x%x' % algo.symbols['ProgramPage']}},
33+
.static_base = {{'0x%x' % algo.rw_start}},
34+
.algo_blob = FLASH_ALGO
35+
};
36+
37+
static const sector_info_t sectors_info[] = {
38+
{%- for start, size in algo.sector_sizes %}
39+
{{ "{0x%x, 0x%x}" % (start + algo.flash_start, size) }},
40+
{%- endfor %}
41+
};
42+
43+
static const flash_target_config_t flash_target_config = {
44+
.page_size = {{'0x%x' % algo.page_size}},
45+
.flash_start = {{'0x%x' % algo.flash_start}},
46+
.flash_size = {{'0x%x' % algo.flash_size}},
47+
.sectors = sectors_info,
48+
.sector_info_count = sizeof(sectors_info) / sizeof(sector_info_t)
49+
};
50+
51+
void flash_set_target_config(flash_t *obj)
52+
{
53+
obj->flash_algo = &flash_algo_config;
54+
obj->target_config = &flash_target_config;
55+
}
56+

tools/flash_algo/extract.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#!/usr/bin/env python
2+
"""
3+
mbed
4+
Copyright (c) 2017-2017 ARM Limited
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
"""
18+
19+
from __future__ import print_function
20+
import sys
21+
import os
22+
import argparse
23+
from os.path import join, abspath, dirname
24+
from flash_algo import PackFlashAlgo
25+
26+
# Be sure that the tools directory is in the search path
27+
ROOT = abspath(join(dirname(__file__), "..", ".."))
28+
sys.path.insert(0, ROOT)
29+
30+
from tools.targets import TARGETS
31+
from tools.arm_pack_manager import Cache
32+
33+
TEMPLATE_PATH = "c_blob_mbed.tmpl"
34+
35+
36+
def main():
37+
"""Generate flash algorithms"""
38+
parser = argparse.ArgumentParser(description='Flash generator')
39+
parser.add_argument("--rebuild_all", action="store_true",
40+
help="Rebuild entire cache")
41+
parser.add_argument("--rebuild_descriptors", action="store_true",
42+
help="Rebuild descriptors")
43+
parser.add_argument("--target", default=None,
44+
help="Name of target to generate algo for")
45+
parser.add_argument("--all", action="store_true",
46+
help="Build all flash algos for devcies")
47+
args = parser.parse_args()
48+
49+
cache = Cache(True, True)
50+
if args.rebuild_all:
51+
cache.cache_everything()
52+
print("Cache rebuilt")
53+
return
54+
55+
if args.rebuild_descriptors:
56+
cache.cache_descriptors()
57+
print("Descriptors rebuilt")
58+
return
59+
60+
if args.target is None:
61+
device_and_filenames = [(target.device_name, target.name) for target
62+
in TARGETS if hasattr(target, "device_name")]
63+
else:
64+
device_and_filenames = [(args.target, args.target.replace("/", "-"))]
65+
66+
try:
67+
os.mkdir("output")
68+
except OSError:
69+
# Directory already exists
70+
pass
71+
72+
for device, filename in device_and_filenames:
73+
dev = cache.index[device]
74+
binaries = cache.get_flash_algorthim_binary(device, all=True)
75+
algos = [PackFlashAlgo(binary.read()) for binary in binaries]
76+
filtered_algos = algos if args.all else filter_algos(dev, algos)
77+
for idx, algo in enumerate(filtered_algos):
78+
file_name = ("%s_%i.c" % (filename, idx)
79+
if args.all or len(filtered_algos) != 1
80+
else "%s.c" % filename)
81+
output_path = join("output", file_name)
82+
algo.process_template(TEMPLATE_PATH, output_path)
83+
print("%s: %s \r" % (device, filename))
84+
85+
86+
def filter_algos(dev, algos):
87+
if "memory" not in dev:
88+
return algos
89+
if "IROM1" not in dev["memory"]:
90+
return algos
91+
if "IROM2" in dev["memory"]:
92+
return algos
93+
94+
rom_rgn = dev["memory"]["IROM1"]
95+
try:
96+
start = int(rom_rgn["start"], 0)
97+
size = int(rom_rgn["size"], 0)
98+
except ValueError:
99+
return algos
100+
101+
matching_algos = [algo for algo in algos if
102+
algo.flash_start == start and algo.flash_size == size]
103+
return matching_algos if len(matching_algos) == 1 else algos
104+
105+
106+
if __name__ == '__main__':
107+
main()

0 commit comments

Comments
 (0)