1*e38ac3b5Swiz.\" $NetBSD: spi.9,v 1.2 2019/02/23 17:37:10 wiz Exp $ 2975ce0baSmlelstv.\" 3975ce0baSmlelstv.\" Copyright (c) 2019 The NetBSD Foundation 4975ce0baSmlelstv.\" All rights reserved. 5975ce0baSmlelstv.\" 6975ce0baSmlelstv.\" Written by Michael van Elst 7975ce0baSmlelstv.\" 8975ce0baSmlelstv.\" Redistribution and use in source and binary forms, with or without 9975ce0baSmlelstv.\" modification, are permitted provided that the following conditions 10975ce0baSmlelstv.\" are met: 11975ce0baSmlelstv.\" 1. Redistributions of source code must retain the above copyright 12975ce0baSmlelstv.\" notice, this list of conditions and the following disclaimer. 13975ce0baSmlelstv.\" 2. Redistributions in binary form must reproduce the above copyright 14975ce0baSmlelstv.\" notice, this list of conditions and the following disclaimer in the 15975ce0baSmlelstv.\" documentation and/or other materials provided with the distribution. 16975ce0baSmlelstv.\" 17975ce0baSmlelstv.\" THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 18975ce0baSmlelstv.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 19975ce0baSmlelstv.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20975ce0baSmlelstv.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 21975ce0baSmlelstv.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22975ce0baSmlelstv.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23975ce0baSmlelstv.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24975ce0baSmlelstv.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25975ce0baSmlelstv.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26975ce0baSmlelstv.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27975ce0baSmlelstv.\" POSSIBILITY OF SUCH DAMAGE. 28975ce0baSmlelstv.\" 29975ce0baSmlelstv.Dd February 23, 2019 30975ce0baSmlelstv.Dt SPI 9 31975ce0baSmlelstv.Os 32975ce0baSmlelstv.Sh NAME 33975ce0baSmlelstv.Nm spi_configure , 34975ce0baSmlelstv.Nm spi_transfer , 35975ce0baSmlelstv.Nm spi_transfer_init , 36975ce0baSmlelstv.Nm spi_chunk_init , 37975ce0baSmlelstv.Nm spi_transfer_add , 38975ce0baSmlelstv.Nm spi_wait , 39975ce0baSmlelstv.Nm spi_done , 40975ce0baSmlelstv.Nm spi_send , 41975ce0baSmlelstv.Nm spi_recv , 42975ce0baSmlelstv.Nm spi_send_recv 43*e38ac3b5Swiz.Nd Serial Peripheral Interface (SPI) kernel interface 44975ce0baSmlelstv.Sh SYNOPSIS 45975ce0baSmlelstv.In dev/spi/spivar.h 46975ce0baSmlelstv.Ft int 47975ce0baSmlelstv.Fo spi_configure 48975ce0baSmlelstv.Fa "struct spi_handle *sh" 49975ce0baSmlelstv.Fa "int mode" 50975ce0baSmlelstv.Fa "int speed" 51975ce0baSmlelstv.Fc 52975ce0baSmlelstv.Ft int 53975ce0baSmlelstv.Fo spi_transfer 54975ce0baSmlelstv.Fa "struct spi_handle *sh" 55975ce0baSmlelstv.Fa "struct spi_transfer *st" 56975ce0baSmlelstv.Fc 57975ce0baSmlelstv.Ft void 58975ce0baSmlelstv.Fo spi_transfer_init 59975ce0baSmlelstv.Fa "struct spi_transfer *st" 60975ce0baSmlelstv.Fc 61975ce0baSmlelstv.Ft void 62975ce0baSmlelstv.Fo spi_chunk_init 63975ce0baSmlelstv.Fa "struct spi_chunk *chunk" 64975ce0baSmlelstv.Fa "int cnt" 65975ce0baSmlelstv.Fa "const uint8_t *wptr" 66975ce0baSmlelstv.Fa "uint8_t *rptr" 67975ce0baSmlelstv.Fc 68975ce0baSmlelstv.Ft void 69975ce0baSmlelstv.Fo spi_transfer_add 70975ce0baSmlelstv.Fa "struct spi_transfer *st" 71975ce0baSmlelstv.Fa "struct spi_chunk *chunk" 72975ce0baSmlelstv.Fc 73975ce0baSmlelstv.Ft void 74975ce0baSmlelstv.Fo spi_wait 75975ce0baSmlelstv.Fa "struct spi_transfer *st" 76975ce0baSmlelstv.Fc 77975ce0baSmlelstv.Ft void 78975ce0baSmlelstv.Fo spi_done 79975ce0baSmlelstv.Fa "struct spi_transfer *st" 80975ce0baSmlelstv.Fa "int err" 81975ce0baSmlelstv.Fc 82975ce0baSmlelstv.Ft int 83975ce0baSmlelstv.Fo spi_recv 84975ce0baSmlelstv.Fa "struct spi_handle *sh" 85975ce0baSmlelstv.Fa "int cnt" 86975ce0baSmlelstv.Fa "uint8_t *data" 87975ce0baSmlelstv.Fc 88975ce0baSmlelstv.Ft int 89975ce0baSmlelstv.Fo spi_send 90975ce0baSmlelstv.Fa "struct spi_handle *sh" 91975ce0baSmlelstv.Fa "int cnt" 92975ce0baSmlelstv.Fa "const uint8_t *data" 93975ce0baSmlelstv.Fc 94975ce0baSmlelstv.Ft int 95975ce0baSmlelstv.Fo spi_send_recv 96975ce0baSmlelstv.Fa "struct spi_handle *sh" 97975ce0baSmlelstv.Fa "int scnt" 98975ce0baSmlelstv.Fa "const uint8_t *snd" 99975ce0baSmlelstv.Fa "int rcnt" 100975ce0baSmlelstv.Fa "const uint8_t *rcv" 101975ce0baSmlelstv.Fc 102975ce0baSmlelstv.Sh DESCRIPTION 103975ce0baSmlelstvSPI is a 4-wire synchronous full-duplex serial bus. 104975ce0baSmlelstvIt is commonly used for connecting devices such as EEPROMs, 105975ce0baSmlelstvdisplays, and other types of integrated circuits. 106975ce0baSmlelstvThe 107975ce0baSmlelstv.Nm spi 108975ce0baSmlelstvinterface provides a means of communicating with SPI-connected devices. 109975ce0baSmlelstv.Sh FUNCTIONS 110975ce0baSmlelstvThe following functions comprise the API provided to drivers of 111975ce0baSmlelstvSPI-connected devices. 112975ce0baSmlelstv.Pp 113975ce0baSmlelstvThe 114975ce0baSmlelstv.Fa struct spi_handle 115975ce0baSmlelstvcorresponding to the device is passed in the driver attachment. 116975ce0baSmlelstv.Bl -tag -width spi_transfer_init 117975ce0baSmlelstv.It Fn spi_configure "sh" "mode" "speed" 118975ce0baSmlelstvSets mode and speed for subsequent communication with a SPI slave. 119975ce0baSmlelstv.It Fn spi_transfer "sh" "st" 120975ce0baSmlelstvQueue transfer to SPI controller. 121975ce0baSmlelstv.Fn spi_transfer 122975ce0baSmlelstvreturns an errno value when the transfer couldn't be queued. 123975ce0baSmlelstv.It Fn spi_transfer_init "st" 124975ce0baSmlelstvInitialize a transfer structure. 125975ce0baSmlelstv.It Fn spi_chunk_init "chunk" "cnt" "wptr" rptr" 126975ce0baSmlelstvInitialize a chunk structure, each chunk corresponds to 127975ce0baSmlelstva possibly bi-directional transfer where the same amount 128975ce0baSmlelstvof bytes is shifted in and out. 129975ce0baSmlelstv.It Fn spi_transfer_add "st" "chunk" 130975ce0baSmlelstvAppend a chunk to transfer structure. 131975ce0baSmlelstv.It Fn spi_wait "st" 132*e38ac3b5SwizWait for a transfer to complete. 133*e38ac3b5SwizWhen the transfer has failed for some reason, the field 134975ce0baSmlelstv.Va st->st_errno 135975ce0baSmlelstvis set to a non-zero value. 136975ce0baSmlelstv.It Fn spi_done "st" "err" 137975ce0baSmlelstvCalled back machine-dependent backend to signal completion 138975ce0baSmlelstvof a transfer. 139975ce0baSmlelstv.El 140975ce0baSmlelstv.Pp 141975ce0baSmlelstvFor simplicity there are convenience functions that combine 142975ce0baSmlelstvcommon operations. 143975ce0baSmlelstvThese functions return an errno value when the transfer failed. 144975ce0baSmlelstv.Bl -tag -width spi_transfer_init 145975ce0baSmlelstv.It Fn spi_recv "sh" "cnt" "data" 146975ce0baSmlelstvPrepares a chunk for receiving data, queues a transfer and 147975ce0baSmlelstvwaits for it to complete. 148975ce0baSmlelstv.It Fn spi_send "sh" "cnt" "data" 149975ce0baSmlelstvPrepares a chunk for sending data, queues a transfer and 150975ce0baSmlelstvwaits for it to complete. 151975ce0baSmlelstv.It Fn spi_send_recv "sh" "scnt" "snd" "rcnt" "rcv" 152975ce0baSmlelstvPrepares two chunks for sending data first and then receiving 153*e38ac3b5Swizan answer, queues a transfer and waits for it to complete. 154975ce0baSmlelstvThis is not a full-duplex operation. 155975ce0baSmlelstv.El 156975ce0baSmlelstv.Sh SEE ALSO 157975ce0baSmlelstv.Xr spi 4 158975ce0baSmlelstv.Sh HISTORY 159975ce0baSmlelstvThe 160975ce0baSmlelstv.Nm spi 161975ce0baSmlelstvAPI first appeared in 162975ce0baSmlelstv.Nx 4.0 . 163