Skip to content

Commit 86ea216

Browse files
aykevldeadprogram
authored andcommitted
machine: move SPI Tx function into separate file
This makes the code a bit cleaner because ErrTxInvalidSliceSize isn't redefined in every file that uses SPI and Mode0/Mode1/Mode2/Mode3 is defined for every target that uses SPI.
1 parent 5a92c35 commit 86ea216

File tree

6 files changed

+67
-76
lines changed

6 files changed

+67
-76
lines changed

src/machine/machine_atsamd21.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ package machine
1010
import (
1111
"device/arm"
1212
"device/sam"
13-
"errors"
1413
"runtime/interrupt"
1514
"unsafe"
1615
)
@@ -1274,10 +1273,6 @@ func (spi SPI) Transfer(w byte) (byte, error) {
12741273
return byte(spi.Bus.DATA.Get()), nil
12751274
}
12761275

1277-
var (
1278-
ErrTxInvalidSliceSize = errors.New("SPI write and read slices must be same size")
1279-
)
1280-
12811276
// Tx handles read/write operation for SPI interface. Since SPI is a syncronous write/read
12821277
// interface, there must always be the same number of bytes written as bytes read.
12831278
// The Tx method knows about this, and offers a few different ways of calling it.

src/machine/machine_atsamd51.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ package machine
1010
import (
1111
"device/arm"
1212
"device/sam"
13-
"errors"
1413
"runtime/interrupt"
1514
"unsafe"
1615
)
@@ -1527,10 +1526,6 @@ func (spi SPI) Transfer(w byte) (byte, error) {
15271526
return byte(spi.Bus.DATA.Get()), nil
15281527
}
15291528

1530-
var (
1531-
ErrTxInvalidSliceSize = errors.New("SPI write and read slices must be same size")
1532-
)
1533-
15341529
// Tx handles read/write operation for SPI interface. Since SPI is a syncronous write/read
15351530
// interface, there must always be the same number of bytes written as bytes read.
15361531
// The Tx method knows about this, and offers a few different ways of calling it.

src/machine/machine_nrf.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,10 @@ package machine
55

66
import (
77
"device/nrf"
8-
"errors"
98
"runtime/interrupt"
109
"unsafe"
1110
)
1211

13-
var (
14-
ErrTxInvalidSliceSize = errors.New("SPI write and read slices must be same size")
15-
)
16-
1712
const deviceName = nrf.Device
1813

