blob: b97bd8721bcf5a2110abfcd2077234c7251e9a78 [file] [log] [blame]
Varun Sharma708d38c2021-04-29 09:03:12 -07001# 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")
19
20import("$dir_pw_third_party/stm32cube/stm32cube.gni")
21
22assert(dir_pw_third_party_stm32cube != "",
23 "The target must specify the stm32cube directory.")
24
25stm32cube_builder_script =
26 "$dir_pw_stm32cube_build/py/pw_stm32cube_build/__main__.py"
27
28find_files_args = [
29 "find_files",
30 dir_pw_third_party_stm32cube,
31 pw_third_party_stm32cube_PRODUCT,
32]
33if (pw_third_party_stm32cube_CORE_INIT ==
34 "$dir_pw_third_party/stm32cube:core_init_template") {
35 find_files_args += [ "--init" ]
36}
37
38# This script finds the files relavent for the current product.
39files = exec_script(stm32cube_builder_script,
40 find_files_args,
41 "scope",
42 [ "$dir_pw_third_party_stm32cube/files.txt" ])
43
44if (pw_third_party_stm32cube_CORE_INIT ==
45 "$dir_pw_third_party/stm32cube:core_init_template") {
46 assert(files.gcc_linker != "" || files.iar_linker != "",
47 "No linker file found")
48
49 gcc_linker = files.gcc_linker
50 if (gcc_linker == "") {
51 gcc_linker = "$target_gen_dir/linker.ld"
52 gcc_linker_str = exec_script(stm32cube_builder_script,
53 [
54 "icf_to_ld",
55 files.iar_linker,
56 ],
57 "string",
58 [ files.iar_linker ])
59 write_file(gcc_linker, gcc_linker_str)
60 }
61
62 startup_file = "$target_gen_dir/startup.s"
63 startup_file_str = exec_script(stm32cube_builder_script,
64 [
65 "inject_init",
66 files.startup,
67 ],
68 "string",
69 [ files.startup ])
70 write_file(startup_file, startup_file_str)
71
72 pw_linker_script("linker_script_template") {
73 linker_script = gcc_linker
74 }
75
76 pw_source_set("core_init_template") {
77 deps = [ ":linker_script_template" ]
78 sources = [ startup_file ]
79 }
80}
81
82pw_source_set("hal_timebase_template") {
83 deps = [ ":stm32cube_headers" ]
84 sources = [ "$dir_pw_third_party_stm32cube/hal_driver/Src/${files.family}_hal_timebase_tim_template.c" ]
85}
86
87pw_source_set("cmsis_init_template") {
88 deps = [ ":stm32cube_headers" ]
89 sources = [ "$dir_pw_third_party_stm32cube/cmsis_device/Source/Templates/system_${files.family}.c" ]
90}
91
92# Generate a stub config header that points to the correct template.
93write_file("$target_gen_dir/template_config/${files.family}_hal_conf.h",
94 "#include \"${files.family}_hal_conf_template.h\"")
95config("hal_config_template_includes") {
96 include_dirs = [ "$target_gen_dir/template_config" ]
97}
98pw_source_set("hal_config_template") {
99 public_configs = [ ":hal_config_template_includes" ]
100
101 # This is to make sure GN properly detects changes to these files. The
102 # generated file shouldn't change, but the file it redirects to might.
103 public = [ "$target_gen_dir/template_config/${files.family}_hal_conf.h" ]
104 inputs = [ "$dir_pw_third_party_stm32cube/hal_driver/Inc/${files.family}_hal_conf_template.h" ]
105}
106
107config("flags") {
108 cflags = [ "-Wno-unused-parameter" ]
109 cflags_c = [
110 "-Wno-redundant-decls",
111 "-Wno-sign-compare",
112 "-Wno-old-style-declaration",
113 "-Wno-maybe-uninitialized",
114 ]
115 defines = [
116 "USE_HAL_DRIVER",
117 files.product_define,
118 "STM32CUBE_HEADER=\"${files.family}.h\"",
119 "__ARMCC_VERSION=0", # workaround for bug at stm32l552xx.h:1303
120 ]
121 visibility = [ ":*" ]
122}
123
124config("public_include_paths") {
125 include_dirs = files.include_dirs
126 include_dirs += [ "public" ]
127 visibility = [ ":*" ]
128}
129
130# Only libraries that implement parts of the stm32cube hal should depend on
131# this. If you just want to depend on the hal, depend on stm32cube directly.
132pw_source_set("stm32cube_headers") {
133 public_configs = [
134 ":flags",
135 ":public_include_paths",
136 ]
137 public = [
138 "public/stm32cube/init.h",
139 "public/stm32cube/stm32cube.h",
140 ]
141 public += files.headers
142 public_deps = [ pw_third_party_stm32cube_CONFIG ]
143 visibility = [ ":*" ]
144}
145
146pw_source_set("stm32cube") {
147 public_deps = [ ":stm32cube_headers" ]
148 sources = files.sources
149 deps = [
150 pw_third_party_stm32cube_CMSIS_INIT,
151 pw_third_party_stm32cube_TIMEBASE,
152 ]
153 if (pw_third_party_stm32cube_CORE_INIT != "") {
154 deps += [ pw_third_party_stm32cube_CORE_INIT ]
155 }
156}