stm32f429i target: early_boot.c refactor
Change-Id: Id7c03621a1bfe1fd3fdb6b6e8bfbd71703edbabd
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/14261
Reviewed-by: Alexei Frolov <frolv@google.com>
Reviewed-by: Armando Montanez <amontanez@google.com>
Commit-Queue: Chenghan Zhou <chenghanzh@google.com>
diff --git a/pw_sys_io_baremetal_stm32f429/BUILD b/pw_sys_io_baremetal_stm32f429/BUILD
index 3537740..48cbc0a 100644
--- a/pw_sys_io_baremetal_stm32f429/BUILD
+++ b/pw_sys_io_baremetal_stm32f429/BUILD
@@ -12,14 +12,22 @@
# License for the specific language governing permissions and limitations under
# the License.
+load(
+ "//pw_build:pigweed.bzl",
+ "pw_cc_library",
+)
+
package(default_visibility = ["//visibility:public"])
licenses(["notice"]) # Apache License 2.0
-filegroup(
+pw_cc_library(
name = "pw_sys_io_baremetal_stm32f429",
- srcs = [
- "early_boot.c",
- "sys_io_baremetal.cc",
- ],
+ srcs = ["sys_io_baremetal.cc"],
+ hdrs = ["public/pw_sys_io_baremetal_stm32f429/init.h"],
+ deps = [
+ "//pw_boot_armv7m",
+ "//pw_preprocessor",
+ "//pw_sys_io",
+ ]
)
diff --git a/pw_sys_io_baremetal_stm32f429/BUILD.gn b/pw_sys_io_baremetal_stm32f429/BUILD.gn
index f4a652e..9df8bbf 100644
--- a/pw_sys_io_baremetal_stm32f429/BUILD.gn
+++ b/pw_sys_io_baremetal_stm32f429/BUILD.gn
@@ -17,26 +17,20 @@
import("$dir_pw_build/target_types.gni")
import("$dir_pw_docgen/docs.gni")
-import("$dir_pw_malloc/backend.gni")
-config("pw_malloc_active") {
- if (pw_malloc_BACKEND != "") {
- defines = [ "PW_MALLOC_ACTIVE=1" ]
- }
+config("default_config") {
+ include_dirs = [ "public" ]
}
pw_source_set("pw_sys_io_baremetal_stm32f429") {
- configs = [ ":pw_malloc_active" ]
+ public_configs = [ ":default_config" ]
+ public = [ "public/pw_sys_io_baremetal_stm32f429/init.h" ]
public_deps = [ "$dir_pw_boot_armv7m" ]
deps = [
- "$dir_pw_malloc",
"$dir_pw_preprocessor",
"$dir_pw_sys_io:default_putget_bytes",
"$dir_pw_sys_io:facade",
]
- sources = [
- "early_boot.c",
- "sys_io_baremetal.cc",
- ]
+ sources = [ "sys_io_baremetal.cc" ]
}
pw_doc_group("docs") {
diff --git a/pw_sys_io_baremetal_stm32f429/public/pw_sys_io_baremetal_stm32f429/init.h b/pw_sys_io_baremetal_stm32f429/public/pw_sys_io_baremetal_stm32f429/init.h
new file mode 100644
index 0000000..352a73f
--- /dev/null
+++ b/pw_sys_io_baremetal_stm32f429/public/pw_sys_io_baremetal_stm32f429/init.h
@@ -0,0 +1,17 @@
+// Copyright 2020 The Pigweed Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+#pragma once
+
+// The actual implement of PreMainInit() in sys_io_BACKEND.
+void pw_sys_io_Init();
diff --git a/pw_sys_io_baremetal_stm32f429/sys_io_baremetal.cc b/pw_sys_io_baremetal_stm32f429/sys_io_baremetal.cc
index 3f0444b..7865693 100644
--- a/pw_sys_io_baremetal_stm32f429/sys_io_baremetal.cc
+++ b/pw_sys_io_baremetal_stm32f429/sys_io_baremetal.cc
@@ -133,49 +133,9 @@
volatile UsartBlock& usart1 =
*reinterpret_cast<volatile UsartBlock*>(kApb2PeripheralBase + 0x1000U);
-// Default handler to insert into the ARMv7-M vector table (below).
-// This function exists for convenience. If a device isn't doing what you
-// expect, it might have hit a fault and ended up here.
-void DefaultFaultHandler(void) {
- while (true) {
- // Wait for debugger to attach.
- }
-}
-
-// This is the device's interrupt vector table. It's not referenced in any
-// code because the platform (STM32F4xx) expects this table to be present at the
-// beginning of flash. The exact address is specified in the pw_boot_armv7m
-// configuration as part of the target config.
-//
-// For more information, see ARMv7-M Architecture Reference Manual DDI 0403E.b
-// section B1.5.3.
-
-// This typedef is for convenience when building the vector table. With the
-// exception of SP_main (0th entry in the vector table), all the entries of the
-// vector table are function pointers.
-typedef void (*InterruptHandler)();
-
-PW_KEEP_IN_SECTION(".vector_table")
-const InterruptHandler vector_table[] = {
- // The starting location of the stack pointer.
- // This address is NOT an interrupt handler/function pointer, it is simply
- // the address that the main stack pointer should be initialized to. The
- // value is reinterpret casted because it needs to be in the vector table.
- [0] = reinterpret_cast<InterruptHandler>(&pw_stack_high_addr),
-
- // Reset handler, dictates how to handle reset interrupt. This is the
- // address that the Program Counter (PC) is initialized to at boot.
- [1] = pw_BootEntry,
-
- // NMI handler.
- [2] = DefaultFaultHandler,
- // HardFault handler.
- [3] = DefaultFaultHandler,
-};
-
} // namespace
-extern "C" void pw_PreMainInit() {
+extern "C" void pw_sys_io_Init() {
// Enable 'A' GIPO clocks.
platform_rcc.ahb1_config |= kGpioAEnable;
diff --git a/targets/stm32f429i-disc1/BUILD b/targets/stm32f429i-disc1/BUILD
new file mode 100644
index 0000000..a3b72d7
--- /dev/null
+++ b/targets/stm32f429i-disc1/BUILD
@@ -0,0 +1,36 @@
+# Copyright 2020 The Pigweed Authors
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may not
+# use this file except in compliance with the License. You may obtain a copy of
+# the License at
+#
+# https://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations under
+# the License.
+
+load(
+ "//pw_build:pigweed.bzl",
+ "pw_cc_library",
+)
+
+package(default_visibility = ["//visibility:public"])
+
+licenses(["notice"]) # Apache License 2.0
+
+pw_cc_library(
+ name = "pw_pre_init",
+ srcs = [
+ "early_boot.c",
+ "vector_table.cc"
+ ],
+ deps = [
+ "//pw_boot_armv7m",
+ "//pw_malloc",
+ "//pw_preprocessor",
+ "//pw_sys_io_baremetal_stm32f429",
+ ],
+)
diff --git a/targets/stm32f429i-disc1/BUILD.gn b/targets/stm32f429i-disc1/BUILD.gn
index ec4b4b3..29d81b2 100644
--- a/targets/stm32f429i-disc1/BUILD.gn
+++ b/targets/stm32f429i-disc1/BUILD.gn
@@ -15,13 +15,39 @@
# gn-format disable
import("//build_overrides/pigweed.gni")
+import("$dir_pw_build/target_types.gni")
import("$dir_pw_docgen/docs.gni")
+import("$dir_pw_malloc/backend.gni")
import("$dir_pw_toolchain/generate_toolchain.gni")
import("target_toolchains.gni")
generate_toolchains("target_toolchains") {
toolchains = pw_target_toolchain_stm32f429i_disc1_list
}
+config("pw_malloc_active") {
+ if (pw_malloc_BACKEND != "") {
+ defines = [ "PW_MALLOC_ACTIVE=1" ]
+ }
+}
+
+if (current_toolchain != default_toolchain) {
+ pw_source_set("pw_pre_init") {
+ configs = [ ":pw_malloc_active" ]
+ public_deps = [
+ "$dir_pw_boot_armv7m",
+ "$dir_pw_sys_io_baremetal_stm32f429",
+ ]
+ deps = [
+ "$dir_pw_malloc",
+ "$dir_pw_preprocessor",
+ ]
+ sources = [
+ "early_boot.c",
+ "vector_table.cc",
+ ]
+ }
+}
+
pw_doc_group("target_docs") {
sources = [ "target_docs.rst" ]
}
diff --git a/pw_sys_io_baremetal_stm32f429/early_boot.c b/targets/stm32f429i-disc1/early_boot.c
similarity index 92%
rename from pw_sys_io_baremetal_stm32f429/early_boot.c
rename to targets/stm32f429i-disc1/early_boot.c
index e10a361..0345ffc 100644
--- a/pw_sys_io_baremetal_stm32f429/early_boot.c
+++ b/targets/stm32f429i-disc1/early_boot.c
@@ -12,10 +12,9 @@
// License for the specific language governing permissions and limitations under
// the License.
-#include <inttypes.h>
-
#include "pw_boot_armv7m/boot.h"
#include "pw_malloc/malloc.h"
+#include "pw_sys_io_baremetal_stm32f429/init.h"
void pw_PreStaticConstructorInit() {
// TODO(pwbug/17): Optionally enable Replace when Pigweed config system is
@@ -30,8 +29,9 @@
*arm_v7m_cpacr |= kFpuEnableMask;
#endif // PW_ARMV7M_ENABLE_FPU
-
#if PW_MALLOC_ACTIVE
pw_MallocInit();
#endif // PW_MALLOC_ACTIVE
}
+
+void pw_PreMainInit() { pw_sys_io_Init(); }
diff --git a/targets/stm32f429i-disc1/stm32f429i_executable.gni b/targets/stm32f429i-disc1/stm32f429i_executable.gni
index 5f336e2..55e2a4a 100644
--- a/targets/stm32f429i-disc1/stm32f429i_executable.gni
+++ b/targets/stm32f429i-disc1/stm32f429i_executable.gni
@@ -23,7 +23,7 @@
if (!defined(deps)) {
deps = []
}
- deps += [ dir_pw_sys_io_baremetal_stm32f429 ]
+ deps += [ "$dir_pigweed/targets/stm32f429i-disc1:pw_pre_init" ]
if (pw_malloc_BACKEND != "") {
if (!defined(configs)) {
configs = []
diff --git a/targets/stm32f429i-disc1/vector_table.cc b/targets/stm32f429i-disc1/vector_table.cc
new file mode 100644
index 0000000..d5e3065
--- /dev/null
+++ b/targets/stm32f429i-disc1/vector_table.cc
@@ -0,0 +1,57 @@
+// Copyright 2020 The Pigweed Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+#include "pw_boot_armv7m/boot.h"
+
+namespace {
+// Default handler to insert into the ARMv7-M vector table (below).
+// This function exists for convenience. If a device isn't doing what you
+// expect, it might have hit a fault and ended up here.
+void DefaultFaultHandler(void) {
+ while (true) {
+ // Wait for debugger to attach.
+ }
+}
+
+// This is the device's interrupt vector table. It's not referenced in any
+// code because the platform (STM32F4xx) expects this table to be present at the
+// beginning of flash. The exact address is specified in the pw_boot_armv7m
+// configuration as part of the target config.
+//
+// For more information, see ARMv7-M Architecture Reference Manual DDI 0403E.b
+// section B1.5.3.
+
+// This typedef is for convenience when building the vector table. With the
+// exception of SP_main (0th entry in the vector table), all the entries of the
+// vector table are function pointers.
+typedef void (*InterruptHandler)();
+
+PW_KEEP_IN_SECTION(".vector_table")
+const InterruptHandler vector_table[] = {
+ // The starting location of the stack pointer.
+ // This address is NOT an interrupt handler/function pointer, it is simply
+ // the address that the main stack pointer should be initialized to. The
+ // value is reinterpret casted because it needs to be in the vector table.
+ [0] = reinterpret_cast<InterruptHandler>(&pw_stack_high_addr),
+
+ // Reset handler, dictates how to handle reset interrupt. This is the
+ // address that the Program Counter (PC) is initialized to at boot.
+ [1] = pw_BootEntry,
+
+ // NMI handler.
+ [2] = DefaultFaultHandler,
+ // HardFault handler.
+ [3] = DefaultFaultHandler,
+};
+} // namespace