xref: /aosp_15_r20/external/pigweed/targets/rp2040/BUILD.bazel (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1# Copyright 2022 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
15load("@rules_python//sphinxdocs:sphinx_docs_library.bzl", "sphinx_docs_library")
16load("//pw_build:compatibility.bzl", "incompatible_with_mcu")
17load("//pw_build:merge_flags.bzl", "flags_from_dict")
18load("//pw_build:pw_cc_binary.bzl", "pw_cc_binary")
19load("//pw_build:pw_copy_and_patch_file.bzl", "pw_copy_and_patch_file")
20load("//pw_toolchain/cc/current_toolchain:conditions.bzl", "if_compiler_is_clang")
21load("//targets/rp2040:flash.bzl", "flash_rp2040", "flash_rp2350")
22load("//targets/rp2040:transition.bzl", "RP2_SYSTEM_FLAGS", "rp2040_binary", "rp2350_binary")
23
24package(default_visibility = ["//visibility:public"])
25
26licenses(["notice"])
27
28config_setting(
29    name = "pico_clang_build",
30    constraint_values = [
31        "//pw_build/constraints/chipset:rp2040",
32    ],
33    flag_values = {
34        "@bazel_tools//tools/cpp:compiler": "clang",
35    },
36)
37
38# This is an incomplete platform, do NOT try to pass this
39# as a --platforms flag value. Use :rp2040 or :rp2350.
40platform(
41    name = "rp2_common",
42    constraint_values = [
43        "@freertos//:disable_task_statics",
44        "freertos_config_cv",
45        "//pw_build/constraints/chipset:rp2040",  # TODO: https://pwbug.dev/343487589 - Use Pico SDK constraints.
46        "//pw_build/constraints/rtos:freertos",
47        "//pw_cpu_exception:enabled",
48        "//pw_interrupt_cortex_m:backend",
49        "@rust_crates//:no_std",
50        "@platforms//os:none",
51    ],
52    flags = flags_from_dict(RP2_SYSTEM_FLAGS),
53    visibility = ["//visibility:private"],
54)
55
56platform(
57    name = "rp2040",
58    constraint_values = [
59        "@freertos//:port_ARM_CM0",
60        "@pico-sdk//bazel/constraint:rp2040",
61        # For toolchain selection.
62        "@platforms//cpu:armv6-m",
63        "@pw_toolchain//constraints/arm_mcpu:cortex-m0plus",
64    ],
65    flags = flags_from_dict({
66        "@pico-sdk//bazel/config:PICO_DEFAULT_LINKER_SCRIPT": ":rp2040_linker_script",
67    }),
68    parents = [":rp2_common"],
69)
70
71platform(
72    name = "rp2350",
73    constraint_values = [
74        "@freertos//:port_ARM_CM33_NTZ",
75        "@pico-sdk//bazel/constraint:rp2350",
76        # For toolchain selection.
77        "@platforms//cpu:armv8-m",
78        "@pw_toolchain//constraints/arm_mcpu:cortex-m33",
79    ],
80    flags = flags_from_dict({
81        "@pico-sdk//bazel/config:PICO_DEFAULT_LINKER_SCRIPT": ":rp2350_linker_script",
82    }),
83    parents = [":rp2_common"],
84)
85
86cc_library(
87    name = "extra_platform_libs",
88    deps = [
89        ":pre_init",
90        "//pw_tokenizer:linker_script",
91        "@pico-sdk//src/rp2_common/pico_stdlib",
92    ] + if_compiler_is_clang(
93        [
94            "//pw_libcxx",
95            "@pico-sdk//src/rp2_common/pico_clib_interface:llvm_libc_interface",
96        ],
97        otherwise = [],
98    ),
99)
100
101alias(
102    name = "pico_sdk_clib_select",
103    actual = if_compiler_is_clang(
104        "@pico-sdk//src/rp2_common/pico_clib_interface:llvm_libc_interface",
105        otherwise = "@pico-sdk//src/rp2_common/pico_clib_interface:newlib_interface",
106    ),
107)
108
109cc_library(
110    name = "freertos_config",
111    hdrs = [
112        "config/FreeRTOSConfig.h",
113    ],
114    includes = ["config/"],
115    target_compatible_with = [":freertos_config_cv"],
116    deps = [
117        "//third_party/freertos:config_assert",
118        "@pico-sdk//src/rp2_common/cmsis:cmsis_core",
119    ],
120)
121
122constraint_value(
123    name = "freertos_config_cv",
124    constraint_setting = "@freertos//:freertos_config_setting",
125)
126
127cc_library(
128    name = "unit_test_app",
129    testonly = True,
130    srcs = [
131        "unit_test_app.cc",
132    ],
133    deps = [
134        "//pw_status",
135        "//pw_system",
136        "//pw_unit_test:rpc_service",
137    ],
138    alwayslink = True,
139)
140
141# This is just a stub to silence warnings saying that boot.cc and *.h files are
142# missing from the bazel build. There's no plans yet to do a Bazel build for
143# the Pi Pico.
144filegroup(
145    name = "rp2040_files",
146    srcs = [
147        "config/rp2040_hal_config.h",
148    ],
149)
150
151# This is a duplication of the contents of pigweed_module_config.h since we
152# can't do a -include because copts do not propagate to dependencies.
153cc_library(
154    name = "pigweed_module_config",
155    hdrs = ["config/pigweed_module_config.h"],
156    defines = [
157        # LINT.IfChange
158        "PW_THREAD_FREERTOS_CONFIG_JOINING_ENABLED=1",
159        "PW_ASSERT_BASIC_ACTION=PW_ASSERT_BASIC_ACTION_EXIT",
160        # LINT.ThenChange(//targets/rp2040/config/pigweed_module_config.h)
161    ],
162)
163
164cc_library(
165    name = "pre_init",
166    srcs = [
167        "boot.cc",
168    ],
169    target_compatible_with = [":freertos_config_cv"],
170    deps = [
171        ":freertos_config",
172        "//pw_assert",
173        "//pw_assert:backend_impl",
174        "//pw_cpu_exception:entry_backend_impl",
175        "//pw_string",
176        "//pw_system:init",
177        "//third_party/freertos:support",
178        "@freertos",
179        "@pico-sdk//src/rp2_common/hardware_exception:hardware_exception",
180        "@pico-sdk//src/rp2_common/pico_stdlib",
181    ],
182    alwayslink = 1,
183)
184
185cc_library(
186    name = "device_handler",
187    srcs = [
188        "device_handler.cc",
189    ],
190    strip_include_prefix = "public",
191    deps = [
192        "//pw_cpu_exception_cortex_m:snapshot",
193        "//pw_system:device_handler.facade",
194        "//pw_thread_freertos:snapshot",
195        "@pico-sdk//src/rp2_common/hardware_watchdog:hardware_watchdog",
196    ],
197)
198
199pw_cc_binary(
200    name = "system_async_example",
201    testonly = True,
202    srcs = ["system_async_example.cc"],
203    # TODO(b/365184562): This target does not build with asan and fuzztest.
204    target_compatible_with = select({
205        "//pw_fuzzer:use_fuzztest": ["@platforms//:incompatible"],
206        "//conditions:default": [],
207    }),
208    deps = [
209        "//pw_channel:rp2_stdio_channel",
210        "//pw_libcxx",
211        "//pw_multibuf:testing",
212        "//pw_system:async",
213        "//third_party/freertos:support",
214        "@freertos",
215        "@pico-sdk//src/rp2_common/pico_stdlib",
216    ],
217)
218
219rp2040_binary(
220    name = "rp2040_system_async_example",
221    testonly = True,
222    binary = ":system_async_example",
223)
224
225flash_rp2040(
226    name = "flash_rp2040_system_async_example",
227    testonly = True,
228    rp2040_binary = ":rp2040_system_async_example",
229)
230
231pw_copy_and_patch_file(
232    name = "rp2040_memmap_default",
233    src = "@pico-sdk//src/rp2_common/pico_crt0/rp2040:memmap_default.ld",
234    out = "patched_rp2040_memmap_default.ld",
235    patch_file = "patches/rp2040_memmap_default.ld.patch",
236)
237
238cc_library(
239    name = "rp2040_linker_script",
240    additional_linker_inputs = [":rp2040_memmap_default"],
241    linkopts = ["-T$(execpath :rp2040_memmap_default)"],
242    deps = [
243        "@pico-sdk//src/rp2_common/pico_standard_link:default_flash_region",
244    ],
245)
246
247rp2350_binary(
248    name = "rp2350_system_async_example",
249    testonly = True,
250    binary = ":system_async_example",
251)
252
253flash_rp2350(
254    name = "flash_rp2350_system_async_example",
255    testonly = True,
256    rp2350_binary = ":rp2350_system_async_example",
257)
258
259pw_copy_and_patch_file(
260    name = "rp2350_memmap_default",
261    src = "@pico-sdk//src/rp2_common/pico_crt0/rp2350:memmap_default.ld",
262    out = "patched_rp2350_memmap_default.ld",
263    patch_file = "patches/rp2350_memmap_default.ld.patch",
264)
265
266cc_library(
267    name = "rp2350_linker_script",
268    additional_linker_inputs = [":rp2350_memmap_default"],
269    linkopts = ["-T$(execpath :rp2350_memmap_default)"],
270    deps = [
271        "@pico-sdk//src/rp2_common/pico_standard_link:default_flash_region",
272    ],
273)
274
275sphinx_docs_library(
276    name = "docs",
277    srcs = [
278        "target_docs.rst",
279        "upstream.rst",
280    ],
281    target_compatible_with = incompatible_with_mcu(),
282)
283