targets: Fork the Pigweed arduino target

This will allow overriding facade implementations like pw_log_BACKEND.
Also set default baudrate to 1,000,000.

Change-Id: Ic5e5f317d9e720e52e7d46a1ec7ad9154313f768
Reviewed-on: https://pigweed-review.googlesource.com/c/gonk/+/184830
Reviewed-by: Armando Montanez <amontanez@google.com>
Commit-Queue: Anthony DiGirolamo <tonymd@google.com>
diff --git a/BUILD.gn b/BUILD.gn
index 2120617..ea9870c 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -35,8 +35,8 @@
     "//applications/system_example(//targets/stm32f769i_disc0_stm32cube:stm32f769i_disc0_stm32cube.size_optimized)",
 
     # Arduino targets
-    "//applications/fpga_config($dir_pigweed/targets/arduino:arduino_size_optimized)",
-    "//applications/spi_flash_test($dir_pigweed/targets/arduino:arduino_size_optimized)",
+    "//applications/fpga_config(//targets/stm32f730r8tx_arduino:arduino_size_optimized)",
+    "//applications/spi_flash_test(//targets/stm32f730r8tx_arduino:arduino_size_optimized)",
   ]
 }
 
diff --git a/README.md b/README.md
index 508ba95..8bf1a10 100644
--- a/README.md
+++ b/README.md
@@ -68,13 +68,13 @@
 1. Connect over serial.
 
    ```sh
-   python -m serial.tools.miniterm --raw /dev/ttyGonk 115200
+   python -m serial.tools.miniterm --raw /dev/ttyGonk 1000000
    ```
 
    You should see output matching:
 
    ```
-   --- Miniterm on /dev/ttyGonk  115200,8,N,1 ---
+   --- Miniterm on /dev/ttyGonk  1000000,8,N,1 ---
    --- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
    INF  Device id: 1f 84 1
    INF  Device id: 1f 84 1
diff --git a/targets/stm32f730r8tx_arduino/BUILD.gn b/targets/stm32f730r8tx_arduino/BUILD.gn
new file mode 100644
index 0000000..2e33b31
--- /dev/null
+++ b/targets/stm32f730r8tx_arduino/BUILD.gn
@@ -0,0 +1,105 @@
+# Copyright 2023 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.
+
+import("//build_overrides/pigweed.gni")
+
+import("$dir_pw_arduino_build/arduino.gni")
+import("$dir_pw_build/target_types.gni")
+import("$dir_pw_toolchain/generate_toolchain.gni")
+
+if (pw_arduino_build_CORE_PATH != "") {
+  import("target_toolchains.gni")
+
+  generate_toolchains("target_toolchains") {
+    toolchains = pw_target_toolchain_arduino_list
+  }
+
+  if (current_toolchain ==
+      "//targets/stm32f730r8tx_arduino:arduino_size_optimized") {
+    config("arduino_build") {
+      exec_script(arduino_builder_script,
+                  filter_exclude(arduino_run_command_args,
+                                 [ "--save-config" ]) + [ "--run-prebuilds" ],
+                  "string")
+
+      _exclude_flags = [ "-std=gnu++14" ]
+
+      _cflags = exec_script(arduino_builder_script,
+                            arduino_show_command_args + [ "--c-flags" ],
+                            "list lines")
+      cflags = filter_exclude(_cflags, _exclude_flags)
+
+      asmflags = exec_script(arduino_builder_script,
+                             arduino_show_command_args + [ "--s-only-flags" ],
+                             "list lines")
+
+      _cflags_cc =
+          exec_script(arduino_builder_script,
+                      arduino_show_command_args + [ "--cpp-only-flags" ],
+                      "list lines")
+      cflags_cc = filter_exclude(_cflags_cc, _exclude_flags)
+
+      _ldflags = exec_script(arduino_builder_script,
+                             arduino_show_command_args + [ "--ld-flags" ],
+                             "list lines")
+      ldflags =
+          filter_exclude(_ldflags,
+                         [
+                           # Remove arguments ending in 'core.a', for example:
+                           # C:/Users/username/pigweed/out/core.a
+                           "*core.a\b",
+
+                           # Remove .elf output file, for example:
+                           # -o C:/Users/username/pigweed/out/pigweed.elf
+                           "\b-o\b",
+                           "*.elf\b",
+
+                           # Remove the Arduino {object_files} variable
+                           "{object_files}",
+                         ])
+
+      # Teensy4 core recipe uses: '-larm_cortexM7lfsp_math -lm -lstdc++'
+      libs = filter_exclude(
+              exec_script(arduino_builder_script,
+                          arduino_show_command_args + [ "--ld-lib-names" ],
+                          "list lines"),
+              [
+                # Exclude stdc++ and precompiled math libraries which causes
+                # linking errors for teensy cores.
+                "\bstdc++\b",
+                "\barm_cortexM0l_math\b",
+                "\barm_cortexM4lf_math\b",
+                "\barm_cortexM4l_math\b",
+                "\barm_cortexM7lfsp_math\b",
+              ])
+    }
+
+    pw_source_set("pre_init") {
+      sources = [ "init.cc" ]
+      public_deps = [
+        "$dir_pw_sys_io_arduino",
+        "$dir_pw_third_party/arduino:arduino_core_sources",
+      ]
+      deps = [
+        "$dir_pw_arduino_build:arduino_init.facade",
+        "$dir_pw_preprocessor",
+      ]
+    }
+  }
+} else {
+  config("arduino_build") {
+  }
+  group("pre_init") {
+  }
+}
diff --git a/targets/stm32f730r8tx_arduino/arduino_executable.gni b/targets/stm32f730r8tx_arduino/arduino_executable.gni
new file mode 100644
index 0000000..2013bf0
--- /dev/null
+++ b/targets/stm32f730r8tx_arduino/arduino_executable.gni
@@ -0,0 +1,27 @@
+# Copyright 2023 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.
+
+import("//build_overrides/pigweed.gni")
+import("$dir_pw_malloc/backend.gni")
+
+# Executable wrapper that includes some baremetal startup code.
+template("arduino_executable") {
+  target("executable", target_name) {
+    forward_variables_from(invoker, "*")
+    if (!defined(deps)) {
+      deps = []
+    }
+    deps += [ "$dir_pw_arduino_build:arduino_main_wrapper" ]
+  }
+}
diff --git a/targets/stm32f730r8tx_arduino/init.cc b/targets/stm32f730r8tx_arduino/init.cc
new file mode 100644
index 0000000..437ee3d
--- /dev/null
+++ b/targets/stm32f730r8tx_arduino/init.cc
@@ -0,0 +1,21 @@
+// Copyright 2023 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 <Arduino.h>
+
+#include "pw_arduino_build/init.h"
+
+// Arduino target specific init. pw_sys_io_arduino_Init() normally calls
+// Serial.begin(115200). Instead, we init serial to a higher baudrate here.
+extern "C" void pw_arduino_Init() { Serial.begin(1000000); }
diff --git a/targets/stm32f730r8tx_arduino/target_toolchains.gni b/targets/stm32f730r8tx_arduino/target_toolchains.gni
new file mode 100644
index 0000000..57528f2
--- /dev/null
+++ b/targets/stm32f730r8tx_arduino/target_toolchains.gni
@@ -0,0 +1,127 @@
+# Copyright 2023 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.
+
+import("//build_overrides/pigweed.gni")
+
+import("$dir_pw_build/defaults.gni")
+import("$dir_pw_sys_io/backend.gni")
+import("$dir_pw_toolchain/arm_gcc/toolchains.gni")
+
+declare_args() {
+  # Enable the pw_target_runner for on-device testing.
+  pw_arduino_use_test_server = false
+}
+_target_config = {
+  # Use the logging main.
+  pw_unit_test_MAIN = "$dir_pw_unit_test:simple_printing_main"
+
+  # Configuration options for Pigweed executable targets.
+  pw_build_EXECUTABLE_TARGET_TYPE = "arduino_executable"
+
+  pw_build_EXECUTABLE_TARGET_TYPE_FILE =
+      get_path_info("arduino_executable.gni", "abspath")
+
+  # Path to the bloaty config file for the output binaries.
+  pw_bloat_BLOATY_CONFIG = "$dir_pw_boot_cortex_m/bloaty_config.bloaty"
+
+  if (pw_arduino_use_test_server) {
+    _test_runner_script =
+        "$dir_pw_arduino_build/py/pw_arduino_build/unit_test_client.py"
+    pw_unit_test_AUTOMATIC_RUNNER =
+        get_path_info(_test_runner_script, "abspath")
+  }
+
+  # Facade backends
+  pw_assert_BACKEND = dir_pw_assert_basic
+  pw_log_BACKEND = dir_pw_log_basic
+  pw_sync_INTERRUPT_SPIN_LOCK_BACKEND =
+      "$dir_pw_sync_baremetal:interrupt_spin_lock"
+  pw_sync_MUTEX_BACKEND = "$dir_pw_sync_baremetal:mutex"
+  pw_rpc_CONFIG = "$dir_pw_rpc:disable_global_mutex"
+  pw_sys_io_BACKEND = dir_pw_sys_io_arduino
+  pw_rpc_system_server_BACKEND = "$dir_pw_hdlc:hdlc_sys_io_system_server"
+  pw_arduino_build_INIT_BACKEND = "//targets/stm32f730r8tx_arduino:pre_init"
+
+  pw_build_LINK_DEPS = [
+    "$dir_pw_assert:impl",
+    "$dir_pw_log:impl",
+  ]
+
+  current_cpu = "arm"
+  current_os = ""
+}
+
+_toolchain_properties = {
+  final_binary_extension = ".elf"
+}
+
+_target_default_configs = pigweed_default_configs + [
+                            "$dir_pw_toolchain/arm_gcc:enable_float_printf",
+                            "//targets/stm32f730r8tx_arduino:arduino_build",
+                          ]
+
+pw_target_toolchain_arduino = {
+  _excluded_members = [
+    "defaults",
+    "name",
+  ]
+
+  debug = {
+    name = "arduino_debug"
+    _toolchain_base = pw_toolchain_arm_gcc.cortex_m4f_debug
+    forward_variables_from(_toolchain_base, "*", _excluded_members)
+    forward_variables_from(_toolchain_properties, "*")
+    defaults = {
+      forward_variables_from(_toolchain_base.defaults, "*")
+      forward_variables_from(_target_config, "*")
+      default_configs = []
+      default_configs = _target_default_configs
+    }
+  }
+
+  speed_optimized = {
+    name = "arduino_speed_optimized"
+    _toolchain_base = pw_toolchain_arm_gcc.cortex_m4f_speed_optimized
+    forward_variables_from(_toolchain_base, "*", _excluded_members)
+    forward_variables_from(_toolchain_properties, "*")
+    defaults = {
+      forward_variables_from(_toolchain_base.defaults, "*")
+      forward_variables_from(_target_config, "*")
+      default_configs = []
+      default_configs = _target_default_configs
+    }
+  }
+
+  size_optimized = {
+    name = "arduino_size_optimized"
+    _toolchain_base = pw_toolchain_arm_gcc.cortex_m4f_size_optimized
+    forward_variables_from(_toolchain_base, "*", _excluded_members)
+    forward_variables_from(_toolchain_properties, "*")
+    defaults = {
+      forward_variables_from(_toolchain_base.defaults, "*")
+      forward_variables_from(_target_config, "*")
+      default_configs = []
+      default_configs = _target_default_configs
+    }
+  }
+}
+
+# This list just contains the members of the above scope for convenience to make
+# it trivial to generate all the toolchains in this file via a
+# `generate_toolchains` target.
+pw_target_toolchain_arduino_list = [
+  pw_target_toolchain_arduino.debug,
+  pw_target_toolchain_arduino.speed_optimized,
+  pw_target_toolchain_arduino.size_optimized,
+]