1# Copyright 2021 The Pigweed Authors 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); you may not 4# use this file except in compliance with the License. You may obtain a copy of 5# the License at 6# 7# https://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, WITHOUT 11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12# License for the specific language governing permissions and limitations under 13# the License. 14 15import("//build_overrides/pigweed.gni") 16 17import("$dir_pw_build/linker_script.gni") 18import("$dir_pw_build/target_types.gni") 19import("$dir_pw_third_party/stm32cube/stm32cube.gni") 20 21if (dir_pw_third_party_stm32cube == "") { 22 group("linker_script_template") { 23 } 24 group("core_init_template") { 25 } 26 group("cmsis_init_template") { 27 } 28 group("hal_config_template") { 29 } 30 group("stm32cube_headers") { 31 } 32 group("stm32cube") { 33 } 34} else { 35 stm32cube_builder_script = 36 "$dir_pw_stm32cube_build/py/pw_stm32cube_build/__main__.py" 37 38 rebased_dir_pw_third_party_stm32cube = 39 rebase_path(dir_pw_third_party_stm32cube) 40 41 find_files_args = [ 42 "find_files", 43 rebased_dir_pw_third_party_stm32cube, 44 pw_third_party_stm32cube_PRODUCT, 45 ] 46 if (pw_third_party_stm32cube_CORE_INIT == 47 "$dir_pw_third_party/stm32cube:core_init_template") { 48 find_files_args += [ "--init" ] 49 } 50 51 # This script finds the files relavent for the current product. 52 files = exec_script(stm32cube_builder_script, 53 find_files_args, 54 "scope", 55 [ "$rebased_dir_pw_third_party_stm32cube/files.txt" ]) 56 57 if (pw_third_party_stm32cube_CORE_INIT == 58 "$dir_pw_third_party/stm32cube:core_init_template") { 59 assert(files.gcc_linker != "" || files.iar_linker != "", 60 "No linker file found") 61 62 gcc_linker = files.gcc_linker 63 if (gcc_linker == "") { 64 gcc_linker = "$target_gen_dir/linker.ld" 65 gcc_linker_str = exec_script(stm32cube_builder_script, 66 [ 67 "icf_to_ld", 68 files.iar_linker, 69 ], 70 "string", 71 [ files.iar_linker ]) 72 write_file(gcc_linker, gcc_linker_str) 73 } 74 75 startup_file = "$target_gen_dir/startup.s" 76 startup_file_str = exec_script(stm32cube_builder_script, 77 [ 78 "inject_init", 79 files.startup, 80 ], 81 "string", 82 [ files.startup ]) 83 write_file(startup_file, startup_file_str) 84 85 pw_linker_script("linker_script_template") { 86 linker_script = gcc_linker 87 } 88 89 pw_source_set("core_init_template") { 90 deps = [ ":linker_script_template" ] 91 sources = [ startup_file ] 92 } 93 } 94 95 pw_source_set("hal_timebase_template") { 96 deps = [ ":stm32cube_headers" ] 97 sources = [ "$dir_pw_third_party_stm32cube/hal_driver/Src/${files.family}_hal_timebase_tim_template.c" ] 98 } 99 100 pw_source_set("cmsis_init_template") { 101 deps = [ ":stm32cube_headers" ] 102 sources = [ "$dir_pw_third_party_stm32cube/cmsis_device/Source/Templates/system_${files.family}.c" ] 103 } 104 105 # Generate a stub config header that points to the correct template. 106 write_file("$target_gen_dir/template_config/${files.family}_hal_conf.h", 107 "#include \"${files.family}_hal_conf_template.h\"") 108 config("hal_config_template_includes") { 109 include_dirs = [ "$target_gen_dir/template_config" ] 110 } 111 pw_source_set("hal_config_template") { 112 public_configs = [ ":hal_config_template_includes" ] 113 114 # This is to make sure GN properly detects changes to these files. The 115 # generated file shouldn't change, but the file it redirects to might. 116 public = [ "$target_gen_dir/template_config/${files.family}_hal_conf.h" ] 117 inputs = [ "$dir_pw_third_party_stm32cube/hal_driver/Inc/${files.family}_hal_conf_template.h" ] 118 } 119 120 config("flags") { 121 cflags = [ "-Wno-unused-parameter" ] 122 cflags_c = [ 123 "-Wno-redundant-decls", 124 "-Wno-sign-compare", 125 "-Wno-undef", 126 "-Wno-implicit-function-declaration", 127 "-Wno-switch-enum", 128 ] 129 if (get_path_info(pw_toolchain_SCOPE.cc, "file") == "clang") { 130 cflags += [ "-Wno-deprecated-volatile" ] 131 cflags_c += [ "-Wno-parentheses-equality" ] 132 } else { 133 cflags_c += [ 134 "-Wno-old-style-declaration", 135 "-Wno-maybe-uninitialized", 136 ] 137 cflags_cc = [ "-Wno-volatile" ] 138 } 139 140 defines = [ 141 "USE_HAL_DRIVER", 142 files.product_define, 143 "STM32CUBE_HEADER=\"${files.family}.h\"", 144 "__ARMCC_VERSION=0", # workaround for bug at stm32l552xx.h:1303 145 ] 146 visibility = [ ":*" ] 147 } 148 149 config("public_include_paths") { 150 include_dirs = files.include_dirs 151 include_dirs += [ "public" ] 152 visibility = [ ":*" ] 153 } 154 155 # Only libraries that implement parts of the stm32cube hal should depend on 156 # this. If you just want to depend on the hal, depend on stm32cube directly. 157 pw_source_set("stm32cube_headers") { 158 public_configs = [ 159 ":flags", 160 ":public_include_paths", 161 ] 162 public = [ 163 "public/stm32cube/init.h", 164 "public/stm32cube/stm32cube.h", 165 ] 166 public += files.headers 167 public_deps = [ pw_third_party_stm32cube_CONFIG ] 168 visibility = [ ":*" ] 169 if (pw_third_party_stm32cube_CORE_INIT != "") { 170 visibility += [ pw_third_party_stm32cube_CORE_INIT ] 171 } 172 } 173 174 pw_source_set("stm32cube") { 175 public_deps = [ ":stm32cube_headers" ] 176 sources = files.sources 177 deps = [ 178 pw_third_party_stm32cube_CMSIS_INIT, 179 pw_third_party_stm32cube_TIMEBASE, 180 ] 181 if (pw_third_party_stm32cube_CORE_INIT != "") { 182 deps += [ pw_third_party_stm32cube_CORE_INIT ] 183 } 184 } 185} 186