xref: /aosp_15_r20/external/pigweed/pw_digital_io_rp2040/digital_io.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2023 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #include "pw_digital_io_rp2040/digital_io.h"
16 
17 #include "hardware/gpio.h"
18 #include "pw_digital_io/digital_io.h"
19 #include "pw_status/status.h"
20 
21 namespace pw::digital_io {
22 
Rp2040DigitalIn(Rp2040Config config)23 Rp2040DigitalIn::Rp2040DigitalIn(Rp2040Config config) : config_(config) {}
24 
DoEnable(bool enable)25 Status Rp2040DigitalIn::DoEnable(bool enable) {
26   if (!enable) {
27     gpio_deinit(config_.pin);
28     return OkStatus();
29   }
30 
31   gpio_init(config_.pin);
32   gpio_set_dir(config_.pin, GPIO_IN);
33   gpio_set_pulls(config_.pin, config_.enable_pull_up, config_.enable_pull_down);
34   return OkStatus();
35 }
36 
DoGetState()37 Result<State> Rp2040DigitalIn::DoGetState() {
38   if (gpio_get_function(config_.pin) != GPIO_FUNC_SIO ||
39       gpio_get_dir(config_.pin) != GPIO_IN) {
40     return Status::FailedPrecondition();
41   }
42 
43   const bool pin_value = gpio_get(config_.pin);
44   const State state = config_.PhysicalToLogical(pin_value);
45   return pw::Result<State>(state);
46 }
47 
Rp2040DigitalInOut(Rp2040Config config)48 Rp2040DigitalInOut::Rp2040DigitalInOut(Rp2040Config config) : config_(config) {}
49 
DoEnable(bool enable)50 Status Rp2040DigitalInOut::DoEnable(bool enable) {
51   if (!enable) {
52     gpio_deinit(config_.pin);
53     return OkStatus();
54   }
55 
56   gpio_init(config_.pin);
57   gpio_set_dir(config_.pin, GPIO_OUT);
58   gpio_set_pulls(config_.pin, config_.enable_pull_up, config_.enable_pull_down);
59   return OkStatus();
60 }
61 
DoSetState(State level)62 Status Rp2040DigitalInOut::DoSetState(State level) {
63   if (gpio_get_function(config_.pin) != GPIO_FUNC_SIO ||
64       gpio_get_dir(config_.pin) != GPIO_OUT) {
65     return Status::FailedPrecondition();
66   }
67 
68   gpio_put(config_.pin, config_.LogicalToPhysical(level));
69   return OkStatus();
70 }
71 
DoGetState()72 Result<State> Rp2040DigitalInOut::DoGetState() {
73   if (gpio_get_function(config_.pin) != GPIO_FUNC_SIO ||
74       gpio_get_dir(config_.pin) != GPIO_OUT) {
75     return Status::FailedPrecondition();
76   }
77 
78   const bool pin_value = gpio_get(config_.pin);
79   const State state = config_.PhysicalToLogical(pin_value);
80   return pw::Result<State>(state);
81 }
82 
83 }  // namespace pw::digital_io
84