LoginSignup
15
17

More than 5 years have passed since last update.

Digispark(ATTiny85) をI2CのSlaveで使用(TinyWireS)して、RaspberryPi(Master)と通信してみる

Last updated at Posted at 2016-01-11

概要

  • TinyWireSを使用して、DigisparkをI2CのSlaveとして使用する
    • => TinyWireSのインストール、サンプルスケッチ
  • Raspberry Pi 2(I2C Master)と接続して、通信テストする。
    • => Raspbian Jessie Lite(2015-11-21)ベースで、I2Cを使う設定とツールi2c-toolsのインストール
  • #ラズパイにADCがない! => 手元にあるATTiny85のADCの値をI2Cで受け渡せば... ってのが動機。

    ATTiny85-I2C.JPG

環境

ATTiny85

TinyWireSライブラリの準備

  1. https://github.com/rambo/TinyWire から、 gitか、[Download ZIP]でファイルを取得する(ZIPは解凍する)
  2. Arduinoソフトウェアを起動
  3. スケッチ-> Include Library -> Add ZIP Library...を選択
  4. ファイル選択ダイアログから、TinyWireSのフォルダを選択

サンプルコード

  • attiny85_i2c_slave.inoをベースにちょっと変更したもの
  • Slaveアドレスは 0x5A
  • 4バイトのデータを保持。
  • それぞれ0x00,0x01,0x02,0x03のデータアドレスから設定・取得 default = {0xDE, 0xAD, 0xBE, 0xEF}

#define I2C_SLAVE_ADDRESS 0x5A // the 7-bit address
#define LED_PIN         1      // DigiSpark LED pin

#include "TinyWireS.h"

#ifndef TWI_RX_BUFFER_SIZE
#define TWI_RX_BUFFER_SIZE ( 16 )
#endif

volatile uint8_t i2c_regs[] =
{
    0xDE, 
    0xAD, 
    0xBE, 
    0xEF, 
};

volatile byte reg_position;
const byte reg_size = sizeof(i2c_regs);

void requestEvent()
{  
    reg_position %= reg_size;
    TinyWireS.send(i2c_regs[reg_position]);
    reg_position++;
}

void receiveEvent(uint8_t howMany)
{
    // Sanity check
    if ((howMany < 1) || (howMany > TWI_RX_BUFFER_SIZE)) return;
    
    reg_position = TinyWireS.receive();
    howMany--;

    while(howMany--)
    {
        reg_position %= reg_size;
        i2c_regs[reg_position] = TinyWireS.receive();
        reg_position++;
    }
}

void setup(){
    pinMode(LED_PIN,OUTPUT);
    mt08Blink(LED_PIN, 2);               //開始時LED点滅

    TinyWireS.begin(I2C_SLAVE_ADDRESS);
    TinyWireS.onReceive(receiveEvent);
    TinyWireS.onRequest(requestEvent);
}

void loop()
{
    TinyWireS_stop_check();
}

void mt08Blink(byte led, byte times)
{
  times *= 2;
  while(times > 0) {
    digitalWrite(led,(times & 0x01) ? LOW : HIGH);
    delay (200);
    times--;
  }
}

Raspberry Pi 2

  • Raspbian: 2015-11-21-raspbian-jessie-lite
  • i2cの設定

初期設定

  1. microSDにJessie Liteのイメージをいれて、起動
  2. sudo raspi-config で設定
    • 1 Expand Filesystem
    • 5 Internationalisation Options
      • I2 Change Timezone
    • 9 Advanced Options
      • A2 Hostname
      • A7 I2C => <Yes> => <OK> => <Yes> (I2Cを使えるようにする)
    • リブート => Yes
  3. i2c-toolsのインストール

    sudo apt-get update
    sudo apt-get install i2c-tools
    
  4. WiFi設定(必要あれば)

    % sudo iwlist wlan0 scan
    % sudo su
    % wpa_passphrase SSID 'KEY' >> /etc/wpa_supplicant/wpa_supplicant.conf
    ###
    # Wifiの素のパスワードを消す
    sudo vi /etc/wpa_supplicant/wpa_supplicant.conf
    
  5. swap無効(必要あれば)

    sudo dphys-swapfile uninstall
    sudo dpkg --purge dphys-swapfile
    sudo rm -f /var/swap
    
  6. リブート (% sudo reboot)

接続

ATTiny85-I2C.JPG

ATTiny85 . RaspberryPi
P0(SDA) 330Ω (適当な抵抗) 3番ピンSDA
P2(SCL) 330Ω (適当な抵抗) 5番ピンSCL
5V -- 2または4番ピン(5V)
GND -- 6または9番ピン(GND)

テスト

  • i2cdetect : 接続されているSlaveのリスト
  • i2cget : データの読み出し
  • i2cset : データの書き込み

    pi@raspberrypi:~ $ i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
    00:          -- -- -- -- -- -- -- -- -- -- -- -- --
    10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    50: -- -- -- -- -- -- -- -- -- -- 5a -- -- -- -- --
    60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    70: -- -- -- -- -- -- -- --
    pi@raspberrypi:~$ i2cget -y 1 0x5a 0x00
    0xde
    pi@raspberrypi:~$ i2cget -y 1 0x5a 0x01
    0xad
    pi@raspberrypi:~$ i2cget -y 1 0x5a 0x02
    0xbe
    pi@raspberrypi:~$ i2cget -y 1 0x5a 0x03
    0xef
    pi@raspberrypi:~$ i2cget -y 1 0x5a 0x00 w
    0xadde
    pi@raspberrypi:~$ i2cget -y 1 0x5a 0x02 w
    0xefbe
    pi@raspberrypi:~$ i2cget -y 1 0x5a 0x03 w
    0xdeef
    pi@raspberrypi:~ $ i2cset -y 1 0x5a 0x00 0x15
    pi@raspberrypi:~ $ i2cset -y 1 0x5a 0x01 0x26
    pi@raspberrypi:~ $ i2cset -y 1 0x5a 0x02 0x37
    pi@raspberrypi:~ $ i2cset -y 1 0x5a 0x03 0x48
    pi@raspberrypi:~ $ i2cget -y 1 0x5a 0x00
    0x15
    pi@raspberrypi:~ $ i2cget -y 1 0x5a 0x01
    0x26
    pi@raspberrypi:~ $ i2cget -y 1 0x5a 0x02
    0x37
    pi@raspberrypi:~ $ i2cget -y 1 0x5a 0x03
    0x48
    pi@raspberrypi:~ $
    

メモ

  • Raspberry PiのでI2CのDefaultのバススピードは、100000(100kHz)らしい。
  • 変更するには、以下のようにするらしい。
32kHzにバススピード変更
sudo rmmod i2c_bcm2708
sudo modprobe i2c_bcm2708 baudrate=32000
15
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
17