Index: stable/11/share/man/man4/spigen.4 =================================================================== --- stable/11/share/man/man4/spigen.4 (revision 366224) +++ stable/11/share/man/man4/spigen.4 (revision 366225) @@ -1,206 +1,207 @@ .\" .\" Copyright (c) 2018 Ian Lepore .\" All rights reserved. .\" .\" 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 THE AUTHOR ``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 THE AUTHOR 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. .\" .\" $FreeBSD$ .\" -.Dd April 7, 2018 +.Dd August 21, 2020 .Dt SPIGEN 4 .Os .Sh NAME .Nm spigen .Nd SPI generic I/O device driver .Sh SYNOPSIS To compile this driver into the kernel, place the following lines in your kernel configuration file: .Bd -ragged -offset indent .Cd "device spi" .Cd "device spibus" .Cd "device spigen" .Ed .Pp Alternatively, to load the driver as a module at boot time, place the following line in .Xr loader.conf 5 : .Bd -literal -offset indent spigen_load="YES" .Ed .Sh DESCRIPTION The .Nm driver provides direct access to a slave device on the SPI bus. Each instance of a .Nm device is associated with a single chip-select line on the bus, and all I/O performed through that instance is done with that chip-select line asserted. .Pp SPI data transfers are inherently bi-directional; there are not separate read and write operations. When commands and data are sent to a device, data also comes back from the device, although in some cases the data may not be useful (or even documented or predictable for some devices). Likewise on a read operation, whatever data is in the buffer at the start of the operation is sent to (and typically ignored by) the device, with each outgoing byte then replaced in the buffer by the corresponding incoming byte. Thus, all buffers passed to the transfer functions are both input and output buffers. .Pp The .Nm driver provides access to the SPI slave device with the following .Xr ioctl 2 calls, defined in .In sys/spigenio.h : .Bl -tag -width indent .It Dv SPIGENIOC_TRANSFER Pq Vt "struct spigen_transfer" Transfer a command and optional associated data to/from the device, using the buffers described by the st_command and st_data fields in the .Vt spigen_transfer . Set .Vt st_data.iov_len to zero if there is no data associated with the command. .Bd -literal struct spigen_transfer { struct iovec st_command; struct iovec st_data; }; .Ed .It Dv SPIGENIOC_TRANSFER_MMAPPED Pq Vt "spigen_transfer_mmapped" Transfer a command and optional associated data to/from the device. The buffers for the transfer are a previously-mmap'd region. The length of the command and data within that region are described by the .Vt stm_command_length and .Vt stm_data_length fields of .Vt spigen_transfer_mmapped . If .Vt stm_data_length is non-zero, the data appears in the memory region immediately following the command (that is, at offset .Vt stm_command_length from the start of the mapped region). .Bd -literal struct spigen_transfer_mmapped { size_t stm_command_length; size_t stm_data_length; }; .Ed .It Dv SPIGENIOC_GET_CLOCK_SPEED Pq Vt uint32_t Get the maximum clock speed (bus frequency in Hertz) to be used when communicating with this slave device. .It Dv SPIGENIOC_SET_CLOCK_SPEED Pq Vt uint32_t Set the maximum clock speed (bus frequency in Hertz) to be used when communicating with this slave device. The setting remains in effect for subsequent transfers; it is not necessary to reset this before each transfer. The actual bus frequency may be lower due to hardware limitiations of the SPI bus controller device. .It Dv SPIGENIOC_GET_SPI_MODE Pq Vt uint32_t Get the SPI mode (clock polarity and phase) to be used when communicating with this device. .It Dv SPIGENIOC_SET_SPI_MODE Pq Vt uint32_t Set the SPI mode (clock polarity and phase) to be used when communicating with this device. The setting remains in effect for subsequent transfers; it is not necessary to reset this before each transfer. .El .Sh HINTS CONFIGURATION On a .Xr device.hints 5 based system, such as .Li MIPS , these values are configurable for .Nm : .Bl -tag -width indent .It Va hint.spigen.%d.at The spibus the .Nm instance is attached to. .It Va hint.spigen.%d.clock The maximum bus frequency to use when communicating with this device. Actual bus speed may be lower, depending on the capabilities of the SPI bus controller hardware. .It Va hint.spigen.%d.cs The chip-select number to assert when performing I/O for this device. Set the high bit (1 << 31) to invert the logic level of the chip select line. .It Va hint.spigen.%d.mode The SPI mode (0-3) to use when communicating with this device. .El .Sh FDT CONFIGURATION On an .Xr fdt 4 based system, the spigen device is defined as a slave device subnode of the SPI bus controller node. All properties documented in the .Va spibus.txt bindings document can be used with the .Nm device. The most commonly-used ones are documented below. .Pp The following properties are required in the .Nm device subnode: .Bl -tag -width indent .It Va compatible Must be the string "freebsd,spigen". .It Va reg Chip select address of device. .It Va spi-max-frequency The maximum bus frequency to use when communicating with this slave device. Actual bus speed may be lower, depending on the capabilities of the SPI bus controller hardware. .El .Pp The following properties are optional for the .Nm device subnode: .Bl -tag -width indent .It Va spi-cpha Empty property indicating the slave device requires shifted clock phase (CPHA) mode. .It Va spi-cpol Empty property indicating the slave device requires inverse clock polarity (CPOL) mode. .It Va spi-cs-high Empty property indicating the slave device requires chip select active high. .El .Sh FILES .Bl -tag -width -compact .It Pa /dev/spigen* .El .Sh SEE ALSO .Xr fdt 4 , -.Xr device.hints 5 +.Xr device.hints 5 , +.Xr spi 8 .Sh HISTORY The .Nm driver appeared in .Fx 11.0 . FDT support appeared in .Fx 11.2 . Index: stable/11/usr.sbin/spi/spi.8 =================================================================== --- stable/11/usr.sbin/spi/spi.8 (revision 366224) +++ stable/11/usr.sbin/spi/spi.8 (revision 366225) @@ -1,203 +1,216 @@ .\" Copyright (c) 2018 by S.F.T. Inc. -.\" +.\" .\" 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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. .\" .\" $FreeBSD$ .\" -.Dd "15 April 2018" -.Dt spi 8 +.Dd August 21, 2020 +.Dt SPI 8 .Os .Sh NAME .Nm spi .Nd communicate on SPI bus with slave devices .Sh SYNOPSIS .Nm -.Op Fl f Ar device -.Op Fl d Ar r|w|rw -.Op Fl m Ar mode -.Op Fl s Ar max-speed -.Op Fl c Ar count -.Op Fl C Ar cmd_bytes .Op Fl A .Op Fl b .Op Fl L .Op Fl v +.Op Fl C Ar command-bytes +.Op Fl c Ar count +.Op Fl d Cm r Ns | Ns Cm w Ns | Ns Cm rw +.Op Fl f Ar device +.Op Fl m Ar mode +.Op Fl s Ar max-speed .Nm .Op Fl i -.Op Fl f Ar device .Op Fl v +.Op Fl f Ar device .Nm .Op Fl h .Sh DESCRIPTION The .Nm utility can be used to perform raw data transfers .Pq read, write, or simultaneous read/write with devices on the SPI bus, via the .Xr spigen 4 device. .Pp Each .Xr spigen 4 device is associated with a specific -.Sq chip select +.Dq chip select .Pq cs pin on the spibus, and therefore needs to be specified. If no device name is specified on the command line, .Nm assumes -.Sq spigen0.0 . +.Dq spigen0.0 . .Pp For more information on the spigen device, see .Xr spigen 4 . .Pp The options are as follows: -.Bl -tag -width ".Fl f Ar device" +.Bl -tag -width "-f device" .It Fl A Specifies ASCII mode. Both read and write data is input and output as 2-character hexadecimal values, optionally separated by white space, such as 00 01 02 etc. When combined with the -.Sq -b +.Fl b flag, the data on stdin remains a sequence of ASCII hexadecimal byte values, but the output reverts to binary mode. .It Fl b Binary .Pq output mode. Only has an effect when -.Sq -A +.Fl A has been specified. Reverts the output back to binary .Pq rather than ASCII , while leaving the input format as-is. Use in combination with -.Sq -A +.Fl A to allow using something like -.Sq echo +.Dq echo to pass hexadecimal values to the SPI device, but output the received data on stdout as binary. -.It Fl C Ar command bytes -Sends one or more -.Sq command -bytes, skipping any bytes read-in during the transfer. +.It Fl C Ar command-bytes +Sends one or more command bytes, +skipping any bytes read-in during the transfer. The byte values should be specified as a quoted parameter, similar to the format for data on stdin for -.Sq -A , +.Fl A , that is, 2 character hexadecimal values, optionally separated by white space. An SPI device will typically require that a command be sent, followed by bytes of data. You can use this option to send the command without receiving any data bytes during the command sequence. .It Fl c Ar count The total number of bytes to transfer as a decimal integer. If a write or a read/write transaction is being performed, and fewer than this number of bytes are read in from stdin, the remaining bytes will be sent with a value of -.Sq 0 . +.Dq 0 . If the length can be determined from the input file size, you can use a -.Sq count +.Ar count value of -.Sq -1 +.Dq -1 to base the transfer on the input file's size. -.It Fl d Ar r|w|rw +.It Fl d Cm r Ns | Ns Cm w Ns | Ns Cm rw Transfer direction: Use -.Sq r +.Cm r for read, -.Sq w for write, and -.Sq rw +.Cm w +for write, and +.Cm rw for simultaneous read and write. .It Fl f Ar device SPI device to use -.Pq default is /dev/spigen0 . +.Pq default is Pa /dev/spigen0 . .It Fl h Print help text to stderr, explaining the command line options. .It Fl i Displays information about the SPI device to stderr. Whenever this flag is specified, no data is read or written, and the mode and clock speed are not changed. .It Fl L LSB bit order. The default is MSB, i.e., the highest order bit is transmitted first. -Specifying -L caused the LSB to be transmitted and read first. -.It Fl m Ar 0 - 3 +Specifying +.Fl L +caused the LSB to be transmitted and read first. +.It Fl m Cm 0 Ns | Ns Cm 1 Ns | Ns Cm 2 Ns | Ns Cm 3 SPI mode, 0 through 3. This defines the clock phase and timing with respect to reading and writing data, as per the SPI specification. .It Fl s Ar speed Specify the maximum speed, in Hz, for the SPI clock. The bus will operate at its highest available speed which does not exceed this maximum. .It Fl v Specifies Verbose mode. Diagnostics and information are written to stderr. You can specify -.Sq -v +.Fl v more than once to increase verbosity. .El .Sh EXAMPLES Here are a few examples of using the spi utility: .Bl -bullet .It Get information about the default SPI device -.Pp +.Bd -literal spi -i +.Ed .It Set the maximum clock speed to 200Khz and the mode to 3 on spigen0.1, but do not transmit nor receive any data -.Pp +.Bd -literal spi -f spigen0.1 -s 200000 -m 3 +.Ed .It Send a command sequence consisting of 2 bytes, and read 2 additional bytes from the SPI device, using the current mode and speed on the default device -.Pp +.Bd -literal spi -d r -C "00 01" -c 2 +.Ed .It Transmit a byte value of 5, and receive 2 bytes, displaying their values as 2-byte ASCII hexadecimal, with mode 2, and a maximum clock speed of 500khz. -.Pp +.Bd -literal echo "05" | spi -A -d rw -m 2 -s 500000 -c 2 +.Ed .It Send a binary file, and output the SPI result through -.Sq od +.Xr od 1 as hexadecimal bytes, using the current maximum clock speed and SPI mode. -.Pp +.Bd -literal spi -d rw -c -1