samd21,samd51,nrf52840: refactor handleStandardSetup and initEndpoint (#2968)

* samd21,samd51,nrf52840: refactor handleStandardSetup and initEndpoint
This commit is contained in:
sago35
2022-07-07 23:43:57 +09:00
committed by GitHub
parent 17deac116f
commit 335a7ad0b7
4 changed files with 138 additions and 302 deletions
+77
View File
@@ -324,6 +324,83 @@ func sendDescriptor(setup USBSetup) {
return
}
func handleStandardSetup(setup USBSetup) bool {
switch setup.BRequest {
case usb_GET_STATUS:
buf := []byte{0, 0}
if setup.BmRequestType != 0 { // endpoint
if isEndpointHalt {
buf[0] = 1
}
}
sendUSBPacket(0, buf, setup.WLength)
return true
case usb_CLEAR_FEATURE:
if setup.WValueL == 1 { // DEVICEREMOTEWAKEUP
isRemoteWakeUpEnabled = false
} else if setup.WValueL == 0 { // ENDPOINTHALT
isEndpointHalt = false
}
SendZlp()
return true
case usb_SET_FEATURE:
if setup.WValueL == 1 { // DEVICEREMOTEWAKEUP
isRemoteWakeUpEnabled = true
} else if setup.WValueL == 0 { // ENDPOINTHALT
isEndpointHalt = true
}
SendZlp()
return true
case usb_SET_ADDRESS:
return handleUSBSetAddress(setup)
case usb_GET_DESCRIPTOR:
sendDescriptor(setup)
return true
case usb_SET_DESCRIPTOR:
return false
case usb_GET_CONFIGURATION:
buff := []byte{usbConfiguration}
sendUSBPacket(0, buff, setup.WLength)
return true
case usb_SET_CONFIGURATION:
if setup.BmRequestType&usb_REQUEST_RECIPIENT == usb_REQUEST_DEVICE {
for i := 1; i < len(endPoints); i++ {
initEndpoint(uint32(i), endPoints[i])
}
usbConfiguration = setup.WValueL
SendZlp()
return true
} else {
return false
}
case usb_GET_INTERFACE:
buff := []byte{usbSetInterface}
sendUSBPacket(0, buff, setup.WLength)
return true
case usb_SET_INTERFACE:
usbSetInterface = setup.WValueL
SendZlp()
return true
default:
return true
}
}
// EnableHID enables HID. This function must be executed from the init().
func EnableHID(callback func()) {
usbDescriptor = descriptorCDCHID