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
+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)
}