i2c-tools ツールセットのインストール、構成、使用方法を詳しく説明します。

i2c-tools ツールセットは、組み込みシステムまたは電子デバイスの I2C バスをデバッグおよびテストする必要がある場合に非常に便利なリソースです。i2c-tools は、I2C デバイスのスキャン、読み取り、書き込み、および I2C バス通信に関連するその他のタスクの実行に役立つ一連のコマンド ライン ツールを提供します。このブログでは、i2c-tools ツールセットのインストール、設定、使用方法を詳しく紹介します。

i2c ツールとは何ですか?

i2c-tools は、I2C バスと通信するためのオープン ソース コマンド ライン ツールのセットです。これらは、I2C デバイスとの対話、デバッグ、テストのために Linux オペレーティング システムで広く使用されています。一般的な i2c-tools ツールをいくつか示します。

  • i2cdetect: I2C バスをスキャンし、接続されているデバイスをリストするために使用されます。
  • i2cget: 指定された I2C デバイスのレジスタからデータを読み取るために使用されます。
  • i2cset: 指定した I2C デバイスのレジスタにデータを書き込むために使用されます。
  • i2cdump: I2C デバイスのレジスタ内容を 16 進形式で表示するために使用されます。
  • i2ctransfer: 複雑な I2C 転送操作を実行するために使用されます。

次に、これらのツールをインストールして使用する方法を詳しく説明します。

Usage: i2c-test <arguments>
Arguments:

*** I2C Functions ***
        -b <bus number>: Set the bus number for this transaction.  Defaults to 0
        -mm : Puts the device in Slave-Recieve mode and reponds to the message request
        -mmd : Puts the device in Slave-Recieve mode and show the receive data
        --sethost <addr>:       Set the host slave address
        --gethost:      Get the current host slave address for the specified bus
        --reset:        Reset the I2C controller
        --sysreset:     Emergency Reset the I2C controller Module
        --setspeed <speed>:     Set the bus speed
                        (Supported speed modes are: Standard - 0x64(100Kbits/sec), Fast - 0x190(400Kbits/sec)
        --scan:         Scan the I2C bus and show the slave addresses
                        that respond
        -s slave:       Communicate with the specified slave address in 7-bit format (in
                        hexadecimal)
                        Defaults to 0x5a
        -d <bytes>:     Send any number of data bytes to the specified slave.
                        Separate hexadecimal data bytes with spaces.  If this
                        flag is used, it must be the last one on the
                        command line.
                        EG: i2c-test -m 1 -d 0x00 0x01 0x02 0x03
        -rc count:      Read the specified number of bytes.  Defaults to 1
        -r:             Just read from the specified address, don't do a write.
        -w:             Just write to the specified address, don't do a read.
        -m mode:        Send and receive in the specified mode:
                        0: Write a data byte, then receive a data byte in
                           separate operations
                        1: Combined write and read using repeated start
                        Default mode is 0
        -f:             Repeat the specified test forever (stress test mode)
        -sbd:           Don't display any output unless there is an error
        --delay:        Specify the delay in milliseconds between tests in run forever mode
        --getrecinfo:   Get the current recovery info

i2c ツールをインストールする

i2c-tools は、ほとんどの Linux ディストリビューションのパッケージ マネージャーを介して簡単にインストールできます。i2c-tools をインストールするための一般的なパッケージ マネージャー コマンドをいくつか示します。

Debian/Ubuntu の場合:

sudo apt-get install i2c-tools

Fedora の場合:

sudo dnf install i2c-tools

Arch Linux の場合:

sudo pacman -S i2c-tools

I2Cバスを構成する

i2c-tools を使用する前に、I2C バスが適切に構成され有効になっていることを確認する必要があります。通常、I2C バスの構成ファイルは/boot/config.txt(Raspberry Pi の場合) または/etc/modules(一部の Linux システムの場合) にあります。詳細な構成手順については、システムのマニュアルを確認してください。

i2c ツールを使用する

i2c-tools をインストールして構成したら、それらを使用して I2C デバイスと対話できるようになります。いくつかの使用例を次に示します。

1. I2C バスをスキャンします

このコマンドを使用してi2cdetect、I2C バス上のデバイスをスキャンし、そのアドレスをリストします。たとえば、I2C-1 バスをスキャンするには、次のコマンドを実行します。

sudo i2cdetect -y 1

2. I2C デバイスレジスタを読み取ります。

コマンドを使用して、i2cget指定した I2C デバイスのレジスタからデータを読み取ります。たとえば、I2C デバイス 0x50 のレジスタ 0x10 から 1 バイトのデータを読み取るには、次のコマンドを実行します。

sudo i2cget -y 1 0x50 0x10

3. I2C デバイスレジスタへの書き込み

コマンドを使用して、i2cset指定した I2C デバイスのレジスタにデータを書き込みます。たとえば、値 0x42 を I2C デバイス 0x51 のレジスタ 0x20 に書き込むには、次のコマンドを実行します。

sudo i2cset -y 1 0x51 0x20 0x42

4. I2Cデバイスレジスタの内容を表示

このコマンドを使用してi2cdump、I2C デバイスのレジスタ内容を 16 進形式で表示します。たとえば、I2C デバイス 0x52 のレジスタ内容を表示するには、次のコマンドを実行します。

sudo i2cdump -y 1 0x52

i2ctransferこれらは i2c ツールの基本的な使用例の一部にすぎません。このツールセットは、複雑な I2C 転送操作の実行など、より高度な機能も提供します。ターミナルでコマンドを実行すると、man i2c-tools詳細なヘルプ ドキュメントを表示できます。

結論は

i2c-tools を使用すると、複雑なコードを記述することなく、I2C デバイスの操作、デバッグ、テストを簡単に行うことができます。このブログでは、i2c-tools のインストール、設定、基本的な使用法を紹介しました。これが、I2C バス通信とデバイス管理をより簡単に扱うのに役立つことを願っています。さらに詳しい情報が必要な場合は、i2c-tools の公式ドキュメントまたはオンライン コミュニティを参照してください。

おすすめ

転載: blog.csdn.net/qq_37037348/article/details/132698708