================================== これは、 linux-2.6.13/Documentation/i2c/dev-interface の和訳(ドラフト) です。 翻訳団体: JF プロジェクト < http://www.linux.or.jp/JF/ > 更新日 : 2005/9/2 翻訳者 : Hiroshi.Suzuki < setter at reset dot jp > 校正者 : ================================== Usually, i2c devices are controlled by a kernel driver. But it is also possible to access all devices on an adapter from userspace, through the /dev interface. You need to load module i2c-dev for this. 通常、I2C デバイスは、カーネルドライバが制御します。しかし、アダプタ上の すべてのデバイスは、/dev インタフェース経由で、ユーザ空間からもアクセスできます。 この機能を使うには、i2c-dev モジュールを組み込む必要があります。 Each registered i2c adapter gets a number, counting from 0. You can examine /sys/class/i2c-dev/ to see what number corresponds to which adapter. I2C device files are character device files with major device number 89 and a minor device number corresponding to the number assigned as explained above. They should be called "i2c-%d" (i2c-0, i2c-1, ..., i2c-10, ...). All 256 minor device numbers are reserved for i2c. 登録された、それぞれの i2c アダプタには、0 からはじまる番号が付けられます。 番号とアダプタの関連づけは、/sys/class/i2c-dev/ をみることで知ることができます。 I2C デバイスファイルは、メジャーデバイス番号 89 のキャラクタデバイスで、 マイナーデバイス番号は、上述のような番号が割り当てられます。 それらは、"i2c-%d" (i2c-0, i2c-1, ..., i2c-10, ...) のようになるはずです。 i2c のために、256 個のマイナーデバイス番号が予約されています。 C example C 言語でのコーディング例 ========= So let's say you want to access an i2c adapter from a C program. The first thing to do is "#include ". Please note that there are two files named "i2c-dev.h" out there, one is distributed with the Linux kernel and is meant to be included from kernel driver code, the other one is distributed with lm_sensors and is meant to be included from user-space programs. You obviously want the second one here. C プログラムから i2c アダプタにアクセスする必要があるなら、 最初にすることは、"#include " です。 ファイル "i2c-dev.h" が、同じ名前で 2つ存在することに注意してください。 ひとつは、Linux カーネルで配布され、それは、カーネルドライバコード用に 作られたもので、もうひとつは、lm_sensors で配布され、ユーザ空間プログラム用 に作られたものです。明らかに、2番目のものが必要です。 Now, you have to decide which adapter you want to access. You should inspect /sys/class/i2c-dev/ to decide this. Adapter numbers are assigned somewhat dynamically, so you can not even assume /dev/i2c-0 is the first adapter. アクセスしたいアダプタを決めようとするなら、アダプタを決定するために、 /sys/class/i2c-dev/ を調査しなければなりません。 アダプタ番号は、ある程度動的に割り当てられるので、/dev/i2c-0 が常に、最初のアダ プタになるとはかぎりません。 Next thing, open the device file, as follows: 次に、デバイスファイルを開きます: int file; int adapter_nr = 2; /* probably dynamically determined */ /* 通常、動的に決定されます */ char filename[20]; sprintf(filename,"/dev/i2c-%d",adapter_nr); if ((file = open(filename,O_RDWR)) < 0) { /* ERROR HANDLING; you can check errno to see what went wrong */ /* エラーハンドリング; エラー番号を検査することで、何が悪いかを見つけられます */ exit(1); } When you have opened the device, you must specify with what device address you want to communicate: デバイスを開いた後で、通信したいデバイスアドレスを指定しなければなりません: int addr = 0x40; /* The I2C address */ /* I2C アドレス */ if (ioctl(file,I2C_SLAVE,addr) < 0) { /* ERROR HANDLING; you can check errno to see what went wrong */ /* エラーハンドリング; エラー番号を検査することで、何が悪いかを見つけられます */ exit(1); } Well, you are all set up now. You can now use SMBus commands or plain I2C to communicate with your device. SMBus commands are preferred if the device supports them. Both are illustrated below. すべての準備ができました。すでに、SMBus コマンドや、素のI2Cを使って、デバイスと 通信できるようになっています。 デバイスが、SMBus コマンドをサポートするなら、それを使います。 それぞれを、以下に示します。 __u8 register = 0x10; /* Device register to access */ /* アクセスするデバイスレジスタ */ __s32 res; char buf[10]; /* Using SMBus commands */ /* SMBus コマンドを使う */ res = i2c_smbus_read_word_data(file,register); if (res < 0) { /* ERROR HANDLING: i2c transaction failed */ /* エラーハンドリング: i2c 処理失敗 */ } else { /* res contains the read word */ /* 変数 res は読み込んだワードを格納しています */ } /* Using I2C Write, equivalent of i2c_smbus_write_word_data(file,register,0x6543) */ /* i2c_smbus_write_word_data(file,register,0x6543) と同等の、I2C 書き込みを使う */ buf[0] = register; buf[1] = 0x43; buf[2] = 0x65; if ( write(file,buf,3) != 3) { /* ERROR HANDLING: i2c transaction failed */ /* エラーハンドリング: i2c 処理失敗 */ } /* Using I2C Read, equivalent of i2c_smbus_read_byte(file) */ /* i2c_smbus_read_byte(file) と同等の、I2C 読み込みを使う */ if (read(file,buf,1) != 1) { /* ERROR HANDLING: i2c transaction failed */ /* エラーハンドリング: i2c 処理失敗 */ } else { /* buf[0] contains the read byte */ /* 変数 buf[0] は読み込んだバイトを格納しています */ } IMPORTANT: because of the use of inline functions, you *have* to use '-O' or some variation when you compile your program! 重要事項: インラインファンクションを使うなら、プログラムコンパイル時に、 '-O' または、いくつかの組合せを試さなければなりません! Full interface description インタフェースの詳細説明 ========================== The following IOCTLs are defined and fully supported (see also i2c-dev.h): 次に示す IOCTL が定義され、完全にサポートされます (i2c-dev.h もみてください): ioctl(file,I2C_SLAVE,long addr) Change slave address. The address is passed in the 7 lower bits of the argument (except for 10 bit addresses, passed in the 10 lower bits in this case). スレーブアドレスを変更します。引数の下位 7ビットがアドレスとして渡されます (例外として、10ビットアドレスでは、(この場合) 下位 10ビットが渡されます)。 ioctl(file,I2C_TENBIT,long select) Selects ten bit addresses if select not equals 0, selects normal 7 bit addresses if select equals 0. Default 0. select が 0 以外なら、10ビットアドレスが選択され、select が 0 なら、7ビット アドレスが選択されます。初期値は 0 です。 ioctl(file,I2C_PEC,long select) Selects SMBus PEC (packet error checking) generation and verification if select not equals 0, disables if select equals 0. Default 0. Used only for SMBus transactions. select が 0 以外なら、SMBus PEC (パケットエラー検査) の生成と検証が有効にな り、select が 0 なら、無効になります。初期値は 0 です。 SMBus 処理専用です。 ioctl(file,I2C_FUNCS,unsigned long *funcs) Gets the adapter functionality and puts it in *funcs. アダプタの機能一覧を取り込み、*func に格納します。 ioctl(file,I2C_RDWR,struct i2c_rdwr_ioctl_data *msgset) Do combined read/write transaction without stop in between. 読み込み/書き込みの連結処理 (読み込み/書き込みの間で停止せずに) を行います。 The argument is a pointer to a struct i2c_rdwr_ioctl_data { struct i2c_msg *msgs; /* ptr to array of simple messages */ int nmsgs; /* number of messages to exchange */ } 引数は、構造体 i2c_rdwr_ioctl_data { struct i2c_msg *msgs; /* 単純メッセージ配列へのポインタ */ int nmsgs; /* 交換されるメッセージ数 */ } へのポインタです。 The msgs[] themselves contain further pointers into data buffers. The function will write or read data to or from that buffers depending on whether the I2C_M_RD flag is set in a particular message or not. The slave address and whether to use ten bit address mode has to be set in each message, overriding the values set with the above ioctl's. msgs[] は、データバッファへのさらなるポインタを自身に格納しています。 関数は、各メッセージに、I2C_M_RD フラグがセットされているかにより、バッファに 対して読み込むか、書き込むかを選択し、実行します。 スレーブアドレスと、10ビットアドレスモードのどちらを使うか、各メッセージに設 定 (上記 ioctl で上書きされた値) しなければなりません。 Other values are NOT supported at this moment, except for I2C_SMBUS, which you should never directly call; instead, use the access functions below. ほかの値は、現時点では、I2C_SMBUS を除いて、サポートされて*いません*。 直接 I2C_SMBUS を呼び出してはいけません; 代わりに、次に示すアクセス関数を 使います。 You can do plain i2c transactions by using read(2) and write(2) calls. You do not need to pass the address byte; instead, set it through ioctl I2C_SLAVE before you try to access the device. read(2) や、write(2) を呼び出せば、素の i2c 処理ができます。 アドレスバイトを渡す必要がないなら、代わりに、デバイスにアクセスしようとする前 に、ioctl I2C_SLAVE でアドレスバイトを設定します。 You can do SMBus level transactions (see documentation file smbus-protocol for details) through the following functions: SMBus 階層処理 (詳細は、ファイル smbus-protocol をみてください) は、次に示す関 数でできます: __s32 i2c_smbus_write_quick(int file, __u8 value); __s32 i2c_smbus_read_byte(int file); __s32 i2c_smbus_write_byte(int file, __u8 value); __s32 i2c_smbus_read_byte_data(int file, __u8 command); __s32 i2c_smbus_write_byte_data(int file, __u8 command, __u8 value); __s32 i2c_smbus_read_word_data(int file, __u8 command); __s32 i2c_smbus_write_word_data(int file, __u8 command, __u16 value); __s32 i2c_smbus_process_call(int file, __u8 command, __u16 value); __s32 i2c_smbus_read_block_data(int file, __u8 command, __u8 *values); __s32 i2c_smbus_write_block_data(int file, __u8 command, __u8 length, __u8 *values); All these transactions return -1 on failure; you can read errno to see what happened. The 'write' transactions return 0 on success; the 'read' transactions return the read value, except for read_block, which returns the number of values read. The block buffers need not be longer than 32 bytes. これらすべての関数は、処理が失敗すると -1 を返します; errno をみることで、 何が起きたかわかります。'write' (が関数名に含まれる) 処理は、成功すると 0 を返 します; 'read' (が関数名に含まれる) 処理は、read_block を除き、読み込んだ値を返 します。read_block 処理では、読み込んだ値の数を返します。ブロックバッファは、 32バイトより多くを必要としません。 The above functions are all macros, that resolve to calls to the i2c_smbus_access function, that on its turn calls a specific ioctl with the data in a specific format. Read the source code if you want to know what happens behind the screens. 上記の関数はすべてマクロです。マクロは、特定の形をしたデータで、指定した ioctl を呼び出すようにする、i2c_smbus_access 関数の呼び出しに変化します。 画面の裏で何が起きているか知りたいなら、ソースコードをみてください。