Brandon Wyman | 3f1242f | 2020-01-28 13:11:25 -0600 | [diff] [blame] | 1 | #include "util.hpp" |
| 2 | |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 3 | #include <gpiod.hpp> |
| 4 | |
Brandon Wyman | 3f1242f | 2020-01-28 13:11:25 -0600 | [diff] [blame] | 5 | namespace phosphor::power::psu |
| 6 | { |
| 7 | |
| 8 | const UtilBase& getUtils() |
| 9 | { |
| 10 | static Util util; |
| 11 | return util; |
| 12 | } |
| 13 | |
Adriana Kobylak | 3ca062a | 2021-10-20 15:27:23 +0000 | [diff] [blame] | 14 | GPIOInterface::GPIOInterface(const std::string& namedGpio) |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 15 | { |
| 16 | try |
| 17 | { |
| 18 | line = gpiod::find_line(namedGpio); |
Adriana Kobylak | a588eaf | 2021-09-20 20:05:25 +0000 | [diff] [blame] | 19 | if (!line) |
| 20 | { |
| 21 | throw std::runtime_error("Line does not exist: " + namedGpio); |
| 22 | } |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 23 | } |
Patrick Williams | c1d4de5 | 2021-10-06 12:45:57 -0500 | [diff] [blame] | 24 | catch (const std::exception& e) |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 25 | { |
Adriana Kobylak | c0a0758 | 2021-10-13 15:52:25 +0000 | [diff] [blame] | 26 | throw std::runtime_error(std::string("Failed to find line: ") + |
| 27 | e.what()); |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 28 | } |
| 29 | } |
| 30 | |
Adriana Kobylak | 3ca062a | 2021-10-20 15:27:23 +0000 | [diff] [blame] | 31 | std::unique_ptr<GPIOInterfaceBase> |
| 32 | GPIOInterface::createGPIO(const std::string& namedGpio) |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 33 | { |
Adriana Kobylak | 3ca062a | 2021-10-20 15:27:23 +0000 | [diff] [blame] | 34 | return std::make_unique<GPIOInterface>(namedGpio); |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Adriana Kobylak | 3ca062a | 2021-10-20 15:27:23 +0000 | [diff] [blame] | 37 | std::string GPIOInterface::getName() const |
B. J. Wyman | d8b8cb1 | 2021-07-15 22:03:34 +0000 | [diff] [blame] | 38 | { |
| 39 | return line.name(); |
| 40 | } |
| 41 | |
Adriana Kobylak | 3ca062a | 2021-10-20 15:27:23 +0000 | [diff] [blame] | 42 | int GPIOInterface::read() |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 43 | { |
| 44 | using namespace phosphor::logging; |
| 45 | |
| 46 | int value = -1; |
| 47 | |
| 48 | if (!line) |
| 49 | { |
| 50 | log<level::ERR>("Failed line"); |
| 51 | throw std::runtime_error{std::string{"Failed to find line"}}; |
| 52 | } |
| 53 | |
| 54 | try |
| 55 | { |
| 56 | line.request({__FUNCTION__, gpiod::line_request::DIRECTION_INPUT, |
| 57 | gpiod::line_request::FLAG_ACTIVE_LOW}); |
| 58 | try |
| 59 | { |
| 60 | value = line.get_value(); |
| 61 | } |
Patrick Williams | c1d4de5 | 2021-10-06 12:45:57 -0500 | [diff] [blame] | 62 | catch (const std::exception& e) |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 63 | { |
| 64 | log<level::ERR>( |
| 65 | fmt::format("Failed to get_value of GPIO line: {}", e.what()) |
| 66 | .c_str()); |
| 67 | line.release(); |
| 68 | throw; |
| 69 | } |
| 70 | |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 71 | line.release(); |
| 72 | } |
Patrick Williams | c1d4de5 | 2021-10-06 12:45:57 -0500 | [diff] [blame] | 73 | catch (const std::exception& e) |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 74 | { |
| 75 | log<level::ERR>("Failed to request GPIO line", |
| 76 | entry("MSG=%s", e.what())); |
| 77 | throw; |
| 78 | } |
| 79 | |
| 80 | return value; |
| 81 | } |
| 82 | |
Adriana Kobylak | 52245b6 | 2021-09-13 15:46:21 +0000 | [diff] [blame] | 83 | void GPIOInterface::write(int value, std::bitset<32> flags) |
| 84 | { |
| 85 | using namespace phosphor::logging; |
| 86 | |
| 87 | if (!line) |
| 88 | { |
| 89 | log<level::ERR>("Failed line"); |
| 90 | throw std::runtime_error{std::string{"Failed to find line"}}; |
| 91 | } |
| 92 | |
| 93 | try |
| 94 | { |
| 95 | line.request( |
| 96 | {__FUNCTION__, gpiod::line_request::DIRECTION_OUTPUT, flags}, |
| 97 | value); |
| 98 | |
| 99 | line.release(); |
| 100 | } |
| 101 | catch (std::exception& e) |
| 102 | { |
| 103 | log<level::ERR>("Failed to set GPIO line", entry("MSG=%s", e.what()), |
| 104 | entry("VALUE=%d", value)); |
| 105 | throw; |
| 106 | } |
| 107 | } |
| 108 | |
Brandon Wyman | 18a24d9 | 2022-04-19 22:48:34 +0000 | [diff] [blame] | 109 | void GPIOInterface::toggleLowHigh(const std::chrono::milliseconds& delay) |
| 110 | { |
| 111 | auto flags = gpiod::line_request::FLAG_OPEN_DRAIN; |
| 112 | write(0, flags); |
| 113 | std::this_thread::sleep_for(delay); |
| 114 | write(1, flags); |
| 115 | } |
| 116 | |
Adriana Kobylak | 3ca062a | 2021-10-20 15:27:23 +0000 | [diff] [blame] | 117 | std::unique_ptr<GPIOInterfaceBase> createGPIO(const std::string& namedGpio) |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 118 | { |
Adriana Kobylak | 3ca062a | 2021-10-20 15:27:23 +0000 | [diff] [blame] | 119 | return GPIOInterface::createGPIO(namedGpio); |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Brandon Wyman | 3f1242f | 2020-01-28 13:11:25 -0600 | [diff] [blame] | 122 | } // namespace phosphor::power::psu |