machine/usb: add support for ISERIAL descriptor

Этот коммит содержится в:
sago35 2023-11-06 09:57:04 +09:00 коммит произвёл Ron Evans
родитель ce25f00769
коммит 2b215955ca
3 изменённых файлов: 25 добавлений и 4 удалений

Просмотреть файл

@ -1,6 +1,6 @@
// to override the USB Manufacturer or Product names: // 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, // 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. // since changing it can make it difficult to reflash some devices.
@ -15,7 +15,7 @@ import (
) )
var usbVID, usbPID string var usbVID, usbPID string
var usbManufacturer, usbProduct string var usbManufacturer, usbProduct, usbSerial string
func main() { func main() {
button := machine.BUTTON button := machine.BUTTON
@ -49,4 +49,8 @@ func init() {
if usbProduct != "" { if usbProduct != "" {
usb.Product = usbProduct usb.Product = usbProduct
} }
if usbSerial != "" {
usb.Serial = usbSerial
}
} }

Просмотреть файл

@ -63,6 +63,13 @@ func usbProduct() string {
return usb_STRING_PRODUCT return usb_STRING_PRODUCT
} }
func usbSerial() string {
if usb.Serial != "" {
return usb.Serial
}
return ""
}
// strToUTF16LEDescriptor converts a utf8 string into a string descriptor // strToUTF16LEDescriptor converts a utf8 string into a string descriptor
// note: the following code only converts ascii characters to UTF16LE. In order // 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' // to do a "proper" conversion, we would need to pull in the 'unicode/utf16'
@ -160,8 +167,14 @@ func sendDescriptor(setup usb.Setup) {
sendUSBPacket(0, b, setup.WLength) sendUSBPacket(0, b, setup.WLength)
case usb.ISERIAL: case usb.ISERIAL:
// TODO: allow returning a product serial number sz := len(usbSerial())
SendZlp() if sz == 0 {
SendZlp()
} else {
b := usb_trans_buffer[:(sz<<1)+2]
strToUTF16LEDescriptor(usbSerial(), b)
sendUSBPacket(0, b, setup.WLength)
}
} }
return return
case descriptor.TypeHIDReport: case descriptor.TypeHIDReport:

Просмотреть файл

@ -134,4 +134,8 @@ var (
// Product is the product name displayed for this USB device. // Product is the product name displayed for this USB device.
Product string 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
) )