OrangePi-PCを使ってみる

2つのポートを使ったi2c通信

OrangePi-PC のピンヘッダーには SDA.0/SCL.0 と SDA.1/SCL.1 の2つのi2cポートがあります。
/dev/i2c-0 はPin#3/#5を使います。
/dev/i2c-1 はPin#27/#28を使います。
mcp23017をPin#27/#28につないで、ic2detect してみます。


i2c-bus1で正しく認識ました。
WiringOPライブラリ(OrangePi用のWiringライブラリ)では、WiringPiとの互換性を保つために、同じピン位置の ic2-bus0(SDA.0/SCL.0) しかサポートしていません。
i2c-bus1(SDA.1/SCL.1)を使う場合、以下の様に直接デバイスファイルをオープンする必要があります。
    if ((fd = open (device, O_RDWR)) < 0) {
      printf("open Error\n");
      return 1;
    }
    if (ioctl (fd, I2C_SLAVE, ADDRESS) < 0) {
      printf("open Error\n");
      return 1;
    }

こちらのソースを一部変更し、ic2- bus0(SDA.0/SCL.0) と i2c-bus1(SDA.1/SCL.1) のどちらでも使えるようにしてみました。
起動時に引数を指定すると i2c-bus1(SDA.1/SCL.1)をオープンします。
赤字の部分が変更した部分です。
//
// test program for MCP23017 with wiringPi
// cc -o mcp23017Led mcp23017Led.c -l wiringPi
//
#include <stdio.h>
#include <time.h>
#include <wiringPi.h>
#include <wiringPiI2C.h>
#include <fcntl.h>
#include <sys/ioctl.h>

#define IODIRA          0x00            // MCP23017 address of I/O direction
#define IODIRB          0x01            // MCP23017 address of I/O direction
#define GPIOA           0x12            // MCP23017 address of GP Value
#define GPIOB           0x13            // MCP23017 address of GP Value
#define ADDRESS         0x20            // MCP23017 I2C address

#define I2C_SLAVE       0x0703

int main(int argc, char **argv) {
  int i;
  int fd;
  int byte;
  char *device0="/dev/i2c-0";
  char *device1="/dev/i2c-1";


//    if ((fd=wiringPiI2CSetup(ADDRESS)) == -1){
//      printf("wiringPiI2CSetup Error\n");
//      return 1;
//    }

  if (argc == 1) {
    printf("using /dev/i2c-0\n");
    if ((fd = open (device0, O_RDWR)) < 0) {
      printf("open Error\n");
      return 1;
    }
    if (ioctl (fd, I2C_SLAVE, ADDRESS) < 0) {
      printf("open Error\n");
      return 1;
    }
  } else {
    printf("using /dev/i2c-1\n");
    if ((fd = open (device1, O_RDWR)) < 0) {
      printf("open Error\n");
      return 1;
    }
    if (ioctl (fd, I2C_SLAVE, ADDRESS) < 0) {
      printf("open Error\n");
      return 1;
    }
  }

  wiringPiI2CWriteReg16(fd,IODIRA,0x00);
  wiringPiI2CWriteReg16(fd,IODIRB,0x00);
  wiringPiI2CWriteReg16(fd,GPIOA,0x00);
  wiringPiI2CWriteReg16(fd,GPIOB,0x00);

  byte=0;
  for(i=0;i<8;i++) {
    byte=(byte<<1) + 1;
    wiringPiI2CWriteReg16(fd,GPIOA,byte);
    delay(1000);
  }
  delay(2000);

  for(i=0;i<10;i++) {
    byte=0;
    wiringPiI2CWriteReg16(fd,GPIOA,byte);
    delay(100);
    byte=0xff;
    wiringPiI2CWriteReg16(fd,GPIOA,byte);
    delay(100);
  }
  delay(2000);

  byte=0xff;
  for(i=0;i<8;i++) {
    byte=(byte>>1);
    wiringPiI2CWriteReg16(fd,GPIOA,byte);
    delay(1000);
  }
}

ic2-bus0(SDA.0/SCL.0) と i2c-bus1(SDA.1/SCL.1) のどちらでも正常に動作することを確認しました。

次回はi2cのEEPROMの使い方を紹介します。