Skip to content

Commit 2b21595

Browse files
sago35deadprogram
authored andcommitted
machine/usb: add support for ISERIAL descriptor
1 parent ce25f00 commit 2b21595

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

src/examples/hid-keyboard/main.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// to override the USB Manufacturer or Product names:
22
//
3-
// tinygo flash -target circuitplay-express -ldflags="-X main.usbManufacturer='TinyGopher Labs' -X main.usbProduct='GopherKeyboard'" examples/hid-keyboard
3+
// tinygo flash -target circuitplay-express -ldflags="-X main.usbManufacturer='TinyGopher Labs' -X main.usbProduct='GopherKeyboard' -X main.usbSerial='XXXXX'" examples/hid-keyboard
44
//
55
// you can also override the VID/PID. however, only set this if you know what you are doing,
66
// since changing it can make it difficult to reflash some devices.
@@ -15,7 +15,7 @@ import (
1515
)
1616

1717
var usbVID, usbPID string
18-
var usbManufacturer, usbProduct string
18+
var usbManufacturer, usbProduct, usbSerial string
1919

2020
func main() {
2121
button := machine.BUTTON
@@ -49,4 +49,8 @@ func init() {
4949
if usbProduct != "" {
5050
usb.Product = usbProduct
5151
}
52+
53+
if usbSerial != "" {
54+
usb.Serial = usbSerial
55+
}
5256
}

src/machine/usb.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ func usbProduct() string {
6363
return usb_STRING_PRODUCT
6464
}
6565

66+
func usbSerial() string {
67+
if usb.Serial != "" {
68+
return usb.Serial
69+
}
70+
return ""
71+
}
72+
6673
// strToUTF16LEDescriptor converts a utf8 string into a string descriptor
6774
// note: the following code only converts ascii characters to UTF16LE. In order
6875
// to do a "proper" conversion, we would need to pull in the 'unicode/utf16'
@@ -160,8 +167,14 @@ func sendDescriptor(setup usb.Setup) {
160167
sendUSBPacket(0, b, setup.WLength)
161168

162169
case usb.ISERIAL:
163-
// TODO: allow returning a product serial number
164-
SendZlp()
170+
sz := len(usbSerial())
171+
if sz == 0 {
172+
SendZlp()
173+
} else {
174+
b := usb_trans_buffer[:(sz<<1)+2]
175+
strToUTF16LEDescriptor(usbSerial(), b)
176+
sendUSBPacket(0, b, setup.WLength)
177+
}
165178
}
166179
return
167180
case descriptor.TypeHIDReport:

src/machine/usb/usb.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,8 @@ var (
134134

135135
// Product is the product name displayed for this USB device.
136136
Product string
137+
138+
// Serial is the serial value displayed for this USB device. Assign a value to
139+
// transmit the serial to the host when requested.
140+
Serial string
137141
)

0 commit comments

Comments
 (0)