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