-
Notifications
You must be signed in to change notification settings - Fork 0
/
doc.go
64 lines (50 loc) · 2.32 KB
/
doc.go
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
// SPDX-FileCopyrightText: 2023 Kent Gibson <warthog618@gmail.com>
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
/*
Package gpiosim is a library for creating and controlling GPIO simulators for testing
users of the Linux GPIO uAPI (both v1 and v2).
The simulators are provided by the Linux [gpio-sim] kernel module and require a
recent kernel (5.19 or later) built with CONFIG_GPIO_SIM.
Simulators ([Sim]) contain one or more gpiochips ([Chip]), each with a
collection of lines being simulated. Configuring a simulator involves adding
banks ([Bank]), each containing the configuration for a gpiochip, to [NewSim],
which will construct the corresponding simulator using gpio-sim and take it live.
Once live, the [Chip] exposes lines which may be manipulated to drive the
GPIO uAPI from the kernel side.
For input lines, applying a pull using [Chip.SetPull], or related methods, controls
the level of the simulated line. For output lines, the [Chip.Level] method returns
the level the simulated line is being driven to by userspace.
For tests that only require vanilla lines on a single chip, the [Simpleton]
provides a slightly simpler interface.
Closing the [Sim] deconstructs the simulator, removing the gpio-sim
configuration and the corresponding gpiochips.
Configuring a simulator involves configfs, and manipulating the chips once live
involves sysfs, so root permissions are typically required to run a simulator.
# Example Usage
Create a [Simpleton] with 12 lines:
s, err := gpiosim.NewSimpleton(12)
s.SetPull(5, 1)
level, err := s.Level(3)
Creating a simulator with two chips, with 8 and 42 lines respectively, each with
several named lines and some hogged lines:
s, err := gpiosim.NewSim(
gpiosim.WithName("gpiosim_test"),
gpiosim.WithBank(gpiosim.NewBank("left", 8,
gpiosim.WithNamedLine(3, "LED0"),
gpiosim.WithNamedLine(5, "BUTTON1"),
gpiosim.WithHoggedLine(2, "piggy", gpiosim.HogDirectionOutputLow),
)),
gpiosim.WithBank(gpiosim.NewBank("right", 42,
gpiosim.WithNamedLine(3, "BUTTON2"),
gpiosim.WithNamedLine(4, "LED2"),
gpiosim.WithHoggedLine(7, "hogster", gpiosim.HogDirectionOutputHigh),
gpiosim.WithHoggedLine(9, "piggy", gpiosim.HogDirectionInput),
)),
)
c := &sim.Chips[0]
c.Pullup(5);
level, err := c.Level(3);
[gpio-sim]: https://www.kernel.org/doc/html/latest/admin-guide/gpio/gpio-sim.html
*/
package gpiosim