This may not be in great demand, but I created a Raspberry Pi version of the following "USB-connected PC volume control button created with Arduino" and this is the procedure and explanation memo.
A rough description of turning a Raspberry Pi into a USB device (gadget)
Normally, the USB port on the Raspberry Pi is used to connect peripherals used with the Raspberry Pi, such as a mouse or keyboard, but the Raspberry Pi Zero can connect to a PC with a USB cable to make itself a USB device (gadget) for the PC.
USB has a standard called OTG, which allows USB devices to connect to each other without a PC, making it possible to be the parent or child of a USB device.
And since Linux includes a USB OTG driver, we are using it to turn the Raspberry Pi into a USB gadget by making the Raspberry Pi act as a USB child device.
Unfortunately, however, not all Raspberry Pi can be USB gadgets, and according to the article below, other Raspberry Pi cannot be USB gadgets due to hardware specifications.
It may seem daunting to create a full-scratch USB gadget using a driver, but fortunately, Linux includes a USB OTG wrapper framework called "Composite USB Gadgets" that allows you to easily create USB gadgets with just a few settings. Fortunately, Linux comes with a USB OTG wrapper framework called "Composite USB Gadgets" that allows you to easily create USB gadgets with just a few configurations.
procedure
Loading USB OTG Driver and Composite USB Gadgets
Add the following to "/boot/config.txt
dtoverlay=dwc2
Add the following to "/etc/modules
dwc2 libcomposite
dwc2" is the USB OTG driver and "libcomposite" is the Composite USB Gadgets, which will be loaded when the Raspberry Pi starts.
Restart the Raspberry Pi.
Once the Raspberry Pi is up and running, Composite USB Gadgets is already running, followed by the creation of the USB gadget.
Creating USB gadgets
Execute the following shell script with sudo
.
usb_volume_button.sh
#!/bin/bash CONFIGS_HOME=/sys/kernel/config GADGET_NAME=g1 NAME=c NUMBER=1 FUNC_NAME=hid INSTANCE_NAME=usb0 BASE=${CONFIGS_HOME}/usb_gadget/${GADGET_NAME} mkdir -p ${BASE}/ cd ${BASE}/ echo 0x1d6b > idVendor echo 0x0104 > idProduct mkdir -p ${BASE}/configs/${NAME}.${NUMBER}/ mkdir -p ${BASE}/functions/${FUNC_NAME}.${INSTANCE_NAME}/ cd ${BASE}/functions/${FUNC_NAME}.${INSTANCE_NAME}/ echo 0 > protocol echo 0 > subclass echo 1 > report_length echo -ne \\x05\\x0c\\x09\\x01\\xa1\\x01\\x15\\x00\\x25\\x01\\x09\\xe9\\x09\\xea\\x09\\xe2\\x75\\x01\\x95\\x03\\x81\\x02\\x75\\x01\\x95\\x05\\x81\\x03\\xc0 > report_desc cd ${BASE}/ ln -s functions/${FUNC_NAME}.${INSTANCE_NAME} configs/${NAME}.${NUMBER} ls /sys/class/udc > UDC
The descriptors are the same as when created on the Arduino. However, ReportID seems to be determined by Composite USB Gadgets, and it did not work when specified, so I removed it here.
Running the shell script turns the Raspberry Pi into a USB gadget for volume control controller.
When the Raspberry Pi is connected to the PC, it will appear in the Device Manager under [Human Interface Devices] - [HID Compliant Consumer Control Devices].
There are several "HID compliant consumer control devices," but if you look at [Properties]-[Details]-[Hardware ID], the one with vendor ID "1D6B" and product ID "0104" set in the shell script above is the Raspberry Pi.
Transmission of volume control signal
When the USB gadget is created, a file named "/dev/hidg0" is created, and data is sent to and received from the PC via this file.
The volume control signals were assigned in the order of "volume UP, volume DOWN, mute" from the lower bits, so a sample transmission is shown below. (Run with sudo)
send_volume_control_signal.sh
#!/bin/bash echo -ne "\x1" > /dev/hidg0 # UP echo -ne "\0" > /dev/hidg0 # RELEASE echo -ne "\x2" > /dev/hidg0 # DOWN echo -ne "\0" > /dev/hidg0 # RELEASE echo -ne "\x4" > /dev/hidg0 # MUTE echo -ne "\0" > /dev/hidg0 # RELEASE
Delete USB gadget
USB gadgets created with sudo echo "" > /sys/kernel/config/usb_gadget/g1/UDC
will be deleted.
You can create and delete USB gadgets while connected to a PC.
When deleted, the USB gadget appears to have been removed from the PC.
application
When creating other HIDs
- For the mouse, "protocol as 2, subclass as 1."
- For keyboard, "protocol as 1, subclass as 1"
- Otherwise, "0 for protocol, 0 for subclass"
Set to
Normally, HIDs are recognized after the PC OS is booted, but this setting allows the mouse and keyboard to be recognized and used even at PC boot time.
Composite USB Gadgets can create USB gadgets for storage, LAN, serial, etc. in addition to HID, so I would like to try it when I have time.
Composite USB Gadgets Function List
1. ACM function 2. ECM function 3. ECM subset function 4. EEM function 5. FFS function 6. HID function 7. LOOPBACK function 8. MASS STORAGE function 9. MIDI function 10. NCM function 11. OBEX function 12. PHONET function 13. RNDIS function 14. SERIAL function 15. SOURCESINK function 16. UAC1 function (legacy implementation) 17. UAC2 function 18. UVC function 19. PRINTER function 20. UAC1 function (new API)