machine: make UART objects pointer receivers

This means that machine.UART0, machine.UART1, etc are of type
*machine.UART, not machine.UART. This makes them easier to pass around
and avoids surprises when they are passed around by value while they
should be passed around by reference.

There is a small code size impact in some cases, but it is relatively
minor.
This commit is contained in:
Ayke van Laethem
2021-05-13 12:32:12 +02:00
committed by Ron Evans
parent 7c949ad386
commit aa5b8d0df7
46 changed files with 246 additions and 179 deletions
+7 -7
View File
@@ -7,7 +7,7 @@ package machine
var (
SPI0 = SPI{0}
I2C0 = &I2C{0}
UART0 = UART{0}
UART0 = &UART{0}
)
const (
@@ -124,34 +124,34 @@ type UARTConfig struct {
}
// Configure the UART.
func (uart UART) Configure(config UARTConfig) {
func (uart *UART) Configure(config UARTConfig) {
uartConfigure(uart.Bus, config.TX, config.RX)
}
// Read from the UART.
func (uart UART) Read(data []byte) (n int, err error) {
func (uart *UART) Read(data []byte) (n int, err error) {
return uartRead(uart.Bus, &data[0], len(data)), nil
}
// Write to the UART.
func (uart UART) Write(data []byte) (n int, err error) {
func (uart *UART) Write(data []byte) (n int, err error) {
return uartWrite(uart.Bus, &data[0], len(data)), nil
}
// Buffered returns the number of bytes currently stored in the RX buffer.
func (uart UART) Buffered() int {
func (uart *UART) Buffered() int {
return 0
}
// ReadByte reads a single byte from the UART.
func (uart UART) ReadByte() (byte, error) {
func (uart *UART) ReadByte() (byte, error) {
var b byte
uartRead(uart.Bus, &b, 1)
return b, nil
}
// WriteByte writes a single byte to the UART.
func (uart UART) WriteByte(b byte) error {
func (uart *UART) WriteByte(b byte) error {
uartWrite(uart.Bus, &b, 1)
return nil
}