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-old-style-declaration", 126 "-Wno-maybe-uninitialized", 127 "-Wno-undef", 128 "-Wno-implicit-function-declaration", 129 ] 130 defines = [ 131 "USE_HAL_DRIVER", 132 files.product_define, 133 "STM32CUBE_HEADER=\"${files.family}.h\"", 134 "__ARMCC_VERSION=0", # workaround for bug at stm32l552xx.h:1303 135 ] 136 visibility = [ ":*" ] 137 } 138 139 config("public_include_paths") { 140 include_dirs = files.include_dirs 141 include_dirs += [ "public" ] 142 visibility = [ ":*" ] 143 } 144 145 # Only libraries that implement parts of the stm32cube hal should depend on 146 # this. If you just want to depend on the hal, depend on stm32cube directly. 147 pw_source_set("stm32cube_headers") { 148 public_configs = [ 149 ":flags", 150 ":public_include_paths", 151 ] 152 public = [ 153 "public/stm32cube/init.h", 154 "public/stm32cube/stm32cube.h", 155 ] 156 public += files.headers 157 public_deps = [ pw_third_party_stm32cube_CONFIG ] 158 visibility = [ ":*" ] 159 if (pw_third_party_stm32cube_CORE_INIT != "") { 160 visibility += [ pw_third_party_stm32cube_CORE_INIT ] 161 } 162 } 163 164 pw_source_set("stm32cube") { 165 public_deps = [ ":stm32cube_headers" ] 166 sources = files.sources 167 deps = [ 168 pw_third_party_stm32cube_CMSIS_INIT, 169 pw_third_party_stm32cube_TIMEBASE, 170 ] 171 if (pw_third_party_stm32cube_CORE_INIT != "") { 172 deps += [ pw_third_party_stm32cube_CORE_INIT ] 173 } 174 } 175} 176