Test SPI flash is readable

- Update hello_world to read the SPI flash chip ID in a loop.
- README
  - Revised flashing instructions
  - Add a pin map
- Update flash-with-blackmagic-probe.sh to reset after loading.

Bug: b/310962981
Change-Id: I772ef135bda1d1397d802a0e45aa5b82b556290a
Reviewed-on: https://pigweed-review.googlesource.com/c/gonk/+/177990
Presubmit-Verified: CQ Bot Account <pigweed-scoped@luci-project-accounts.iam.gserviceaccount.com>
Pigweed-Auto-Submit: Anthony DiGirolamo <tonymd@google.com>
Commit-Queue: Auto-Submit <auto-submit@pigweed-service-accounts.iam.gserviceaccount.com>
Reviewed-by: Eric Holland <hollande@google.com>
diff --git a/BUILD.gn b/BUILD.gn
index e14edf1..53fc8c2 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -35,8 +35,7 @@
     "//applications/system_example(//targets/stm32f769i_disc0_stm32cube:stm32f769i_disc0_stm32cube.size_optimized)",
 
     # Arduino targets
-    "$dir_pw_status:status_test($dir_pigweed/targets/arduino:arduino_size_optimized)",
-    "//applications/hello_world($dir_pigweed/targets/arduino:arduino_size_optimized)",
+    "//applications/spi_flash_test($dir_pigweed/targets/arduino:arduino_size_optimized)",
   ]
 }
 
diff --git a/README.md b/README.md
index 06067c1..508ba95 100644
--- a/README.md
+++ b/README.md
@@ -40,9 +40,57 @@
 
 The build commands are defined in: `//tools/gonk_tools/build_project.py`.
 
-## Run:
+## Gonk `spi_flash_test` Example
 
-### Host
+### 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.
+
+   ```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
+   ```
+
+1. Unplug Gonk from USB and replug to reset the hardware. SPI bus issues have
+   been observed without this step.
+
+1. Connect over serial.
+
+   ```sh
+   python -m serial.tools.miniterm --raw /dev/ttyGonk 115200
+   ```
+
+   You should see output matching:
+
+   ```
+   --- Miniterm on /dev/ttyGonk  115200,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
+   INF  Device id: 1f 84 1
+   INF  Device id: 1f 84 1
+   ```
+
+
+### Flash with BlackMagic Probe:
+
+```sh
+./scripts/flash-with-blackmagic-probe.sh ./out/gn/arduino_size_optimized/obj/applications/spi_flash_test/bin/spi_flash_test.elf
+```
+
+## pw_system Example
+### Run on Host
 
 Run the host app and connect to it via `pw-system-console`:
 
@@ -53,7 +101,7 @@
   killall system_example
 ```
 
-### On device
+### Run on Device
 
 Flashing
 
@@ -78,3 +126,34 @@
 ```sh
 python -m pw_stm32cube_build gen_file_list third_party/stm32cube_f7
 ```
+
+# Pin Map
+
+## Misc
+
+| Net    | STM32 Pin | STM32 Function | Function |
+|--------+-----------+----------------+----------|
+| STATUS | PB13      | GPIO_Output    | STAT LED |
+
+## SPI Flash Connection
+
+| Net          | STM32 Pin | STM32 Function | Flash Pin  |
+|--------------|-----------|----------------|------------|
+| ICE_SPI_SS   | PD2       | GPIO_Output    | S#         |
+| ICE_SPI_MISO | PB4       | SPI1_MISO      | DQ1        |
+| ICE_SPI_MOSI | PB5       | SPI1_MOSI      | DQ0        |
+| ICE_SPI_SCK  | PB3       | SPI1_SCK       | C          |
+| FLASH_HOLD   | PC11      | GPIO_Output    | HOLD#/DQ3  |
+| FLASH_WP     | PC12      | GPIO_Output    | W#/VPP/DQ2 |
+
+## FPGA Connection
+
+| Net               | Function | FPGA IO# | STM32 Pin | STM32 Function  | Notes               |
+|-------------------|----------|----------|-----------|-----------------|---------------------|
+| FPGA_IO_SPARE_0_2 | rst_i    |      135 | PA0       | GPIO_Output     | Active high         |
+| FPGA_IO_SPARE_0_0 | mode_i   |      137 | PB11      | GPIO_Output     | FPGA operation mode |
+| FPGA_IO_SPARE_0_1 | valid_o  |      136 | PB10      | GPIO_Output     | Data/Transfer Valid |
+| DSPI_SCK          | sclk_i   |       76 | PB2       | QUADSPI_CLK     |                     |
+| DSPI_CS           | cs_n     |       75 | PB6       | QUADSPI_BK1_NCS |                     |
+| DSPI_IO1          | mosi_i   |       74 | PC10      | QUADSPI_BK1_IO1 |                     |
+| DSPI_IO0          | miso_i   |       73 | PC9       | QUADSPI_BK1_IO0 |                     |
diff --git a/applications/hello_world/BUILD.gn b/applications/hello_world/BUILD.gn
deleted file mode 100644
index 3be9ef7..0000000
--- a/applications/hello_world/BUILD.gn
+++ /dev/null
@@ -1,56 +0,0 @@
-# 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.
-
-import("//build_overrides/pigweed.gni")
-
-import("$dir_pw_arduino_build/arduino.gni")
-import("$dir_pw_build/target_types.gni")
-
-pw_executable("hello_world") {
-  # Example of how to include Arduino style libraries:
-  #
-  # Common library arguments to the arduino_builder tool.
-  # _library_args = [
-  #   "--library-path",
-  #   rebase_path("libraries"),
-  #   "--library-names",
-  #   "ExampleLibraryName",
-  # ]
-  #
-  # _library_cpp_files = exec_script(
-  #     arduino_builder_script,
-  #     arduino_show_command_args + _library_args +
-  #     [ "--library-cpp-files" ],
-  #     "list lines"
-  # )
-  #
-  # include_dirs = exec_script(
-  #     arduino_builder_script,
-  #     arduino_show_command_args + _library_args +
-  #     [ "--library-include-dirs" ],
-  #     "list lines"
-  # )
-
-  sources = [ "main.cc" ]
-
-  deps = [
-    "$dir_pw_log",
-    "$dir_pw_string",
-    "$dir_pw_third_party/arduino:arduino_core_sources",
-  ]
-
-  ldflags = [ "-Wl,--print-memory-usage" ]
-
-  remove_configs = [ "$dir_pw_toolchain/arm_gcc:enable_float_printf" ]
-}
diff --git a/applications/hello_world/main.cc b/applications/hello_world/main.cc
deleted file mode 100644
index 390e6ca..0000000
--- a/applications/hello_world/main.cc
+++ /dev/null
@@ -1,34 +0,0 @@
-// 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 <cstdint>
-
-#include "pw_log/log.h"
-#include "pw_string/format.h"
-
-int main() {
-  char buffer[32];
-  uint16_t update_count = 0;
-
-  while (true) {
-    pw::string::Format(buffer, "Message number: %u", update_count);
-    PW_LOG_INFO("%s", buffer);
-    delay(1000);
-
-    update_count = (update_count + 1) % UINT16_MAX;
-  }
-
-  return 0;
-}
diff --git a/applications/spi_flash_test/BUILD.gn b/applications/spi_flash_test/BUILD.gn
new file mode 100644
index 0000000..69cb2f4
--- /dev/null
+++ b/applications/spi_flash_test/BUILD.gn
@@ -0,0 +1,38 @@
+# 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.
+
+import("//build_overrides/pigweed.gni")
+
+import("$dir_pw_arduino_build/arduino.gni")
+import("$dir_pw_build/target_types.gni")
+
+pw_executable("spi_flash_test") {
+  include_dirs = [ "//third_party/stm32duino/arduino-core/libraries/SPI/src" ]
+
+  sources = [
+    "//third_party/stm32duino/arduino-core/libraries/SPI/src/SPI.cpp",
+    "//third_party/stm32duino/arduino-core/libraries/SPI/src/utility/spi_com.c",
+    "main.cc",
+  ]
+
+  deps = [
+    "$dir_pw_log",
+    "$dir_pw_string",
+    "$dir_pw_third_party/arduino:arduino_core_sources",
+  ]
+
+  ldflags = [ "-Wl,--print-memory-usage" ]
+
+  remove_configs = [ "$dir_pw_toolchain/arm_gcc:enable_float_printf" ]
+}
diff --git a/applications/spi_flash_test/main.cc b/applications/spi_flash_test/main.cc
new file mode 100644
index 0000000..e1d9bef
--- /dev/null
+++ b/applications/spi_flash_test/main.cc
@@ -0,0 +1,90 @@
+// 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 <cstdint>
+
+#include <SPI.h>
+
+#include "pw_log/log.h"
+#include "pw_string/format.h"
+
+// STM32 Port & pin          // PCB NET Name
+const int ICE40Reset = PC13; // ICE_RST_N
+const int ICE40Done = PC8;   // ICE_CDONE
+const int FlashCS = PD2;     // ICE_SPI_SS
+const int FlashHold = PC11;  // FLASH_HOLD
+const int FlashWP = PC12;    // FLASH_WP
+const int FlashMOSI = PB5;   // ICE_SPI_MOSI
+const int FlashMISO = PB4;   // ICE_SPI_MOSI
+const int FlashCLK = PB3;    // ICE_SPI_SCK
+const int StatusLed = PB13;  // STATUS
+
+int main() {
+  Serial.begin(115200);
+
+  // Put FPGA in reset.
+  PW_LOG_INFO("Hold FPGA in reset");
+  pinMode(ICE40Reset, OUTPUT);
+  digitalWrite(ICE40Reset, LOW);
+  delay(10);
+
+  // Debug status LED
+  pinMode(StatusLed, OUTPUT);
+  digitalWrite(StatusLed, LOW);
+
+  // SPI pin definitions
+  pinMode(FlashCS, OUTPUT);
+  digitalWrite(FlashCS, HIGH);
+
+  SPI.setMISO(FlashMISO);
+  SPI.setMOSI(FlashMOSI);
+  SPI.setSCLK(FlashCLK);
+  SPI.begin();
+
+  char buffer[32];
+  uint16_t update_count = 0;
+  uint8_t manufacturer_id[4];
+
+  while (true) {
+
+    // Read the SPI Flash JEDEC ID.
+    manufacturer_id[0] = 0x9f;
+    manufacturer_id[1] = 0;
+    manufacturer_id[2] = 0;
+    manufacturer_id[3] = 0;
+    SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));
+    digitalWrite(FlashCS, LOW);
+    SPI.transfer(manufacturer_id, 4);
+    digitalWrite(FlashCS, HIGH);
+    SPI.endTransaction();
+
+    pw::string::Format(buffer, "SPI Flash JEDEC ID: %x %x %x",
+                       manufacturer_id[1], manufacturer_id[2],
+                       manufacturer_id[3]);
+    PW_LOG_INFO("%s", buffer);
+    delay(1000);
+
+    update_count = (update_count + 1) % UINT16_MAX;
+
+    // Toggle status LED each loop.
+    if (update_count % 2 == 0) {
+      digitalWrite(StatusLed, HIGH);
+    } else {
+      digitalWrite(StatusLed, LOW);
+    }
+  }
+
+  return 0;
+}
diff --git a/scripts/flash-with-blackmagic-probe.sh b/scripts/flash-with-blackmagic-probe.sh
index df3248b..8b87f09 100755
--- a/scripts/flash-with-blackmagic-probe.sh
+++ b/scripts/flash-with-blackmagic-probe.sh
@@ -22,10 +22,13 @@
     -ex "set confirm off" \
     -ex "target extended-remote ${DEVBMP}" \
     -ex "monitor version" \
-    -ex "monitor tpwr enable" \
+    -ex "monitor tpwr disable" \
     -ex "monitor swdp_scan" \
     -ex "attach 1" \
     -ex "load" \
     -ex "compare-sections" \
     -ex "kill" \
+    -ex "shell sleep 1" \
+    -ex "monitor reset" \
     $@
+