From 8d93b9e54592a0069b7ea3a993b5fa0a0ed3fde3 Mon Sep 17 00:00:00 2001 From: sago35 Date: Wed, 24 Mar 2021 08:32:40 +0900 Subject: [PATCH] atsamd21, atsamd51, nrf52840: unify usbcdc code --- src/machine/machine_atsamd21.go | 10 +++------- src/machine/machine_atsamd51.go | 10 +++------- src/machine/machine_nrf52840_usb.go | 3 ++- 3 files changed, 8 insertions(+), 15 deletions(-) diff --git a/src/machine/machine_atsamd21.go b/src/machine/machine_atsamd21.go index 69abee25..c01b69b9 100644 --- a/src/machine/machine_atsamd21.go +++ b/src/machine/machine_atsamd21.go @@ -1646,6 +1646,7 @@ func (usbcdc *USBCDC) Flush() error { if usbcdc.waitTxc { // waiting for the next flush(), because the transmission is not complete + usbcdc.waitTxcRetryCount++ return nil } usbcdc.waitTxc = true @@ -1700,7 +1701,7 @@ func (usbcdc *USBCDC) WriteByte(c byte) error { mask := interrupt.Disable() UART0.waitTxc = false UART0.waitTxcRetryCount = 0 - usbcdc.TxIdx.Set(0) + UART0.TxIdx.Set(0) usbLineInfo.lineState = 0 interrupt.Restore(mask) break @@ -1868,6 +1869,7 @@ func handleUSB(intr interrupt.Interrupt) { // Start of frame if (flags & sam.USB_DEVICE_INTFLAG_SOF) > 0 { + UART0.Flush() // if you want to blink LED showing traffic, this would be the place... } @@ -1931,13 +1933,7 @@ func handleUSB(intr interrupt.Interrupt) { } } } - - if i == usb_CDC_ENDPOINT_IN && UART0.waitTxc { - UART0.waitTxcRetryCount++ - } } - - UART0.Flush() } func initEndpoint(ep, config uint32) { diff --git a/src/machine/machine_atsamd51.go b/src/machine/machine_atsamd51.go index 5cb7df40..97165874 100644 --- a/src/machine/machine_atsamd51.go +++ b/src/machine/machine_atsamd51.go @@ -1850,6 +1850,7 @@ func (usbcdc *USBCDC) Flush() error { if usbcdc.waitTxc { // waiting for the next flush(), because the transmission is not complete + usbcdc.waitTxcRetryCount++ return nil } usbcdc.waitTxc = true @@ -1904,7 +1905,7 @@ func (usbcdc *USBCDC) WriteByte(c byte) error { mask := interrupt.Disable() UART0.waitTxc = false UART0.waitTxcRetryCount = 0 - usbcdc.TxIdx.Set(0) + UART0.TxIdx.Set(0) usbLineInfo.lineState = 0 interrupt.Restore(mask) break @@ -2074,6 +2075,7 @@ func handleUSBIRQ(interrupt.Interrupt) { // Start of frame if (flags & sam.USB_DEVICE_INTFLAG_SOF) > 0 { + UART0.Flush() // if you want to blink LED showing traffic, this would be the place... } @@ -2137,13 +2139,7 @@ func handleUSBIRQ(interrupt.Interrupt) { } } } - - if i == usb_CDC_ENDPOINT_IN && UART0.waitTxc { - UART0.waitTxcRetryCount++ - } } - - UART0.Flush() } func initEndpoint(ep, config uint32) { diff --git a/src/machine/machine_nrf52840_usb.go b/src/machine/machine_nrf52840_usb.go index 3a04599f..26f240c3 100644 --- a/src/machine/machine_nrf52840_usb.go +++ b/src/machine/machine_nrf52840_usb.go @@ -65,7 +65,7 @@ func (usbcdc *USBCDC) Flush() error { } // WriteByte writes a byte of data to the USB CDC interface. -func (usbcdc USBCDC) WriteByte(c byte) error { +func (usbcdc *USBCDC) WriteByte(c byte) error { // Supposedly to handle problem with Windows USB serial ports? if usbLineInfo.lineState > 0 { ok := false @@ -199,6 +199,7 @@ func (usbcdc *USBCDC) handleInterrupt(interrupt.Interrupt) { if nrf.USBD.EVENTS_SOF.Get() == 1 { nrf.USBD.EVENTS_SOF.Set(0) USB.Flush() + // if you want to blink LED showing traffic, this would be the place... } // USBD ready event