u-boot=> i2c
i2c - I2C sub-system
Usage:
i2c bus [muxtype:muxaddr:muxchannel] - show I2C bus info
i2c crc32 chip address[.0, .1, .2] count - compute CRC32 checksum
i2c dev [dev] - show or set current I2C bus
i2c loop chip address[.0, .1, .2] [# of objects] - looping read of device
i2c md chip address[.0, .1, .2] [# of objects] - read from I2C device
i2c mm chip address[.0, .1, .2] - write to I2C device (auto-incrementing)
i2c mw chip address[.0, .1, .2] value [count] - write to I2C device (fill)
i2c nm chip address[.0, .1, .2] - write to I2C device (constant address)
i2c probe [address] - test for and show device(s) on the I2C bus
i2c read chip address[.0, .1, .2] length memaddress - read to memory
i2c write memaddress chip address[.0, .1, .2] length [-s] - write memory
to I2C; the -s option selects bulk write in a single transaction
i2c flags chip [flags] - set or get chip flags
i2c olen chip [offset_length] - set or get chip offset length
i2c reset - re-init the I2C Controller
i2c speed [speed] - show or set I2C bus speed
u-boot=> i2c speed
Current bus speed=400000
u-boot=> i2c bus
Bus 0: i2c@44340000
Bus 1: i2c@44350000 (active 1)
25: pmic@25, offset len 1, flags 0
34: gpio@34, offset len 1, flags 0
Bus 2: i2c@42530000
53: rtc@53, offset len 1, flags 0
u-boot=> i2c dev 1
Setting bus to 1
u-boot=> i2c crc32 0x25 0x1B.1 1
CRC32 for 0000001b ... 0000001b ==> abde5729
1,选择你要使用的i2c bus号码
// Where <bus> is the number of the bus to select:
# i2c dev <bus>
// Example - Select bus 0
# i2c dev 0
2,在当前的i2c bus上搜索设备
// Attempt to detect the addresses of all devices on the bus
# i2c probe
// Check if a device is present, where <chip> is the address of the I2C device
# i2c probe <chip>
// Example - Check if a device with address 0x50 is present
# i2c probe 0x50
3,地址说明
I2C 地址通常为 7 位,第 8 位表示 I2C 操作是读取(0)还是写入(1)。U-Boot 希望地址只有 7 位,在一个字节内正确排列。例如 0b0AAAAAAA,其中 A 是地址位。不同的集成电路数据手册以不同的方式指定 I2C 地址。有些可能会指定左对齐并附加读取位的地址。即 0bAAAAAAA0。在这种情况下,应将数据表地址右移一位,然后再将其用于 U-Boot I2C 命令。U-Boot 会自动设置地址字段中的读/写位。
I2C addresses are typically 7 bits with the 8th bit indicating if the I2C operation is a read (0) or a write (1). U-Boot expects the address as just 7 bits, right alined within a byte. I.e. 0b0AAAAAAA, where A is an address bit. Different IC datasheets specify the I2C address in different ways. Some may specify the address left aligned with the read bit appended. I.e. 0bAAAAAAA0. In these cases, the datasheet address should be right shifted by one bit before using it for the U-Boot I2C commands. U-Boot automatically sets the read/write bit in the address field.
4,从I2C设备读取寄存器值 Reading from an I2C device
您可以使用以下命令将内存转储到屏幕上:
You can dump memory to the screen using the following commands:
// <chip> is the address of the I2C IC.
// <register_address> is the register within the I2C device that you wish to read
// <address_length> is either 0, 1, or 2. 0 = no address, 1 = 8 bit address, 2 = 16 bit address.
// <length> is the number of bytes to read from the I2C device.
i2c md <chip> <register_address>[.<address_length>] <length>
// Example 1 - Read 0x100 bytes starting at register 0x00, from device with address 0x51. Address is 1 byte.
i2c md 0x51 0x00.1 0x100
// Example 2 - Read 0x1000 byte starting at register 0x0400, from device with address 0x34. Address is 2 bytes.
i2c md 0x34 0x0400.2 0x1000
// Example 3 - Read a single byte from device with address 0x60. Don't send register address word.
i2c md 0x60 0x00.0 0x1
您还可以使用以下命令直接从 I2C 设备读取数据到内存中:
You can also read from the I2C device directly into memory using the following command:
// <register_address>, <address_length>, and <length> are as per the i2c md command.
// <memory_address> is the address in the processors memory to copy the I2C data to.
i2c read <chip> <register_address>[.<address_length>] <length> <memory_address>
// Example: copy into memory location 0x40000000, 0x80 bytes starting at register
// address 0x10 of the I2C device with address 0x50.
i2c read 0x50 0x10 0x80 0x40000000
5,向I2C设备的寄存器写入数据 Writing to an I2C device
您可以使用以下命令直接写入 I2C 设备的寄存器:
You write to I2C device's registers directly using the following command:
// <chip> is the address of the I2C IC.
// <register_address> is the register within the I2C device that you wish to read
// <address_length> is either 0, 1, or 2. 0 = no address, 1 = 8 bit address, 2 = 16 bit address.
// <value> is the value to write to the register address.
// <length> is the number of bytes to write to the I2C device.
i2c mw <chip> <register_address>[.<address_length>] <value> <length>
// Example 1 - Write 0x1 byte with value 0xAA to register 0xE5 of the I2C
// device with address 0x28. Address is 1 byte.
i2c mw 0x28 0xE5.1 0xAA 0x1
参考: