Browse Source

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.
pull/1882/head
Ayke van Laethem 4 years ago
committed by Ron Evans
parent
commit
7c949ad386
  1. 2
      src/machine/board_circuitplay_bluefruit.go
  2. 2
      src/machine/board_clue_alpha.go
  3. 2
      src/machine/board_feather-nrf52840.go
  4. 2
      src/machine/board_itsybitsy-nrf52840.go
  5. 2
      src/machine/board_particle_argon.go
  6. 2
      src/machine/board_particle_boron.go
  7. 2
      src/machine/board_particle_xenon.go
  8. 32
      src/machine/machine_atsamd21.go
  9. 32
      src/machine/machine_atsamd51.go
  10. 39
      src/machine/machine_nrf52840_usb.go
  11. 10
      src/machine/usb.go

2
src/machine/board_circuitplay_bluefruit.go

@ -58,7 +58,7 @@ const (
// UART0 is the USB device // UART0 is the USB device
var ( var (
UART0 = &USB UART0 = USB
) )
// I2C pins // I2C pins

2
src/machine/board_clue_alpha.go

@ -105,7 +105,7 @@ const (
// UART0 is the USB device // UART0 is the USB device
var ( var (
UART0 = &USB UART0 = USB
) )
// I2C pins // I2C pins

2
src/machine/board_feather-nrf52840.go

@ -77,7 +77,7 @@ const (
// UART0 is the USB device // UART0 is the USB device
var ( var (
UART0 = &USB UART0 = USB
) )
// I2C pins // I2C pins

2
src/machine/board_itsybitsy-nrf52840.go

@ -72,7 +72,7 @@ const (
// UART0 is the USB device // UART0 is the USB device
var ( var (
UART0 = &USB UART0 = USB
) )
// I2C pins // I2C pins

2
src/machine/board_particle_argon.go

@ -41,7 +41,7 @@ const (
// UART // UART
var ( var (
Serial = &USB Serial = USB
UART0 = NRF_UART0 UART0 = NRF_UART0
) )

2
src/machine/board_particle_boron.go

@ -41,7 +41,7 @@ const (
// UART // UART
var ( var (
Serial = &USB Serial = USB
UART0 = NRF_UART0 UART0 = NRF_UART0
) )

2
src/machine/board_particle_xenon.go

@ -41,7 +41,7 @@ const (
// UART // UART
var ( var (
Serial = &USB Serial = USB
UART0 = NRF_UART0 UART0 = NRF_UART0
) )

32
src/machine/machine_atsamd21.go

@ -506,7 +506,7 @@ type UART struct {
var ( var (
// UART0 is actually a USB CDC interface. // UART0 is actually a USB CDC interface.
UART0 = USBCDC{Buffer: NewRingBuffer()} UART0 = &USBCDC{Buffer: NewRingBuffer()}
) )
const ( const (
@ -1788,7 +1788,7 @@ func (usbcdc *USBCDC) Flush() error {
// send data by setting bank ready // send data by setting bank ready
setEPSTATUSSET(usb_CDC_ENDPOINT_IN, sam.USB_DEVICE_EPSTATUSSET_BK1RDY) setEPSTATUSSET(usb_CDC_ENDPOINT_IN, sam.USB_DEVICE_EPSTATUSSET_BK1RDY)
UART0.sent = true usbcdc.sent = true
} }
} }
return nil return nil
@ -1802,10 +1802,10 @@ func (usbcdc *USBCDC) WriteByte(c byte) error {
for { for {
mask := interrupt.Disable() mask := interrupt.Disable()
idx := UART0.TxIdx.Get() idx := usbcdc.TxIdx.Get()
if (idx & usbcdcTxSizeMask) < usbcdcTxSizeMask { if (idx & usbcdcTxSizeMask) < usbcdcTxSizeMask {
udd_ep_in_cache_buffer[usb_CDC_ENDPOINT_IN][idx] = c udd_ep_in_cache_buffer[usb_CDC_ENDPOINT_IN][idx] = c
UART0.TxIdx.Set(idx + 1) usbcdc.TxIdx.Set(idx + 1)
ok = true ok = true
} }
@ -1813,26 +1813,26 @@ func (usbcdc *USBCDC) WriteByte(c byte) error {
if ok { if ok {
break break
} else if usbcdcTxMaxRetriesAllowed < UART0.waitTxcRetryCount { } else if usbcdcTxMaxRetriesAllowed < usbcdc.waitTxcRetryCount {
mask := interrupt.Disable() mask := interrupt.Disable()
UART0.waitTxc = false usbcdc.waitTxc = false
UART0.waitTxcRetryCount = 0 usbcdc.waitTxcRetryCount = 0
UART0.TxIdx.Set(0) usbcdc.TxIdx.Set(0)
usbLineInfo.lineState = 0 usbLineInfo.lineState = 0
interrupt.Restore(mask) interrupt.Restore(mask)
break break
} else { } else {
mask := interrupt.Disable() mask := interrupt.Disable()
if UART0.sent { if usbcdc.sent {
if UART0.waitTxc { if usbcdc.waitTxc {
if (getEPINTFLAG(usb_CDC_ENDPOINT_IN) & sam.USB_DEVICE_EPINTFLAG_TRCPT1) != 0 { if (getEPINTFLAG(usb_CDC_ENDPOINT_IN) & sam.USB_DEVICE_EPINTFLAG_TRCPT1) != 0 {
setEPSTATUSCLR(usb_CDC_ENDPOINT_IN, sam.USB_DEVICE_EPSTATUSCLR_BK1RDY) setEPSTATUSCLR(usb_CDC_ENDPOINT_IN, sam.USB_DEVICE_EPSTATUSCLR_BK1RDY)
setEPINTFLAG(usb_CDC_ENDPOINT_IN, sam.USB_DEVICE_EPINTFLAG_TRCPT1) setEPINTFLAG(usb_CDC_ENDPOINT_IN, sam.USB_DEVICE_EPINTFLAG_TRCPT1)
UART0.waitTxc = false usbcdc.waitTxc = false
UART0.Flush() usbcdc.Flush()
} }
} else { } else {
UART0.Flush() usbcdc.Flush()
} }
} }
interrupt.Restore(mask) interrupt.Restore(mask)
@ -1843,11 +1843,11 @@ func (usbcdc *USBCDC) WriteByte(c byte) error {
return nil return nil
} }
func (usbcdc USBCDC) DTR() bool { func (usbcdc *USBCDC) DTR() bool {
return (usbLineInfo.lineState & usb_CDC_LINESTATE_DTR) > 0 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 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. // 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 // reset USB interface
sam.USB_DEVICE.CTRLA.SetBits(sam.USB_DEVICE_CTRLA_SWRST) sam.USB_DEVICE.CTRLA.SetBits(sam.USB_DEVICE_CTRLA_SWRST)
for sam.USB_DEVICE.SYNCBUSY.HasBits(sam.USB_DEVICE_SYNCBUSY_SWRST) || for sam.USB_DEVICE.SYNCBUSY.HasBits(sam.USB_DEVICE_SYNCBUSY_SWRST) ||

32
src/machine/machine_atsamd51.go

@ -952,7 +952,7 @@ type UART struct {
var ( var (
// UART0 is actually a USB CDC interface. // UART0 is actually a USB CDC interface.
UART0 = USBCDC{Buffer: NewRingBuffer()} UART0 = &USBCDC{Buffer: NewRingBuffer()}
) )
const ( const (
@ -1994,7 +1994,7 @@ func (usbcdc *USBCDC) Flush() error {
// send data by setting bank ready // send data by setting bank ready
setEPSTATUSSET(usb_CDC_ENDPOINT_IN, sam.USB_DEVICE_ENDPOINT_EPSTATUSSET_BK1RDY) setEPSTATUSSET(usb_CDC_ENDPOINT_IN, sam.USB_DEVICE_ENDPOINT_EPSTATUSSET_BK1RDY)
UART0.sent = true usbcdc.sent = true
} }
} }
return nil return nil
@ -2008,10 +2008,10 @@ func (usbcdc *USBCDC) WriteByte(c byte) error {
for { for {
mask := interrupt.Disable() mask := interrupt.Disable()
idx := UART0.TxIdx.Get() idx := usbcdc.TxIdx.Get()
if (idx & usbcdcTxSizeMask) < usbcdcTxSizeMask { if (idx & usbcdcTxSizeMask) < usbcdcTxSizeMask {
udd_ep_in_cache_buffer[usb_CDC_ENDPOINT_IN][idx] = c udd_ep_in_cache_buffer[usb_CDC_ENDPOINT_IN][idx] = c
UART0.TxIdx.Set(idx + 1) usbcdc.TxIdx.Set(idx + 1)
ok = true ok = true
} }
@ -2019,26 +2019,26 @@ func (usbcdc *USBCDC) WriteByte(c byte) error {
if ok { if ok {
break break
} else if usbcdcTxMaxRetriesAllowed < UART0.waitTxcRetryCount { } else if usbcdcTxMaxRetriesAllowed < usbcdc.waitTxcRetryCount {
mask := interrupt.Disable() mask := interrupt.Disable()
UART0.waitTxc = false usbcdc.waitTxc = false
UART0.waitTxcRetryCount = 0 usbcdc.waitTxcRetryCount = 0
UART0.TxIdx.Set(0) usbcdc.TxIdx.Set(0)
usbLineInfo.lineState = 0 usbLineInfo.lineState = 0
interrupt.Restore(mask) interrupt.Restore(mask)
break break
} else { } else {
mask := interrupt.Disable() mask := interrupt.Disable()
if UART0.sent { if usbcdc.sent {
if UART0.waitTxc { if usbcdc.waitTxc {
if (getEPINTFLAG(usb_CDC_ENDPOINT_IN) & sam.USB_DEVICE_ENDPOINT_EPINTFLAG_TRCPT1) != 0 { if (getEPINTFLAG(usb_CDC_ENDPOINT_IN) & sam.USB_DEVICE_ENDPOINT_EPINTFLAG_TRCPT1) != 0 {
setEPSTATUSCLR(usb_CDC_ENDPOINT_IN, sam.USB_DEVICE_ENDPOINT_EPSTATUSCLR_BK1RDY) setEPSTATUSCLR(usb_CDC_ENDPOINT_IN, sam.USB_DEVICE_ENDPOINT_EPSTATUSCLR_BK1RDY)
setEPINTFLAG(usb_CDC_ENDPOINT_IN, sam.USB_DEVICE_ENDPOINT_EPINTFLAG_TRCPT1) setEPINTFLAG(usb_CDC_ENDPOINT_IN, sam.USB_DEVICE_ENDPOINT_EPINTFLAG_TRCPT1)
UART0.waitTxc = false usbcdc.waitTxc = false
UART0.Flush() usbcdc.Flush()
} }
} else { } else {
UART0.Flush() usbcdc.Flush()
} }
} }
interrupt.Restore(mask) interrupt.Restore(mask)
@ -2049,11 +2049,11 @@ func (usbcdc *USBCDC) WriteByte(c byte) error {
return nil return nil
} }
func (usbcdc USBCDC) DTR() bool { func (usbcdc *USBCDC) DTR() bool {
return (usbLineInfo.lineState & usb_CDC_LINESTATE_DTR) > 0 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 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. // 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 // reset USB interface
sam.USB_DEVICE.CTRLA.SetBits(sam.USB_DEVICE_CTRLA_SWRST) sam.USB_DEVICE.CTRLA.SetBits(sam.USB_DEVICE_CTRLA_SWRST)
for sam.USB_DEVICE.SYNCBUSY.HasBits(sam.USB_DEVICE_SYNCBUSY_SWRST) || for sam.USB_DEVICE.SYNCBUSY.HasBits(sam.USB_DEVICE_SYNCBUSY_SWRST) ||

39
src/machine/machine_nrf52840_usb.go

@ -58,7 +58,7 @@ func (usbcdc *USBCDC) Flush() error {
usbcdc.TxIdx.Set(usbcdcTxBank1st) usbcdc.TxIdx.Set(usbcdcTxBank1st)
} }
USB.sent = true usbcdc.sent = true
} }
} }
return nil return nil
@ -72,10 +72,10 @@ func (usbcdc *USBCDC) WriteByte(c byte) error {
for { for {
mask := interrupt.Disable() mask := interrupt.Disable()
idx := USB.TxIdx.Get() idx := usbcdc.TxIdx.Get()
if (idx & usbcdcTxSizeMask) < usbcdcTxSizeMask { if (idx & usbcdcTxSizeMask) < usbcdcTxSizeMask {
udd_ep_in_cache_buffer[usb_CDC_ENDPOINT_IN][idx] = c udd_ep_in_cache_buffer[usb_CDC_ENDPOINT_IN][idx] = c
USB.TxIdx.Set(idx + 1) usbcdc.TxIdx.Set(idx + 1)
ok = true ok = true
} }
@ -83,24 +83,24 @@ func (usbcdc *USBCDC) WriteByte(c byte) error {
if ok { if ok {
break break
} else if usbcdcTxMaxRetriesAllowed < USB.waitTxcRetryCount { } else if usbcdcTxMaxRetriesAllowed < usbcdc.waitTxcRetryCount {
mask := interrupt.Disable() mask := interrupt.Disable()
USB.waitTxc = false usbcdc.waitTxc = false
USB.waitTxcRetryCount = 0 usbcdc.waitTxcRetryCount = 0
USB.TxIdx.Set(0) usbcdc.TxIdx.Set(0)
usbLineInfo.lineState = 0 usbLineInfo.lineState = 0
interrupt.Restore(mask) interrupt.Restore(mask)
break break
} else { } else {
mask := interrupt.Disable() mask := interrupt.Disable()
if USB.sent { if usbcdc.sent {
if USB.waitTxc { if usbcdc.waitTxc {
if !easyDMABusy.HasBits(1) { if !easyDMABusy.HasBits(1) {
USB.waitTxc = false usbcdc.waitTxc = false
USB.Flush() usbcdc.Flush()
} }
} else { } else {
USB.Flush() usbcdc.Flush()
} }
} }
interrupt.Restore(mask) interrupt.Restore(mask)
@ -111,16 +111,17 @@ func (usbcdc *USBCDC) WriteByte(c byte) error {
return nil return nil
} }
func (usbcdc USBCDC) DTR() bool { func (usbcdc *USBCDC) DTR() bool {
return (usbLineInfo.lineState & usb_CDC_LINESTATE_DTR) > 0 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 return (usbLineInfo.lineState & usb_CDC_LINESTATE_RTS) > 0
} }
var ( var (
USB = USBCDC{Buffer: NewRingBuffer()} USB = &_USB
_USB = USBCDC{Buffer: NewRingBuffer()}
usbEndpointDescriptors [8]usbDeviceDescriptor 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 // 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 // shouldn't generally do that but it is useful for debugging and panic
// logging. // 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.SetPriority(0x40) // interrupt priority 2 (lower number means more important)
usbcdc.interrupt.Enable() usbcdc.interrupt.Enable()
@ -198,7 +199,7 @@ func (usbcdc *USBCDC) Configure(config UARTConfig) {
func (usbcdc *USBCDC) handleInterrupt(interrupt.Interrupt) { func (usbcdc *USBCDC) handleInterrupt(interrupt.Interrupt) {
if nrf.USBD.EVENTS_SOF.Get() == 1 { if nrf.USBD.EVENTS_SOF.Get() == 1 {
nrf.USBD.EVENTS_SOF.Set(0) nrf.USBD.EVENTS_SOF.Set(0)
USB.Flush() usbcdc.Flush()
// if you want to blink LED showing traffic, this would be the place... // 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: case usb_CDC_ENDPOINT_IN: //, usb_CDC_ENDPOINT_ACM:
if inDataDone { if inDataDone {
USB.waitTxc = false usbcdc.waitTxc = false
exitCriticalSection() 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 // get data
count := int(nrf.USBD.EPOUT[ep].AMOUNT.Get()) count := int(nrf.USBD.EPOUT[ep].AMOUNT.Get())

10
src/machine/usb.go

@ -604,7 +604,7 @@ func newUSBSetup(data []byte) usbSetup {
// //
// Read from the RX buffer. // 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 // check if RX buffer is empty
size := usbcdc.Buffered() size := usbcdc.Buffered()
if size == 0 { if size == 0 {
@ -626,7 +626,7 @@ func (usbcdc USBCDC) Read(data []byte) (n int, err error) {
} }
// Write data to the USBCDC. // 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 { for _, v := range data {
usbcdc.WriteByte(v) 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. // ReadByte reads a single byte from the RX buffer.
// If there is no data in the buffer, returns an error. // 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 // check if RX buffer is empty
buf, ok := usbcdc.Buffer.Get() buf, ok := usbcdc.Buffer.Get()
if !ok { if !ok {
@ -645,13 +645,13 @@ func (usbcdc USBCDC) ReadByte() (byte, error) {
} }
// Buffered returns the number of bytes currently stored in the RX buffer. // 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()) return int(usbcdc.Buffer.Used())
} }
// Receive handles adding data to the UART's data buffer. // Receive handles adding data to the UART's data buffer.
// Usually called by the IRQ handler for a machine. // 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) usbcdc.Buffer.Put(data)
} }

Loading…
Cancel
Save