Created
March 18, 2016 16:08
-
-
Save hexagon5un/c88f505e53f9b8ae64ff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import re | |
import shutil | |
import glob | |
## List of the STM32 chips supported by OpenCM3 | |
chips = ["f0", "f1", "f3", "f4", "f2", "l0", "l1"] | |
## Representative projects for each chip | |
## These really only vary in their linker files and peripherals | |
## Look at the examples (below) for specifics | |
projects = ["stm32f0-discovery", "stm32vl-discovery", "stm32f3-discovery", | |
"stm32f4-discovery", "jobygps", "stm32l0538-disco", "stm32l-discovery"] | |
template_directory_name = "stm32%s_libopencm3_template" | |
## This selects the sub-project that you'd like the template to be based on | |
## Note: not all chips/boards support all sub-projects | |
example_code = "miniblink" | |
# example_code = "usart" | |
## Other choices: usart, (TODO) | |
## If you're doing this yourself, git-clone and change this directory | |
## It's easier than pulling each file individually | |
examples_dir = "/home/elliot/arm/stm32/opencm3/libopencm3-examples/examples" | |
stm32_dir = os.path.join(examples_dir, "stm32") | |
## Utility function -- allows inline search and replace | |
def change_matches(filename, regex_search_string, replacement, keep_backup=False): | |
tempfile = filename + ".bak" | |
shutil.copy(filename, tempfile) | |
out = open(filename, "w") | |
for line in open(tempfile).readlines(): | |
line = re.sub(regex_search_string, replacement, line) | |
out.write(line) | |
os.unlink(tempfile) | |
out.close() | |
## Create one folder for each chip family -- from the generic project | |
for c, p in zip(chips, projects): | |
subproject_name = template_directory_name % c | |
os.mkdir(subproject_name) | |
os.chdir(subproject_name) | |
os.mkdir("support") | |
## Copy over all possible linker scripts | |
linker_search_path = os.path.join(stm32_dir, c, "*", "*.ld") | |
for linker in glob.glob(linker_search_path): | |
shutil.copy(linker, "support") | |
## Copy over custom linker scripts | |
## (@_@) TODO! | |
## Remove "include " lines from original target-specific makefile | |
## and copy to suport directory | |
makefile_path = os.path.join(stm32_dir, c, "Makefile.include") | |
destfile_path = os.path.join("support", "libopencm3.target.mk") | |
shutil.copy(makefile_path, destfile_path) | |
change_matches(destfile_path, "include .*?Makefile.rules", "" ) | |
## Copy over main opencm3 makefile rules -- without changes | |
makefile_path = os.path.join(examples_dir, "Makefile.rules") | |
destfile_path = os.path.join("support", "libopencm3.rules.mk") | |
shutil.copy(makefile_path, destfile_path) | |
## Copy over example base code | |
example_dir = os.path.join(stm32_dir, c, p, example_code) | |
files = glob.glob(example_dir + "/*") | |
for f in files: | |
shutil.copy(f, ".") | |
## add new includes to Makefile | |
out = open("Makefile", "a") | |
out.write("OPENCM3_DIR = libopencm3\n\n") | |
out.write("## Include makefile extensions from OpenCM3 project -- note: order matters\n") | |
out.write("include support/libopencm3.target.mk\n") | |
out.write("include support/libopencm3.rules.mk\n") | |
out.close() | |
## Delete line pointing to old location of makefile includes | |
change_matches("Makefile", r"include.*?Makefile.include", "") | |
## LD script is now in support subdirectory | |
change_matches("Makefile", r"LDSCRIPT = ../", "LDSCRIPT = support/") | |
## README! | |
os.system("cp ../README.md .") | |
## Tempy handling of opencm3 library -- eventually needs to be | |
## clone/checkout | |
## os.symlink("/home/elliot/arm/stm32/opencm3/libopencm3","libopencm3") | |
os.system("git init .") | |
os.system("git add .") | |
os.system('git ci -m"initial commit"') | |
os.system("git submodule add https://github.com/libopencm3/libopencm3.git") | |
os.system('git ci -m"added libopencm3 submodule"') | |
os.system("curl -u 'hexagon5un' https://api.github.com/user/repos -d '{\"name\":\"%s\", \"description\":\"The simplest possible working OpenCM3 application for the STM32%s chips.\"}'"% (subproject_name, c)) | |
os.system('git remote add origin https://github.com/hexagon5un/%s.git' % subproject_name) | |
os.system('git push -u origin master') | |
## Need to do | |
## git submodule init && git submodule update | |
## cd libopencm3 && make && cd .. | |
## before anything will work... | |
## Ideally, this should be linked in to your own library, though | |
## And do a minimal test: | |
if False: | |
print "######### %s ##########" % subproject_name | |
os.system("make clean") | |
os.system("make") | |
os.system("arm-none-eabi-size *.elf") | |
print "\n\n" | |
## And deploy | |
os.chdir("..") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment