targets: Enable rp2040 based examples

Bug: 325649415, 300318373
Change-Id: I9146041a1f26805944b38c0505a9da827b6f0026
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/examples/+/209455
Commit-Queue: Anthony DiGirolamo <tonymd@google.com>
Reviewed-by: Ted Pudlik <tpudlik@google.com>
Lint: Lint 🤖 <android-build-ayeaye@system.gserviceaccount.com>
diff --git a/BUILD.gn b/BUILD.gn
index 65f7275..d6a427e 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -133,13 +133,12 @@
     "//examples/03_rpc:rpc_main(//targets/stm32f429i_disc1_stm32cube:stm32f429i_disc1_stm32cube.size_optimized)",
   ]
 
-  # TODO: https://pwbug.dev/325649415 - Replace broken bare metal rp2040
-  # examples with pw_system based targets.
-  #
-  # deps += [
-  #  "//examples/01_blinky:blinky(//targets/rp2040:rp2040.size_optimized)",
-  #  "//examples/02_unit_testing:test_runner_app(//targets/rp2040:rp2040.size_optimized)",
-  # ]
+  # Raspberry Pi Pico RP2040 examples.
+  deps += [
+    "//examples/01_blinky:blinky(//targets/rp2040:rp2040.size_optimized)",
+    "//examples/02_unit_testing:test_runner_app(//targets/rp2040:rp2040.size_optimized)",
+    "//examples/03_rpc:rpc_main(//targets/rp2040:rp2040.size_optimized)",
+  ]
 
   # Host examples.
   deps += [
diff --git a/docs/index.rst b/docs/index.rst
index 5c28c3b..ab049e3 100644
--- a/docs/index.rst
+++ b/docs/index.rst
@@ -5,18 +5,18 @@
 ======================
 
 .. toctree::
-  :maxdepth: 1
-  :hidden:
+   :maxdepth: 1
+   :hidden:
 
-  Home <self>
-  Examples <examples/docs>
-  Customization <customization>
-  Pigweed Docs <https://pigweed.dev>
-  Mailing List <https://groups.google.com/forum/#!forum/pigweed>
-  Chat Room <https://discord.gg/M9NSeTA>
-  Source Code <https://cs.pigweed.dev/pigweed>
-  Code Reviews <https://pigweed-review.googlesource.com>
-  Issue Tracker <https://issues.pigweed.dev/issues?q=status:open>
+   Home <self>
+   Examples <examples/docs>
+   Customization <customization>
+   Pigweed Docs <https://pigweed.dev>
+   Mailing List <https://groups.google.com/forum/#!forum/pigweed>
+   Chat Room <https://discord.gg/M9NSeTA>
+   Source Code <https://cs.pigweed.dev/pigweed>
+   Code Reviews <https://pigweed-review.googlesource.com>
+   Issue Tracker <https://issues.pigweed.dev/issues?q=status:open>
 
 This repository outlines the recommended way of using Pigweed in a new or
 existing project. Feel free to fork this repository, or read it as a reference.
diff --git a/examples/01_blinky/docs.rst b/examples/01_blinky/docs.rst
index 1ca8ff3..e7f97c7 100644
--- a/examples/01_blinky/docs.rst
+++ b/examples/01_blinky/docs.rst
@@ -18,7 +18,7 @@
 
 #. Flash ``blinky.elf``.
 
-   **STM32F429I_DISC1 (Linux/macOS)**
+   **STM32F429I_DISC1 (Linux/MacOS)**
 
    .. code-block:: sh
 
@@ -29,6 +29,19 @@
       We don't yet have OpenOCD for Windows. See
       `b/300986008 <https://issues.pigweed.dev/300986008>`_ for updates.
 
+   **Raspberry Pi Pico (RP2404) (Windows/Linux/MacOS)**
+
+   1. Reboot the Pico into BOOTSEL mode by holding the bootsel button when
+      plugging into USB.
+   2. Copy ``./out/gn/rp2040.size_optimized/obj/examples/01_blinky/blinky.uf2``
+      to your Pi Pico.
+
+   .. note::
+      It is also possible to flash a Pico board with `picotool
+      <https://github.com/raspberrypi/picotool>`_. We will be adding support for
+      that in this repo soon. See `b/300321451
+      <https://issues.pigweed.dev/300321451>`_ for updates.
+
 ---------
 View logs
 ---------
diff --git a/examples/02_unit_testing/docs.rst b/examples/02_unit_testing/docs.rst
index 5ac8b62..b432d5e 100644
--- a/examples/02_unit_testing/docs.rst
+++ b/examples/02_unit_testing/docs.rst
@@ -17,17 +17,29 @@
 
 #. Flash ``test_runner_app.elf``.
 
-   **STM32F429I_DISC1 (Linux/macOS)**
+   **STM32F429I_DISC1 (Linux/MacOS)**
 
    .. code-block:: sh
 
       pw flash --device STM32-Discovery out/gn/stm32f429i_disc1_stm32cube.size_optimized/obj/examples/02_unit_testing/bin/test_runner_app.elf
 
    .. note::
-
       We don't yet have OpenOCD for Windows. See
       `b/300986008 <https://issues.pigweed.dev/300986008>`_ for updates.
 
+   **Raspberry Pi Pico (RP2404) (Windows/Linux/MacOS)**
+
+   1. Reboot the Pico into BOOTSEL mode by holding the bootsel button when
+      plugging into USB.
+   2. Copy ``./out/gn/rp2040.size_optimized/obj/examples/02_unit_testing/test_runner_app.uf2``
+      to your Pi Pico.
+
+   .. note::
+      It is also possible to flash a Pico board with `picotool
+      <https://github.com/raspberrypi/picotool>`_. We will be adding support for
+      that in this repo soon. See `b/300321451
+      <https://issues.pigweed.dev/300321451>`_ for updates.
+
 #. Open `pw_console <https://pigweed.dev/pw_console/>`_.
 
    **Device**
diff --git a/examples/03_rpc/docs.rst b/examples/03_rpc/docs.rst
index ab9d9aa..6ee746d 100644
--- a/examples/03_rpc/docs.rst
+++ b/examples/03_rpc/docs.rst
@@ -15,7 +15,7 @@
 
 #. Launch the RPC example on a device, or using the simulated device.
 
-   **STM32F429I_DISC1 (Linux/macOS)**
+   **STM32F429I_DISC1 (Linux/MacOS)**
 
    * Flash ``rpc_main.elf``:
 
@@ -24,10 +24,24 @@
         pw flash --device STM32-Discovery out/gn/stm32f429i_disc1_stm32cube.size_optimized/obj/examples/03_rpc/bin/rpc_main.elf
 
      .. note::
-
         We don't yet have OpenOCD for Windows. See
         `b/300986008 <https://issues.pigweed.dev/300986008>`_ for updates.
 
+   **Raspberry Pi Pico (RP2404) (Windows/Linux/MacOS)**
+
+   * Flash ``./out/gn/rp2040.size_optimized/obj/examples/03_rpc/rpc_main.uf2``:
+
+     1. Reboot the Pico into BOOTSEL mode by holding the bootsel button when
+        plugging into USB.
+     2. Copy ``./out/gn/rp2040.size_optimized/obj/examples/03_rpc/rpc_main.uf2``
+        to your Pi Pico.
+
+     .. note::
+        It is also possible to flash a Pico board with `picotool
+        <https://github.com/raspberrypi/picotool>`_. We will be adding support for
+        that in this repo soon. See `b/300321451
+        <https://issues.pigweed.dev/300321451>`_ for updates.
+
    * Open `pw_console <https://pigweed.dev/pw_console/>`_.
 
      .. code-block:: sh
@@ -35,7 +49,6 @@
         pw console -d /dev/ttyACM0 -b 115200 --token-databases out/gn/stm32f429i_disc1_stm32cube.size_optimized/obj/examples/03_rpc/bin/rpc_main.elf
 
      .. tip::
-
         On macOS, your device will look like ``/dev/cu.usbmodem2141403``, but
         will most likely end with a different number.
 
diff --git a/libraries/pw_board_led_host/led.cc b/libraries/pw_board_led_host/led.cc
index a41d76c..3cef884 100644
--- a/libraries/pw_board_led_host/led.cc
+++ b/libraries/pw_board_led_host/led.cc
@@ -18,6 +18,8 @@
 
 #include "pw_log/log.h"
 
+// pw::board_led API implementation for the a host machine using log statements
+// to simulate blinking an LED.
 namespace pw::board_led {
 namespace {
 
diff --git a/libraries/pw_board_led_rp2040/BUILD.gn b/libraries/pw_board_led_rp2040/BUILD.gn
new file mode 100644
index 0000000..d863302
--- /dev/null
+++ b/libraries/pw_board_led_rp2040/BUILD.gn
@@ -0,0 +1,28 @@
+# Copyright 2024 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/pi_pico.gni")
+import("//build_overrides/pigweed.gni")
+
+import("$dir_pw_build/target_types.gni")
+
+pw_source_set("pw_board_led_rp2040") {
+  deps = [
+    "$PICO_ROOT/src/common/pico_base",
+    "$PICO_ROOT/src/common/pico_stdlib",
+    "//libraries/pw_board_led:pw_board_led.facade",
+  ]
+  sources = [ "led.cc" ]
+  remove_configs = [ "$dir_pw_build:strict_warnings" ]
+}
diff --git a/libraries/pw_board_led_rp2040/led.cc b/libraries/pw_board_led_rp2040/led.cc
new file mode 100644
index 0000000..119cbd4
--- /dev/null
+++ b/libraries/pw_board_led_rp2040/led.cc
@@ -0,0 +1,53 @@
+// Copyright 2024 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_board_led/led.h"
+
+#include "pico/stdlib.h"
+
+// pw::board_led API implementation for the rp2040 using the pico-sdk.
+namespace pw::board_led {
+namespace {
+
+constexpr uint kLedPin = PICO_DEFAULT_LED_PIN;
+bool led_on = false;
+
+}  // namespace
+
+void Init() {
+  gpio_init(kLedPin);
+  gpio_set_dir(kLedPin, GPIO_OUT);
+  TurnOff();
+}
+
+void TurnOff() {
+  gpio_put(kLedPin, 0);
+  led_on = false;
+}
+
+void TurnOn() {
+  gpio_put(kLedPin, 1);
+  led_on = true;
+}
+
+void Toggle() {
+  // Check if the LED is on. If so, turn it off.
+  if (led_on) {
+    TurnOff();
+  } else {
+    TurnOn();
+  }
+}
+
+}  // namespace pw::board_led
diff --git a/libraries/pw_board_led_stm32f429i_disc1/led.cc b/libraries/pw_board_led_stm32f429i_disc1/led.cc
index 77fcab5..aa94f0c 100644
--- a/libraries/pw_board_led_stm32f429i_disc1/led.cc
+++ b/libraries/pw_board_led_stm32f429i_disc1/led.cc
@@ -18,6 +18,8 @@
 
 #include "pw_preprocessor/compiler.h"
 
+// pw::board_led API implementation for the stm32f429i using direct register
+// manipulation.
 namespace pw::board_led {
 namespace {
 
diff --git a/libraries/pw_board_led_teensy/led.cc b/libraries/pw_board_led_teensy/led.cc
index 4a81011..e6c8563 100644
--- a/libraries/pw_board_led_teensy/led.cc
+++ b/libraries/pw_board_led_teensy/led.cc
@@ -18,6 +18,8 @@
 
 #include <cinttypes>
 
+// pw::board_led API implementation for the Teensy 3.x and 4.x using the Arduino
+// digitalWrite function.
 namespace pw::board_led {
 namespace {
 
diff --git a/targets/rp2040/BUILD.gn b/targets/rp2040/BUILD.gn
index b979c11..dc93a57 100644
--- a/targets/rp2040/BUILD.gn
+++ b/targets/rp2040/BUILD.gn
@@ -21,7 +21,7 @@
   cpu = PW_SYSTEM_CPU.CORTEX_M0PLUS
   scheduler = PW_SYSTEM_SCHEDULER.FREERTOS
   use_pw_malloc = false
-  global_configs = [ "$dir_pigweed/targets/rp2040_pw_system:rp2040_hal_config" ]
+  global_configs = [ "$dir_pigweed/targets/rp2040:rp2040_hal_config" ]
 
   build_args = {
     pw_build_EXECUTABLE_TARGET_TYPE = "pico_executable"
@@ -33,16 +33,19 @@
     pw_log_tokenized_HANDLER_BACKEND = "$dir_pw_system:log_backend.impl"
 
     pw_third_party_freertos_CONFIG =
-        "$dir_pigweed/targets/rp2040_pw_system:rp2040_freertos_config"
+        "$dir_pigweed/targets/rp2040:rp2040_freertos_config"
     pw_third_party_freertos_PORT = "$dir_pw_third_party/freertos:arm_cm0"
 
-    pw_sys_io_BACKEND = dir_pw_sys_io_stdio
+    pw_sys_io_BACKEND = dir_pw_sys_io_rp2040
     pw_build_LINK_DEPS += [
-      "$dir_pigweed/targets/rp2040_pw_system:pre_init",
+      "$dir_pigweed/targets/rp2040:pre_init",
       "$dir_pw_assert:impl",
       "$dir_pw_log:impl",
     ]
 
-    pw_board_led_BACKEND = "//libraries/pw_board_led_host"
+    pw_sync_COUNTING_SEMAPHORE_BACKEND =
+        "$dir_pw_sync_freertos:counting_semaphore"
+
+    pw_board_led_BACKEND = "//libraries/pw_board_led_rp2040"
   }
 }