It's easy to use the USB Host BFF with CircuitPython and the max3421e core module. This module allows you to easily write Python code that lets you utilize the MAX3421E USB Host chip. CircuitPython support is available for these QT Py and Xiao form factor boards:
This support is currently available in the 9.1 beta builds of CircuitPython. On CircuitPython.org, search for and select your board via the Downloads tab. On the board page, scroll down to the 9.1 beta and select your UF2 and/or BIN file.
Plug a USB Host BFF into your QT Py or Xiao form factor board. Then, plug an OTG adapter into the BFF. Here's an example connecting a QT Py ESP32-S3 to the BFF with a micro-B USB OTG cable plugged into the micro-B USB port on the BFF.
CircuitPython Usage
To use with CircuitPython, you need to update code.py with the example script.
Thankfully, we can do this in one go. In the example below, click the Download Project Bundle button below to download the code.py file in a zip file. Extract the contents of the zip file and copy the code.py file to your CIRCUITPY drive.
Only core modules are used for these examples. No additional libraries need to be added to the /lib folder.
Device Info Example Code
Once everything is saved to the CIRCUITPY drive, connect to the serial console to see the data printed out!
# SPDX-FileCopyrightText: 2024 Scott Shawcroft for Adafruit Industries # # SPDX-License-Identifier: MIT """USB Host BFF Device Info CircuitPython Example""" import time import board import max3421e import usb spi = board.SPI() cs = board.A1 irq = board.A2 host_chip = max3421e.Max3421E(spi, chip_select=cs, irq=irq) while True: print("Finding devices:") for device in usb.core.find(find_all=True): # pylint: disable=line-too-long print(f"{device.idVendor:04x}:{device.idProduct:04x}: {device.manufacturer} {device.product}") time.sleep(5)
First, the MAX3421E is instantiated over SPI. In the loop, a scan is performed every 5 seconds on the USB host port for a connected USB device. If a device is detected, its VID, PID, manufacturer name and product name are printed to the serial monitor. You can plug and unplug devices to experiment with this.
HID Device Report Example
Update the code.py file on your CIRCUITPY drive with the code below. You'll need a USB HID keyboard to read device reports from.
# SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries # # SPDX-License-Identifier: MIT """USB Host BFF CircuitPython Example Read key report from attached keyboard""" import time import array import board import max3421e import usb spi = board.SPI() cs = board.A1 irq = board.A2 host_chip = max3421e.Max3421E(spi, chip_select=cs, irq=irq) device = None vid = None pid = None while device is None: for d in usb.core.find(find_all=True): vid = d.idVendor pid = d.idProduct device = usb.core.find(idVendor=vid, idProduct=pid) time.sleep(1) device.set_configuration() print(f"{device.idVendor:04x}:{device.idProduct:04x}: {device.manufacturer} {device.product}") # Test to see if the kernel is using the device and detach it. if device.is_kernel_driver_active(0): device.detach_kernel_driver(0) # Boot keyboards have 8 byte reports buf = array.array("B", [0] * 8) while True: try: count = device.read(0x81, buf) # pylint: disable=broad-except except Exception as e: continue for i in range(0, 8): print(buf[i], end=" ") print()
In this example, the HID input report from an HID keyboard are logged to the serial monitor. The code begins by instantiating the MAX3421E over SPI. Then, a scan is performed to find a USB device. When the HID device is found, its VID and PID are used to instantiate the device.
In the loop, the read()
function is used to read the incoming report from the device. When a report is received, the buffer is printed to the serial monitor. The buffer is 8 bytes long. The first index contains any modifier keys and the third index contains the key number. For example, the letter A is 0x04 or 4
. When all keys are released, the buffer is filled with a 0
byte in each index.
If you plug in a USB HID keyboard to the USB Host BFF and press keys on it, you'll see those keycodes print to the serial monitor as keys are pressed and released.
Text editor powered by tinymce.