Khadas Docs

Amazing Khadas, always amazes you!

User Tools

Site Tools


products:sbc:common:applications:gpio:i2c

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
products:sbc:common:applications:gpio:i2c [2022/07/04 03:14]
frank [Hardware Connection] merge VIM1 and VIM2
products:sbc:common:applications:gpio:i2c [2023/11/07 04:51] (current)
nick [Enable I2C]
Line 3: Line 3:
 ===== Introduction ===== ===== Introduction =====
  
-This document mainly introduces how to use I2C on Khadas SBC. You will learn how to connect I2C devices, view I2C devices, and read device register values.+This page introduces the usage of I2C bus on [[products:sbc:common:applications:gpio:40pin-header|40-Pin Header]].
  
-===== Hardware Connection =====+===== I2C Information =====
  
-<tabbox VIM1/VIM2> +|            I2C      PIN      ^  GPIO Name  ^  DT Overlays Node  ^  Bus Number  ^  Device Node  ^ 
-**I2C A** \\ +^  VIM1/2   |  I2C A   |  22(SCL)  |  GPIODV_25  |  i2c0              |  0            /dev/i2c-0   | 
-''Sensor SCL'' <-> ''I2C_SCK_A'' (PIN 22\\ +| :::       | :::      |  23(SDA  GPIODV_24  | :::                | :::          | :::           | 
-''Sensor SDA'' <-> ''I2C_SDA_A'' (PIN 23\\ +| :::        I2C B    25(SCL  GPIODV_27  |                  |  1           |  /dev/i2c-1   | 
-''Sensor GND'' <-> ''GND'' \\ +| :::       | :::      |  26(SDA  GPIODV_26  | :::                | :::          | :::           | 
-''Sensor VCC'' <-> ''5V'' or ''3.3V'' %%//%%depends on your sensor \\ +^  VIM3/3L  |  I2CM 3  |  22(SCL  GPIOA_15    i2c3              |            |  /dev/i2c-  | 
-**I2C B** \\ +| :::       | :::      |  23(SDA  GPIOA_14   | :::                | :::          | :::           | 
-''Sensor SCL'' <-> ''I2C_SCK_B'' (PIN 25\\ +| :::        I2C AO  |  25(SCL  GPIOAO_2   |                  |  4           |  /dev/i2c-  | 
-''Sensor SDA'' <-> ''I2C_SDA_B'' (PIN 26\\ +| :::       | :::      |  26(SDA  GPIOAO_3   | :::                | :::          | :::           | 
-''Sensor GND'' <-> ''GND'' \\ +^  VIM4      I2CM F  |  22(SCL  GPIOY_17    i2cm_f            |  5           |  /dev/i2c-5   | 
-''Sensor VCC'' <-> ''5V'' or ''3.3V'' %%//%%depends on your sensor +| :::       | :::      |  23(SDA)  |  GPIOY_18   | :::                | :::          | :::           | 
-<tabbox VIM3/3L> +| :::        I2CM   25(SCL)  |  GPIOT_20    i2cm_a            |  0            /dev/i2c-0   | 
-**I2C 3** \\ +| :::       | :::      |  26(SDA  GPIOT_21   | :::                | :::          | :::           | 
-''Sensor SCL'' <-> ''I2C0_SCK'' (PIN 22\\ +^  VIM1S    |  I2CM B  |  22(SCL  GPIOD_7    |                  |  1           |  /dev/i2c-1   | 
-''Sensor SDA'' <-> ''I2C0_SDA'' (PIN 23\\ +| :::       | :::      |  23(SDA  GPIOD_6    | :::                | :::          | :::           | 
-''Sensor GND'' <-> ''GND'' \\ +| :::        I2CM E  |  25(SCL  GPIOZ_11    i2cm_e            |  4           |  /dev/i2c-4   | 
-''Sensor VCC'' <-> ''5V'' or ''3.3V'' %%//%%depends on your sensor \\ +| :::       | :::      |  26(SDA)  |  GPIOZ_12   | :::                | :::          | :::           |
-**I2C 4** \\ +
-''Sensor SCL'' <-> ''I2C1_SCK'' (PIN 25\\ +
-''Sensor SDA'' <-> ''I2C1_SDA'' (PIN 26\\ +
-''Sensor GND'' <-> ''GND'' \\ +
-''Sensor VCC'' <-> ''5V'' or ''3.3V'' %%//%%depends on your sensor +
-<tabbox VIM4> +
-**I2C A** \\ +
-''Sensor SCL'' <-> ''I2CA_SCK'' (PIN 25\\ +
-''Sensor SDA'' <-> ''I2CA_SDA'' (PIN 26\\ +
-''Sensor GND'' <-> ''GND'' \\ +
-''Sensor VCC'' <-> ''5V'' or ''3.3V'' %%//%%depends on your sensor \\ +
-**I2C F** \\ +
-''Sensor SCL'' <-> ''I2CF_SCK'' (PIN 22\\ +
-''Sensor SDA'' <-> ''I2CF_SDA'' (PIN 23\\ +
-''Sensor GND'' <-> ''GND'' \\ +
-''Sensor VCC'' <-> ''5V'' or ''3.3V'' %%//%%depends on your sensor +
-</tabbox>+
  
-===== Device Detection =====+===== Enable I2C =====
  
-<tabbox VIM1> +In order to use the I2C, you need to enable the I2C function via [[products:sbc:common:configurations:device-tree-overlay|Device Tree Overlay]]. 
-Detect device on **I2C A**: + 
-```sh +<tabbox VIM1/2/3/3L/Edge1> 
-$ sudo i2cdetect --r 1 + 
-     1     5  6  7  8  9  a  b  c  d  e  f +Edit ''/boot/env.txt'' to add the i2c node to ''overlays'' node if it doesn't exist. 
-00:          -- -- -- -- -- -- -- -- -- -- -- -- -- + 
-10: -- -- -- -- -- -- -- -- -- -- -- -- -- 1d -- -- +Take VIM3 as an example to enable ''I2CM 3'', you need to add ''i2c3'' node to ''overlays'' node if it doesn't exist. 
-20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- + 
-30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +```shell 
-40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +overlays=uart4 pwm_ao_a pwm_f i2c3
-50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
-60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
-70: -- -- -- -- -- -- -- --+
 ``` ```
-Detect device on **I2C B**: + 
-```sh +After reboot, you will see the I2C device node. 
-sudo i2cdetect -y -r 2 + 
-      1  2    5  6  7  8  9  a  b  c  d  e  f +```shell 
-00:          -- -- -- -- -- -- -- -- -- -- -- -- -- +ls /dev/i2c-3 
-10: -- -- -- -- -- -- -- -- UU -- -- -- -- -- -- -- +/dev/i2c-3
-20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
-30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
-40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
-50: -- 51 -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
-60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
-70: -- -- -- -- -- -- -- --+
 ``` ```
-<tabbox VIM2+ 
-Detect device on **I2C A**: +<tabbox VIM3 with 5.15 kernel
-```sh + 
-$ sudo i2cdetect -y -r 1 +Edit ''/boot/dtb/amlogic/kvim3.dtb.overlay.env'' to add i2c node to ''fdt_overlays'' node if it doesn't exist. 
-     1  2    5  6  7  8  9  a  b  c  d  e  f + 
-00:          -- -- -- -- -- -- -- -- -- -- -- -- -- + 
-10: -- -- -- -- -- -- -- -- -- -- -- -- -- 1d -- -- +e.g. To enable ''I2CM 3'', you need to add ''i2c3'' to node ''fdt_overlays'' if it doesn't exist. 
-20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- + 
-30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +```shell 
-40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +fdt_overlays=i2c3 
-50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
-60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
-70: -- -- -- -- -- -- -- --+
 ``` ```
-Detect device on **I2C B**: + 
-```sh +After reboot, you will see the I2C device node. 
-$ sudo i2cdetect -y -r 2 + 
-      1  2  3  4    7  8  9  a  b  c  d  e  f +<tabbox VIM3L with 5.15 kernel> 
-00:          -- -- -- -- -- -- -- -- -- -- -- -- -- + 
-10: -- -- -- -- -- -- -- -- UU -- -- -- -- -- -- -- +Edit ''/boot/dtb/amlogic/kvim3l.dtb.overlay.env'' to add i2c node to ''fdt_overlays'' node if it doesn't exist. 
-20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- + 
-30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- + 
-40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +e.g. To enable ''I2CM 3'', you need to add ''i2c3'' to node ''fdt_overlays'' if it doesn't exist. 
-50: -- 51 -- -- -- -- -- -- -- -- -- -- -- -- -- -- + 
-60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +```shell 
-70: -- -- -- -- -- -- -- --+fdt_overlays=i2c3 
 ``` ```
-<tabbox VIM3/3L> + 
-Detect device on **I2C 3**: +After reboot, you will see the I2C device node. 
-```sh + 
-$ sudo i2cdetect -y -r 3 +<tabbox VIM4> 
-     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f + 
-00:          -- -- -- -- -- -- -- -- -- -- -- -- -- +Edit ''/boot/dtb/amlogic/kvim4.dtb.overlay.env'' to add i2c node to ''fdt_overlays'' node if it doesn't exist. 
-10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- + 
-20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- + 
-30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +e.g. To enable ''I2CM A'', you need to add ''i2cm_a'' to node ''fdt_overlays'' if it doesn't exist. 
-40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- + 
-50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +```shell 
-60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +fdt_overlays=i2cm_a 
-70: -- -- -- -- -- -- -- --+
 ``` ```
-Detect device on **I2C 4**: + 
-```sh +After reboot, you will see the I2C device node. 
-sudo i2cdetect -y -r 4 + 
-       2  3  4  5  6  7  8  9  a  b  c  d  e  f +```shell 
-00:          -- -- -- -- -- -- -- -- -- -- -- 0e -- +ls /dev/i2c-0 
-10: -- -- -- -- -- -- -- -- UU -- -- -- -- -- -- -- +/dev/i2c-0
-20: UU -- 22 -- -- -- -- -- -- -- -- -- -- -- -- -- +
-30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
-40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
-50: -- UU -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
-60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
-70: -- -- -- -- -- -- -- --+
 ``` ```
-<tabbox VIM4+ 
-Detect device on **I2C A**: +<tabbox VIM1S
-```sh + 
-$ sudo i2cdetect -r -y 0 +Edit ''/boot/dtb/amlogic/kvim1s.dtb.overlay.env'' to add i2c node to ''fdt_overlays'' node if it doesn't exist. 
-     0  1  2  3  4  5  6  7  8  9  a  b  c  d   f + 
-00:          -- -- -- -- -- -- -- -- -- -- -- -- -- + 
-10: UU -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +e.g. To enable ''I2CM E'', you need to add ''i2cm_e'' to node ''fdt_overlays'' if it doesn't exist. 
-20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- + 
-30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +```shell 
-40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +fdt_overlays=i2cm_e 
-50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
-60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
-70: -- -- -- -- -- -- -- --+
 ``` ```
-Detect device on **I2C F**: + 
-```sh +After reboot, you will see the I2C device node. 
-sudo i2cdetect -r -y 5 + 
-      1  2  3    6  7  8  9  a  b  c  d  e  f +```shell 
-00:          -- -- -- -- -- -- -- -- -- -- -- -- -- +ls /dev/i2c-4 
-10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +/dev/i2c-4
-20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
-30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
-40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
-50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
-60: -- -- UU -- -- -- -- -- -- -- -- -- -- -- -- -- +
-70: -- -- -- -- -- -- -- --+
 ``` ```
 +
 </tabbox> </tabbox>
-If you have made a connection, you will see the device address, e.g: ''0x1d'',''0x18'' and ''0x51''. 
  
-===== Read Registers from Device =====+===== I2C Usage =====
  
-Read register ''0x0d'' of device ''0x1d'' on **I2C A**+Please check the [[https://manpages.ubuntu.com/manpages/jammy/en/man8/i2cdetect.8.html|I2C Usage]]
-```sh + 
-$ sudo i2cget -f -y 1 0x1d 0x0d + 
-0x2a +===== Disable I2C to Use GPIO ===== 
-``` + 
-For more information, please refer to the help messages.+If you want to use normal GPIO instead of I2C, you can remove the I2C node in [[products:sbc:common:configurations:device-tree-overlay|Device Tree Overlay]]
Last modified: 2022/07/04 03:14 by frank