1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
.\" $NetBSD: spi.9,v 1.3 2025/10/10 18:36:17 brad Exp $
.\"
.\" Copyright (c) 2019 The NetBSD Foundation
.\" All rights reserved.
.\"
.\" Written by Michael van Elst
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd February 23, 2019
.Dt SPI 9
.Os
.Sh NAME
.Nm spi_configure ,
.Nm spi_transfer ,
.Nm spi_transfer_init ,
.Nm spi_chunk_init ,
.Nm spi_transfer_add ,
.Nm spi_wait ,
.Nm spi_done ,
.Nm spi_send ,
.Nm spi_sendv ,
.Nm spi_recv ,
.Nm spi_send_recv
.Nd Serial Peripheral Interface (SPI) kernel interface
.Sh SYNOPSIS
.In dev/spi/spivar.h
.Ft int
.Fo spi_configure
.Fa "struct spi_handle *sh"
.Fa "int mode"
.Fa "int speed"
.Fc
.Ft int
.Fo spi_transfer
.Fa "struct spi_handle *sh"
.Fa "struct spi_transfer *st"
.Fc
.Ft void
.Fo spi_transfer_init
.Fa "struct spi_transfer *st"
.Fc
.Ft void
.Fo spi_chunk_init
.Fa "struct spi_chunk *chunk"
.Fa "int cnt"
.Fa "const uint8_t *wptr"
.Fa "uint8_t *rptr"
.Fc
.Ft void
.Fo spi_transfer_add
.Fa "struct spi_transfer *st"
.Fa "struct spi_chunk *chunk"
.Fc
.Ft void
.Fo spi_wait
.Fa "struct spi_transfer *st"
.Fc
.Ft void
.Fo spi_done
.Fa "struct spi_transfer *st"
.Fa "int err"
.Fc
.Ft int
.Fo spi_recv
.Fa "struct spi_handle *sh"
.Fa "int cnt"
.Fa "uint8_t *data"
.Fc
.Ft int
.Fo spi_send
.Fa "struct spi_handle *sh"
.Fa "int cnt"
.Fa "const uint8_t *data"
.Fc
.Ft int
.Fo spi_sendv
.Fa "struct spi_handle *sh"
.Fa "struct iovec *iov"
.Fa "int iovcnt"
.Fc
.Ft int
.Fo spi_send_recv
.Fa "struct spi_handle *sh"
.Fa "int scnt"
.Fa "const uint8_t *snd"
.Fa "int rcnt"
.Fa "uint8_t *rcv"
.Fc
.Sh DESCRIPTION
SPI is a 4-wire synchronous full-duplex serial bus.
It is commonly used for connecting devices such as EEPROMs,
displays, and other types of integrated circuits.
The
.Nm spi
interface provides a means of communicating with SPI-connected devices.
.Sh FUNCTIONS
The following functions comprise the API provided to drivers of
SPI-connected devices.
.Pp
The
.Fa struct spi_handle
corresponding to the device is passed in the driver attachment.
.Bl -tag -width spi_transfer_init
.It Fn spi_configure "sh" "mode" "speed"
Sets mode and speed for subsequent communication with a SPI slave.
.It Fn spi_transfer "sh" "st"
Queue transfer to SPI controller.
.Fn spi_transfer
returns an errno value when the transfer couldn't be queued.
.It Fn spi_transfer_init "st"
Initialize a transfer structure.
.It Fn spi_chunk_init "chunk" "cnt" "wptr" rptr"
Initialize a chunk structure, each chunk corresponds to
a possibly bi-directional transfer where the same amount
of bytes is shifted in and out.
.It Fn spi_transfer_add "st" "chunk"
Append a chunk to transfer structure.
.It Fn spi_wait "st"
Wait for a transfer to complete.
When the transfer has failed for some reason, the field
.Va st->st_errno
is set to a non-zero value.
.It Fn spi_done "st" "err"
Called back machine-dependent backend to signal completion
of a transfer.
.El
.Pp
For simplicity there are convenience functions that combine
common operations.
These functions return an errno value when the transfer failed.
.Bl -tag -width spi_transfer_init
.It Fn spi_recv "sh" "cnt" "data"
Prepares a chunk for receiving data, queues a transfer and
waits for it to complete.
.It Fn spi_send "sh" "cnt" "data"
Prepares a chunk for sending data, queues a transfer and
waits for it to complete.
.It Fn spi_sendv "sh" "iov" "iovcnt"
Prepare and queue a scatter of chunks and wait for them to complete.
.It Fn spi_send_recv "sh" "scnt" "snd" "rcnt" "rcv"
Prepares two chunks for sending data first and then receiving
an answer, queues a transfer and waits for it to complete.
This is not a full-duplex operation.
.El
.Sh SEE ALSO
.Xr spi 4
.Sh HISTORY
The
.Nm spi
API first appeared in
.Nx 4.0 .
|