Skip to content

Commit 1c53ff1

Browse files
committed
machine: implement usb receive message throttling
1 parent 0dbb066 commit 1c53ff1

File tree

8 files changed

+31
-26
lines changed

8 files changed

+31
-26
lines changed

src/machine/machine_atsamd21_usb.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,9 @@ func handleUSBIRQ(intr interrupt.Interrupt) {
194194
setEPINTFLAG(i, epFlags)
195195
if (epFlags & sam.USB_DEVICE_EPINTFLAG_TRCPT0) > 0 {
196196
buf := handleEndpointRx(i)
197-
if usbRxHandler[i] != nil {
198-
usbRxHandler[i](buf)
197+
if usbRxHandler[i] == nil || usbRxHandler[i](buf) {
198+
AckUsbOutTransfer(i)
199199
}
200-
handleEndpointRxComplete(i)
201200
} else if (epFlags & sam.USB_DEVICE_EPINTFLAG_TRCPT1) > 0 {
202201
if usbTxHandler[i] != nil {
203202
usbTxHandler[i]()
@@ -417,7 +416,8 @@ func handleEndpointRx(ep uint32) []byte {
417416
return udd_ep_out_cache_buffer[ep][:count]
418417
}
419418

420-
func handleEndpointRxComplete(ep uint32) {
419+
// AckUsbOutTransfer is called to acknowledge the completion of a USB OUT transfer.
420+
func AckUsbOutTransfer(ep uint32) {
421421
// set byte count to zero
422422
usbEndpointDescriptors[ep].DeviceDescBank[0].PCKSIZE.ClearBits(usb_DEVICE_PCKSIZE_BYTE_COUNT_Mask << usb_DEVICE_PCKSIZE_BYTE_COUNT_Pos)
423423

src/machine/machine_atsamd51_usb.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,9 @@ func handleUSBIRQ(intr interrupt.Interrupt) {
197197
setEPINTFLAG(i, epFlags)
198198
if (epFlags & sam.USB_DEVICE_ENDPOINT_EPINTFLAG_TRCPT0) > 0 {
199199
buf := handleEndpointRx(i)
200-
if usbRxHandler[i] != nil {
201-
usbRxHandler[i](buf)
200+
if usbRxHandler[i] == nil || usbRxHandler[i](buf) {
201+
AckUsbOutTransfer(i)
202202
}
203-
handleEndpointRxComplete(i)
204203
} else if (epFlags & sam.USB_DEVICE_ENDPOINT_EPINTFLAG_TRCPT1) > 0 {
205204
if usbTxHandler[i] != nil {
206205
usbTxHandler[i]()
@@ -420,7 +419,8 @@ func handleEndpointRx(ep uint32) []byte {
420419
return udd_ep_out_cache_buffer[ep][:count]
421420
}
422421

423-
func handleEndpointRxComplete(ep uint32) {
422+
// AckUsbOutTransfer is called to acknowledge the completion of a USB OUT transfer.
423+
func AckUsbOutTransfer(ep uint32) {
424424
// set byte count to zero
425425
usbEndpointDescriptors[ep].DeviceDescBank[0].PCKSIZE.ClearBits(usb_DEVICE_PCKSIZE_BYTE_COUNT_Mask << usb_DEVICE_PCKSIZE_BYTE_COUNT_Pos)
426426

src/machine/machine_nrf52840_usb.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,9 @@ func handleUSBIRQ(interrupt.Interrupt) {
206206
if nrf.USBD.EVENTS_ENDEPOUT[i].Get() > 0 {
207207
nrf.USBD.EVENTS_ENDEPOUT[i].Set(0)
208208
buf := handleEndpointRx(uint32(i))
209-
if usbRxHandler[i] != nil {
210-
usbRxHandler[i](buf)
209+
if usbRxHandler[i] == nil || usbRxHandler[i](buf) {
210+
AckUsbOutTransfer(uint32(i))
211211
}
212-
handleEndpointRxComplete(uint32(i))
213212
exitCriticalSection()
214213
}
215214
}
@@ -304,7 +303,8 @@ func handleEndpointRx(ep uint32) []byte {
304303
return udd_ep_out_cache_buffer[ep][:count]
305304
}
306305

307-
func handleEndpointRxComplete(ep uint32) {
306+
// AckUsbOutTransfer is called to acknowledge the completion of a USB OUT transfer.
307+
func AckUsbOutTransfer(ep uint32) {
308308
// set ready for next data
309309
nrf.USBD.SIZE.EPOUT[ep].Set(0)
310310
}

src/machine/machine_rp2040_usb.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,9 @@ func handleUSBIRQ(intr interrupt.Interrupt) {
9595
for i := 0; i < 16; i++ {
9696
if s2&(1<<(i*2+1)) > 0 {
9797
buf := handleEndpointRx(uint32(i))
98-
if usbRxHandler[i] != nil {
99-
usbRxHandler[i](buf)
98+
if usbRxHandler[i] == nil || usbRxHandler[i](buf) {
99+
AckUsbOutTransfer(uint32(i))
100100
}
101-
handleEndpointRxComplete(uint32(i))
102101
}
103102
}
104103

src/machine/machine_rp2350_usb.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,9 @@ func handleUSBIRQ(intr interrupt.Interrupt) {
9898
for i := 0; i < 16; i++ {
9999
if s2&(1<<(i*2+1)) > 0 {
100100
buf := handleEndpointRx(uint32(i))
101-
if usbRxHandler[i] != nil {
102-
usbRxHandler[i](buf)
101+
if usbRxHandler[i] == nil || usbRxHandler[i](buf) {
102+
AckUsbOutTransfer(uint32(i))
103103
}
104-
handleEndpointRxComplete(uint32(i))
105104
}
106105
}
107106

src/machine/machine_rp2_usb.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ func handleEndpointRx(ep uint32) []byte {
128128
return _usbDPSRAM.EPxBuffer[ep].Buffer0[:sz]
129129
}
130130

131-
func handleEndpointRxComplete(ep uint32) {
131+
// AckUsbOutTransfer is called to acknowledge the completion of a USB OUT transfer.
132+
func AckUsbOutTransfer(ep uint32) {
132133
ep = ep & 0x7F
133134
setEPDataPID(ep, !epXdata0[ep])
134135
}

src/machine/usb.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,12 @@ func ConfigureUSBEndpoint(desc descriptor.Descriptor, epSettings []usb.EndpointC
319319
} else {
320320
endPoints[ep.Index] = uint32(ep.Type | usb.EndpointOut)
321321
if ep.RxHandler != nil {
322-
usbRxHandler[ep.Index] = ep.RxHandler
322+
usbRxHandler[ep.Index] = func(b []byte) bool {
323+
ep.RxHandler(b)
324+
return true
325+
}
326+
} else if ep.DelayRxHandler != nil {
327+
usbRxHandler[ep.Index] = ep.DelayRxHandler
323328
}
324329
}
325330
if ep.StallHandler != nil {

src/machine/usb/config.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package usb
22

33
type EndpointConfig struct {
4-
Index uint8
5-
IsIn bool
6-
TxHandler func()
7-
RxHandler func([]byte)
8-
StallHandler func(Setup) bool
9-
Type uint8
4+
Index uint8
5+
IsIn bool
6+
TxHandler func()
7+
RxHandler func([]byte)
8+
DelayRxHandler func([]byte) bool
9+
StallHandler func(Setup) bool
10+
Type uint8
1011
}
1112

1213
type SetupConfig struct {

0 commit comments

Comments
 (0)