You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/docs/reference/machine.md
+57
Original file line number
Diff line number
Diff line change
@@ -84,6 +84,63 @@ SetInterrupt sets an interrupt to be executed when the pin changes state. The pi
84
84
This call will replace a previously set callback on this pin. You can pass `nil` to disable the interrupt. If you do so, the change parameter is ignored and can be set to any value (such as 0).
85
85
86
86
87
+
## SPI
88
+
89
+
```go
90
+
type SPIConfig struct {
91
+
Frequency uint32
92
+
SCK Pin
93
+
SDO Pin
94
+
SDI Pin
95
+
LSBFirst bool
96
+
Mode uint8
97
+
}
98
+
```
99
+
100
+
The `SPIConfig` struct contains the configuration for the SPI peripheral.
101
+
102
+
*`Frequency` is the maximum frequency that will be used: for example `1 * MHz` for 1MHz. Depending on chip capabilities, this or a lower frequency will be selected. When not set (or set to 0), the default of 4MHz will be used.
103
+
*`SCK`, `SDO` and `SDI` are the clock, data out, and data in pins respectively, however support for setting pins other than the default pins may not be supported by a given SPI peripheral. Some chips are flexible and allow the use of any pin, while other boards only allow a limited range of pins or use fixed SCK/SDO/SDI pins. When these pins are left at the zero value, the default for the particular board is used.
104
+
*`LSBFirst` configures the SPI peripheral to clock out the least significant bit (LSB) first. The default and most commonly used configuration is the most significant bit first (`LSBFirst=false`).
105
+
*`Mode` is the [SPI mode (CPOL/CPHA) to be used](https://en.wikipedia.org/wiki/Serial_Peripheral_Interface#Clock_polarity_and_phase). Mode 0 is appropriate for most peripheral chips, but other modes may be needed in some cases. Check your peripheral documentation for details.
106
+
| Mode | CPOL | CPHA |
107
+
| ------- | ---- | ---- |
108
+
|`Mode0`| 0 | 0 |
109
+
|`Mode1`| 0 | 1 |
110
+
|`Mode2`| 1 | 0 |
111
+
|`Mode3`| 1 | 1 |
112
+
113
+
```go
114
+
typeSPIstruct {
115
+
// values are unexported or vary by chip
116
+
}
117
+
118
+
var (
119
+
SPI0 = SPI{...}
120
+
SPI1 = SPI{...}
121
+
)
122
+
```
123
+
124
+
The `SPI` object refers to a single (hardware) SPI instance. Depending on chip capabilities, various objects such as `SPI0` and perhaps others are defined.
125
+
126
+
```go
127
+
func(spiSPI) Configure(configSPIConfig) error
128
+
```
129
+
130
+
The `Configure` call enables and configures the hardware SPI for use, setting the configuration as described in `SPIConfig`. It will return an error when an incorrect configuration is provided (for example, using pins not usable with this SPI instance). See `SPIConfig` for details.
131
+
132
+
```go
133
+
func (spi SPI) Tx(w, r []byte) error
134
+
```
135
+
136
+
The `Tx` performs the actual SPI transaction, and return an error if there was an error during the transaction. Because SPI is a synchronous protocol, reading and writing happens at the same time. Therefore, `w` and `r` usually have to be the same length. There are some exceptions:
137
+
138
+
* When `r` is nil, the SPI peripheral will only transmit the bytes in `w` and ignore what it receives.
139
+
* When `w` is nil, the SPI peripheral will only read the given number of bytes and store it in `r`. It will send out a zero byte for each byte that it reads.
140
+
141
+
Some chips may also support mismatched lengths of `w` and `r`, in which case they will behave like above for the remaining bytes in the byte slice that's the longest of the two.
0 commit comments