summaryrefslogtreecommitdiff
path: root/examples/usb/usb_simple_host_pyusb.py
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2024-04-30 11:33:39 +1000
committerDamien George <damien@micropython.org>2024-05-13 11:26:29 +1000
commitb2df89c417841a7db18120fb40e1dee96cf71865 (patch)
tree86e73b68ac02c48ba011ac91fc6349fee3fa2c1a /examples/usb/usb_simple_host_pyusb.py
parentc3301da17626663d7d6e5fc79a1010842528c9b1 (diff)
examples/usb: Add a very simple USBDevice example with host.
Signed-off-by: Damien George <damien@micropython.org>
Diffstat (limited to 'examples/usb/usb_simple_host_pyusb.py')
-rwxr-xr-xexamples/usb/usb_simple_host_pyusb.py47
1 files changed, 47 insertions, 0 deletions
diff --git a/examples/usb/usb_simple_host_pyusb.py b/examples/usb/usb_simple_host_pyusb.py
new file mode 100755
index 000000000..d8ac2dd9c
--- /dev/null
+++ b/examples/usb/usb_simple_host_pyusb.py
@@ -0,0 +1,47 @@
+#!/usr/bin/env python3
+#
+# Host side of the `usb_simple_device.py` example. This must be run using standard
+# Python on a PC. See further instructions in `usb_simple_device.py`.
+
+import sys
+import usb.core
+import usb.util
+
+# VID and PID of the custom USB device.
+VID = 0xF055
+PID = 0x9999
+
+# USB endpoints used by the device.
+EP_OUT = 0x01
+EP_IN = 0x81
+
+
+def main():
+ # Search for the custom USB device by VID/PID.
+ dev = usb.core.find(idVendor=VID, idProduct=PID)
+
+ if dev is None:
+ print("No USB device found")
+ sys.exit(1)
+
+ # Claim the USB device.
+ usb.util.claim_interface(dev, 0)
+
+ # Read the device's strings.
+ for i in range(0x11, 0x17):
+ print(f"str{i}:", usb.util.get_string(dev, i))
+
+ # Test writing to the device.
+ ret = dev.write(EP_OUT, b"01234567", timeout=1000)
+ print(ret)
+
+ # Test reading from the device.
+ print(dev.read(EP_IN, 64))
+
+ # Release the USB device.
+ usb.util.release_interface(dev, 0)
+ usb.util.dispose_resources(dev)
+
+
+if __name__ == "__main__":
+ main()