lib/pi4ioe5v6416: Pullup configuration

- Add named register values and enable port0 pull up resistors to read
  button states.
- Add ReadPort0 and ReadPort1 for convenience.
- Add the IO interrupt pin definition in common_pico.cc
- Log the button states in binary:

  pi4ioe5v6416 Probe Ok
  Port 0: bf
  Buttons: 10111111
  Port 1: c1

Change-Id: I2a32323f2d54a51db56d17ceb99bb38d3c5359f4
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/kudzu/+/191775
Reviewed-by: Kayce Basques <kayce@google.com>
Commit-Queue: Anthony DiGirolamo <tonymd@google.com>
Presubmit-Verified: CQ Bot Account <pigweed-scoped@luci-project-accounts.iam.gserviceaccount.com>
diff --git a/applications/app_common_impl/common_pico.cc b/applications/app_common_impl/common_pico.cc
index c1b00fe..cf96e7a 100644
--- a/applications/app_common_impl/common_pico.cc
+++ b/applications/app_common_impl/common_pico.cc
@@ -267,6 +267,10 @@
     // IO expander resets when this pin is pulled low.
     .polarity = pw::digital_io::Polarity::kActiveLow,
 });
+Rp2040DigitalInOut s_io_interrupt_n({
+    .pin = 11,
+    .polarity = pw::digital_io::Polarity::kActiveLow,
+});
 Rp2040DigitalInOut s_imu_fsync({
     .pin = 13,
     .polarity = pw::digital_io::Polarity::kActiveHigh,
@@ -321,6 +325,7 @@
   s_io_reset_n.Enable();
   // Disable reset pin - normal operation.
   s_io_reset_n.SetStateInactive();
+  s_io_interrupt_n.Enable();
 
   // IMU FSYNC not used yet
   s_imu_fsync.Enable();
diff --git a/lib/pi4ioe5v6416/device.cc b/lib/pi4ioe5v6416/device.cc
index 2006bd9..4e7124c 100644
--- a/lib/pi4ioe5v6416/device.cc
+++ b/lib/pi4ioe5v6416/device.cc
@@ -14,9 +14,11 @@
 
 #include "pi4ioe5v6416/device.h"
 
+#include <bitset>
 #include <chrono>
 #include <cstddef>
 #include <cstdint>
+#include <string>
 
 #define PW_LOG_MODULE_NAME "pi4ioe5v6416"
 #define PW_LOG_LEVEL PW_LOG_LEVEL_DEBUG
@@ -35,6 +37,18 @@
 namespace {
 
 constexpr pw::i2c::Address kAddress = pw::i2c::Address::SevenBit<0x20>();
+enum Register : uint32_t {
+  InputPort0 = 0x0,
+  InputPort1 = 0x1,
+  OutputPort0 = 0x2,
+  OutputPort1 = 0x3,
+  ConfigPort0 = 0x6,
+  ConfigPort1 = 0x7,
+  PullUpDownEnablePort0 = 0x46,
+  PullUpDownEnablePort1 = 0x47,
+  PullUpDownSelectionPort0 = 0x48,
+  PullUpDownSelectionPort1 = 0x49,
+};
 
 }  // namespace
 
@@ -46,45 +60,68 @@
               pw::i2c::RegisterAddressSize::k1Byte) {}
 
 Status Device::Enable() {
-  // Port 0 is all button input.
+  // Set port 0 as inputs for buttons: 1=input 0=output
   device_.WriteRegister8(
-      0x6, 0xff, pw::chrono::SystemClock::for_at_least(10ms));
-  // Port 1 pins 6 and 7 are DISP_RESET and TOUCH_RESET which should be high.
+      Register::ConfigPort0, 0xff, pw::chrono::SystemClock::for_at_least(10ms));
+  // Select pull up resistors for button input: 1=pull-up 0=pull-down.
+  device_.WriteRegister8(Register::PullUpDownSelectionPort0,
+                         0xff,
+                         pw::chrono::SystemClock::for_at_least(10ms));
+  // Enable pull up/down resistors for button input: 1=enable 0=disable.
+  device_.WriteRegister8(Register::PullUpDownEnablePort0,
+                         0xff,
+                         pw::chrono::SystemClock::for_at_least(10ms));
+
+  // Port 1 pins 6 and 7 are DISP_RESET and TOUCH_RESET
+  // Set port 1 pins 6 and 7 to outputs: 1=input 0=output
+  device_.WriteRegister8(Register::ConfigPort1,
+                         0b00111111,
+                         pw::chrono::SystemClock::for_at_least(10ms));
+  // Set pins 6 and 7 to high.
   device_.WriteRegister8(
-      0x3, 0xff, pw::chrono::SystemClock::for_at_least(10ms));
-  device_.WriteRegister8(
-      0x7, 0x3f, pw::chrono::SystemClock::for_at_least(10ms));
+      Register::OutputPort1, 0xff, pw::chrono::SystemClock::for_at_least(10ms));
 
   return OkStatus();
 }
 
