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 28 bool operator==(const Rp2040Config& rhs) const { 29 return polarity == rhs.polarity && pin == rhs.pin; 30 } PhysicalToLogicalRp2040Config31 State PhysicalToLogical(const bool hal_value) const { 32 return polarity == Polarity::kActiveHigh ? State(hal_value) 33 : State(!hal_value); 34 } LogicalToPhysicalRp2040Config35 bool LogicalToPhysical(const State state) const { 36 return polarity == Polarity::kActiveHigh ? (bool)state : !(bool)state; 37 } 38 }; 39 40 class Rp2040DigitalInOut : public DigitalInOut { 41 public: 42 Rp2040DigitalInOut(Rp2040Config config); 43 44 private: 45 Status DoEnable(bool enable) override; 46 Status DoSetState(State level) override; 47 Result<State> DoGetState() override; 48 49 Rp2040Config config_; 50 }; 51 52 class Rp2040DigitalIn : public DigitalIn { 53 public: 54 Rp2040DigitalIn(Rp2040Config config); 55 56 private: 57 Status DoEnable(bool enable) override; 58 Result<State> DoGetState() override; 59 60 Rp2040Config config_; 61 }; 62 63 } // namespace pw::digital_io 64