1914
const (

src/machine/machine_rp2040_spi.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,9 @@ type SPIConfig struct {
3838
}
3939

4040
var (
41-
ErrLSBNotSupported = errors.New("SPI LSB unsupported on PL022")
42-
ErrTxInvalidSliceSize = errors.New("SPI write and read slices must be same size")
43-
ErrSPITimeout = errors.New("SPI timeout")
44-
ErrSPIBaud = errors.New("SPI baud too low or above 66.5Mhz")
41+
ErrLSBNotSupported = errors.New("SPI LSB unsupported on PL022")
42+
ErrSPITimeout = errors.New("SPI timeout")
43+
ErrSPIBaud = errors.New("SPI baud too low or above 66.5Mhz")
4544
)
4645

4746
type SPI struct {

src/machine/spi.go

Lines changed: 2 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//go:build !baremetal || (stm32 && !stm32f7x2 && !stm32l5x2) || fe310 || k210 || (nxp && !mk66f18) || atmega
2-
// +build !baremetal stm32,!stm32f7x2,!stm32l5x2 fe310 k210 nxp,!mk66f18 atmega
1+
//go:build !baremetal || atmega || esp32 || fe310 || k210 || nrf || (nxp && !mk66f18) || rp2040 || sam || (stm32 && !stm32f7x2 && !stm32l5x2)
2+
// +build !baremetal atmega esp32 fe310 k210 nrf nxp,!mk66f18 rp2040 sam stm32,!stm32f7x2,!stm32l5x2
33

44
package machine
55

@@ -17,58 +17,3 @@ var (
1717
ErrTxInvalidSliceSize = errors.New("SPI write and read slices must be same size")
1818
errSPIInvalidMachineConfig = errors.New("SPI port was not configured properly by the machine")
1919
)
20-
21-
// Tx handles read/write operation for SPI interface. Since SPI is a syncronous write/read
22-
// interface, there must always be the same number of bytes written as bytes read.
23-
// The Tx method knows about this, and offers a few different ways of calling it.
24-
//
25-
// This form sends the bytes in tx buffer, putting the resulting bytes read into the rx buffer.
26-
// Note that the tx and rx buffers must be the same size:
27-
//
28-
// spi.Tx(tx, rx)
29-
//
30-
// This form sends the tx buffer, ignoring the result. Useful for sending "commands" that return zeros
31-
// until all the bytes in the command packet have been received:
32-
//
33-
// spi.Tx(tx, nil)
34-
//
35-
// This form sends zeros, putting the result into the rx buffer. Good for reading a "result packet":
36-
//
37-
// spi.Tx(nil, rx)
38-
func (spi SPI) Tx(w, r []byte) error {
39-
var err error
40-
41-
switch {
42-
case w == nil:
43-
// read only, so write zero and read a result.
44-
for i := range r {
45-
r[i], err = spi.Transfer(0)
46-
if err != nil {
47-
return err
48-
}
49-
}
50-
case r == nil:
51-
// write only
52-
for _, b := range w {
53-
_, err = spi.Transfer(b)
54-
if err != nil {
55-
return err
56-
}
57-
}
58-
59-
default:
60-
// write/read
61-
if len(w) != len(r) {
62-
return ErrTxInvalidSliceSize
63-
}
64-
65-
for i, b := range w {
66-
r[i], err = spi.Transfer(b)
67-
if err != nil {
68-
return err
69-
}
70-
}
71-
}
72-
73-
return nil
74-
}

src/machine/spi_tx.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//go:build !baremetal || atmega || fe310 || k210 || (nxp && !mk66f18) || (stm32 && !stm32f7x2 && !stm32l5x2)
2+
// +build !baremetal atmega fe310 k210 nxp,!mk66f18 stm32,!stm32f7x2,!stm32l5x2
3+
4+
// This file implements the SPI Tx function for targets that don't have a custom
5+
// (faster) implementation for it.
6+
7+
package machine
8+
9+
// Tx handles read/write operation for SPI interface. Since SPI is a syncronous write/read
10+
// interface, there must always be the same number of bytes written as bytes read.
11+
// The Tx method knows about this, and offers a few different ways of calling it.
12+
//
13+
// This form sends the bytes in tx buffer, putting the resulting bytes read into the rx buffer.
14+
// Note that the tx and rx buffers must be the same size:
15+
//
16+
// spi.Tx(tx, rx)
17+
//
18+
// This form sends the tx buffer, ignoring the result. Useful for sending "commands" that return zeros
19+
// until all the bytes in the command packet have been received:
20+
//
21+
// spi.Tx(tx, nil)
22+
//
23+
// This form sends zeros, putting the result into the rx buffer. Good for reading a "result packet":
24+
//
25+
// spi.Tx(nil, rx)
26+
func (spi SPI) Tx(w, r []byte) error {
27+
var err error
28+
29+
switch {
30+
case w == nil:
31+
// read only, so write zero and read a result.
32+
for i := range r {
33+
r[i], err = spi.Transfer(0)
34+
if err != nil {
35+
return err
36+
}
37+
}
38+
case r == nil:
39+
// write only
40+
for _, b := range w {
41+
_, err = spi.Transfer(b)
42+
if err != nil {
43+
return err
44+
}
45+
}
46+
47+
default:
48+
// write/read
49+
if len(w) != len(r) {
50+
return ErrTxInvalidSliceSize
51+
}
52+
53+
for i, b := range w {
54+
r[i], err = spi.Transfer(b)
55+
if err != nil {
56+
return err
57+
}
58+
}
59+
}
60+
61+
return nil
62+
}

0 commit comments

Comments
 (0)