lm3s6965evb-qemu: move early_boot into targets/*

Updates targets/lm3s6965evb-qemu/* and its sys_io counterpart to
match targets/stm32f429i-disc1/* by moving the early boot and
vector tables out of sys_io.

Change-Id: If1f82d5bd242807d9c4914d207b7f332291de8cf
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/16700
Commit-Queue: Ewout van Bekkum <ewout@google.com>
Reviewed-by: Armando Montanez <amontanez@google.com>
Reviewed-by: Keir Mierle <keir@google.com>
diff --git a/pw_sys_io_baremetal_lm3s6965evb/BUILD b/pw_sys_io_baremetal_lm3s6965evb/BUILD
index 78d4b76..85fe01a 100644
--- a/pw_sys_io_baremetal_lm3s6965evb/BUILD
+++ b/pw_sys_io_baremetal_lm3s6965evb/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_lm3s6965evb",
-    srcs = [
-        "early_boot.c",
-        "sys_io_baremetal.cc",
-    ],
+    hdrs = ["public/pw_sys_io_baremetal_lm3s6965evb/init.h"],
+    srcs = ["sys_io_baremetal.cc"],
+		deps = [
+        "//pw_preprocessor",
+				"//pw_sys_io",
+		],
 )
+
diff --git a/pw_sys_io_baremetal_lm3s6965evb/BUILD.gn b/pw_sys_io_baremetal_lm3s6965evb/BUILD.gn
index b6cec70..c64c73f 100644
--- a/pw_sys_io_baremetal_lm3s6965evb/BUILD.gn
+++ b/pw_sys_io_baremetal_lm3s6965evb/BUILD.gn
@@ -17,15 +17,18 @@
 
 import("$dir_pw_build/target_types.gni")
 import("$dir_pw_docgen/docs.gni")
+config("default_config") {
+  include_dirs = [ "public" ]
+}
+
 pw_source_set("pw_sys_io_baremetal_lm3s6965evb") {
+  public_configs = [ ":default_config" ]
+  public = [ "public/pw_sys_io_baremetal_lm3s6965evb/init.h" ]
   public_deps = [ "$dir_pw_boot_armv7m" ]
   deps = [
     "$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" ]
 }
diff --git a/pw_sys_io_baremetal_lm3s6965evb/early_boot.c b/pw_sys_io_baremetal_lm3s6965evb/public/pw_sys_io_baremetal_lm3s6965evb/init.h
similarity index 83%
rename from pw_sys_io_baremetal_lm3s6965evb/early_boot.c
rename to pw_sys_io_baremetal_lm3s6965evb/public/pw_sys_io_baremetal_lm3s6965evb/init.h
index c64e0e4..352a73f 100644
--- a/pw_sys_io_baremetal_lm3s6965evb/early_boot.c
+++ b/pw_sys_io_baremetal_lm3s6965evb/public/pw_sys_io_baremetal_lm3s6965evb/init.h
@@ -11,8 +11,7 @@
 // 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
 
-#include "pw_boot_armv7m/boot.h"
-
-void pw_boot_PreStaticMemoryInit() {}
-void pw_boot_PreStaticConstructorInit() {}
+// The actual implement of PreMainInit() in sys_io_BACKEND.
+void pw_sys_io_Init();
diff --git a/pw_sys_io_baremetal_lm3s6965evb/sys_io_baremetal.cc b/pw_sys_io_baremetal_lm3s6965evb/sys_io_baremetal.cc
index ec45eed..253b0c5 100644
--- a/pw_sys_io_baremetal_lm3s6965evb/sys_io_baremetal.cc
+++ b/pw_sys_io_baremetal_lm3s6965evb/sys_io_baremetal.cc
@@ -14,7 +14,6 @@
 
 #include <cinttypes>
 
-#include "pw_boot_armv7m/boot.h"
 #include "pw_preprocessor/compiler.h"
 #include "pw_sys_io/sys_io.h"
 
@@ -62,12 +61,6 @@
 constexpr uint32_t kRcgcUart0EnableMask = 0x1;
 volatile uint32_t& rcgc1 = *reinterpret_cast<volatile uint32_t*>(0x400FE104U);
 
-constexpr uint32_t kRccDefault = 0x078E3AD1U;
-volatile uint32_t& rcc = *reinterpret_cast<volatile uint32_t*>(0x400FE070U);
-
-constexpr uint32_t kRcc2Default = 0x07802810U;
-volatile uint32_t& rcc2 = *reinterpret_cast<volatile uint32_t*>(0x400FE070U);
-
 // Calculate a baud rate multiplier such that we have 16 bits of precision for
 // the integer portion and 6 bits for the fractional portion.
 void SetBaudRate(uint32_t clock, uint32_t target_baud) {
@@ -77,53 +70,9 @@
   uart0.fractional_baud = (((remainder << 7) / divisor + 1) >> 1) & 0x3f;
 }
 
-// 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 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_boot_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_boot_Entry,
-
-    // NMI handler.
-    [2] = DefaultFaultHandler,
-    // HardFault handler.
-    [3] = DefaultFaultHandler,
-};
-
 }  // namespace
 
-extern "C" void pw_boot_PreMainInit() {
-  // Force RCC to be at default at boot.
-  rcc = kRccDefault;
-  rcc2 = kRcc2Default;
-
+extern "C" void pw_sys_io_Init() {
   rcgc1 |= kRcgcUart0EnableMask;
   for (volatile int i = 0; i < 3; ++i) {
     // We must wait after enabling uart.
diff --git a/pw_sys_io_baremetal_stm32f429/sys_io_baremetal.cc b/pw_sys_io_baremetal_stm32f429/sys_io_baremetal.cc
index 74531ef..46f5e40 100644
--- a/pw_sys_io_baremetal_stm32f429/sys_io_baremetal.cc
+++ b/pw_sys_io_baremetal_stm32f429/sys_io_baremetal.cc
@@ -14,7 +14,6 @@
 
 #include <cinttypes>
 
-#include "pw_boot_armv7m/boot.h"
 #include "pw_preprocessor/compiler.h"
 #include "pw_sys_io/sys_io.h"
 
diff --git a/targets/lm3s6965evb-qemu/BUILD b/targets/lm3s6965evb-qemu/BUILD
new file mode 100644
index 0000000..3d66397
--- /dev/null
+++ b/targets/lm3s6965evb-qemu/BUILD
@@ -0,0 +1,35 @@
+# 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 = "pre_init",
+    srcs = [
+        "early_boot.c",
+        "vector_table.cc"
+    ],
+    deps = [
+        "//pw_boot_armv7m",
+        "//pw_preprocessor",
+        "//pw_sys_io_baremetal_lm3s6965evb",
+    ],
+)
diff --git a/targets/lm3s6965evb-qemu/BUILD.gn b/targets/lm3s6965evb-qemu/BUILD.gn
index a326df0..9903972 100644
--- a/targets/lm3s6965evb-qemu/BUILD.gn
+++ b/targets/lm3s6965evb-qemu/BUILD.gn
@@ -15,6 +15,7 @@
 # gn-format disable
 import("//build_overrides/pigweed.gni")
 
+import("$dir_pw_build/target_types.gni")
 import("$dir_pw_docgen/docs.gni")
 import("$dir_pw_toolchain/generate_toolchain.gni")
 import("target_toolchains.gni")
@@ -22,6 +23,20 @@
   toolchains = pw_target_toolchain_lm3s6965evb_qemu_list
 }
 
+if (current_toolchain != default_toolchain) {
+  pw_source_set("pre_init") {
+    public_deps = [
+      "$dir_pw_boot_armv7m",
+      "$dir_pw_sys_io_baremetal_lm3s6965evb",
+    ]
+    deps = [ "$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_lm3s6965evb/early_boot.c b/targets/lm3s6965evb-qemu/early_boot.c
similarity index 60%
copy from pw_sys_io_baremetal_lm3s6965evb/early_boot.c
copy to targets/lm3s6965evb-qemu/early_boot.c
index c64e0e4..27bf869 100644
--- a/pw_sys_io_baremetal_lm3s6965evb/early_boot.c
+++ b/targets/lm3s6965evb-qemu/early_boot.c
@@ -13,6 +13,18 @@
 // the License.
 
 #include "pw_boot_armv7m/boot.h"
+#include "pw_sys_io_baremetal_lm3s6965evb/init.h"
 
-void pw_boot_PreStaticMemoryInit() {}
+void pw_boot_PreStaticMemoryInit() {
+  // Force RCC to be at default at boot.
+  const uint32_t kRccDefault = 0x078E3AD1U;
+  volatile uint32_t* rcc = (volatile uint32_t*)0x400FE070U;
+  *rcc = kRccDefault;
+  const uint32_t kRcc2Default = 0x07802810U;
+  volatile uint32_t* rcc2 = (volatile uint32_t*)0x400FE070U;
+  *rcc2 = kRcc2Default;
+}
+
 void pw_boot_PreStaticConstructorInit() {}
+
+void pw_boot_PreMainInit() { pw_sys_io_Init(); }
diff --git a/targets/lm3s6965evb-qemu/lm3s6965evb_executable.gni b/targets/lm3s6965evb-qemu/lm3s6965evb_executable.gni
index 7576fce..347aff5 100644
--- a/targets/lm3s6965evb-qemu/lm3s6965evb_executable.gni
+++ b/targets/lm3s6965evb-qemu/lm3s6965evb_executable.gni
@@ -22,6 +22,6 @@
     if (!defined(deps)) {
       deps = []
     }
-    deps += [ dir_pw_sys_io_baremetal_lm3s6965evb ]
+    deps += [ "$dir_pigweed/targets/lm3s6965evb-qemu:pre_init" ]
   }
 }
diff --git a/targets/lm3s6965evb-qemu/vector_table.cc b/targets/lm3s6965evb-qemu/vector_table.cc
new file mode 100644
index 0000000..575a673
--- /dev/null
+++ b/targets/lm3s6965evb-qemu/vector_table.cc
@@ -0,0 +1,59 @@
+// 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_boot_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_boot_Entry,
+
+    // NMI handler.
+    [2] = DefaultFaultHandler,
+    // HardFault handler.
+    [3] = DefaultFaultHandler,
+};
+
+}  // namespace
diff --git a/targets/stm32f429i-disc1/BUILD b/targets/stm32f429i-disc1/BUILD
index a3b72d7..8be6916 100644
--- a/targets/stm32f429i-disc1/BUILD
+++ b/targets/stm32f429i-disc1/BUILD
@@ -22,7 +22,7 @@
 licenses(["notice"])  # Apache License 2.0
 
 pw_cc_library(
-    name = "pw_pre_init",
+    name = "pre_init",
     srcs = [
         "early_boot.c",
         "vector_table.cc"
diff --git a/targets/stm32f429i-disc1/BUILD.gn b/targets/stm32f429i-disc1/BUILD.gn
index 29d81b2..03169f7 100644
--- a/targets/stm32f429i-disc1/BUILD.gn
+++ b/targets/stm32f429i-disc1/BUILD.gn
@@ -31,7 +31,7 @@
 }
 
 if (current_toolchain != default_toolchain) {
-  pw_source_set("pw_pre_init") {
+  pw_source_set("pre_init") {
     configs = [ ":pw_malloc_active" ]
     public_deps = [
       "$dir_pw_boot_armv7m",
diff --git a/targets/stm32f429i-disc1/stm32f429i_executable.gni b/targets/stm32f429i-disc1/stm32f429i_executable.gni
index 55e2a4a..b7b21a5 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_pigweed/targets/stm32f429i-disc1:pw_pre_init" ]
+    deps += [ "$dir_pigweed/targets/stm32f429i-disc1: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
index 33b66f2..575a673 100644
--- a/targets/stm32f429i-disc1/vector_table.cc
+++ b/targets/stm32f429i-disc1/vector_table.cc
@@ -15,6 +15,7 @@
 #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.
@@ -54,4 +55,5 @@
     // HardFault handler.
     [3] = DefaultFaultHandler,
 };
+
 }  // namespace