Skip to content

Commit

Permalink
cores/usb/generic_device: add vendor specific request handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
povauboin committed Sep 12, 2024
1 parent c079119 commit 564a68f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lambdalib/cores/usb/generic_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def __init__(self, pins,

self.ep_pairs = ep_pairs
self.ep_sizes = ep_sizes
self.control_ep_handlers = []

self.kwargs = kwargs

Expand Down Expand Up @@ -104,14 +105,21 @@ def create_descriptors(self):

return descriptors

def add_control_ep_handler(self, handler):
self.control_ep_handlers.append(handler)

def elaborate(self, platform):
m = Module()

m.submodules.usb = usb = USBDevice(bus=self.pins, **self.kwargs)

# Add our standard control endpoint to the device.
descriptors = self.create_descriptors()
usb.add_standard_control_endpoint(descriptors)
control_ep = usb.add_standard_control_endpoint(descriptors)

# Add optional custom requests handlers (vendor)
for handler in self.control_ep_handlers:
control_ep.add_request_handler(handler)

for k, (i, o) in enumerate(self.ep_sizes):
# Add a stream OUT endpoint to our device.
Expand Down
6 changes: 6 additions & 0 deletions lambdalib/software/usb/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ def __init__(self, bulksize, pid=0x1234, vid=0xffff, idx=0, interface=0):
raise usb1.USBError("Device not present, check udev rules")
self.handle.claimInterface(self.interface)

def control_write(self, *args):
self.handle.controlWrite(*args)

def control_read(self, *args):
return self.handle.controlRead(*args)

def get_endpoint(self, num, asynchronous=True):
return USBEndpoint(self.handle, self.bulksize, num,
asynchronous=asynchronous, context=self.context)
Expand Down

0 comments on commit 564a68f

Please sign in to comment.