+pw::Result<uint8_t> Device::ReadPort0() {
+  return device_.ReadRegister8(Register::InputPort0,
+                               pw::chrono::SystemClock::for_at_least(10ms));
+}
+
+pw::Result<uint8_t> Device::ReadPort1() {
+  return device_.ReadRegister8(Register::InputPort1,
+                               pw::chrono::SystemClock::for_at_least(10ms));
+}
+
 Status Device::Probe() {
   pw::Status probe_result(initiator_.ProbeDeviceFor(
       kAddress, pw::chrono::SystemClock::for_at_least(10ms)));
 
-  if (probe_result != pw::OkStatus()) {
-    PW_LOG_DEBUG("pi4ioe5v6416 Probe Failed");
+  if (probe_result.ok()) {
+    PW_LOG_DEBUG("PI4IOE5V6416 Probe Ok");
   } else {
-    PW_LOG_DEBUG("pi4ioe5v6416 Probe Ok");
+    PW_LOG_ERROR("PI4IOE5V6416 Probe Failed");
   }
   return probe_result;
 }
 
 void Device::LogControllerInfo() {
-  auto port0 =
-      device_.ReadRegister8(0x0, pw::chrono::SystemClock::for_at_least(10ms));
-  if (port0.ok()) {
-    PW_LOG_INFO("port 0: %02x", *port0);
+  pw::Result<uint8_t> port0_result = ReadPort0();
+  if (port0_result.ok()) {
+    PW_LOG_INFO("Port 0: %02x", port0_result.value());
+    std::bitset<8> button(port0_result.value());
+    std::string button_str = button.to_string();
+    PW_LOG_INFO("Buttons: %s", button_str.data());
   } else {
-    PW_LOG_INFO("port 0 read failed");
+    PW_LOG_ERROR("Port 0 read failed");
   }
 
-  auto port1 =
-      device_.ReadRegister8(0x1, pw::chrono::SystemClock::for_at_least(10ms));
-  if (port1.ok()) {
-    PW_LOG_INFO("port 1: %02x", *port1);
+  pw::Result<uint8_t> port1_result = ReadPort1();
+  if (port1_result.ok()) {
+    PW_LOG_INFO("Port 1: %02x", port1_result.value());
   } else {
-    PW_LOG_INFO("port 1 read failed");
+    PW_LOG_ERROR("Port 1 read failed");
   }
 }
 
diff --git a/lib/pi4ioe5v6416/public/pi4ioe5v6416/device.h b/lib/pi4ioe5v6416/public/pi4ioe5v6416/device.h
index 89d7f91..b6dd4f9 100644
--- a/lib/pi4ioe5v6416/public/pi4ioe5v6416/device.h
+++ b/lib/pi4ioe5v6416/public/pi4ioe5v6416/device.h
@@ -19,6 +19,7 @@
 #include "pw_i2c/address.h"
 #include "pw_i2c/initiator.h"
 #include "pw_i2c/register_device.h"
+#include "pw_result/result.h"
 #include "pw_status/status.h"
 
 namespace pw::pi4ioe5v6416 {
@@ -31,6 +32,8 @@
   Status Enable();
   Status Probe();
   void LogControllerInfo();
+  pw::Result<uint8_t> ReadPort0();
+  pw::Result<uint8_t> ReadPort1();
 
  private:
   pw::i2c::Initiator& initiator_;