Skip to content

Commit 815be48

Browse files
committed
feat: allow bidirectional usb endpoint numbers
1 parent 3869f76 commit 815be48

File tree

13 files changed

+292
-163
lines changed

13 files changed

+292
-163
lines changed

src/machine/machine_atsamd21_usb.go

Lines changed: 110 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,24 @@ const (
2020
usb_DEVICE_PCKSIZE_MULTI_PACKET_SIZE_Pos = 14
2121
usb_DEVICE_PCKSIZE_MULTI_PACKET_SIZE_Mask = 0x3FFF
2222

23-
NumberOfUSBEndpoints = 8
23+
NumberOfUSBEndpoints = 6
2424
)
2525

2626
var (
27-
endPoints = []uint32{
27+
inEndpoints = []uint32{
28+
usb.CONTROL_ENDPOINT: usb.ENDPOINT_TYPE_CONTROL,
29+
usb.CDC_ENDPOINT_ACM: (usb.ENDPOINT_TYPE_INTERRUPT | usb.EndpointIn),
30+
usb.CDC_ENDPOINT_IN: (usb.ENDPOINT_TYPE_BULK | usb.EndpointIn),
31+
usb.HID_ENDPOINT_IN: (usb.ENDPOINT_TYPE_DISABLE), // Interrupt In
32+
usb.MIDI_ENDPOINT_IN: (usb.ENDPOINT_TYPE_DISABLE), // Bulk In
33+
usb.MSC_ENDPOINT_IN: (usb.ENDPOINT_TYPE_DISABLE), // Bulk In
34+
}
35+
outEndpoints = []uint32{
2836
usb.CONTROL_ENDPOINT: usb.ENDPOINT_TYPE_CONTROL,
29-
usb.CDC_ENDPOINT_ACM: (usb.ENDPOINT_TYPE_INTERRUPT | usb.EndpointIn),
3037
usb.CDC_ENDPOINT_OUT: (usb.ENDPOINT_TYPE_BULK | usb.EndpointOut),
31-
usb.CDC_ENDPOINT_IN: (usb.ENDPOINT_TYPE_BULK | usb.EndpointIn),
32-
usb.HID_ENDPOINT_IN: (usb.ENDPOINT_TYPE_DISABLE), // Interrupt In
3338
usb.HID_ENDPOINT_OUT: (usb.ENDPOINT_TYPE_DISABLE), // Interrupt Out
34-
usb.MIDI_ENDPOINT_IN: (usb.ENDPOINT_TYPE_DISABLE), // Bulk In
3539
usb.MIDI_ENDPOINT_OUT: (usb.ENDPOINT_TYPE_DISABLE), // Bulk Out
40+
usb.MSC_ENDPOINT_OUT: (usb.ENDPOINT_TYPE_DISABLE), // Bulk Out
3641
}
3742
)
3843

@@ -188,7 +193,7 @@ func handleUSBIRQ(intr interrupt.Interrupt) {
188193

189194
// Now the actual transfer handlers, ignore endpoint number 0 (setup)
190195
var i uint32
191-
for i = 1; i < uint32(len(endPoints)); i++ {
196+
for i = 1; i < NumberOfUSBEndpoints; i++ {
192197
// Check if endpoint has a pending interrupt
193198
epFlags := getEPINTFLAG(i)
194199
setEPINTFLAG(i, epFlags)
@@ -197,7 +202,8 @@ func handleUSBIRQ(intr interrupt.Interrupt) {
197202
if usbRxHandler[i] == nil || usbRxHandler[i](buf) {
198203
AckUsbOutTransfer(i)
199204
}
200-
} else if (epFlags & sam.USB_DEVICE_EPINTFLAG_TRCPT1) > 0 {
205+
}
206+
if (epFlags & sam.USB_DEVICE_EPINTFLAG_TRCPT1) > 0 {
201207
if usbTxHandler[i] != nil {
202208
usbTxHandler[i]()
203209
}
@@ -215,9 +221,10 @@ func initEndpoint(ep, config uint32) {
215221
usbEndpointDescriptors[ep].DeviceDescBank[1].ADDR.Set(uint32(uintptr(unsafe.Pointer(&udd_ep_in_cache_buffer[ep]))))
216222

217223
// set endpoint type
218-
setEPCFG(ep, ((usb.ENDPOINT_TYPE_INTERRUPT + 1) << sam.USB_DEVICE_EPCFG_EPTYPE1_Pos))
224+
setEPCFGEPType1(ep, (usb.ENDPOINT_TYPE_INTERRUPT + 1))
219225

220-
setEPINTENSET(ep, sam.USB_DEVICE_EPINTENSET_TRCPT1)
226+
// Set interrupt enable
227+
setEPINTENSETTRCPT1(ep, sam.USB_DEVICE_EPINTENSET_TRCPT1)
221228

222229
case usb.ENDPOINT_TYPE_BULK | usb.EndpointOut:
223230
// set packet size
@@ -227,10 +234,10 @@ func initEndpoint(ep, config uint32) {
227234
usbEndpointDescriptors[ep].DeviceDescBank[0].ADDR.Set(uint32(uintptr(unsafe.Pointer(&udd_ep_out_cache_buffer[ep]))))
228235

229236
// set endpoint type
230-
setEPCFG(ep, ((usb.ENDPOINT_TYPE_BULK + 1) << sam.USB_DEVICE_EPCFG_EPTYPE0_Pos))
237+
setEPCFGEPType0(ep, (usb.ENDPOINT_TYPE_BULK + 1))
231238

232239
// receive interrupts when current transfer complete
233-
setEPINTENSET(ep, sam.USB_DEVICE_EPINTENSET_TRCPT0)
240+
setEPINTENSETTRCPT0(ep, sam.USB_DEVICE_EPINTENSET_TRCPT0)
234241

235242
// set byte count to zero, we have not received anything yet
236243
usbEndpointDescriptors[ep].DeviceDescBank[0].PCKSIZE.ClearBits(usb_DEVICE_PCKSIZE_BYTE_COUNT_Mask << usb_DEVICE_PCKSIZE_BYTE_COUNT_Pos)
@@ -246,10 +253,10 @@ func initEndpoint(ep, config uint32) {
246253
usbEndpointDescriptors[ep].DeviceDescBank[0].ADDR.Set(uint32(uintptr(unsafe.Pointer(&udd_ep_out_cache_buffer[ep]))))
247254

248255
// set endpoint type
249-
setEPCFG(ep, ((usb.ENDPOINT_TYPE_INTERRUPT + 1) << sam.USB_DEVICE_EPCFG_EPTYPE0_Pos))
256+
setEPCFGEPType0(ep, (usb.ENDPOINT_TYPE_INTERRUPT + 1))
250257

251258
// receive interrupts when current transfer complete
252-
setEPINTENSET(ep, sam.USB_DEVICE_EPINTENSET_TRCPT0)
259+
setEPINTENSETTRCPT0(ep, sam.USB_DEVICE_EPINTENSET_TRCPT0)
253260

254261
// set byte count to zero, we have not received anything yet
255262
usbEndpointDescriptors[ep].DeviceDescBank[0].PCKSIZE.ClearBits(usb_DEVICE_PCKSIZE_BYTE_COUNT_Mask << usb_DEVICE_PCKSIZE_BYTE_COUNT_Pos)
@@ -265,12 +272,13 @@ func initEndpoint(ep, config uint32) {
265272
usbEndpointDescriptors[ep].DeviceDescBank[1].ADDR.Set(uint32(uintptr(unsafe.Pointer(&udd_ep_in_cache_buffer[ep]))))
266273

267274
// set endpoint type
268-
setEPCFG(ep, ((usb.ENDPOINT_TYPE_BULK + 1) << sam.USB_DEVICE_EPCFG_EPTYPE1_Pos))
275+
setEPCFGEPType1(ep, (usb.ENDPOINT_TYPE_BULK + 1))
269276

270277
// NAK on endpoint IN, the bank is not yet filled in.
271278
setEPSTATUSCLR(ep, sam.USB_DEVICE_EPSTATUSCLR_BK1RDY)
272279

273-
setEPINTENSET(ep, sam.USB_DEVICE_EPINTENSET_TRCPT1)
280+
// Set interrupt enable
281+
setEPINTENSETTRCPT1(ep, sam.USB_DEVICE_EPINTENSET_TRCPT1)
274282

275283
case usb.ENDPOINT_TYPE_CONTROL:
276284
// Control OUT
@@ -281,7 +289,7 @@ func initEndpoint(ep, config uint32) {
281289
usbEndpointDescriptors[ep].DeviceDescBank[0].ADDR.Set(uint32(uintptr(unsafe.Pointer(&udd_ep_out_cache_buffer[ep]))))
282290

283291
// set endpoint type
284-
setEPCFG(ep, getEPCFG(ep)|((usb.ENDPOINT_TYPE_CONTROL+1)<<sam.USB_DEVICE_EPCFG_EPTYPE0_Pos))
292+
setEPCFGEPType0(ep, (usb.ENDPOINT_TYPE_CONTROL + 1))
285293

286294
// Control IN
287295
// set packet size
@@ -291,7 +299,7 @@ func initEndpoint(ep, config uint32) {
291299
usbEndpointDescriptors[ep].DeviceDescBank[1].ADDR.Set(uint32(uintptr(unsafe.Pointer(&udd_ep_in_cache_buffer[ep]))))
292300

293301
// set endpoint type
294-
setEPCFG(ep, getEPCFG(ep)|((usb.ENDPOINT_TYPE_CONTROL+1)<<sam.USB_DEVICE_EPCFG_EPTYPE1_Pos))
302+
setEPCFGEPType1(ep, (usb.ENDPOINT_TYPE_CONTROL + 1))
295303

296304
// Prepare OUT endpoint for receive
297305
// set multi packet size for expected number of receive bytes on control OUT
@@ -304,7 +312,7 @@ func initEndpoint(ep, config uint32) {
304312
setEPSTATUSSET(ep, sam.USB_DEVICE_EPSTATUSSET_BK0RDY)
305313

306314
// Enable Setup-Received interrupt
307-
setEPINTENSET(0, sam.USB_DEVICE_EPINTENSET_RXSTP)
315+
setEPINTENSETRXSTP(0, sam.USB_DEVICE_EPINTENSET_RXSTP)
308316
}
309317
}
310318

@@ -426,7 +434,6 @@ func AckUsbOutTransfer(ep uint32) {
426434

427435
// set ready for next data
428436
setEPSTATUSCLR(ep, sam.USB_DEVICE_EPSTATUSCLR_BK0RDY)
429-
430437
}
431438

432439
func SendZlp() {
@@ -479,24 +486,47 @@ func getEPCFG(ep uint32) uint8 {
479486
}
480487
}
481488

482-
func setEPCFG(ep uint32, val uint8) {
489+
func setEPCFGEPType0(ep uint32, val uint8) {
483490
switch ep {
484491
case 0:
485-
sam.USB_DEVICE.EPCFG0.Set(val)
492+
sam.USB_DEVICE.SetEPCFG0_EPTYPE0(val)
486493
case 1:
487-
sam.USB_DEVICE.EPCFG1.Set(val)
494+
sam.USB_DEVICE.SetEPCFG1_EPTYPE0(val)
488495
case 2:
489-
sam.USB_DEVICE.EPCFG2.Set(val)
496+
sam.USB_DEVICE.SetEPCFG2_EPTYPE0(val)
490497
case 3:
491-
sam.USB_DEVICE.EPCFG3.Set(val)
498+
sam.USB_DEVICE.SetEPCFG3_EPTYPE0(val)
492499
case 4:
493-
sam.USB_DEVICE.EPCFG4.Set(val)
500+
sam.USB_DEVICE.SetEPCFG4_EPTYPE0(val)
494501
case 5:
495-
sam.USB_DEVICE.EPCFG5.Set(val)
502+
sam.USB_DEVICE.SetEPCFG5_EPTYPE0(val)
496503
case 6:
497-
sam.USB_DEVICE.EPCFG6.Set(val)
504+
sam.USB_DEVICE.SetEPCFG6_EPTYPE0(val)
498505
case 7:
499-
sam.USB_DEVICE.EPCFG7.Set(val)
506+
sam.USB_DEVICE.SetEPCFG7_EPTYPE0(val)
507+
default:
508+
return
509+
}
510+
}
511+
512+
func setEPCFGEPType1(ep uint32, val uint8) {
513+
switch ep {
514+
case 0:
515+
sam.USB_DEVICE.SetEPCFG0_EPTYPE1(val)
516+
case 1:
517+
sam.USB_DEVICE.SetEPCFG1_EPTYPE1(val)
518+
case 2:
519+
sam.USB_DEVICE.SetEPCFG2_EPTYPE1(val)
520+
case 3:
521+
sam.USB_DEVICE.SetEPCFG3_EPTYPE1(val)
522+
case 4:
523+
sam.USB_DEVICE.SetEPCFG4_EPTYPE1(val)
524+
case 5:
525+
sam.USB_DEVICE.SetEPCFG5_EPTYPE1(val)
526+
case 6:
527+
sam.USB_DEVICE.SetEPCFG6_EPTYPE1(val)
528+
case 7:
529+
sam.USB_DEVICE.SetEPCFG7_EPTYPE1(val)
500530
default:
501531
return
502532
}
@@ -640,25 +670,67 @@ func setEPINTENCLR(ep uint32, val uint8) {
640670
}
641671
}
642672

643-
func setEPINTENSET(ep uint32, val uint8) {
673+
func setEPINTENSETRXSTP(ep uint32, val uint8) {
644674
switch ep {
645675
case 0:
646-
sam.USB_DEVICE.EPINTENSET0.Set(val)
676+
sam.USB_DEVICE.SetEPINTENSET0_RXSTP(val)
647677
case 1:
648-
sam.USB_DEVICE.EPINTENSET1.Set(val)
678+
sam.USB_DEVICE.SetEPINTENSET1_RXSTP(val)
649679
case 2:
650-
sam.USB_DEVICE.EPINTENSET2.Set(val)
680+
sam.USB_DEVICE.SetEPINTENSET2_RXSTP(val)
651681
case 3:
652-
sam.USB_DEVICE.EPINTENSET3.Set(val)
682+
sam.USB_DEVICE.SetEPINTENSET3_RXSTP(val)
653683
case 4:
654-
sam.USB_DEVICE.EPINTENSET4.Set(val)
684+
sam.USB_DEVICE.SetEPINTENSET4_RXSTP(val)
655685
case 5:
656-
sam.USB_DEVICE.EPINTENSET5.Set(val)
686+
sam.USB_DEVICE.SetEPINTENSET5_RXSTP(val)
657687
case 6:
658-
sam.USB_DEVICE.EPINTENSET6.Set(val)
688+
sam.USB_DEVICE.SetEPINTENSET6_RXSTP(val)
659689
case 7:
660-
sam.USB_DEVICE.EPINTENSET7.Set(val)
690+
sam.USB_DEVICE.SetEPINTENSET7_RXSTP(val)
661691
default:
662692
return
663693
}
664694
}
695+
696+
func setEPINTENSETTRCPT0(ep uint32, val uint8) {
697+
switch ep {
698+
case 0:
699+
sam.USB_DEVICE.SetEPINTENSET0_TRCPT0(val)
700+
case 1:
701+
sam.USB_DEVICE.SetEPINTENSET1_TRCPT0(val)
702+
case 2:
703+
sam.USB_DEVICE.SetEPINTENSET2_TRCPT0(val)
704+
case 3:
705+
sam.USB_DEVICE.SetEPINTENSET3_TRCPT0(val)
706+
case 4:
707+
sam.USB_DEVICE.SetEPINTENSET4_TRCPT0(val)
708+
case 5:
709+
sam.USB_DEVICE.SetEPINTENSET5_TRCPT0(val)
710+
case 6:
711+
sam.USB_DEVICE.SetEPINTENSET6_TRCPT0(val)
712+
case 7:
713+
sam.USB_DEVICE.SetEPINTENSET7_TRCPT0(val)
714+
}
715+
}
716+
717+
func setEPINTENSETTRCPT1(ep uint32, val uint8) {
718+
switch ep {
719+
case 0:
720+
sam.USB_DEVICE.SetEPINTENSET0_TRCPT1(val)
721+
case 1:
722+
sam.USB_DEVICE.SetEPINTENSET1_TRCPT1(val)
723+
case 2:
724+
sam.USB_DEVICE.SetEPINTENSET2_TRCPT1(val)
725+
case 3:
726+
sam.USB_DEVICE.SetEPINTENSET3_TRCPT1(val)
727+
case 4:
728+
sam.USB_DEVICE.SetEPINTENSET4_TRCPT1(val)
729+
case 5:
730+
sam.USB_DEVICE.SetEPINTENSET5_TRCPT1(val)
731+
case 6:
732+
sam.USB_DEVICE.SetEPINTENSET6_TRCPT1(val)
733+
case 7:
734+
sam.USB_DEVICE.SetEPINTENSET7_TRCPT1(val)
735+
}
736+
}

0 commit comments

Comments
 (0)