.\" .\" Copyright (C) 2008-2009 Semihalf, Michal Hajduk and Bartlomiej Sieka .\" 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 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 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. .\" .Dd May 22, 2019 .Dt I2C 8 .Os .Sh NAME .Nm i2c .Nd test I2C bus and slave devices .Sh SYNOPSIS .Nm .Cm -a Ar address .Op Fl f Ar device .Op Fl d Ar r|w .Op Fl w Ar 0|8|16|16LE|16BE .Op Fl o Ar offset .Op Fl c Ar count .Op Fl m Ar tr|ss|rs|no .Op Fl b .Op Fl v .Nm .Cm -h .Nm .Cm -i .Op Fl v .Op Ar cmd ... .Op Ar - .Nm .Cm -r .Op Fl f Ar device .Op Fl v .Nm .Cm -s .Op Fl f Ar device .Op Fl n Ar skip_addr .Op Fl v .Sh DESCRIPTION The .Nm utility can be used to perform raw data transfers (read or write) to devices on an I2C bus. It can also scan the bus for available devices and reset the I2C controller. .Pp The options are as follows: .Bl -tag -width ".Fl d Ar direction" .It Fl a Ar address 7-bit address on the I2C device to operate on (hex). .It Fl b binary mode - when performing a read operation, the data read from the device is output in binary format on stdout. .It Fl c Ar count number of bytes to transfer (decimal). .It Fl d Ar r|w transfer direction: r - read, w - write. Data to be written is read from stdin as binary bytes. .It Fl f Ar device I2C bus to use (default is /dev/iic0). .It Fl i Interpreted mode .It Fl h Help .It Fl m Ar tr|ss|rs|no addressing mode, i.e., I2C bus operations performed after the offset for the transfer has been written to the device and before the actual read/write operation. .Bl -tag -compact -offset indent .It Va tr complete-transfer .It Va ss stop then start .It Va rs repeated start .It Va no none .El Some I2C bus hardware does not provide control over the individual start, repeat-start, and stop operations. Such hardware can only perform a complete transfer of the offset and the data as a single operation. The .Va tr mode creates control structures describing the transfer and submits them to the driver as a single complete transaction. This mode works on all types of I2C hardware. .It Fl n Ar skip_addr address(es) to be skipped during bus scan. One or more addresses ([0x]xx) or ranges of addresses ([0x]xx-[0x]xx or [0x]xx..[0x]xx) separated by commas or colons. .It Fl o Ar offset offset within the device for data transfer (hex). The default is zero. Use .Dq -w 0 to disable writing of the offset to the slave. .It Fl r reset the controller. .It Fl s scan the bus for devices. .It Fl v be verbose. .It Fl w Ar 0|8|16|16LE|16BE device offset width (in bits). This is used to determine how to pass .Ar offset specified with .Fl o to the slave. Zero means that the offset is ignored and not passed to the slave at all. The endianness defaults to little-endian. .El .Sh INTERPRETED MODE When started with .Fl i any remaining arguments are interpreted as commands, and if the last argument is '-', or there are no arguments, commands will (also) be read from stdin. .Pp Available commands: .Bl -tag -compact .It 'r' bus address [0|8|16|16LE|16BE] offset count Read command, count bytes are read and hexdumped to stdout. .It 'w' bus address [0|8|16|16LE|16BE] offset hexstring Write command, hexstring (white-space is allowed) is written to device. .It 'p' anything Print command, the entire line is printed to stdout. (This can be used for synchronization.) .El .Pp All numeric fields accept canonical decimal/octal/hex notation. .Pp Without the .Fl v option, all errors are fatal with non-zero exit status. .Pp With the .Fl v option, no errors are fatal, and all commands will return either "OK\en" or "ERROR\en" on stdout. In case of error, detailed diagnostics will precede that on stderr. .Pp Blank lines and lines starting with '#' are ignored. .Sh EXAMPLES .Bl -bullet .It Scan the default bus (/dev/iic0) for devices: .Pp i2c -s .It Scan the default bus (/dev/iic0) for devices and skip addresses 0x45 to 0x47 (inclusive) and 0x56. .Pp i2c -s -n 0x56,45-47 .It Read 8 bytes of data from device at address 0x56 (e.g., an EEPROM): .Pp i2c -a 0x56 -d r -c 8 .It Write 16 bytes of data from file data.bin to device 0x56 at offset 0x10: .Pp i2c -a 0x56 -d w -c 16 -o 0x10 -b < data.bin .It Copy 4 bytes between two EEPROMs (0x56 on /dev/iic1 to 0x57 on /dev/iic0): .Pp i2c -a 0x56 -f /dev/iic1 -d r -c 0x4 -b | i2c -a 0x57 -f /dev/iic0 -d w -c 4 -b .It Reset the controller: .Pp i2c -f /dev/iic1 -r .It Read 8 bytes at address 24 in an EEPROM: .Pp i2c -i 'r 0 0x50 16BE 24 8' .It Read 2x8 bytes at address 24 and 48 in an EEPROM: .Pp echo 'r 0 0x50 16BE 48 8' | i2c -i 'r 0 0x50 16BE 24 8' - .El .Sh WARNING Many systems store critical low-level information in I2C memories, and may contain other I2C devices, such as temperature or voltage sensors. Reading these can disturb the firmware's operation and writing to them can "brick" the hardware. .Sh SEE ALSO .Xr iic 4 , .Xr iicbus 4 , .Xr smbus 4 .Sh HISTORY The .Nm utility appeared in .Fx 8.0 . .Sh AUTHORS .An -nosplit The .Nm utility and this manual page were written by .An Bartlomiej Sieka Aq Mt tur@semihalf.com and .An Michal Hajduk Aq Mt mih@semihalf.com . .Pp .An Poul-Henning Kamp Aq Mt phk@FreeBSD.org added interpreted mode.