Khadas Docs

Amazing Khadas, always amazes you!

User Tools

Site Tools


Sidebar

products:sbc:common:applications:gpio:i2c

This is an old revision of the document!


I2C

Introduction

This document mainly introduces how to use I2C bus on Khadas SBC. You will learn how to connect I2C devices, check I2C devices, and read device register values.

Hardware Connection

VIM1/2

I2C A
Sensor SCLI2C_SCK_A (PIN 22)
Sensor SDAI2C_SDA_A (PIN 23)
Sensor GNDGND
Sensor VCC5V or 3.3V //depends on your sensor
I2C B
Sensor SCLI2C_SCK_B (PIN 25)
Sensor SDAI2C_SDA_B (PIN 26)
Sensor GNDGND
Sensor VCC5V or 3.3V //depends on your sensor

VIM3/3L

I2C 3
Sensor SCLI2C0_SCK (PIN 22)
Sensor SDAI2C0_SDA (PIN 23)
Sensor GNDGND
Sensor VCC5V or 3.3V //depends on your sensor
I2C 4
Sensor SCLI2C1_SCK (PIN 25)
Sensor SDAI2C1_SDA (PIN 26)
Sensor GNDGND
Sensor VCC5V or 3.3V //depends on your sensor

VIM4

I2CM A
Sensor SCLI2CM_A_SCL (PIN 25)
Sensor SDAI2CM_A_SDA (PIN 26)
Sensor GNDGND
Sensor VCC5V or 3.3V //depends on your sensor
I2CM F
Sensor SCLI2CM_F_SCL (PIN 22)
Sensor SDAI2CM_F_SDA (PIN 23)
Sensor GNDGND
Sensor VCC5V or 3.3V //depends on your sensor

I2C PIN & GPIO Number DT Overlays Node Bus Number Device Node
I2CM A SCL - PIN25 - GPIOT_20 (#466)
SDA - PIN26 - GPIOT_21 (#467)
i2cm_a 0 /dev/i2c-0
I2CM F SCL - PIN22 - GPIOY_17 (#501)
SDA - PIN23 - GPIOY_18 (#502)
i2cm_f 5 /dev/i2c-5

Enabel I2C Function

In order to use the I2C, you need to enable the I2C function via Device Tree Overlay.

VIM1/2/3/3L/Edge1

Edit /boot/env.txt to add the i2c node to overlays node.

Take VIM3 as an example to enable i2c3:

overlays=uart4 pwm_ao_a pwm_f i2c3

VIM4

Edit /boot/dtb/amlogic/kvim4.dtb.overlay.env to add i2c node to fdt_overlays node.

e.g. Enable I2CM A, you need to add i2cm_a to node fdt_overlays.

fdt_overlays=i2cm_a

Reboot to effect.

Device Detection

VIM1/2

Detect device on I2C A:

$ sudo i2cdetect -y -r 1
    0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- 1d -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

Detect device on I2C B:

$ sudo i2cdetect -y -r 2
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- UU -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- 51 -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

VIM3/3L

Detect device on I2C 3:

$ sudo i2cdetect -y -r 3
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

Detect device on I2C 4:

$ sudo i2cdetect -y -r 4
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- 0e --
10: -- -- -- -- -- -- -- -- UU -- -- -- -- -- -- --
20: UU -- 22 -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- UU -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

VIM4

Detect device on I2C A:

$ sudo i2cdetect -r -y 0
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: UU -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

Detect device on I2C F:

$ sudo i2cdetect -r -y 5
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- UU -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

If you have made a connection, you will see the device address, e.g: 0x1d,0x18 and 0x51.

Read Registers from Device

Read register 0x0d of device 0x1d on I2C A.

$ sudo i2cget -f -y 1 0x1d 0x0d
0x2a

For more information, please refer to the help messages.

Last modified: 2022/07/12 04:18 by nick