Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

machine/usb: add support for ISERIAL descriptor #3989

Merged
merged 1 commit into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/examples/hid-keyboard/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// to override the USB Manufacturer or Product names:
//
// tinygo flash -target circuitplay-express -ldflags="-X main.usbManufacturer='TinyGopher Labs' -X main.usbProduct='GopherKeyboard'" examples/hid-keyboard
// tinygo flash -target circuitplay-express -ldflags="-X main.usbManufacturer='TinyGopher Labs' -X main.usbProduct='GopherKeyboard' -X main.usbSerial='XXXXX'" examples/hid-keyboard
//
// you can also override the VID/PID. however, only set this if you know what you are doing,
// since changing it can make it difficult to reflash some devices.
Expand All @@ -15,7 +15,7 @@ import (
)

var usbVID, usbPID string
var usbManufacturer, usbProduct string
var usbManufacturer, usbProduct, usbSerial string

func main() {
button := machine.BUTTON
Expand Down Expand Up @@ -49,4 +49,8 @@ func init() {
if usbProduct != "" {
usb.Product = usbProduct
}

if usbSerial != "" {
usb.Serial = usbSerial
}
}
17 changes: 15 additions & 2 deletions src/machine/usb.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ func usbProduct() string {
return usb_STRING_PRODUCT
}

func usbSerial() string {
if usb.Serial != "" {
return usb.Serial
}
return ""
}

// strToUTF16LEDescriptor converts a utf8 string into a string descriptor
// note: the following code only converts ascii characters to UTF16LE. In order
// to do a "proper" conversion, we would need to pull in the 'unicode/utf16'
Expand Down Expand Up @@ -160,8 +167,14 @@ func sendDescriptor(setup usb.Setup) {
sendUSBPacket(0, b, setup.WLength)

case usb.ISERIAL:
// TODO: allow returning a product serial number
SendZlp()
sz := len(usbSerial())
if sz == 0 {
SendZlp()
} else {
b := usb_trans_buffer[:(sz<<1)+2]
strToUTF16LEDescriptor(usbSerial(), b)
sendUSBPacket(0, b, setup.WLength)
}
}
return
case descriptor.TypeHIDReport:
Expand Down
4 changes: 4 additions & 0 deletions src/machine/usb/usb.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,8 @@ var (

// Product is the product name displayed for this USB device.
Product string

// Serial is the serial value displayed for this USB device. Assign a value to
// transmit the serial to the host when requested.
Serial string
)
Loading