166660095SIan Lepore.\" Copyright (c) 2018 by S.F.T. Inc. 266660095SIan Lepore.\" 366660095SIan Lepore.\" Redistribution and use in source and binary forms, with or without 466660095SIan Lepore.\" modification, are permitted provided that the following conditions 566660095SIan Lepore.\" are met: 666660095SIan Lepore.\" 1. Redistributions of source code must retain the above copyright 766660095SIan Lepore.\" notice, this list of conditions and the following disclaimer. 866660095SIan Lepore.\" 2. Redistributions in binary form must reproduce the above copyright 966660095SIan Lepore.\" notice, this list of conditions and the following disclaimer in the 1066660095SIan Lepore.\" documentation and/or other materials provided with the distribution. 1166660095SIan Lepore.\" 1266660095SIan Lepore.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1366660095SIan Lepore.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1466660095SIan Lepore.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1566660095SIan Lepore.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1666660095SIan Lepore.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1766660095SIan Lepore.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 1866660095SIan Lepore.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 1966660095SIan Lepore.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2066660095SIan Lepore.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2166660095SIan Lepore.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2266660095SIan Lepore.\" SUCH DAMAGE. 2366660095SIan Lepore.\" 24*4863a703SMateusz Piotrowski.Dd August 21, 2020 25*4863a703SMateusz Piotrowski.Dt SPI 8 2666660095SIan Lepore.Os 2766660095SIan Lepore.Sh NAME 2866660095SIan Lepore.Nm spi 2966660095SIan Lepore.Nd communicate on SPI bus with slave devices 3066660095SIan Lepore.Sh SYNOPSIS 3166660095SIan Lepore.Nm 3266660095SIan Lepore.Op Fl A 3366660095SIan Lepore.Op Fl b 3466660095SIan Lepore.Op Fl L 3566660095SIan Lepore.Op Fl v 36*4863a703SMateusz Piotrowski.Op Fl C Ar command-bytes 37*4863a703SMateusz Piotrowski.Op Fl c Ar count 38*4863a703SMateusz Piotrowski.Op Fl d Cm r Ns | Ns Cm w Ns | Ns Cm rw 39*4863a703SMateusz Piotrowski.Op Fl f Ar device 40*4863a703SMateusz Piotrowski.Op Fl m Ar mode 41*4863a703SMateusz Piotrowski.Op Fl s Ar max-speed 4266660095SIan Lepore.Nm 4366660095SIan Lepore.Op Fl i 4466660095SIan Lepore.Op Fl v 45*4863a703SMateusz Piotrowski.Op Fl f Ar device 4666660095SIan Lepore.Nm 4766660095SIan Lepore.Op Fl h 4866660095SIan Lepore.Sh DESCRIPTION 4966660095SIan LeporeThe 5066660095SIan Lepore.Nm 5166660095SIan Leporeutility can be used to perform raw data transfers 5266660095SIan Lepore.Pq read, write, or simultaneous read/write 5366660095SIan Leporewith devices on the SPI bus, via the 5466660095SIan Lepore.Xr spigen 4 5566660095SIan Leporedevice. 5666660095SIan Lepore.Pp 5766660095SIan LeporeEach 5866660095SIan Lepore.Xr spigen 4 5966660095SIan Leporedevice is associated with a specific 60*4863a703SMateusz Piotrowski.Dq chip select 6166660095SIan Lepore.Pq cs 6266660095SIan Leporepin on the spibus, and therefore needs to be specified. 6366660095SIan LeporeIf no device name is specified on the command line, 6466660095SIan Lepore.Nm 6566660095SIan Leporeassumes 66*4863a703SMateusz Piotrowski.Dq spigen0.0 . 6766660095SIan Lepore.Pp 6866660095SIan LeporeFor more information on the spigen device, see 6966660095SIan Lepore.Xr spigen 4 . 7066660095SIan Lepore.Pp 7166660095SIan LeporeThe options are as follows: 72*4863a703SMateusz Piotrowski.Bl -tag -width "-f device" 7366660095SIan Lepore.It Fl A 7466660095SIan LeporeSpecifies ASCII mode. 7566660095SIan LeporeBoth read and write data is input and output as 7666660095SIan Lepore2-character hexadecimal values, optionally separated by white space, 7766660095SIan Leporesuch as 00 01 02 etc. 7866660095SIan LeporeWhen combined with the 79*4863a703SMateusz Piotrowski.Fl b 8066660095SIan Leporeflag, the data on stdin remains a sequence of ASCII hexadecimal 8166660095SIan Leporebyte values, but the output reverts to binary mode. 8266660095SIan Lepore.It Fl b 8366660095SIan LeporeBinary 8466660095SIan Lepore.Pq output 8566660095SIan Leporemode. 8666660095SIan LeporeOnly has an effect when 87*4863a703SMateusz Piotrowski.Fl A 8866660095SIan Leporehas been specified. 8966660095SIan LeporeReverts the output back to binary 9066660095SIan Lepore.Pq rather than ASCII , 9166660095SIan Leporewhile leaving the input format as-is. 9266660095SIan LeporeUse in combination with 93*4863a703SMateusz Piotrowski.Fl A 9466660095SIan Leporeto allow using something like 95*4863a703SMateusz Piotrowski.Dq echo 9666660095SIan Leporeto pass hexadecimal values to the SPI device, but output the received data 9766660095SIan Leporeon stdout as binary. 98*4863a703SMateusz Piotrowski.It Fl C Ar command-bytes 99*4863a703SMateusz PiotrowskiSends one or more command bytes, 100*4863a703SMateusz Piotrowskiskipping any bytes read-in during the transfer. 10166660095SIan LeporeThe byte values should be specified as a quoted parameter, similar to the 10266660095SIan Leporeformat for data on stdin for 103*4863a703SMateusz Piotrowski.Fl A , 10466660095SIan Leporethat is, 2 character hexadecimal values, optionally separated by white space. 10566660095SIan LeporeAn SPI device will typically require that a command be sent, followed by 10666660095SIan Leporebytes of data. 10766660095SIan LeporeYou can use this option to send the command without receiving any data bytes 10866660095SIan Leporeduring the command sequence. 10966660095SIan Lepore.It Fl c Ar count 11066660095SIan LeporeThe total number of bytes to transfer as a decimal integer. 11166660095SIan LeporeIf a write or a read/write transaction is being performed, and fewer than 11266660095SIan Leporethis number of bytes are read in from stdin, the remaining bytes will be 11366660095SIan Leporesent with a value of 114*4863a703SMateusz Piotrowski.Dq 0 . 11566660095SIan LeporeIf the length can be determined from the input file size, you can use a 116*4863a703SMateusz Piotrowski.Ar count 11766660095SIan Leporevalue of 118*4863a703SMateusz Piotrowski.Dq -1 11966660095SIan Leporeto base the transfer on the input file's size. 120*4863a703SMateusz Piotrowski.It Fl d Cm r Ns | Ns Cm w Ns | Ns Cm rw 12166660095SIan LeporeTransfer direction: Use 122*4863a703SMateusz Piotrowski.Cm r 12366660095SIan Leporefor read, 124*4863a703SMateusz Piotrowski.Cm w 125*4863a703SMateusz Piotrowskifor write, and 126*4863a703SMateusz Piotrowski.Cm rw 12766660095SIan Leporefor simultaneous read and write. 12866660095SIan Lepore.It Fl f Ar device 12966660095SIan LeporeSPI device to use 130*4863a703SMateusz Piotrowski.Pq default is Pa /dev/spigen0 . 13166660095SIan Lepore.It Fl h 13266660095SIan LeporePrint help text to stderr, explaining the command line options. 13366660095SIan Lepore.It Fl i 13466660095SIan LeporeDisplays information about the SPI device to stderr. 13566660095SIan LeporeWhenever this flag is specified, no data is read or written, and the mode 13666660095SIan Leporeand clock speed are not changed. 13766660095SIan Lepore.It Fl L 13866660095SIan LeporeLSB bit order. 13966660095SIan LeporeThe default is MSB, i.e., the highest order bit is 14066660095SIan Leporetransmitted first. 141*4863a703SMateusz PiotrowskiSpecifying 142*4863a703SMateusz Piotrowski.Fl L 143*4863a703SMateusz Piotrowskicaused the LSB to be transmitted and read first. 144*4863a703SMateusz Piotrowski.It Fl m Cm 0 Ns | Ns Cm 1 Ns | Ns Cm 2 Ns | Ns Cm 3 14566660095SIan LeporeSPI mode, 0 through 3. 14666660095SIan LeporeThis defines the clock phase and timing with respect to reading and writing 14766660095SIan Leporedata, as per the SPI specification. 14866660095SIan Lepore.It Fl s Ar speed 14966660095SIan LeporeSpecify the maximum speed, in Hz, for the SPI clock. 15066660095SIan LeporeThe bus will operate at its highest available speed which does not 15166660095SIan Leporeexceed this maximum. 15266660095SIan Lepore.It Fl v 15366660095SIan LeporeSpecifies Verbose mode. 15466660095SIan LeporeDiagnostics and information are written to stderr. 15566660095SIan LeporeYou can specify 156*4863a703SMateusz Piotrowski.Fl v 15766660095SIan Leporemore than once to increase verbosity. 15866660095SIan Lepore.El 15966660095SIan Lepore.Sh EXAMPLES 16066660095SIan LeporeHere are a few examples of using the spi utility: 16166660095SIan Lepore.Bl -bullet 16266660095SIan Lepore.It 16366660095SIan LeporeGet information about the default SPI device 164*4863a703SMateusz Piotrowski.Bd -literal 16566660095SIan Leporespi -i 166*4863a703SMateusz Piotrowski.Ed 16766660095SIan Lepore.It 16866660095SIan LeporeSet the maximum clock speed to 200Khz and the mode to 3 on spigen0.1, but do 16966660095SIan Leporenot transmit nor receive any data 170*4863a703SMateusz Piotrowski.Bd -literal 17166660095SIan Leporespi -f spigen0.1 -s 200000 -m 3 172*4863a703SMateusz Piotrowski.Ed 17366660095SIan Lepore.It 17466660095SIan LeporeSend a command sequence consisting of 2 bytes, and read 2 additional bytes 17566660095SIan Leporefrom the SPI device, using the current mode and speed on the default device 176*4863a703SMateusz Piotrowski.Bd -literal 17766660095SIan Leporespi -d r -C "00 01" -c 2 178*4863a703SMateusz Piotrowski.Ed 17966660095SIan Lepore.It 18066660095SIan LeporeTransmit a byte value of 5, and receive 2 bytes, displaying their values as 18166660095SIan Lepore2-byte ASCII hexadecimal, with mode 2, and a maximum clock speed of 500khz. 182*4863a703SMateusz Piotrowski.Bd -literal 18366660095SIan Leporeecho "05" | spi -A -d rw -m 2 -s 500000 -c 2 184*4863a703SMateusz Piotrowski.Ed 18566660095SIan Lepore.It 18666660095SIan LeporeSend a binary file, and output the SPI result through 187*4863a703SMateusz Piotrowski.Xr od 1 18866660095SIan Leporeas hexadecimal bytes, using the current maximum clock speed and SPI mode. 189*4863a703SMateusz Piotrowski.Bd -literal 19066660095SIan Leporespi -d rw -c -1 <input_file.bin | od -An -t x1 191*4863a703SMateusz Piotrowski.Ed 19266660095SIan Lepore.It 19366660095SIan LeporeSend 2 bytes of data, receive a total of 4 bytes, and output the SPI result 19466660095SIan Leporeas binary data, piped through 195*4863a703SMateusz Piotrowski.Xr od 1 , 19666660095SIan Leporedisplaying it as two hexadecimal unsigned short integer values. 197*4863a703SMateusz Piotrowski.Bd -literal 19866660095SIan Leporeecho "00 01" | spi -A -b -d rw -c 4 | od -t x2 199*4863a703SMateusz Piotrowski.Ed 200110d4e2dSIan Lepore.It 201110d4e2dSIan LeporeQuery the manufacturer ID and size from a standard spiflash device, by 202110d4e2dSIan Leporesending the command byte 0x9f and displaying the 3-byte reply in ASCII hex. 203*4863a703SMateusz Piotrowski.Bd -literal 204110d4e2dSIan Leporespi -f spigen0.0 -m 0 -s 1000000 -d r -c 3 -A -C 9f 205*4863a703SMateusz Piotrowski.Ed 20666660095SIan Lepore.El 20766660095SIan Lepore.Sh SEE ALSO 20866660095SIan Lepore.Xr spigen 4 209*4863a703SMateusz Piotrowski.Sh HISTORY 210*4863a703SMateusz PiotrowskiThe 211*4863a703SMateusz Piotrowski.Nm 212*4863a703SMateusz Piotrowskiutility 213*4863a703SMateusz Piotrowskiappeared in 214*4863a703SMateusz Piotrowski.Fx 11.3 . 215