samd21,samd51,nrf52840: change usbSetup and sendZlp to public
Этот коммит содержится в:
родитель
b112477e95
коммит
17deac116f
4 изменённых файлов: 132 добавлений и 154 удалений
|
@ -275,15 +275,15 @@ func handleUSBIRQ(intr interrupt.Interrupt) {
|
||||||
usbEndpointDescriptors[0].DeviceDescBank[0].PCKSIZE.ClearBits(usb_DEVICE_PCKSIZE_BYTE_COUNT_Mask << usb_DEVICE_PCKSIZE_BYTE_COUNT_Pos)
|
usbEndpointDescriptors[0].DeviceDescBank[0].PCKSIZE.ClearBits(usb_DEVICE_PCKSIZE_BYTE_COUNT_Mask << usb_DEVICE_PCKSIZE_BYTE_COUNT_Pos)
|
||||||
|
|
||||||
ok := false
|
ok := false
|
||||||
if (setup.bmRequestType & usb_REQUEST_TYPE) == usb_REQUEST_STANDARD {
|
if (setup.BmRequestType & usb_REQUEST_TYPE) == usb_REQUEST_STANDARD {
|
||||||
// Standard Requests
|
// Standard Requests
|
||||||
ok = handleStandardSetup(setup)
|
ok = handleStandardSetup(setup)
|
||||||
} else {
|
} else {
|
||||||
// Class Interface Requests
|
// Class Interface Requests
|
||||||
if setup.wIndex == usb_CDC_ACM_INTERFACE {
|
if setup.WIndex == usb_CDC_ACM_INTERFACE {
|
||||||
ok = cdcSetup(setup)
|
ok = cdcSetup(setup)
|
||||||
} else if setup.bmRequestType == usb_SET_REPORT_TYPE && setup.bRequest == usb_SET_IDLE {
|
} else if setup.BmRequestType == usb_SET_REPORT_TYPE && setup.BRequest == usb_SET_IDLE {
|
||||||
sendZlp()
|
SendZlp()
|
||||||
ok = true
|
ok = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -411,37 +411,37 @@ func initEndpoint(ep, config uint32) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleStandardSetup(setup usbSetup) bool {
|
func handleStandardSetup(setup USBSetup) bool {
|
||||||
switch setup.bRequest {
|
switch setup.BRequest {
|
||||||
case usb_GET_STATUS:
|
case usb_GET_STATUS:
|
||||||
buf := []byte{0, 0}
|
buf := []byte{0, 0}
|
||||||
|
|
||||||
if setup.bmRequestType != 0 { // endpoint
|
if setup.BmRequestType != 0 { // endpoint
|
||||||
// TODO: actually check if the endpoint in question is currently halted
|
// TODO: actually check if the endpoint in question is currently halted
|
||||||
if isEndpointHalt {
|
if isEndpointHalt {
|
||||||
buf[0] = 1
|
buf[0] = 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sendUSBPacket(0, buf, setup.wLength)
|
sendUSBPacket(0, buf, setup.WLength)
|
||||||
return true
|
return true
|
||||||
|
|
||||||
case usb_CLEAR_FEATURE:
|
case usb_CLEAR_FEATURE:
|
||||||
if setup.wValueL == 1 { // DEVICEREMOTEWAKEUP
|
if setup.WValueL == 1 { // DEVICEREMOTEWAKEUP
|
||||||
isRemoteWakeUpEnabled = false
|
isRemoteWakeUpEnabled = false
|
||||||
} else if setup.wValueL == 0 { // ENDPOINTHALT
|
} else if setup.WValueL == 0 { // ENDPOINTHALT
|
||||||
isEndpointHalt = false
|
isEndpointHalt = false
|
||||||
}
|
}
|
||||||
sendZlp()
|
SendZlp()
|
||||||
return true
|
return true
|
||||||
|
|
||||||
case usb_SET_FEATURE:
|
case usb_SET_FEATURE:
|
||||||
if setup.wValueL == 1 { // DEVICEREMOTEWAKEUP
|
if setup.WValueL == 1 { // DEVICEREMOTEWAKEUP
|
||||||
isRemoteWakeUpEnabled = true
|
isRemoteWakeUpEnabled = true
|
||||||
} else if setup.wValueL == 0 { // ENDPOINTHALT
|
} else if setup.WValueL == 0 { // ENDPOINTHALT
|
||||||
isEndpointHalt = true
|
isEndpointHalt = true
|
||||||
}
|
}
|
||||||
sendZlp()
|
SendZlp()
|
||||||
return true
|
return true
|
||||||
|
|
||||||
case usb_SET_ADDRESS:
|
case usb_SET_ADDRESS:
|
||||||
|
@ -465,7 +465,7 @@ func handleStandardSetup(setup usbSetup) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
// last, set the device address to that requested by host
|
// last, set the device address to that requested by host
|
||||||
sam.USB_DEVICE.DADD.SetBits(setup.wValueL)
|
sam.USB_DEVICE.DADD.SetBits(setup.WValueL)
|
||||||
sam.USB_DEVICE.DADD.SetBits(sam.USB_DEVICE_DADD_ADDEN)
|
sam.USB_DEVICE.DADD.SetBits(sam.USB_DEVICE_DADD_ADDEN)
|
||||||
|
|
||||||
return true
|
return true
|
||||||
|
@ -479,16 +479,16 @@ func handleStandardSetup(setup usbSetup) bool {
|
||||||
|
|
||||||
case usb_GET_CONFIGURATION:
|
case usb_GET_CONFIGURATION:
|
||||||
buff := []byte{usbConfiguration}
|
buff := []byte{usbConfiguration}
|
||||||
sendUSBPacket(0, buff, setup.wLength)
|
sendUSBPacket(0, buff, setup.WLength)
|
||||||
return true
|
return true
|
||||||
|
|
||||||
case usb_SET_CONFIGURATION:
|
case usb_SET_CONFIGURATION:
|
||||||
if setup.bmRequestType&usb_REQUEST_RECIPIENT == usb_REQUEST_DEVICE {
|
if setup.BmRequestType&usb_REQUEST_RECIPIENT == usb_REQUEST_DEVICE {
|
||||||
for i := 1; i < len(endPoints); i++ {
|
for i := 1; i < len(endPoints); i++ {
|
||||||
initEndpoint(uint32(i), endPoints[i])
|
initEndpoint(uint32(i), endPoints[i])
|
||||||
}
|
}
|
||||||
|
|
||||||
usbConfiguration = setup.wValueL
|
usbConfiguration = setup.WValueL
|
||||||
|
|
||||||
// Enable interrupt for CDC control messages from host (OUT packet)
|
// Enable interrupt for CDC control messages from host (OUT packet)
|
||||||
setEPINTENSET(usb_CDC_ENDPOINT_ACM, sam.USB_DEVICE_EPINTENSET_TRCPT1)
|
setEPINTENSET(usb_CDC_ENDPOINT_ACM, sam.USB_DEVICE_EPINTENSET_TRCPT1)
|
||||||
|
@ -501,7 +501,7 @@ func handleStandardSetup(setup usbSetup) bool {
|
||||||
setEPINTENSET(usb_HID_ENDPOINT_IN, sam.USB_DEVICE_EPINTENSET_TRCPT1)
|
setEPINTENSET(usb_HID_ENDPOINT_IN, sam.USB_DEVICE_EPINTENSET_TRCPT1)
|
||||||
}
|
}
|
||||||
|
|
||||||
sendZlp()
|
SendZlp()
|
||||||
return true
|
return true
|
||||||
} else {
|
} else {
|
||||||
return false
|
return false
|
||||||
|
@ -509,13 +509,13 @@ func handleStandardSetup(setup usbSetup) bool {
|
||||||
|
|
||||||
case usb_GET_INTERFACE:
|
case usb_GET_INTERFACE:
|
||||||
buff := []byte{usbSetInterface}
|
buff := []byte{usbSetInterface}
|
||||||
sendUSBPacket(0, buff, setup.wLength)
|
sendUSBPacket(0, buff, setup.WLength)
|
||||||
return true
|
return true
|
||||||
|
|
||||||
case usb_SET_INTERFACE:
|
case usb_SET_INTERFACE:
|
||||||
usbSetInterface = setup.wValueL
|
usbSetInterface = setup.WValueL
|
||||||
|
|
||||||
sendZlp()
|
SendZlp()
|
||||||
return true
|
return true
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
@ -523,9 +523,9 @@ func handleStandardSetup(setup usbSetup) bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func cdcSetup(setup usbSetup) bool {
|
func cdcSetup(setup USBSetup) bool {
|
||||||
if setup.bmRequestType == usb_REQUEST_DEVICETOHOST_CLASS_INTERFACE {
|
if setup.BmRequestType == usb_REQUEST_DEVICETOHOST_CLASS_INTERFACE {
|
||||||
if setup.bRequest == usb_CDC_GET_LINE_CODING {
|
if setup.BRequest == usb_CDC_GET_LINE_CODING {
|
||||||
var b [cdcLineInfoSize]byte
|
var b [cdcLineInfoSize]byte
|
||||||
b[0] = byte(usbLineInfo.dwDTERate)
|
b[0] = byte(usbLineInfo.dwDTERate)
|
||||||
b[1] = byte(usbLineInfo.dwDTERate >> 8)
|
b[1] = byte(usbLineInfo.dwDTERate >> 8)
|
||||||
|
@ -535,13 +535,13 @@ func cdcSetup(setup usbSetup) bool {
|
||||||
b[5] = byte(usbLineInfo.bParityType)
|
b[5] = byte(usbLineInfo.bParityType)
|
||||||
b[6] = byte(usbLineInfo.bDataBits)
|
b[6] = byte(usbLineInfo.bDataBits)
|
||||||
|
|
||||||
sendUSBPacket(0, b[:], setup.wLength)
|
sendUSBPacket(0, b[:], setup.WLength)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if setup.bmRequestType == usb_REQUEST_HOSTTODEVICE_CLASS_INTERFACE {
|
if setup.BmRequestType == usb_REQUEST_HOSTTODEVICE_CLASS_INTERFACE {
|
||||||
if setup.bRequest == usb_CDC_SET_LINE_CODING {
|
if setup.BRequest == usb_CDC_SET_LINE_CODING {
|
||||||
b, err := receiveUSBControlPacket()
|
b, err := receiveUSBControlPacket()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
|
@ -553,25 +553,25 @@ func cdcSetup(setup usbSetup) bool {
|
||||||
usbLineInfo.bDataBits = b[6]
|
usbLineInfo.bDataBits = b[6]
|
||||||
}
|
}
|
||||||
|
|
||||||
if setup.bRequest == usb_CDC_SET_CONTROL_LINE_STATE {
|
if setup.BRequest == usb_CDC_SET_CONTROL_LINE_STATE {
|
||||||
usbLineInfo.lineState = setup.wValueL
|
usbLineInfo.lineState = setup.WValueL
|
||||||
}
|
}
|
||||||
|
|
||||||
if setup.bRequest == usb_CDC_SET_LINE_CODING || setup.bRequest == usb_CDC_SET_CONTROL_LINE_STATE {
|
if setup.BRequest == usb_CDC_SET_LINE_CODING || setup.BRequest == usb_CDC_SET_CONTROL_LINE_STATE {
|
||||||
// auto-reset into the bootloader
|
// auto-reset into the bootloader
|
||||||
if usbLineInfo.dwDTERate == 1200 && usbLineInfo.lineState&usb_CDC_LINESTATE_DTR == 0 {
|
if usbLineInfo.dwDTERate == 1200 && usbLineInfo.lineState&usb_CDC_LINESTATE_DTR == 0 {
|
||||||
EnterBootloader()
|
EnterBootloader()
|
||||||
} else {
|
} else {
|
||||||
// TODO: cancel any reset
|
// TODO: cancel any reset
|
||||||
}
|
}
|
||||||
sendZlp()
|
SendZlp()
|
||||||
}
|
}
|
||||||
|
|
||||||
if setup.bRequest == usb_CDC_SEND_BREAK {
|
if setup.BRequest == usb_CDC_SEND_BREAK {
|
||||||
// TODO: something with this value?
|
// TODO: something with this value?
|
||||||
// breakValue = ((uint16_t)setup.wValueH << 8) | setup.wValueL;
|
// breakValue = ((uint16_t)setup.wValueH << 8) | setup.wValueL;
|
||||||
// return false;
|
// return false;
|
||||||
sendZlp()
|
SendZlp()
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@ -678,7 +678,7 @@ func handleEndpoint(ep uint32) {
|
||||||
setEPSTATUSCLR(ep, sam.USB_DEVICE_EPSTATUSCLR_BK0RDY)
|
setEPSTATUSCLR(ep, sam.USB_DEVICE_EPSTATUSCLR_BK0RDY)
|
||||||
}
|
}
|
||||||
|
|
||||||
func sendZlp() {
|
func SendZlp() {
|
||||||
usbEndpointDescriptors[0].DeviceDescBank[1].PCKSIZE.ClearBits(usb_DEVICE_PCKSIZE_BYTE_COUNT_Mask << usb_DEVICE_PCKSIZE_BYTE_COUNT_Pos)
|
usbEndpointDescriptors[0].DeviceDescBank[1].PCKSIZE.ClearBits(usb_DEVICE_PCKSIZE_BYTE_COUNT_Mask << usb_DEVICE_PCKSIZE_BYTE_COUNT_Pos)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -279,15 +279,15 @@ func handleUSBIRQ(interrupt.Interrupt) {
|
||||||
usbEndpointDescriptors[0].DeviceDescBank[0].PCKSIZE.ClearBits(usb_DEVICE_PCKSIZE_BYTE_COUNT_Mask << usb_DEVICE_PCKSIZE_BYTE_COUNT_Pos)
|
usbEndpointDescriptors[0].DeviceDescBank[0].PCKSIZE.ClearBits(usb_DEVICE_PCKSIZE_BYTE_COUNT_Mask << usb_DEVICE_PCKSIZE_BYTE_COUNT_Pos)
|
||||||
|
|
||||||
ok := false
|
ok := false
|
||||||
if (setup.bmRequestType & usb_REQUEST_TYPE) == usb_REQUEST_STANDARD {
|
if (setup.BmRequestType & usb_REQUEST_TYPE) == usb_REQUEST_STANDARD {
|
||||||
// Standard Requests
|
// Standard Requests
|
||||||
ok = handleStandardSetup(setup)
|
ok = handleStandardSetup(setup)
|
||||||
} else {
|
} else {
|
||||||
// Class Interface Requests
|
// Class Interface Requests
|
||||||
if setup.wIndex == usb_CDC_ACM_INTERFACE {
|
if setup.WIndex == usb_CDC_ACM_INTERFACE {
|
||||||
ok = cdcSetup(setup)
|
ok = cdcSetup(setup)
|
||||||
} else if setup.bmRequestType == usb_SET_REPORT_TYPE && setup.bRequest == usb_SET_IDLE {
|
} else if setup.BmRequestType == usb_SET_REPORT_TYPE && setup.BRequest == usb_SET_IDLE {
|
||||||
sendZlp()
|
SendZlp()
|
||||||
ok = true
|
ok = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -415,37 +415,37 @@ func initEndpoint(ep, config uint32) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleStandardSetup(setup usbSetup) bool {
|
func handleStandardSetup(setup USBSetup) bool {
|
||||||
switch setup.bRequest {
|
switch setup.BRequest {
|
||||||
case usb_GET_STATUS:
|
case usb_GET_STATUS:
|
||||||
buf := []byte{0, 0}
|
buf := []byte{0, 0}
|
||||||
|
|
||||||
if setup.bmRequestType != 0 { // endpoint
|
if setup.BmRequestType != 0 { // endpoint
|
||||||
// TODO: actually check if the endpoint in question is currently halted
|
// TODO: actually check if the endpoint in question is currently halted
|
||||||
if isEndpointHalt {
|
if isEndpointHalt {
|
||||||
buf[0] = 1
|
buf[0] = 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sendUSBPacket(0, buf, setup.wLength)
|
sendUSBPacket(0, buf, setup.WLength)
|
||||||
return true
|
return true
|
||||||
|
|
||||||
case usb_CLEAR_FEATURE:
|
case usb_CLEAR_FEATURE:
|
||||||
if setup.wValueL == 1 { // DEVICEREMOTEWAKEUP
|
if setup.WValueL == 1 { // DEVICEREMOTEWAKEUP
|
||||||
isRemoteWakeUpEnabled = false
|
isRemoteWakeUpEnabled = false
|
||||||
} else if setup.wValueL == 0 { // ENDPOINTHALT
|
} else if setup.WValueL == 0 { // ENDPOINTHALT
|
||||||
isEndpointHalt = false
|
isEndpointHalt = false
|
||||||
}
|
}
|
||||||
sendZlp()
|
SendZlp()
|
||||||
return true
|
return true
|
||||||
|
|
||||||
case usb_SET_FEATURE:
|
case usb_SET_FEATURE:
|
||||||
if setup.wValueL == 1 { // DEVICEREMOTEWAKEUP
|
if setup.WValueL == 1 { // DEVICEREMOTEWAKEUP
|
||||||
isRemoteWakeUpEnabled = true
|
isRemoteWakeUpEnabled = true
|
||||||
} else if setup.wValueL == 0 { // ENDPOINTHALT
|
} else if setup.WValueL == 0 { // ENDPOINTHALT
|
||||||
isEndpointHalt = true
|
isEndpointHalt = true
|
||||||
}
|
}
|
||||||
sendZlp()
|
SendZlp()
|
||||||
return true
|
return true
|
||||||
|
|
||||||
case usb_SET_ADDRESS:
|
case usb_SET_ADDRESS:
|
||||||
|
@ -469,7 +469,7 @@ func handleStandardSetup(setup usbSetup) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
// last, set the device address to that requested by host
|
// last, set the device address to that requested by host
|
||||||
sam.USB_DEVICE.DADD.SetBits(setup.wValueL)
|
sam.USB_DEVICE.DADD.SetBits(setup.WValueL)
|
||||||
sam.USB_DEVICE.DADD.SetBits(sam.USB_DEVICE_DADD_ADDEN)
|
sam.USB_DEVICE.DADD.SetBits(sam.USB_DEVICE_DADD_ADDEN)
|
||||||
|
|
||||||
return true
|
return true
|
||||||
|
@ -483,16 +483,16 @@ func handleStandardSetup(setup usbSetup) bool {
|
||||||
|
|
||||||
case usb_GET_CONFIGURATION:
|
case usb_GET_CONFIGURATION:
|
||||||
buff := []byte{usbConfiguration}
|
buff := []byte{usbConfiguration}
|
||||||
sendUSBPacket(0, buff, setup.wLength)
|
sendUSBPacket(0, buff, setup.WLength)
|
||||||
return true
|
return true
|
||||||
|
|
||||||
case usb_SET_CONFIGURATION:
|
case usb_SET_CONFIGURATION:
|
||||||
if setup.bmRequestType&usb_REQUEST_RECIPIENT == usb_REQUEST_DEVICE {
|
if setup.BmRequestType&usb_REQUEST_RECIPIENT == usb_REQUEST_DEVICE {
|
||||||
for i := 1; i < len(endPoints); i++ {
|
for i := 1; i < len(endPoints); i++ {
|
||||||
initEndpoint(uint32(i), endPoints[i])
|
initEndpoint(uint32(i), endPoints[i])
|
||||||
}
|
}
|
||||||
|
|
||||||
usbConfiguration = setup.wValueL
|
usbConfiguration = setup.WValueL
|
||||||
|
|
||||||
// Enable interrupt for CDC control messages from host (OUT packet)
|
// Enable interrupt for CDC control messages from host (OUT packet)
|
||||||
setEPINTENSET(usb_CDC_ENDPOINT_ACM, sam.USB_DEVICE_ENDPOINT_EPINTENSET_TRCPT1)
|
setEPINTENSET(usb_CDC_ENDPOINT_ACM, sam.USB_DEVICE_ENDPOINT_EPINTENSET_TRCPT1)
|
||||||
|
@ -505,7 +505,7 @@ func handleStandardSetup(setup usbSetup) bool {
|
||||||
setEPINTENSET(usb_HID_ENDPOINT_IN, sam.USB_DEVICE_ENDPOINT_EPINTENSET_TRCPT1)
|
setEPINTENSET(usb_HID_ENDPOINT_IN, sam.USB_DEVICE_ENDPOINT_EPINTENSET_TRCPT1)
|
||||||
}
|
}
|
||||||
|
|
||||||
sendZlp()
|
SendZlp()
|
||||||
return true
|
return true
|
||||||
} else {
|
} else {
|
||||||
return false
|
return false
|
||||||
|
@ -513,13 +513,13 @@ func handleStandardSetup(setup usbSetup) bool {
|
||||||
|
|
||||||
case usb_GET_INTERFACE:
|
case usb_GET_INTERFACE:
|
||||||
buff := []byte{usbSetInterface}
|
buff := []byte{usbSetInterface}
|
||||||
sendUSBPacket(0, buff, setup.wLength)
|
sendUSBPacket(0, buff, setup.WLength)
|
||||||
return true
|
return true
|
||||||
|
|
||||||
case usb_SET_INTERFACE:
|
case usb_SET_INTERFACE:
|
||||||
usbSetInterface = setup.wValueL
|
usbSetInterface = setup.WValueL
|
||||||
|
|
||||||
sendZlp()
|
SendZlp()
|
||||||
return true
|
return true
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
@ -527,9 +527,9 @@ func handleStandardSetup(setup usbSetup) bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func cdcSetup(setup usbSetup) bool {
|
func cdcSetup(setup USBSetup) bool {
|
||||||
if setup.bmRequestType == usb_REQUEST_DEVICETOHOST_CLASS_INTERFACE {
|
if setup.BmRequestType == usb_REQUEST_DEVICETOHOST_CLASS_INTERFACE {
|
||||||
if setup.bRequest == usb_CDC_GET_LINE_CODING {
|
if setup.BRequest == usb_CDC_GET_LINE_CODING {
|
||||||
var b [cdcLineInfoSize]byte
|
var b [cdcLineInfoSize]byte
|
||||||
b[0] = byte(usbLineInfo.dwDTERate)
|
b[0] = byte(usbLineInfo.dwDTERate)
|
||||||
b[1] = byte(usbLineInfo.dwDTERate >> 8)
|
b[1] = byte(usbLineInfo.dwDTERate >> 8)
|
||||||
|
@ -539,13 +539,13 @@ func cdcSetup(setup usbSetup) bool {
|
||||||
b[5] = byte(usbLineInfo.bParityType)
|
b[5] = byte(usbLineInfo.bParityType)
|
||||||
b[6] = byte(usbLineInfo.bDataBits)
|
b[6] = byte(usbLineInfo.bDataBits)
|
||||||
|
|
||||||
sendUSBPacket(0, b[:], setup.wLength)
|
sendUSBPacket(0, b[:], setup.WLength)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if setup.bmRequestType == usb_REQUEST_HOSTTODEVICE_CLASS_INTERFACE {
|
if setup.BmRequestType == usb_REQUEST_HOSTTODEVICE_CLASS_INTERFACE {
|
||||||
if setup.bRequest == usb_CDC_SET_LINE_CODING {
|
if setup.BRequest == usb_CDC_SET_LINE_CODING {
|
||||||
b, err := receiveUSBControlPacket()
|
b, err := receiveUSBControlPacket()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
|
@ -557,25 +557,25 @@ func cdcSetup(setup usbSetup) bool {
|
||||||
usbLineInfo.bDataBits = b[6]
|
usbLineInfo.bDataBits = b[6]
|
||||||
}
|
}
|
||||||
|
|
||||||
if setup.bRequest == usb_CDC_SET_CONTROL_LINE_STATE {
|
if setup.BRequest == usb_CDC_SET_CONTROL_LINE_STATE {
|
||||||
usbLineInfo.lineState = setup.wValueL
|
usbLineInfo.lineState = setup.WValueL
|
||||||
}
|
}
|
||||||
|
|
||||||
if setup.bRequest == usb_CDC_SET_LINE_CODING || setup.bRequest == usb_CDC_SET_CONTROL_LINE_STATE {
|
if setup.BRequest == usb_CDC_SET_LINE_CODING || setup.BRequest == usb_CDC_SET_CONTROL_LINE_STATE {
|
||||||
// auto-reset into the bootloader
|
// auto-reset into the bootloader
|
||||||
if usbLineInfo.dwDTERate == 1200 && usbLineInfo.lineState&usb_CDC_LINESTATE_DTR == 0 {
|
if usbLineInfo.dwDTERate == 1200 && usbLineInfo.lineState&usb_CDC_LINESTATE_DTR == 0 {
|
||||||
EnterBootloader()
|
EnterBootloader()
|
||||||
} else {
|
} else {
|
||||||
// TODO: cancel any reset
|
// TODO: cancel any reset
|
||||||
}
|
}
|
||||||
sendZlp()
|
SendZlp()
|
||||||
}
|
}
|
||||||
|
|
||||||
if setup.bRequest == usb_CDC_SEND_BREAK {
|
if setup.BRequest == usb_CDC_SEND_BREAK {
|
||||||
// TODO: something with this value?
|
// TODO: something with this value?
|
||||||
// breakValue = ((uint16_t)setup.wValueH << 8) | setup.wValueL;
|
// breakValue = ((uint16_t)setup.wValueH << 8) | setup.wValueL;
|
||||||
// return false;
|
// return false;
|
||||||
sendZlp()
|
SendZlp()
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@ -683,7 +683,7 @@ func handleEndpoint(ep uint32) {
|
||||||
setEPSTATUSCLR(ep, sam.USB_DEVICE_ENDPOINT_EPSTATUSCLR_BK0RDY)
|
setEPSTATUSCLR(ep, sam.USB_DEVICE_ENDPOINT_EPSTATUSCLR_BK0RDY)
|
||||||
}
|
}
|
||||||
|
|
||||||
func sendZlp() {
|
func SendZlp() {
|
||||||
usbEndpointDescriptors[0].DeviceDescBank[1].PCKSIZE.ClearBits(usb_DEVICE_PCKSIZE_BYTE_COUNT_Mask << usb_DEVICE_PCKSIZE_BYTE_COUNT_Pos)
|
usbEndpointDescriptors[0].DeviceDescBank[1].PCKSIZE.ClearBits(usb_DEVICE_PCKSIZE_BYTE_COUNT_Mask << usb_DEVICE_PCKSIZE_BYTE_COUNT_Pos)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -242,14 +242,14 @@ func handleUSBIRQ(interrupt.Interrupt) {
|
||||||
setup := parseUSBSetupRegisters()
|
setup := parseUSBSetupRegisters()
|
||||||
|
|
||||||
ok := false
|
ok := false
|
||||||
if (setup.bmRequestType & usb_REQUEST_TYPE) == usb_REQUEST_STANDARD {
|
if (setup.BmRequestType & usb_REQUEST_TYPE) == usb_REQUEST_STANDARD {
|
||||||
// Standard Requests
|
// Standard Requests
|
||||||
ok = handleStandardSetup(setup)
|
ok = handleStandardSetup(setup)
|
||||||
} else {
|
} else {
|
||||||
if setup.wIndex == usb_CDC_ACM_INTERFACE {
|
if setup.WIndex == usb_CDC_ACM_INTERFACE {
|
||||||
ok = cdcSetup(setup)
|
ok = cdcSetup(setup)
|
||||||
} else if setup.bmRequestType == usb_SET_REPORT_TYPE && setup.bRequest == usb_SET_IDLE {
|
} else if setup.BmRequestType == usb_SET_REPORT_TYPE && setup.BRequest == usb_SET_IDLE {
|
||||||
sendZlp()
|
SendZlp()
|
||||||
ok = true
|
ok = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -323,14 +323,14 @@ func parseUSBLineInfo(b []byte) {
|
||||||
usbLineInfo.bDataBits = b[6]
|
usbLineInfo.bDataBits = b[6]
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseUSBSetupRegisters() usbSetup {
|
func parseUSBSetupRegisters() USBSetup {
|
||||||
return usbSetup{
|
return USBSetup{
|
||||||
bmRequestType: uint8(nrf.USBD.BMREQUESTTYPE.Get()),
|
BmRequestType: uint8(nrf.USBD.BMREQUESTTYPE.Get()),
|
||||||
bRequest: uint8(nrf.USBD.BREQUEST.Get()),
|
BRequest: uint8(nrf.USBD.BREQUEST.Get()),
|
||||||
wValueL: uint8(nrf.USBD.WVALUEL.Get()),
|
WValueL: uint8(nrf.USBD.WVALUEL.Get()),
|
||||||
wValueH: uint8(nrf.USBD.WVALUEH.Get()),
|
WValueH: uint8(nrf.USBD.WVALUEH.Get()),
|
||||||
wIndex: uint16((nrf.USBD.WINDEXH.Get() << 8) | nrf.USBD.WINDEXL.Get()),
|
WIndex: uint16((nrf.USBD.WINDEXH.Get() << 8) | nrf.USBD.WINDEXL.Get()),
|
||||||
wLength: uint16(((nrf.USBD.WLENGTHH.Get() & 0xff) << 8) | (nrf.USBD.WLENGTHL.Get() & 0xff)),
|
WLength: uint16(((nrf.USBD.WLENGTHH.Get() & 0xff) << 8) | (nrf.USBD.WLENGTHL.Get() & 0xff)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -360,33 +360,33 @@ func initEndpoint(ep, config uint32) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleStandardSetup(setup usbSetup) bool {
|
func handleStandardSetup(setup USBSetup) bool {
|
||||||
switch setup.bRequest {
|
switch setup.BRequest {
|
||||||
case usb_GET_STATUS:
|
case usb_GET_STATUS:
|
||||||
buf := []byte{0, 0}
|
buf := []byte{0, 0}
|
||||||
|
|
||||||
if setup.bmRequestType != 0 { // endpoint
|
if setup.BmRequestType != 0 { // endpoint
|
||||||
if isEndpointHalt {
|
if isEndpointHalt {
|
||||||
buf[0] = 1
|
buf[0] = 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sendUSBPacket(0, buf, setup.wLength)
|
sendUSBPacket(0, buf, setup.WLength)
|
||||||
return true
|
return true
|
||||||
|
|
||||||
case usb_CLEAR_FEATURE:
|
case usb_CLEAR_FEATURE:
|
||||||
if setup.wValueL == 1 { // DEVICEREMOTEWAKEUP
|
if setup.WValueL == 1 { // DEVICEREMOTEWAKEUP
|
||||||
isRemoteWakeUpEnabled = false
|
isRemoteWakeUpEnabled = false
|
||||||
} else if setup.wValueL == 0 { // ENDPOINTHALT
|
} else if setup.WValueL == 0 { // ENDPOINTHALT
|
||||||
isEndpointHalt = false
|
isEndpointHalt = false
|
||||||
}
|
}
|
||||||
nrf.USBD.TASKS_EP0STATUS.Set(1)
|
nrf.USBD.TASKS_EP0STATUS.Set(1)
|
||||||
return true
|
return true
|
||||||
|
|
||||||
case usb_SET_FEATURE:
|
case usb_SET_FEATURE:
|
||||||
if setup.wValueL == 1 { // DEVICEREMOTEWAKEUP
|
if setup.WValueL == 1 { // DEVICEREMOTEWAKEUP
|
||||||
isRemoteWakeUpEnabled = true
|
isRemoteWakeUpEnabled = true
|
||||||
} else if setup.wValueL == 0 { // ENDPOINTHALT
|
} else if setup.WValueL == 0 { // ENDPOINTHALT
|
||||||
isEndpointHalt = true
|
isEndpointHalt = true
|
||||||
}
|
}
|
||||||
nrf.USBD.TASKS_EP0STATUS.Set(1)
|
nrf.USBD.TASKS_EP0STATUS.Set(1)
|
||||||
|
@ -405,11 +405,11 @@ func handleStandardSetup(setup usbSetup) bool {
|
||||||
|
|
||||||
case usb_GET_CONFIGURATION:
|
case usb_GET_CONFIGURATION:
|
||||||
buff := []byte{usbConfiguration}
|
buff := []byte{usbConfiguration}
|
||||||
sendUSBPacket(0, buff, setup.wLength)
|
sendUSBPacket(0, buff, setup.WLength)
|
||||||
return true
|
return true
|
||||||
|
|
||||||
case usb_SET_CONFIGURATION:
|
case usb_SET_CONFIGURATION:
|
||||||
if setup.bmRequestType&usb_REQUEST_RECIPIENT == usb_REQUEST_DEVICE {
|
if setup.BmRequestType&usb_REQUEST_RECIPIENT == usb_REQUEST_DEVICE {
|
||||||
nrf.USBD.TASKS_EP0STATUS.Set(1)
|
nrf.USBD.TASKS_EP0STATUS.Set(1)
|
||||||
for i := 1; i < len(endPoints); i++ {
|
for i := 1; i < len(endPoints); i++ {
|
||||||
initEndpoint(uint32(i), endPoints[i])
|
initEndpoint(uint32(i), endPoints[i])
|
||||||
|
@ -420,7 +420,7 @@ func handleStandardSetup(setup usbSetup) bool {
|
||||||
nrf.USBD.INTENSET.Set(nrf.USBD_INTENSET_ENDEPOUT0 << usb_HID_ENDPOINT_IN)
|
nrf.USBD.INTENSET.Set(nrf.USBD_INTENSET_ENDEPOUT0 << usb_HID_ENDPOINT_IN)
|
||||||
}
|
}
|
||||||
|
|
||||||
usbConfiguration = setup.wValueL
|
usbConfiguration = setup.WValueL
|
||||||
return true
|
return true
|
||||||
} else {
|
} else {
|
||||||
return false
|
return false
|
||||||
|
@ -428,11 +428,11 @@ func handleStandardSetup(setup usbSetup) bool {
|
||||||
|
|
||||||
case usb_GET_INTERFACE:
|
case usb_GET_INTERFACE:
|
||||||
buff := []byte{usbSetInterface}
|
buff := []byte{usbSetInterface}
|
||||||
sendUSBPacket(0, buff, setup.wLength)
|
sendUSBPacket(0, buff, setup.WLength)
|
||||||
return true
|
return true
|
||||||
|
|
||||||
case usb_SET_INTERFACE:
|
case usb_SET_INTERFACE:
|
||||||
usbSetInterface = setup.wValueL
|
usbSetInterface = setup.WValueL
|
||||||
|
|
||||||
nrf.USBD.TASKS_EP0STATUS.Set(1)
|
nrf.USBD.TASKS_EP0STATUS.Set(1)
|
||||||
return true
|
return true
|
||||||
|
@ -442,9 +442,9 @@ func handleStandardSetup(setup usbSetup) bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func cdcSetup(setup usbSetup) bool {
|
func cdcSetup(setup USBSetup) bool {
|
||||||
if setup.bmRequestType == usb_REQUEST_DEVICETOHOST_CLASS_INTERFACE {
|
if setup.BmRequestType == usb_REQUEST_DEVICETOHOST_CLASS_INTERFACE {
|
||||||
if setup.bRequest == usb_CDC_GET_LINE_CODING {
|
if setup.BRequest == usb_CDC_GET_LINE_CODING {
|
||||||
var b [cdcLineInfoSize]byte
|
var b [cdcLineInfoSize]byte
|
||||||
b[0] = byte(usbLineInfo.dwDTERate)
|
b[0] = byte(usbLineInfo.dwDTERate)
|
||||||
b[1] = byte(usbLineInfo.dwDTERate >> 8)
|
b[1] = byte(usbLineInfo.dwDTERate >> 8)
|
||||||
|
@ -454,27 +454,27 @@ func cdcSetup(setup usbSetup) bool {
|
||||||
b[5] = byte(usbLineInfo.bParityType)
|
b[5] = byte(usbLineInfo.bParityType)
|
||||||
b[6] = byte(usbLineInfo.bDataBits)
|
b[6] = byte(usbLineInfo.bDataBits)
|
||||||
|
|
||||||
sendUSBPacket(0, b[:], setup.wLength)
|
sendUSBPacket(0, b[:], setup.WLength)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if setup.bmRequestType == usb_REQUEST_HOSTTODEVICE_CLASS_INTERFACE {
|
if setup.BmRequestType == usb_REQUEST_HOSTTODEVICE_CLASS_INTERFACE {
|
||||||
if setup.bRequest == usb_CDC_SET_LINE_CODING {
|
if setup.BRequest == usb_CDC_SET_LINE_CODING {
|
||||||
epout0data_setlinecoding = true
|
epout0data_setlinecoding = true
|
||||||
nrf.USBD.TASKS_EP0RCVOUT.Set(1)
|
nrf.USBD.TASKS_EP0RCVOUT.Set(1)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
if setup.bRequest == usb_CDC_SET_CONTROL_LINE_STATE {
|
if setup.BRequest == usb_CDC_SET_CONTROL_LINE_STATE {
|
||||||
usbLineInfo.lineState = setup.wValueL
|
usbLineInfo.lineState = setup.WValueL
|
||||||
if usbLineInfo.dwDTERate == 1200 && usbLineInfo.lineState&usb_CDC_LINESTATE_DTR == 0 {
|
if usbLineInfo.dwDTERate == 1200 && usbLineInfo.lineState&usb_CDC_LINESTATE_DTR == 0 {
|
||||||
EnterBootloader()
|
EnterBootloader()
|
||||||
}
|
}
|
||||||
nrf.USBD.TASKS_EP0STATUS.Set(1)
|
nrf.USBD.TASKS_EP0STATUS.Set(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
if setup.bRequest == usb_CDC_SEND_BREAK {
|
if setup.BRequest == usb_CDC_SEND_BREAK {
|
||||||
nrf.USBD.TASKS_EP0STATUS.Set(1)
|
nrf.USBD.TASKS_EP0STATUS.Set(1)
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
|
@ -530,7 +530,7 @@ func (usbcdc *USBCDC) handleEndpoint(ep uint32) {
|
||||||
nrf.USBD.SIZE.EPOUT[ep].Set(0)
|
nrf.USBD.SIZE.EPOUT[ep].Set(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func sendZlp() {
|
func SendZlp() {
|
||||||
nrf.USBD.TASKS_EP0STATUS.Set(1)
|
nrf.USBD.TASKS_EP0STATUS.Set(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -176,13 +176,6 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
// usbDeviceDescBank is the USB device endpoint descriptor.
|
// usbDeviceDescBank is the USB device endpoint descriptor.
|
||||||
// typedef struct {
|
|
||||||
// __IO USB_DEVICE_ADDR_Type ADDR; /**< \brief Offset: 0x000 (R/W 32) DEVICE_DESC_BANK Endpoint Bank, Adress of Data Buffer */
|
|
||||||
// __IO USB_DEVICE_PCKSIZE_Type PCKSIZE; /**< \brief Offset: 0x004 (R/W 32) DEVICE_DESC_BANK Endpoint Bank, Packet Size */
|
|
||||||
// __IO USB_DEVICE_EXTREG_Type EXTREG; /**< \brief Offset: 0x008 (R/W 16) DEVICE_DESC_BANK Endpoint Bank, Extended */
|
|
||||||
// __IO USB_DEVICE_STATUS_BK_Type STATUS_BK; /**< \brief Offset: 0x00A (R/W 8) DEVICE_DESC_BANK Enpoint Bank, Status of Bank */
|
|
||||||
// RoReg8 Reserved1[0x5];
|
|
||||||
// } UsbDeviceDescBank;
|
|
||||||
type usbDeviceDescBank struct {
|
type usbDeviceDescBank struct {
|
||||||
ADDR volatile.Register32
|
ADDR volatile.Register32
|
||||||
PCKSIZE volatile.Register32
|
PCKSIZE volatile.Register32
|
||||||
|
@ -195,38 +188,23 @@ type usbDeviceDescriptor struct {
|
||||||
DeviceDescBank [2]usbDeviceDescBank
|
DeviceDescBank [2]usbDeviceDescBank
|
||||||
}
|
}
|
||||||
|
|
||||||
// typedef struct {
|
type USBSetup struct {
|
||||||
// union {
|
BmRequestType uint8
|
||||||
// uint8_t bmRequestType;
|
BRequest uint8
|
||||||
// struct {
|
WValueL uint8
|
||||||
// uint8_t direction : 5;
|
WValueH uint8
|
||||||
// uint8_t type : 2;
|
WIndex uint16
|
||||||
// uint8_t transferDirection : 1;
|
WLength uint16
|
||||||
// };
|
|
||||||
// };
|
|
||||||
// uint8_t bRequest;
|
|
||||||
// uint8_t wValueL;
|
|
||||||
// uint8_t wValueH;
|
|
||||||
// uint16_t wIndex;
|
|
||||||
// uint16_t wLength;
|
|
||||||
// } USBSetup;
|
|
||||||
type usbSetup struct {
|
|
||||||
bmRequestType uint8
|
|
||||||
bRequest uint8
|
|
||||||
wValueL uint8
|
|
||||||
wValueH uint8
|
|
||||||
wIndex uint16
|
|
||||||
wLength uint16
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func newUSBSetup(data []byte) usbSetup {
|
func newUSBSetup(data []byte) USBSetup {
|
||||||
u := usbSetup{}
|
u := USBSetup{}
|
||||||
u.bmRequestType = uint8(data[0])
|
u.BmRequestType = uint8(data[0])
|
||||||
u.bRequest = uint8(data[1])
|
u.BRequest = uint8(data[1])
|
||||||
u.wValueL = uint8(data[2])
|
u.WValueL = uint8(data[2])
|
||||||
u.wValueH = uint8(data[3])
|
u.WValueH = uint8(data[3])
|
||||||
u.wIndex = uint16(data[4]) | (uint16(data[5]) << 8)
|
u.WIndex = uint16(data[4]) | (uint16(data[5]) << 8)
|
||||||
u.wLength = uint16(data[6]) | (uint16(data[7]) << 8)
|
u.WLength = uint16(data[6]) | (uint16(data[7]) << 8)
|
||||||
return u
|
return u
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -299,41 +277,41 @@ func (usbcdc *USBCDC) Receive(data byte) {
|
||||||
|
|
||||||
// sendDescriptor creates and sends the various USB descriptor types that
|
// sendDescriptor creates and sends the various USB descriptor types that
|
||||||
// can be requested by the host.
|
// can be requested by the host.
|
||||||
func sendDescriptor(setup usbSetup) {
|
func sendDescriptor(setup USBSetup) {
|
||||||
switch setup.wValueH {
|
switch setup.WValueH {
|
||||||
case usb_CONFIGURATION_DESCRIPTOR_TYPE:
|
case usb_CONFIGURATION_DESCRIPTOR_TYPE:
|
||||||
sendUSBPacket(0, usbDescriptor.Configuration, setup.wLength)
|
sendUSBPacket(0, usbDescriptor.Configuration, setup.WLength)
|
||||||
return
|
return
|
||||||
case usb_DEVICE_DESCRIPTOR_TYPE:
|
case usb_DEVICE_DESCRIPTOR_TYPE:
|
||||||
// composite descriptor
|
// composite descriptor
|
||||||
usbDescriptor.Configure(usb_VID, usb_PID)
|
usbDescriptor.Configure(usb_VID, usb_PID)
|
||||||
sendUSBPacket(0, usbDescriptor.Device, setup.wLength)
|
sendUSBPacket(0, usbDescriptor.Device, setup.WLength)
|
||||||
return
|
return
|
||||||
|
|
||||||
case usb_STRING_DESCRIPTOR_TYPE:
|
case usb_STRING_DESCRIPTOR_TYPE:
|
||||||
switch setup.wValueL {
|
switch setup.WValueL {
|
||||||
case 0:
|
case 0:
|
||||||
b := []byte{0x04, 0x03, 0x09, 0x04}
|
b := []byte{0x04, 0x03, 0x09, 0x04}
|
||||||
sendUSBPacket(0, b, setup.wLength)
|
sendUSBPacket(0, b, setup.WLength)
|
||||||
|
|
||||||
case usb_IPRODUCT:
|
case usb_IPRODUCT:
|
||||||
b := make([]byte, (len(usb_STRING_PRODUCT)<<1)+2)
|
b := make([]byte, (len(usb_STRING_PRODUCT)<<1)+2)
|
||||||
strToUTF16LEDescriptor(usb_STRING_PRODUCT, b)
|
strToUTF16LEDescriptor(usb_STRING_PRODUCT, b)
|
||||||
sendUSBPacket(0, b, setup.wLength)
|
sendUSBPacket(0, b, setup.WLength)
|
||||||
|
|
||||||
case usb_IMANUFACTURER:
|
case usb_IMANUFACTURER:
|
||||||
b := make([]byte, (len(usb_STRING_MANUFACTURER)<<1)+2)
|
b := make([]byte, (len(usb_STRING_MANUFACTURER)<<1)+2)
|
||||||
strToUTF16LEDescriptor(usb_STRING_MANUFACTURER, b)
|
strToUTF16LEDescriptor(usb_STRING_MANUFACTURER, b)
|
||||||
sendUSBPacket(0, b, setup.wLength)
|
sendUSBPacket(0, b, setup.WLength)
|
||||||
|
|
||||||
case usb_ISERIAL:
|
case usb_ISERIAL:
|
||||||
// TODO: allow returning a product serial number
|
// TODO: allow returning a product serial number
|
||||||
sendZlp()
|
SendZlp()
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
case usb_HID_REPORT_TYPE:
|
case usb_HID_REPORT_TYPE:
|
||||||
if h, ok := usbDescriptor.HID[setup.wIndex]; ok {
|
if h, ok := usbDescriptor.HID[setup.WIndex]; ok {
|
||||||
sendUSBPacket(0, h, setup.wLength)
|
sendUSBPacket(0, h, setup.WLength)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
case usb_DEVICE_QUALIFIER:
|
case usb_DEVICE_QUALIFIER:
|
||||||
|
@ -342,7 +320,7 @@ func sendDescriptor(setup usbSetup) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// do not know how to handle this message, so return zero
|
// do not know how to handle this message, so return zero
|
||||||
sendZlp()
|
SendZlp()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче