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.
This commit is contained in:
Ayke van Laethem
2021-05-11 13:39:14 +02:00
committed by Ron Evans
parent 1646326164
commit 7c949ad386
11 changed files with 64 additions and 63 deletions
+1 -1
View File
@@ -58,7 +58,7 @@ const (
// UART0 is the USB device
var (
UART0 = &USB
UART0 = USB
)
// I2C pins
+1 -1
View File
@@ -105,7 +105,7 @@ const (
// UART0 is the USB device
var (
UART0 = &USB
UART0 = USB
)
// I2C pins
+1 -1
View File
@@ -77,7 +77,7 @@ const (
// UART0 is the USB device
var (
UART0 = &USB
UART0 = USB
)
// I2C pins
+1 -1
View File
@@ -72,7 +72,7 @@ const (
// UART0 is the USB device
var (
UART0 = &USB
UART0 = USB
)
// I2C pins
+1 -1
View File
@@ -41,7 +41,7 @@ const (
// UART
var (
Serial = &USB
Serial = USB
UART0 = NRF_UART0
)
+1 -1
View File
@@ -41,7 +41,7 @@ const (
// UART
var (
Serial = &USB
Serial = USB
UART0 = NRF_UART0
)
+1 -1
View File
@@ -41,7 +41,7 @@ const (
// UART
var (
Serial = &USB
Serial = USB
UART0 = NRF_UART0
)
+16 -16
View File
@@ -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) ||
+16 -16
View File
@@ -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) ||
+20 -19
View File
@@ -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())
+5 -5
View File
@@ -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)
}