targets/stm32f730r8tx_arduino: Run objcopy

Run objcopy as part of the build to create dfu-util flashable
binfiles.

Change-Id: I525a2253464175f32a9852f11dd4b218d74d62ce
Reviewed-on: https://pigweed-review.googlesource.com/c/gonk/+/185472
Reviewed-by: Armando Montanez <amontanez@google.com>
Commit-Queue: Auto-Submit <auto-submit@pigweed-service-accounts.iam.gserviceaccount.com>
Pigweed-Auto-Submit: Anthony DiGirolamo <tonymd@google.com>
diff --git a/README.md b/README.md
index c9e7157..3ba5179 100644
--- a/README.md
+++ b/README.md
@@ -64,14 +64,6 @@
 
 ### Flash with `dfu-util`
 
-1. Create a bin file from the elf. This will be automated later.
-
-   ```sh
-   arm-none-eabi-objcopy -O binary \
-    ./out/gn/arduino_size_optimized/obj/applications/fpga_config/bin/fpga_config.elf  \
-    ./out/gn/arduino_size_optimized/obj/applications/fpga_config/bin/fpga_config.bin
-   ```
-
 1. Unplug gonk from USB and replug with MODE button held down.
 
 1. Run dfu-util to flash.
@@ -79,7 +71,7 @@
    ```sh
    dfu-util -d 0483:df11 -s 0x08000000:leave \
      --serial STM32FxSTM32 -a 0 \
-     -D ./out/gn/arduino_size_optimized/obj/applications/fpga_config/bin/fpga_config.bin
+     -D ./out/gn/arduino_size_optimized/obj/applications/fpga_config/fpga_config.bin
    ```
 
 ### Write the FPGA bitstream
@@ -95,14 +87,6 @@
 
 ### Flash with `dfu-util`
 
-1. Create a bin file from the elf. This will be automated later.
-
-   ```sh
-   arm-none-eabi-objcopy -O binary \
-     ./out/gn/arduino_size_optimized/obj/applications/spi_flash_test/bin/spi_flash_test.elf \
-     ./out/gn/arduino_size_optimized/obj/applications/spi_flash_test/bin/spi_flash_test.bin
-   ```
-
 1. Unplug gonk from USB and replug with MODE button held down.
 
 1. Run dfu-util to flash.
@@ -110,7 +94,7 @@
    ```sh
    dfu-util -d 0483:df11 -s 0x08000000:leave \
      --serial STM32FxSTM32 -a 0 \
-     -D ./out/gn/arduino_size_optimized/obj/applications/spi_flash_test/bin/spi_flash_test.bin
+     -D ./out/gn/arduino_size_optimized/obj/applications/spi_flash_test/spi_flash_test.bin
    ```
 
 1. Unplug Gonk from USB and replug to reset the hardware. SPI bus issues have
diff --git a/targets/stm32f730r8tx_arduino/arduino_executable.gni b/targets/stm32f730r8tx_arduino/arduino_executable.gni
index 2013bf0..bb286f7 100644
--- a/targets/stm32f730r8tx_arduino/arduino_executable.gni
+++ b/targets/stm32f730r8tx_arduino/arduino_executable.gni
@@ -13,15 +13,34 @@
 # the License.
 
 import("//build_overrides/pigweed.gni")
+
+import("$dir_pw_build/exec.gni")
 import("$dir_pw_malloc/backend.gni")
 
-# Executable wrapper that includes some baremetal startup code.
 template("arduino_executable") {
-  target("executable", target_name) {
+  _bin_name = "${target_name}.bin"
+  _elf_name = "${target_name}.elf"
+
+  # Compile the elf
+  executable(_elf_name) {
     forward_variables_from(invoker, "*")
     if (!defined(deps)) {
       deps = []
     }
     deps += [ "$dir_pw_arduino_build:arduino_main_wrapper" ]
   }
+
+  # objcopy a bin file for dfu-util flashing
+  pw_exec(target_name) {
+    _bin_out_path = "${target_out_dir}/${_bin_name}"
+    deps = [ ":${_elf_name}" ]
+    program = "arm-none-eabi-objcopy"
+    args = [
+      "-O",
+      "binary",
+      "<TARGET_FILE(:${_elf_name})>",
+      rebase_path(_bin_out_path, root_build_dir),
+    ]
+    outputs = [ _bin_out_path ]
+  }
 }