|
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 |
3 | 3 |
|
4 | 4 | package machine
|
5 | 5 |
|
|
17 | 17 | ErrTxInvalidSliceSize = errors.New("SPI write and read slices must be same size")
|
18 | 18 | errSPIInvalidMachineConfig = errors.New("SPI port was not configured properly by the machine")
|
19 | 19 | )
|
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 |
| -} |
0 commit comments