targets: move early_boot.c to boot.cc

Alters the existing targets to use a boot.cc file instead of the
existing early_boot.c.

The name was updated to reflect the fact that it now contains a
pw_boot_PostMain() hook which is after early boot.

C++ was selected over C to give more language flexibility.

Change-Id: I18ca0b8720bf083d4f644f1ca994e8fd7adc89b6
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/16702
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_boot_armv7m/BUILD.gn b/pw_boot_armv7m/BUILD.gn
index 85fb257..4fd2371 100644
--- a/pw_boot_armv7m/BUILD.gn
+++ b/pw_boot_armv7m/BUILD.gn
@@ -41,11 +41,9 @@
 
   pw_source_set("pw_boot_armv7m") {
     public_configs = [ ":default_config" ]
-    deps = [
-      ":armv7m_linker_script",
-      "$dir_pw_preprocessor",
-    ]
     public = [ "public/pw_boot_armv7m/boot.h" ]
+    public_deps = [ "$dir_pw_preprocessor" ]
+    deps = [ ":armv7m_linker_script" ]
     sources = [ "core_init.c" ]
   }
 }
diff --git a/pw_sys_io_baremetal_lm3s6965evb/BUILD.gn b/pw_sys_io_baremetal_lm3s6965evb/BUILD.gn
index c64c73f..017be93 100644
--- a/pw_sys_io_baremetal_lm3s6965evb/BUILD.gn
+++ b/pw_sys_io_baremetal_lm3s6965evb/BUILD.gn
@@ -24,9 +24,11 @@
 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 = [
+  public_deps = [
+    "$dir_pw_boot_armv7m",
     "$dir_pw_preprocessor",
+  ]
+  deps = [
     "$dir_pw_sys_io:default_putget_bytes",
     "$dir_pw_sys_io:facade",
   ]
diff --git a/pw_sys_io_baremetal_lm3s6965evb/public/pw_sys_io_baremetal_lm3s6965evb/init.h b/pw_sys_io_baremetal_lm3s6965evb/public/pw_sys_io_baremetal_lm3s6965evb/init.h
index 352a73f..d4262c5 100644
--- a/pw_sys_io_baremetal_lm3s6965evb/public/pw_sys_io_baremetal_lm3s6965evb/init.h
+++ b/pw_sys_io_baremetal_lm3s6965evb/public/pw_sys_io_baremetal_lm3s6965evb/init.h
@@ -13,5 +13,11 @@
 // the License.
 #pragma once
 
+#include "pw_preprocessor/util.h"
+
+PW_EXTERN_C_START
+
 // The actual implement of PreMainInit() in sys_io_BACKEND.
 void pw_sys_io_Init();
+
+PW_EXTERN_C_END
diff --git a/pw_sys_io_baremetal_stm32f429/BUILD.gn b/pw_sys_io_baremetal_stm32f429/BUILD.gn
index 9df8bbf..334bed0 100644
--- a/pw_sys_io_baremetal_stm32f429/BUILD.gn
+++ b/pw_sys_io_baremetal_stm32f429/BUILD.gn
@@ -24,9 +24,11 @@
 pw_source_set("pw_sys_io_baremetal_stm32f429") {
   public_configs = [ ":default_config" ]
   public = [ "public/pw_sys_io_baremetal_stm32f429/init.h" ]
-  public_deps = [ "$dir_pw_boot_armv7m" ]
-  deps = [
+  public_deps = [
+    "$dir_pw_boot_armv7m",
     "$dir_pw_preprocessor",
+  ]
+  deps = [
     "$dir_pw_sys_io:default_putget_bytes",
     "$dir_pw_sys_io:facade",
   ]
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
index 352a73f..d4262c5 100644
--- 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
@@ -13,5 +13,11 @@
 // the License.
 #pragma once
 
+#include "pw_preprocessor/util.h"
+
+PW_EXTERN_C_START
+
 // The actual implement of PreMainInit() in sys_io_BACKEND.
 void pw_sys_io_Init();
+
+PW_EXTERN_C_END
diff --git a/targets/lm3s6965evb-qemu/BUILD b/targets/lm3s6965evb-qemu/BUILD
index 3d66397..307d555 100644
--- a/targets/lm3s6965evb-qemu/BUILD
+++ b/targets/lm3s6965evb-qemu/BUILD
@@ -24,7 +24,7 @@
 pw_cc_library(
     name = "pre_init",
     srcs = [
-        "early_boot.c",
+        "boot.cc",
         "vector_table.cc"
     ],
     deps = [
diff --git a/targets/lm3s6965evb-qemu/BUILD.gn b/targets/lm3s6965evb-qemu/BUILD.gn
index 9903972..f207068 100644
--- a/targets/lm3s6965evb-qemu/BUILD.gn
+++ b/targets/lm3s6965evb-qemu/BUILD.gn
@@ -31,7 +31,7 @@
     ]
     deps = [ "$dir_pw_preprocessor" ]
     sources = [
-      "early_boot.c",
+      "boot.cc",
       "vector_table.cc",
     ]
   }
diff --git a/targets/lm3s6965evb-qemu/early_boot.c b/targets/lm3s6965evb-qemu/boot.cc
similarity index 61%
rename from targets/lm3s6965evb-qemu/early_boot.c
rename to targets/lm3s6965evb-qemu/boot.cc
index 0d1922f..f4a537c 100644
--- a/targets/lm3s6965evb-qemu/early_boot.c
+++ b/targets/lm3s6965evb-qemu/boot.cc
@@ -13,17 +13,23 @@
 // the License.
 
 #include "pw_boot_armv7m/boot.h"
+
 #include "pw_preprocessor/compiler.h"
 #include "pw_sys_io_baremetal_lm3s6965evb/init.h"
 
+// Note that constexpr is used inside of this function instead of using a static
+// constexpr or declaring it outside of this function in an anonymous namespace,
+// because constexpr makes it available for the compiler to evaluate during
+// copmile time but does NOT require it to be evaluated at compile team and we
+// have to be incredibly careful that this does not end up in the .data section.
 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;
+  constexpr uint32_t kRccDefault = 0x078E3AD1U;
+  volatile uint32_t& rcc = *reinterpret_cast<volatile uint32_t*>(0x400FE070U);
+  rcc = kRccDefault;
+  constexpr uint32_t kRcc2Default = 0x07802810U;
+  volatile uint32_t& rcc2 = *reinterpret_cast<volatile uint32_t*>(0x400FE070U);
+  rcc2 = kRcc2Default;
 }
 
 void pw_boot_PreStaticConstructorInit() {}
@@ -36,7 +42,7 @@
   *aircr = 0x5fa0004;
 
   // In case main() returns, just sit here until the device is reset.
-  while (1) {
+  while (true) {
   }
   PW_UNREACHABLE;
 }
diff --git a/targets/stm32f429i-disc1/BUILD b/targets/stm32f429i-disc1/BUILD
index 8be6916..1414169 100644
--- a/targets/stm32f429i-disc1/BUILD
+++ b/targets/stm32f429i-disc1/BUILD
@@ -24,7 +24,7 @@
 pw_cc_library(
     name = "pre_init",
     srcs = [
-        "early_boot.c",
+        "boot.cc",
         "vector_table.cc"
     ],
     deps = [
diff --git a/targets/stm32f429i-disc1/BUILD.gn b/targets/stm32f429i-disc1/BUILD.gn
index 03169f7..c29c0a2 100644
--- a/targets/stm32f429i-disc1/BUILD.gn
+++ b/targets/stm32f429i-disc1/BUILD.gn
@@ -42,7 +42,7 @@
       "$dir_pw_preprocessor",
     ]
     sources = [
-      "early_boot.c",
+      "boot.cc",
       "vector_table.cc",
     ]
   }
diff --git a/targets/stm32f429i-disc1/early_boot.c b/targets/stm32f429i-disc1/boot.cc
similarity index 70%
rename from targets/stm32f429i-disc1/early_boot.c
rename to targets/stm32f429i-disc1/boot.cc
index e372eaa..439d0a9 100644
--- a/targets/stm32f429i-disc1/early_boot.c
+++ b/targets/stm32f429i-disc1/boot.cc
@@ -13,22 +13,28 @@
 // the License.
 
 #include "pw_boot_armv7m/boot.h"
+
 #include "pw_malloc/malloc.h"
 #include "pw_preprocessor/compiler.h"
 #include "pw_sys_io_baremetal_stm32f429/init.h"
 
+// Note that constexpr is used inside of this function instead of using a static
+// constexpr or declaring it outside of this function in an anonymous namespace,
+// because constexpr makes it available for the compiler to evaluate during
+// copmile time but does NOT require it to be evaluated at compile team and we
+// have to be incredibly careful that this does not end up in the .data section.
 void pw_boot_PreStaticMemoryInit() {
   // TODO(pwbug/17): Optionally enable Replace when Pigweed config system is
   // added.
 #if PW_ARMV7M_ENABLE_FPU
   // Enable FPU if built using hardware FPU instructions.
   // CPCAR mask that enables FPU. (ARMv7-M Section B3.2.20)
-  const uint32_t kFpuEnableMask = (0xFu << 20);
+  constexpr uint32_t kFpuEnableMask = (0xFu << 20);
 
   // Memory mapped register to enable FPU. (ARMv7-M Section B3.2.2, Table B3-4)
-  volatile uint32_t* arm_v7m_cpacr = (volatile uint32_t*)0xE000ED88u;
-
-  *arm_v7m_cpacr |= kFpuEnableMask;
+  volatile uint32_t& arm_v7m_cpacr =
+      *reinterpret_cast<volatile uint32_t*>(0xE000ED88u);
+  arm_v7m_cpacr |= kFpuEnableMask;
 #endif  // PW_ARMV7M_ENABLE_FPU
 }
 
@@ -42,7 +48,7 @@
 
 PW_NO_RETURN void pw_boot_PostMain() {
   // In case main() returns, just sit here until the device is reset.
-  while (1) {
+  while (true) {
   }
   PW_UNREACHABLE;
 }