xref: /aosp_15_r20/external/pigweed/pw_digital_io_rp2040/public/pw_digital_io_rp2040/digital_io.h (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 #pragma once
16 
17 #include <cstdint>
18 
19 #include "pw_digital_io/digital_io.h"
20 #include "pw_digital_io/polarity.h"
21 
22 namespace pw::digital_io {
23 
24 struct Rp2040Config {
25   uint16_t pin;
26   Polarity polarity;
27   bool enable_pull_up = false;
28   bool enable_pull_down = false;
29 
30   bool operator==(const Rp2040Config& rhs) const {
31     return polarity == rhs.polarity && pin == rhs.pin;
32   }
PhysicalToLogicalRp2040Config33   State PhysicalToLogical(const bool hal_value) const {
34     return polarity == Polarity::kActiveHigh ? State(hal_value)
35                                              : State(!hal_value);
36   }
LogicalToPhysicalRp2040Config37   bool LogicalToPhysical(const State state) const {
38     return polarity == Polarity::kActiveHigh ? (bool)state : !(bool)state;
39   }
40 };
41 
42 class Rp2040DigitalInOut : public DigitalInOut {
43  public:
44   Rp2040DigitalInOut(Rp2040Config config);
45 
46  private:
47   Status DoEnable(bool enable) override;
48   Status DoSetState(State level) override;
49   Result<State> DoGetState() override;
50 
51   Rp2040Config config_;
52 };
53 
54 class Rp2040DigitalIn : public DigitalIn {
55  public:
56   Rp2040DigitalIn(Rp2040Config config);
57 
58  private:
59   Status DoEnable(bool enable) override;
60   Result<State> DoGetState() override;
61 
62   Rp2040Config config_;
63 };
64 
65 }  // namespace pw::digital_io
66