I2C実験 その2(プログラム作成)

I2Cアクセスのプログラムを作る。

 

今回使用したEEPROMはページアクセスすることも可能だが、今回はバイトアクセスのみ確認する。

 

1. リード

データシートによると、リードするには、

 1.アドレスをHigh,Lowに分けて書き込む

 2.データの読み込み

を行う必要がある。

そのとおりに、アプリケーションを実装する。

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/i2c-dev.h>

#define DEVICE "/dev/i2c-1"
#define SLAVE_ADDR (0x50)

int main(int argc, char **argv)
{
     int fd = 0;
     int ret = 0;
     unsigned char data;
     unsigned char address[2];

     if( argc != 3 ){
          fprintf(stderr, "usage: read [high address] [low address]\n");
          return 1;
     }

     fd = open( DEVICE, O_RDWR );
     if( fd < 0 ){
          fprintf(stderr, "file open error\n");
          return 1;
     }

     ret = ioctl( fd, I2C_SLAVE, SLAVE_ADDR );
     if( ret < 0 ){
          fprintf(stderr, "slave access error\n");
          close( fd );
          return 1;
     }

     // write address
     address[0] = (unsigned char)strtol(argv[1], NULL, 0); // address(high)
     address[1] = (unsigned char)strtol(argv[2], NULL, 0); // addrees(low)
     printf("read address = 0x%x%x\n", address[0], address[1]);
     ret = write( fd, address, 2 );
     if( ret != 2 ){
          fprintf(stderr, "write error\n");

          close( fd );
          return 1;
     }

     // read data
     ret = read( fd, &data, 1 );
     if( ret != 1 ){
          fprintf(stderr, "read error\n");
          close( fd );
          return 1;
     }

     printf("read data = 0x%x\n", data );

     close( fd );

     return 0;
}

 

実行結果

debian@beaglebone:~/eepromtest$ ./read 0x00 0x00
read address = 0x00
read data = 0x12

 

2. ライト

ライトするには、アドレス(High)、アドレス(Low)、データ、を一度に書き込む。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/i2c-dev.h>

#define DEVICE "/dev/i2c-1"
#define SLAVE_ADDR (0x50)

int main(int argc, char **argv)
{
     int fd = 0;
     int ret = 0;
     unsigned char data[3];

     if( argc != 4 ){
          fprintf(stderr, "usage: write [high address] [low address] [data]\n");
          return 1;
      }

     fd = open( DEVICE, O_RDWR );
     if( fd < 0 ){
          fprintf(stderr, "file open error\n");
          return 1;
     }

     ret = ioctl( fd, I2C_SLAVE, SLAVE_ADDR );
     if( ret < 0 ){
          fprintf(stderr, "slave access error\n");
          close( fd );
          return 1;
     }

     // write address
     data[0] = (unsigned char)strtol(argv[1], NULL, 0); // address(high)
     data[1] = (unsigned char)strtol(argv[2], NULL, 0); // addrees(low)
     data[2] = (unsigned char)strtol(argv[3], NULL, 0); // data
     printf( "write address = 0x%x%x data=0x%x\n", data[0], data[1], data[2]);
     ret = write( fd, data, 3 );
     if( ret != 3 ){
          fprintf(stderr, "write error\n");
          close( fd );
     return 1;
     }

     close( fd );

     return 0;
}

 

実行結果

debian@beaglebone:~/eepromtest$ ./write 0x00 0x00 0x12
write address = 0x00 data=0x12 

 

 

参考

Raspberry Pi でI2C: C言語プログラミング - 猫ぱーんち!