@@ -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
2626var (
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,8 +221,9 @@ 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
226+ // Set interrupt enable
220227 setEPINTENSET (ep , sam .USB_DEVICE_EPINTENSET_TRCPT1 )
221228
222229 case usb .ENDPOINT_TYPE_BULK | usb .EndpointOut :
@@ -227,7 +234,7 @@ 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
233240 setEPINTENSET (ep , sam .USB_DEVICE_EPINTENSET_TRCPT0 )
@@ -246,7 +253,7 @@ 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
252259 setEPINTENSET (ep , sam .USB_DEVICE_EPINTENSET_TRCPT0 )
@@ -265,11 +272,12 @@ 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
280+ // Set interrupt enable
273281 setEPINTENSET (ep , sam .USB_DEVICE_EPINTENSET_TRCPT1 )
274282
275283 case usb .ENDPOINT_TYPE_CONTROL :
@@ -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
@@ -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
432439func SendZlp () {
@@ -479,24 +486,49 @@ func getEPCFG(ep uint32) uint8 {
479486 }
480487}
481488
482- func setEPCFG (ep uint32 , val uint8 ) {
489+ // Configure output endpoint in EPCFG
490+ func setEPCFGEPType0 (ep uint32 , val uint8 ) {
491+ switch ep {
492+ case 0 :
493+ sam .USB_DEVICE .SetEPCFG0_EPTYPE0 (val )
494+ case 1 :
495+ sam .USB_DEVICE .SetEPCFG1_EPTYPE0 (val )
496+ case 2 :
497+ sam .USB_DEVICE .SetEPCFG2_EPTYPE0 (val )
498+ case 3 :
499+ sam .USB_DEVICE .SetEPCFG3_EPTYPE0 (val )
500+ case 4 :
501+ sam .USB_DEVICE .SetEPCFG4_EPTYPE0 (val )
502+ case 5 :
503+ sam .USB_DEVICE .SetEPCFG5_EPTYPE0 (val )
504+ case 6 :
505+ sam .USB_DEVICE .SetEPCFG6_EPTYPE0 (val )
506+ case 7 :
507+ sam .USB_DEVICE .SetEPCFG7_EPTYPE0 (val )
508+ default :
509+ return
510+ }
511+ }
512+
513+ // Configure input endpoint in EPCFG
514+ func setEPCFGEPType1 (ep uint32 , val uint8 ) {
483515 switch ep {
484516 case 0 :
485- sam .USB_DEVICE .EPCFG0 . Set (val )
517+ sam .USB_DEVICE .SetEPCFG0_EPTYPE1 (val )
486518 case 1 :
487- sam .USB_DEVICE .EPCFG1 . Set (val )
519+ sam .USB_DEVICE .SetEPCFG1_EPTYPE1 (val )
488520 case 2 :
489- sam .USB_DEVICE .EPCFG2 . Set (val )
521+ sam .USB_DEVICE .SetEPCFG2_EPTYPE1 (val )
490522 case 3 :
491- sam .USB_DEVICE .EPCFG3 . Set (val )
523+ sam .USB_DEVICE .SetEPCFG3_EPTYPE1 (val )
492524 case 4 :
493- sam .USB_DEVICE .EPCFG4 . Set (val )
525+ sam .USB_DEVICE .SetEPCFG4_EPTYPE1 (val )
494526 case 5 :
495- sam .USB_DEVICE .EPCFG5 . Set (val )
527+ sam .USB_DEVICE .SetEPCFG5_EPTYPE1 (val )
496528 case 6 :
497- sam .USB_DEVICE .EPCFG6 . Set (val )
529+ sam .USB_DEVICE .SetEPCFG6_EPTYPE1 (val )
498530 case 7 :
499- sam .USB_DEVICE .EPCFG7 . Set (val )
531+ sam .USB_DEVICE .SetEPCFG7_EPTYPE1 (val )
500532 default :
501533 return
502534 }
0 commit comments