1.. _module-pw_digital_io_rp2040: 2 3-------------------- 4pw_digital_io_rp2040 5-------------------- 6``pw_digital_io_rp2040`` implements the :ref:`module-pw_digital_io` interface using 7the `Raspberry Pi Pico SDK <https://github.com/raspberrypi/pico-sdk/>`_. 8 9Setup 10===== 11Use of this module requires setting up the Pico SDK for use with Pigweed. Follow 12the steps in :ref:`target-rp2040` to get setup. 13 14Examples 15======== 16Use ``pw::digital_io::Rp2040DigitalIn`` and 17``pw::digital_io::Rp2040DigitalInOut`` classes to control GPIO pins. 18 19Example code to use GPIO pins: 20 21.. code-block:: cpp 22 23 #include "pw_digital_io/polarity.h" 24 #include "pw_digital_io_rp2040/digital_io.h" 25 26 using pw::digital_io::Polarity; 27 using pw::digital_io::Rp2040Config; 28 using pw::digital_io::Rp2040DigitalIn; 29 using pw::digital_io::Rp2040DigitalInOut; 30 using pw::digital_io::State; 31 32 constexpr Rp2040Config output_pin_config{ 33 .pin = 15, 34 .polarity = Polarity::kActiveLow, 35 }; 36 constexpr Rp2040Config input_pin_config{ 37 .pin = 16, 38 .polarity = Polarity::kActiveHigh, 39 }; 40 constexpr Rp2040Config button_connected_to_ground_config{ 41 .pin = 12, 42 .polarity = Polarity::kActiveLow, 43 .enable_pull_up = true, 44 }; 45 46 // Config output pin: 47 Rp2040DigitalInOut out(output_pin_config); 48 out.Enable(); 49 50 // Set the output pin active. 51 // This pulls pin to ground since the polarity is kActiveLow. 52 out.SetState(State::kActive); 53 54 // Config input pins: 55 Rp2040DigitalIn in(input_pin_config); 56 in.Enable(); 57 58 Rp2040DigitalIn button(button_connected_to_ground_config); 59 button.Enable(); 60 61 // Get the pin state. Since the polarity is kActiveHigh this will return 62 // State::kActive if the pin is high or and State::kInactive if the pin is 63 // low (grounded). 64 State pin_state = in.GetState(); 65 66 auto button_result = button.GetState(); 67 if (button_result.ok()) { 68 if (button_result.value() == State::kActive) { 69 PW_LOG_INFO("Button is pressed."); 70 } 71 if (button_result.value() == State::kInactive) { 72 PW_LOG_INFO("Button is released."); 73 } 74 } 75