From 7c949ad386ef36eb0be100eb54a4190740f6f24d Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Tue, 11 May 2021 13:39:14 +0200 Subject: [PATCH] machine: make USBCDC global a pointer Make the USBCDC use a pointer receiver everywhere. This makes it easier to pass around the object in the future. This commit sometimes changes code size, but not significantly (a few bytes) and usually in a positive way. My eventual goal is the following: - Declare `machine.USB` (or similar, name TBD) as a pointer receiver for the USB-CDC interface. - Let `machine.UART0` always point to an UART, never actually to a USBCDC object. - Define `machine.Serial`, which is either a real UART or an USB-CDC, depending on the board. This way, if you want a real UART you can use machine.UARTx and if you just want to print to the default serial port, you can use machine.Serial. This change does have an effect on code size and memory consumption. There is often a small reduction (-8 bytes) in RAM consumption and an increase in flash consumption. --- src/machine/board_circuitplay_bluefruit.go | 2 +- src/machine/board_clue_alpha.go | 2 +- src/machine/board_feather-nrf52840.go | 2 +- src/machine/board_itsybitsy-nrf52840.go | 2 +- src/machine/board_particle_argon.go | 2 +- src/machine/board_particle_boron.go | 2 +- src/machine/board_particle_xenon.go | 2 +- src/machine/machine_atsamd21.go | 32 +++++++++--------- src/machine/machine_atsamd51.go | 32 +++++++++--------- src/machine/machine_nrf52840_usb.go | 39 +++++++++++----------- src/machine/usb.go | 10 +++--- 11 files changed, 64 insertions(+), 63 deletions(-) diff --git a/src/machine/board_circuitplay_bluefruit.go b/src/machine/board_circuitplay_bluefruit.go index 38c3995c..0a78ca5e 100644 --- a/src/machine/board_circuitplay_bluefruit.go +++ b/src/machine/board_circuitplay_bluefruit.go @@ -58,7 +58,7 @@ const ( // UART0 is the USB device var ( - UART0 = &USB + UART0 = USB ) // I2C pins diff --git a/src/machine/board_clue_alpha.go b/src/machine/board_clue_alpha.go index 0397c476..a6c8807b 100644 --- a/src/machine/board_clue_alpha.go +++ b/src/machine/board_clue_alpha.go @@ -105,7 +105,7 @@ const ( // UART0 is the USB device var ( - UART0 = &USB + UART0 = USB ) // I2C pins diff --git a/src/machine/board_feather-nrf52840.go b/src/machine/board_feather-nrf52840.go index ae4b54e0..f353a3b7 100644 --- a/src/machine/board_feather-nrf52840.go +++ b/src/machine/board_feather-nrf52840.go @@ -77,7 +77,7 @@ const ( // UART0 is the USB device var ( - UART0 = &USB + UART0 = USB ) // I2C pins diff --git a/src/machine/board_itsybitsy-nrf52840.go b/src/machine/board_itsybitsy-nrf52840.go index 4d98b669..e61be006 100644 --- a/src/machine/board_itsybitsy-nrf52840.go +++ b/src/machine/board_itsybitsy-nrf52840.go @@ -72,7 +72,7 @@ const ( // UART0 is the USB device var ( - UART0 = &USB + UART0 = USB ) // I2C pins diff --git a/src/machine/board_particle_argon.go b/src/machine/board_particle_argon.go index 44e5a51b..5401c4ad 100644 --- a/src/machine/board_particle_argon.go +++ b/src/machine/board_particle_argon.go @@ -41,7 +41,7 @@ const ( // UART var ( - Serial = &USB + Serial = USB UART0 = NRF_UART0 ) diff --git a/src/machine/board_particle_boron.go b/src/machine/board_particle_boron.go index a50d87eb..cb05e725 100644 --- a/src/machine/board_particle_boron.go +++ b/src/machine/board_particle_boron.go @@ -41,7 +41,7 @@ const ( // UART var ( - Serial = &USB + Serial = USB UART0 = NRF_UART0 ) diff --git a/src/machine/board_particle_xenon.go b/src/machine/board_particle_xenon.go index 7a94cf6d..c1614636 100644 --- a/src/machine/board_particle_xenon.go +++ b/src/machine/board_particle_xenon.go @@ -41,7 +41,7 @@ const ( // UART var ( - Serial = &USB + Serial = USB UART0 = NRF_UART0 ) diff --git a/src/machine/machine_atsamd21.go b/src/machine/machine_atsamd21.go index 4bacd2aa..058099cd 100644 --- a/src/machine/machine_atsamd21.go +++ b/src/machine/machine_atsamd21.go @@ -506,7 +506,7 @@ type UART struct { var ( // UART0 is actually a USB CDC interface. - UART0 = USBCDC{Buffer: NewRingBuffer()} + UART0 = &USBCDC{Buffer: NewRingBuffer()} ) const ( @@ -1788,7 +1788,7 @@ func (usbcdc *USBCDC) Flush() error { // send data by setting bank ready setEPSTATUSSET(usb_CDC_ENDPOINT_IN, sam.USB_DEVICE_EPSTATUSSET_BK1RDY) - UART0.sent = true + usbcdc.sent = true } } return nil @@ -1802,10 +1802,10 @@ func (usbcdc *USBCDC) WriteByte(c byte) error { for { mask := interrupt.Disable() - idx := UART0.TxIdx.Get() + idx := usbcdc.TxIdx.Get() if (idx & usbcdcTxSizeMask) < usbcdcTxSizeMask { udd_ep_in_cache_buffer[usb_CDC_ENDPOINT_IN][idx] = c - UART0.TxIdx.Set(idx + 1) + usbcdc.TxIdx.Set(idx + 1) ok = true } @@ -1813,26 +1813,26 @@ func (usbcdc *USBCDC) WriteByte(c byte) error { if ok { break - } else if usbcdcTxMaxRetriesAllowed < UART0.waitTxcRetryCount { + } else if usbcdcTxMaxRetriesAllowed < usbcdc.waitTxcRetryCount { mask := interrupt.Disable() - UART0.waitTxc = false - UART0.waitTxcRetryCount = 0 - UART0.TxIdx.Set(0) + usbcdc.waitTxc = false + usbcdc.waitTxcRetryCount = 0 + usbcdc.TxIdx.Set(0) usbLineInfo.lineState = 0 interrupt.Restore(mask) break } else { mask := interrupt.Disable() - if UART0.sent { - if UART0.waitTxc { + if usbcdc.sent { + if usbcdc.waitTxc { if (getEPINTFLAG(usb_CDC_ENDPOINT_IN) & sam.USB_DEVICE_EPINTFLAG_TRCPT1) != 0 { setEPSTATUSCLR(usb_CDC_ENDPOINT_IN, sam.USB_DEVICE_EPSTATUSCLR_BK1RDY) setEPINTFLAG(usb_CDC_ENDPOINT_IN, sam.USB_DEVICE_EPINTFLAG_TRCPT1) - UART0.waitTxc = false - UART0.Flush() + usbcdc.waitTxc = false + usbcdc.Flush() } } else { - UART0.Flush() + usbcdc.Flush() } } interrupt.Restore(mask) @@ -1843,11 +1843,11 @@ func (usbcdc *USBCDC) WriteByte(c byte) error { return nil } -func (usbcdc USBCDC) DTR() bool { +func (usbcdc *USBCDC) DTR() bool { return (usbLineInfo.lineState & usb_CDC_LINESTATE_DTR) > 0 } -func (usbcdc USBCDC) RTS() bool { +func (usbcdc *USBCDC) RTS() bool { return (usbLineInfo.lineState & usb_CDC_LINESTATE_RTS) > 0 } @@ -1882,7 +1882,7 @@ var ( ) // Configure the USB CDC interface. The config is here for compatibility with the UART interface. -func (usbcdc USBCDC) Configure(config UARTConfig) { +func (usbcdc *USBCDC) Configure(config UARTConfig) { // reset USB interface sam.USB_DEVICE.CTRLA.SetBits(sam.USB_DEVICE_CTRLA_SWRST) for sam.USB_DEVICE.SYNCBUSY.HasBits(sam.USB_DEVICE_SYNCBUSY_SWRST) || diff --git a/src/machine/machine_atsamd51.go b/src/machine/machine_atsamd51.go index f8b0a865..f21d0173 100644 --- a/src/machine/machine_atsamd51.go +++ b/src/machine/machine_atsamd51.go @@ -952,7 +952,7 @@ type UART struct { var ( // UART0 is actually a USB CDC interface. - UART0 = USBCDC{Buffer: NewRingBuffer()} + UART0 = &USBCDC{Buffer: NewRingBuffer()} ) const ( @@ -1994,7 +1994,7 @@ func (usbcdc *USBCDC) Flush() error { // send data by setting bank ready setEPSTATUSSET(usb_CDC_ENDPOINT_IN, sam.USB_DEVICE_ENDPOINT_EPSTATUSSET_BK1RDY) - UART0.sent = true + usbcdc.sent = true } } return nil @@ -2008,10 +2008,10 @@ func (usbcdc *USBCDC) WriteByte(c byte) error { for { mask := interrupt.Disable() - idx := UART0.TxIdx.Get() + idx := usbcdc.TxIdx.Get() if (idx & usbcdcTxSizeMask) < usbcdcTxSizeMask { udd_ep_in_cache_buffer[usb_CDC_ENDPOINT_IN][idx] = c - UART0.TxIdx.Set(idx + 1) + usbcdc.TxIdx.Set(idx + 1) ok = true } @@ -2019,26 +2019,26 @@ func (usbcdc *USBCDC) WriteByte(c byte) error { if ok { break - } else if usbcdcTxMaxRetriesAllowed < UART0.waitTxcRetryCount { + } else if usbcdcTxMaxRetriesAllowed < usbcdc.waitTxcRetryCount { mask := interrupt.Disable() - UART0.waitTxc = false - UART0.waitTxcRetryCount = 0 - UART0.TxIdx.Set(0) + usbcdc.waitTxc = false + usbcdc.waitTxcRetryCount = 0 + usbcdc.TxIdx.Set(0) usbLineInfo.lineState = 0 interrupt.Restore(mask) break } else { mask := interrupt.Disable() - if UART0.sent { - if UART0.waitTxc { + if usbcdc.sent { + if usbcdc.waitTxc { if (getEPINTFLAG(usb_CDC_ENDPOINT_IN) & sam.USB_DEVICE_ENDPOINT_EPINTFLAG_TRCPT1) != 0 { setEPSTATUSCLR(usb_CDC_ENDPOINT_IN, sam.USB_DEVICE_ENDPOINT_EPSTATUSCLR_BK1RDY) setEPINTFLAG(usb_CDC_ENDPOINT_IN, sam.USB_DEVICE_ENDPOINT_EPINTFLAG_TRCPT1) - UART0.waitTxc = false - UART0.Flush() + usbcdc.waitTxc = false + usbcdc.Flush() } } else { - UART0.Flush() + usbcdc.Flush() } } interrupt.Restore(mask) @@ -2049,11 +2049,11 @@ func (usbcdc *USBCDC) WriteByte(c byte) error { return nil } -func (usbcdc USBCDC) DTR() bool { +func (usbcdc *USBCDC) DTR() bool { return (usbLineInfo.lineState & usb_CDC_LINESTATE_DTR) > 0 } -func (usbcdc USBCDC) RTS() bool { +func (usbcdc *USBCDC) RTS() bool { return (usbLineInfo.lineState & usb_CDC_LINESTATE_RTS) > 0 } @@ -2088,7 +2088,7 @@ var ( ) // Configure the USB CDC interface. The config is here for compatibility with the UART interface. -func (usbcdc USBCDC) Configure(config UARTConfig) { +func (usbcdc *USBCDC) Configure(config UARTConfig) { // reset USB interface sam.USB_DEVICE.CTRLA.SetBits(sam.USB_DEVICE_CTRLA_SWRST) for sam.USB_DEVICE.SYNCBUSY.HasBits(sam.USB_DEVICE_SYNCBUSY_SWRST) || diff --git a/src/machine/machine_nrf52840_usb.go b/src/machine/machine_nrf52840_usb.go index fa117b1c..82a44afe 100644 --- a/src/machine/machine_nrf52840_usb.go +++ b/src/machine/machine_nrf52840_usb.go @@ -58,7 +58,7 @@ func (usbcdc *USBCDC) Flush() error { usbcdc.TxIdx.Set(usbcdcTxBank1st) } - USB.sent = true + usbcdc.sent = true } } return nil @@ -72,10 +72,10 @@ func (usbcdc *USBCDC) WriteByte(c byte) error { for { mask := interrupt.Disable() - idx := USB.TxIdx.Get() + idx := usbcdc.TxIdx.Get() if (idx & usbcdcTxSizeMask) < usbcdcTxSizeMask { udd_ep_in_cache_buffer[usb_CDC_ENDPOINT_IN][idx] = c - USB.TxIdx.Set(idx + 1) + usbcdc.TxIdx.Set(idx + 1) ok = true } @@ -83,24 +83,24 @@ func (usbcdc *USBCDC) WriteByte(c byte) error { if ok { break - } else if usbcdcTxMaxRetriesAllowed < USB.waitTxcRetryCount { + } else if usbcdcTxMaxRetriesAllowed < usbcdc.waitTxcRetryCount { mask := interrupt.Disable() - USB.waitTxc = false - USB.waitTxcRetryCount = 0 - USB.TxIdx.Set(0) + usbcdc.waitTxc = false + usbcdc.waitTxcRetryCount = 0 + usbcdc.TxIdx.Set(0) usbLineInfo.lineState = 0 interrupt.Restore(mask) break } else { mask := interrupt.Disable() - if USB.sent { - if USB.waitTxc { + if usbcdc.sent { + if usbcdc.waitTxc { if !easyDMABusy.HasBits(1) { - USB.waitTxc = false - USB.Flush() + usbcdc.waitTxc = false + usbcdc.Flush() } } else { - USB.Flush() + usbcdc.Flush() } } interrupt.Restore(mask) @@ -111,16 +111,17 @@ func (usbcdc *USBCDC) WriteByte(c byte) error { return nil } -func (usbcdc USBCDC) DTR() bool { +func (usbcdc *USBCDC) DTR() bool { return (usbLineInfo.lineState & usb_CDC_LINESTATE_DTR) > 0 } -func (usbcdc USBCDC) RTS() bool { +func (usbcdc *USBCDC) RTS() bool { return (usbLineInfo.lineState & usb_CDC_LINESTATE_RTS) > 0 } var ( - USB = USBCDC{Buffer: NewRingBuffer()} + USB = &_USB + _USB = USBCDC{Buffer: NewRingBuffer()} usbEndpointDescriptors [8]usbDeviceDescriptor @@ -174,7 +175,7 @@ func (usbcdc *USBCDC) Configure(config UARTConfig) { // that it is possible to print to the console from a BLE interrupt. You // shouldn't generally do that but it is useful for debugging and panic // logging. - usbcdc.interrupt = interrupt.New(nrf.IRQ_USBD, USB.handleInterrupt) + usbcdc.interrupt = interrupt.New(nrf.IRQ_USBD, _USB.handleInterrupt) usbcdc.interrupt.SetPriority(0x40) // interrupt priority 2 (lower number means more important) usbcdc.interrupt.Enable() @@ -198,7 +199,7 @@ func (usbcdc *USBCDC) Configure(config UARTConfig) { func (usbcdc *USBCDC) handleInterrupt(interrupt.Interrupt) { if nrf.USBD.EVENTS_SOF.Get() == 1 { nrf.USBD.EVENTS_SOF.Set(0) - USB.Flush() + usbcdc.Flush() // if you want to blink LED showing traffic, this would be the place... } @@ -292,7 +293,7 @@ func (usbcdc *USBCDC) handleInterrupt(interrupt.Interrupt) { } case usb_CDC_ENDPOINT_IN: //, usb_CDC_ENDPOINT_ACM: if inDataDone { - USB.waitTxc = false + usbcdc.waitTxc = false exitCriticalSection() } } @@ -496,7 +497,7 @@ func sendUSBPacket(ep uint32, data []byte) { ) } -func (usbcdc USBCDC) handleEndpoint(ep uint32) { +func (usbcdc *USBCDC) handleEndpoint(ep uint32) { // get data count := int(nrf.USBD.EPOUT[ep].AMOUNT.Get()) diff --git a/src/machine/usb.go b/src/machine/usb.go index 0b547006..4bea1880 100644 --- a/src/machine/usb.go +++ b/src/machine/usb.go @@ -604,7 +604,7 @@ func newUSBSetup(data []byte) usbSetup { // // Read from the RX buffer. -func (usbcdc USBCDC) Read(data []byte) (n int, err error) { +func (usbcdc *USBCDC) Read(data []byte) (n int, err error) { // check if RX buffer is empty size := usbcdc.Buffered() if size == 0 { @@ -626,7 +626,7 @@ func (usbcdc USBCDC) Read(data []byte) (n int, err error) { } // Write data to the USBCDC. -func (usbcdc USBCDC) Write(data []byte) (n int, err error) { +func (usbcdc *USBCDC) Write(data []byte) (n int, err error) { for _, v := range data { usbcdc.WriteByte(v) } @@ -635,7 +635,7 @@ func (usbcdc USBCDC) Write(data []byte) (n int, err error) { // ReadByte reads a single byte from the RX buffer. // If there is no data in the buffer, returns an error. -func (usbcdc USBCDC) ReadByte() (byte, error) { +func (usbcdc *USBCDC) ReadByte() (byte, error) { // check if RX buffer is empty buf, ok := usbcdc.Buffer.Get() if !ok { @@ -645,13 +645,13 @@ func (usbcdc USBCDC) ReadByte() (byte, error) { } // Buffered returns the number of bytes currently stored in the RX buffer. -func (usbcdc USBCDC) Buffered() int { +func (usbcdc *USBCDC) Buffered() int { return int(usbcdc.Buffer.Used()) } // Receive handles adding data to the UART's data buffer. // Usually called by the IRQ handler for a machine. -func (usbcdc USBCDC) Receive(data byte) { +func (usbcdc *USBCDC) Receive(data byte) { usbcdc.Buffer.Put(data) }