-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpio.cpp
More file actions
187 lines (168 loc) · 5.33 KB
/
gpio.cpp
File metadata and controls
187 lines (168 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright OpenBMC Authors
#include "gpio.hpp"
#include "utilities.hpp"
#include <cstring>
#include <filesystem>
#include <format>
#include <iostream>
#include <unordered_map>
static std::unordered_map<std::string, gpiod::line> io;
using namespace std::chrono_literals;
namespace gpio
{
void set(const char* line_name, int value,
std::chrono::milliseconds find_timeout)
{
std::cerr << std::format("{} Request to set to {}\n", line_name, value);
std::chrono::milliseconds polling_time = 10ms;
gpiod::line& line = io[line_name];
if (!line)
{
do
{
line = gpiod::find_line(line_name);
if (!line)
{
std::cerr << std::format(
"{} not found yet, waiting and retrying\n", line_name);
sleep_milliseconds(polling_time);
find_timeout -= polling_time;
}
} while (!line && find_timeout > 0s);
if (!line && find_timeout <= 0s)
{
std::cerr << std::format("{} Unable to find\n", line_name);
return;
}
try
{
line.request({app_name, gpiod::line_request::DIRECTION_OUTPUT, 0},
value);
}
catch (const std::system_error& e)
{
std::cerr << std::format(
"{} unable to set direction and value {}\n", line_name,
e.what());
return;
}
// No need to set if the init did it for us
std::cerr << std::format("{} Set to {}\n", line_name, value);
return;
}
std::cerr << std::format("{} Settingto {}\n", line_name, value);
line.set_value(value);
}
int get(const char* line_name)
{
std::cerr << std::format("{} Request to get\n", line_name);
gpiod::line line = gpiod::find_line(line_name);
if (!line)
{
std::cerr << std::format("{} Set unable to find\n", line_name);
return -1;
}
try
{
line.request({app_name, gpiod::line_request::DIRECTION_INPUT, 0});
}
catch (const std::system_error& e)
{
std::cerr << std::format("{} unable to set {}\n", line_name, e.what());
}
int value = line.get_value();
std::cerr << std::format("{} was {}\n", line_name, value);
return value;
}
Event::Event(const char* line_name_in, int value_in) :
line_name(line_name_in), value(value_in)
{
line = gpiod::find_line(line_name);
if (!line)
{
std::cerr << std::format("{} GpioEvent: Unable to find\n", line_name);
return;
}
int edge = (value != 0) ? ::gpiod::line_request::EVENT_RISING_EDGE
: ::gpiod::line_request::EVENT_FALLING_EDGE;
line.request({app_name, edge, 0});
int val = line.get_value();
if (val == value)
{
std::cerr << std::format("{} GpioEvent is already {}\n", line_name,
val);
}
else
{
std::cerr << std::format("GpioEvent created for {}\n", line_name);
}
}
EventResult Event::wait()
{
if (!line)
{
std::cerr << std::format("Line {} wasn't initialized\n", line_name);
return EventResult::Error;
}
std::cerr << std::format("{} Waiting to go to {}\n", line_name,
(value != 0) ? "assert" : "deassert");
auto events = line.event_wait(std::chrono::seconds(120));
if (!events)
{
std::cerr << std::format("{} Timeout\n", line_name);
return EventResult::Timeout;
}
std::cerr << std::format("{} Asserted\n", line_name);
return EventResult::Asserted;
}
void set_raw(unsigned int chip_num, unsigned int bit_num, int value)
{
std::string syspath = std::format("gpiochip{}", chip_num);
std::cerr << std::format("Setting gpiochip{} bit {} to {}\n", chip_num,
bit_num, value);
try
{
gpiod::chip chip(syspath);
gpiod::line line = chip.get_line(bit_num);
line.request({app_name, gpiod::line_request::DIRECTION_OUTPUT, 0},
value);
std::cerr << std::format("gpiochip{} bit {} set to {}\n", chip_num,
bit_num, value);
}
catch (const std::system_error& e)
{
std::cerr << std::format("Error setting gpiochip{} bit {}: {}\n",
chip_num, bit_num, e.what());
}
}
int find_chip_idx_from_dir(std::string_view device_path)
{
std::string gpio_chip;
for (const auto& entry : std::filesystem::directory_iterator(device_path))
{
std::string path = entry.path().string();
if (path.find("gpiochip") != std::string::npos)
{
gpio_chip =
path.substr(path.find("gpiochip") + std::strlen("gpiochip"));
break;
}
}
if (gpio_chip.empty())
{
std::cerr << "Error: Could not find GPIO chip number\n";
return -ENOENT;
}
std::cerr << "Found GPIO chip: gpiochip" << gpio_chip << "\n";
unsigned int gpiochipint = 0;
std::from_chars_result r =
std::from_chars(&*gpio_chip.begin(), &*gpio_chip.end(), gpiochipint);
if (r.ec != std::error_code() || r.ptr != &*gpio_chip.end())
{
std::cout << "Failed to convert gpiochip\n";
return -EINVAL;
}
return gpiochipint;
}
} // namespace gpio