Skip to content

Commit

Permalink
Merge pull request #1339 from jeromebrunet/dev/matched
Browse files Browse the repository at this point in the history
Resource: add udev based Sysfs GPIO support
  • Loading branch information
jluebbe authored Mar 20, 2024
2 parents 51ea81f + 63873ee commit 430ec4c
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 20 deletions.
58 changes: 43 additions & 15 deletions doc/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,48 @@ NetworkHIDRelay
+++++++++++++++
A :any:`NetworkHIDRelay` describes an `HIDRelay`_ exported over the network.

SysfsGPIO
+++++++++

A :any:`SysfsGPIO` resource describes a GPIO line.

.. code-block:: yaml
SysfsGPIO:
index: 12
Arguments:
- index (int): index of the GPIO line

Used by:
- `GpioDigitalOutputDriver`_

MatchedSysfsGpio
++++++++++++++++
A MatchedSysfsGpio resource describes a GPIO line, like a SysfsGPIO.
The gpiochip is identified by matching udev properties. This allows
identification through hot-plugging or rebooting for controllers like
USB based gpiochips.

.. code-block:: yaml
MatchedSysfsGpio:
match:
'@SUBSYSTEM': 'usb'
'@ID_SERIAL_SHORT': 'D38EJ8LF'
pin: 0
The example would search for a USB gpiochip with the key `ID_SERIAL_SHORT`
and the value `D38EJ8LF` and use the pin 0 of this device.
The `ID_SERIAL_SHORT` property is set by the usb_id builtin helper program.

Arguments:
- match (dict): key and value pairs for a udev match, see `udev Matching`_
- pin (int): gpio pin number within the matched gpiochip.

Used by:
- `GpioDigitalOutputDriver`_

NetworkService
~~~~~~~~~~~~~~
A :any:`NetworkService` describes a remote SSH connection.
Expand Down Expand Up @@ -958,21 +1000,6 @@ Arguments:
Used by:
- `USBVideoDriver`_

SysfsGPIO
~~~~~~~~~
A :any:`SysfsGPIO` resource describes a GPIO line.

.. code-block:: yaml
SysfsGPIO:
index: 12
Arguments:
- index (int): index of the GPIO line

Used by:
- `GpioDigitalOutputDriver`_

NetworkUSBVideo
~~~~~~~~~~~~~~~
A :any:`NetworkUSBVideo` resource describes a `USBVideo`_ resource available
Expand Down Expand Up @@ -2142,6 +2169,7 @@ While the driver automatically exports the GPIO, it does not configure it in any
Binds to:
gpio:
- `SysfsGPIO`_
- `MatchedSysfsGPIO`_
- NetworkSysfsGPIO

Implements:
Expand Down
13 changes: 8 additions & 5 deletions labgrid/remote/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,11 +579,13 @@ class GPIOSysFSExport(ResourceExport):

def __attrs_post_init__(self):
super().__attrs_post_init__()
local_cls_name = self.cls
self.data['cls'] = f"Network{self.cls}"
from ..resource import base
local_cls = getattr(base, local_cls_name)
self.local = local_cls(target=None, name=None, **self.local_params)
if self.cls == "SysfsGPIO":
from ..resource.base import SysfsGPIO
self.local = SysfsGPIO(target=None, name=None, **self.local_params)
elif self.cls == "MatchedSysfsGPIO":
from ..resource.udev import MatchedSysfsGPIO
self.local = MatchedSysfsGPIO(target=None, name=None, **self.local_params)
self.data['cls'] = "NetworkSysfsGPIO"
self.export_path = Path(GPIOSysFSExport._gpio_sysfs_path_prefix,
f'gpio{self.local.index}')
self.system_exported = False
Expand Down Expand Up @@ -624,6 +626,7 @@ def _stop(self, start_params):
unexport.write(str(index).encode('utf-8'))

exports["SysfsGPIO"] = GPIOSysFSExport
exports["MatchedSysfsGPIO"] = GPIOSysFSExport


@attr.s
Expand Down
1 change: 1 addition & 0 deletions labgrid/resource/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
HIDRelay,
IMXUSBLoader,
LXAUSBMux,
MatchedSysfsGPIO,
MXSUSBLoader,
RKUSBLoader,
SiSPMPowerPort,
Expand Down
4 changes: 4 additions & 0 deletions labgrid/resource/suggest.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
HIDRelay,
USBDebugger,
USBPowerPort,
MatchedSysfsGPIO
)
from ..util import dump

Expand Down Expand Up @@ -56,6 +57,7 @@ def __init__(self, args):
self.resources.append(HIDRelay(**args))
self.resources.append(USBDebugger(**args))
self.resources.append(USBPowerPort(**args, index=0))
self.resources.append(MatchedSysfsGPIO(**args, pin=0))

def suggest_callback(self, resource, meta, suggestions):
cls = type(resource).__name__
Expand Down Expand Up @@ -84,6 +86,8 @@ def suggest_callback(self, resource, meta, suggestions):
))
if cls == 'USBPowerPort':
print(' index: ?')
if cls == 'MatchedSysfsGPIO':
print(' pin: ?')
print(" ---")
print()

Expand Down
29 changes: 29 additions & 0 deletions labgrid/resource/udev.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,3 +718,32 @@ def filter_match(self, device):
return False

return super().filter_match(device)

@target_factory.reg_resource
@attr.s(eq=False)
class MatchedSysfsGPIO(USBResource):
"""The MatchedSysfsGPIO described a SysfsGPIO matched by Udev
Args:
pin (int): gpio pin number within the matched gpiochip."""
pin = attr.ib(default=None, validator=attr.validators.instance_of(int))
index = None

def __attrs_post_init__(self):
self.match['SUBSYSTEM'] = 'gpio'
super().__attrs_post_init__()

def filter_match(self, device):
# Filter out the char device
if device.properties.get('DEVNAME') is not None:
return False
return super().filter_match(device)

def update(self):
super().update()
if self.device is not None:
if self.pin >= int(self.read_attr('ngpio')):
raise ValueError("MatchedSysfsGPIO pin out of bound")
self.index = int(self.read_attr('base')) + self.pin
else:
self.index = None

0 comments on commit 430ec4c

Please sign in to comment.