Guest User

stm32cubemx.py

a guest
May 31st, 2019
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.34 KB | None | 0 0
  1. # Copyright 2014-present PlatformIO <[email protected]>
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. #    http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14.  
  15. """
  16. STM32Cube HAL
  17.  
  18. STM32Cube embedded software libraries, including:
  19. The HAL hardware abstraction layer, enabling portability between different STM32 devices via standardized API calls
  20. The Low-Layer (LL) APIs, a light-weight, optimized, expert oriented set of APIs designed for both performance and runtime efficiency.
  21.  
  22. http://www.st.com/en/embedded-software/stm32cube-embedded-software.html?querycriteria=productId=LN1897
  23. """
  24.  
  25. from glob import glob
  26. from os import listdir
  27. from os.path import basename, isdir, isfile, join
  28. from shutil import copy
  29. from string import Template
  30. import sys
  31.  
  32. from SCons.Script import DefaultEnvironment
  33.  
  34. from platformio import util
  35. from platformio.builder.tools.piolib import PlatformIOLibBuilder
  36.  
  37. env = DefaultEnvironment()
  38. platform = env.PioPlatform()
  39.  
  40. FRAMEWORK_DIR = platform.get_package_dir("framework-stm32cubemx")
  41. assert isdir(FRAMEWORK_DIR)
  42.  
  43. FRAMEWORK_CORE = env.BoardConfig().get("build.mcu")[5:7].lower()
  44. MCU_FAMILY = env.BoardConfig().get("build.mcu")[0:7]
  45.  
  46. PROJECT_PATH = util.get_project_dir()
  47.  
  48. STARTUP_FILE_EXCEPTIONS = {
  49.     "stm32f030f4": "startup_stm32f030x6.s",
  50.     "stm32f103c8": "startup_stm32f103xb.s",
  51.     "stm32f103r8": "startup_stm32f103xb.s",
  52.     "stm32f103rc": "startup_stm32f103xb.s",
  53.     "stm32f103t8": "startup_stm32f103xb.s",
  54.     "stm32f103vc": "startup_stm32f103xe.s",
  55.     "stm32f103vd": "startup_stm32f103xe.s",
  56.     "stm32f103ve": "startup_stm32f103xe.s",
  57.     "stm32f103zc": "startup_stm32f103xe.s",
  58.     "stm32f103zd": "startup_stm32f103xe.s",
  59.     "stm32f303cb": "startup_stm32f303xc.s",
  60.     "stm32f407ve": "startup_stm32f407xx.s"
  61. }
  62.  
  63.  
  64. class CustomLibBuilder(PlatformIOLibBuilder):
  65.  
  66.     PARSE_SRC_BY_H_NAME = False
  67.  
  68.     # Max depth of nested includes:
  69.     # -1 = unlimited
  70.     # 0 - disabled nesting
  71.     # >0 - number of allowed nested includes
  72.     CCONDITIONAL_SCANNER_DEPTH = 0
  73.  
  74.     # For cases when sources located not only in "src" dir
  75.     @property
  76.     def src_dir(self):
  77.         return self.path
  78.  
  79.  
  80. def get_startup_file(mcu):
  81.     if len(mcu) > 12:
  82.         mcu = mcu[:-2]
  83.  
  84.     search_path = join(
  85.         FRAMEWORK_DIR, FRAMEWORK_CORE, "Drivers", "CMSIS", "Device",
  86.         "ST", mcu[0:7].upper() + "xx", "Source", "Templates", "gcc"
  87.     )
  88.  
  89.     search_path = join(
  90.         search_path,
  91.         "startup_" + mcu[0:9] + "[" + mcu[9] + "|x]" +
  92.         "[" + mcu[10] + "|x]" + ".[sS]"
  93.     )
  94.  
  95.     if mcu in STARTUP_FILE_EXCEPTIONS:
  96.         return STARTUP_FILE_EXCEPTIONS[mcu]
  97.  
  98.     startup_file = glob(search_path)
  99.  
  100.     if not startup_file:
  101.         sys.stderr.write(
  102.             """Error: There is no default startup file for %s MCU!
  103.            Please add initialization code to your project manually!""" % mcu)
  104.         env.Exit(1)
  105.     print("Startup file found at: " + basename(startup_file[0]))
  106.     return basename(startup_file[0])
  107.  
  108.  
  109. def get_linker_script(mcu):
  110.     ldscript = join(FRAMEWORK_DIR, "platformio",
  111.                     "ldscripts", mcu[0:11].upper() + "_FLASH.ld")
  112.  
  113.     if isfile(ldscript):
  114.         print("Linker Script found at:" + ldscript)
  115.         return ldscript
  116.  
  117.     default_ldscript = join(FRAMEWORK_DIR, "platformio",
  118.                             "ldscripts", mcu[0:11].upper() + "_DEFAULT.ld")
  119.  
  120.     print("Warning! Cannot find a linker script for the required board! "
  121.           "Firmware will be linked with a default linker script!")
  122.  
  123.     if isfile(default_ldscript):
  124.         return default_ldscript
  125.  
  126.     ram = env.BoardConfig().get("upload.maximum_ram_size", 0)
  127.     flash = env.BoardConfig().get("upload.maximum_size", 0)
  128.     template_file = join(FRAMEWORK_DIR, "platformio",
  129.                          "ldscripts", "tpl", "linker.tpl")
  130.     content = ""
  131.     with open(template_file) as fp:
  132.         data = Template(fp.read())
  133.         content = data.substitute(
  134.             stack=hex(0x20000000 + ram),  # 0x20000000 - start address for RAM
  135.             ram=str(int(ram/1024)) + "K",
  136.             flash=str(int(flash/1024)) + "K"
  137.         )
  138.  
  139.     with open(default_ldscript, "w") as fp:
  140.         fp.write(content)
  141.  
  142.     return default_ldscript
  143.  
  144.  
  145. def generate_hal_config_file(mcu):
  146.     target_path = join(PROJECT_PATH, "Drivers",
  147.                        MCU_FAMILY.upper() + "xx_HAL_Driver", "Inc")
  148.  
  149.     if not isfile(join("Inc", MCU_FAMILY + "xx_hal_conf.h")):
  150.         sys.stderr.write(
  151.             "Error: Cannot find hal_conf file to configure framework!\n")
  152.         env.Exit(1)
  153.  
  154.     copy(join("Inc", MCU_FAMILY + "xx_hal_conf.h"),
  155.          join(target_path, MCU_FAMILY + "xx_hal_conf.h"))
  156.  
  157.  
  158. env.Replace(
  159.     AS="$CC",
  160.     ASCOM="$ASPPCOM",
  161.     LDSCRIPT_PATH=env.subst(
  162.         get_linker_script(env.BoardConfig().get("build.mcu")))
  163. )
  164.  
  165. env.Append(
  166.     ASFLAGS=["-x", "assembler-with-cpp"],
  167.  
  168.     CCFLAGS=[
  169.         "-Os",  # optimize for size
  170.         "-ffunction-sections",  # place each function in its own section
  171.         "-fdata-sections",
  172.         "-Wall",
  173.         "-mthumb",
  174.         "-mcpu=%s" % env.BoardConfig().get("build.cpu"),
  175.         "-nostdlib"
  176.     ],
  177.  
  178.     CPPDEFINES=[
  179.         "USE_HAL_DRIVER",
  180.         ("F_CPU", "$BOARD_F_CPU")
  181.     ],
  182.  
  183.     CXXFLAGS=[
  184.         "-fno-rtti",
  185.         "-fno-exceptions"
  186.     ],
  187.  
  188.     LINKFLAGS=[
  189.         "-Os",
  190.         "-Wl,--gc-sections,--relax",
  191.         "-mthumb",
  192.         "-mcpu=%s" % env.BoardConfig().get("build.cpu"),
  193.         "--specs=nano.specs",
  194.         "--specs=nosys.specs"
  195.     ],
  196.  
  197.     LIBS=["c", "gcc", "m", "stdc++", "nosys"]
  198. )
  199.  
  200. # copy CCFLAGS to ASFLAGS (-x assembler-with-cpp mode)
  201. env.Append(ASFLAGS=env.get("CCFLAGS", [])[:])
  202.  
  203. cpp_flags = env.Flatten(env.get("CPPDEFINES", []))
  204.  
  205. if "F103xC" in cpp_flags:
  206.     env.Append(CPPDEFINES=["STM32F103xE"])
  207. elif "F103x8" in cpp_flags:
  208.     env.Append(CPPDEFINES=["STM32F103xB"])
  209.  
  210.  
  211. env.Append(
  212.     CPPPATH=[
  213.         join(PROJECT_PATH, "Drivers", "CMSIS", "Include"),
  214.         join(PROJECT_PATH, "Drivers", "CMSIS", "Device",
  215.              "ST", MCU_FAMILY.upper() + "xx", "Include"),
  216.  
  217.         join(PROJECT_PATH, "Drivers",
  218.              MCU_FAMILY.upper() + "xx_HAL_Driver", "Inc"),
  219.         join(PROJECT_PATH, "Drivers",
  220.              "BSP", "Components", "Common")
  221.     ],
  222.  
  223.     LIBPATH=[
  224.         join(PROJECT_PATH, "Drivers", "CMSIS", "Lib", "GCC"),
  225.         join(FRAMEWORK_DIR, "platformio", "ldscripts")
  226.     ]
  227. )
  228.  
  229. variants_remap = util.load_json(
  230.     join(FRAMEWORK_DIR, "platformio", "variants_remap.json"))
  231. board_type = env.subst("$BOARD")
  232. variant = variants_remap[
  233.     board_type] if board_type in variants_remap else board_type.upper()
  234.  
  235. #
  236. # Generate framework specific files
  237. #
  238.  
  239. generate_hal_config_file(env.BoardConfig().get("build.mcu"))
  240.  
  241. #
  242. # Process BSP components
  243. #
  244.  
  245. components_dir = join(
  246.     FRAMEWORK_DIR, FRAMEWORK_CORE, "Drivers", "BSP", "Components")
  247. for component in listdir(components_dir):
  248.     env.Append(EXTRA_LIB_BUILDERS=[
  249.         CustomLibBuilder(
  250.             env, join(components_dir, component),
  251.             {"name": "BSP-%s" % component}
  252.         )
  253.     ])
  254.  
  255.  
  256. #
  257. # Target: Build HAL Library
  258. #
  259.  
  260. libs = []
  261.  
  262. bsp_dir = join(FRAMEWORK_DIR, FRAMEWORK_CORE, "Drivers", "BSP", variant)
  263. if isdir(bsp_dir):
  264.     libs.append(env.BuildLibrary(join("$BUILD_DIR", "FrameworkBSP"), bsp_dir))
  265.     env.Append(CPPPATH=[bsp_dir])
  266.  
  267. libs.append(env.BuildLibrary(
  268.     join("$BUILD_DIR", "FrameworkHALDriver"),
  269.     join(PROJECT_PATH, "Drivers",
  270.          MCU_FAMILY.upper() + "xx_HAL_Driver"),
  271.     src_filter="+<*> -<Src/*_template.c> -<Src/Legacy>"
  272. ))
  273.  
  274. libs.append(env.BuildLibrary(
  275.     join("$BUILD_DIR", "FrameworkCMSISDevice"),
  276.     join(FRAMEWORK_DIR, FRAMEWORK_CORE, "Drivers", "CMSIS", "Device", "ST",
  277.          MCU_FAMILY.upper() + "xx", "Source", "Templates"),
  278.     src_filter="-<*> +<*.c> +<gcc/%s>" % get_startup_file(
  279.         env.BoardConfig().get("build.mcu"))
  280. ))
  281.  
  282.  
  283. env.Append(LIBS=libs)
Advertisement
Add Comment
Please, Sign In to add comment