忘備録-備忘録

技術的な備忘録

広告

※このエリアは、60日間投稿が無い場合に表示されます。記事を投稿すると、表示されなくなります。

I2C接続のEEPROMへのnode-redによる読み込み書き込み

2018-12-03 22:32:00 | raspberry ...

raspberry Pi に接続したI2C接続のEEPROMへの読み書きするためのフローです。node-redに用意されているnode-red-contrib-i2cノードはちょっとした工夫でROMへの書き込みはできるのですが、読み込み時にコマンド(ROMアドレス)を2byte出力できないためROMからデータを読み込むことができません。functionフローを使い読み書きできるサンプルを作ってみました。EEPROMはMicrochip社の24LC256を使っています

image

準備

ファイル ~/.node-red/settings.js の212行目くらいのfunctionGlobalContext:の項目に次の項目を追加します。

functionGlobalContext: {
        os:require('os'),
        i2c:require('i2c-bus')                       //この行を追加
        // jfive:require("johnny-five"),
        // j5board:require("johnny-five").Board({repl:false})
  },

フロー

[{"id":"c5a8c6f0.6e63d8","type":"inject","z":"2c774522.8b3eba","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":178,"y":507,"wires":[["f8d420fe.fcfc9"]]},{"id":"46bb3ae9.e37b64","type":"debug","z":"2c774522.8b3eba","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":761,"y":506,"wires":[]},{"id":"7e8ce1b2.d9dbf","type":"function","z":"2c774522.8b3eba","name":"EEPROM read","func":"i2c = global.get('i2c');\n\n//EEPROMのI2Cアドレス\nconst EEPROM_ADDR = 0x50;\n\n/*\n EEPROMの読み出し\n引数 読み出したいアドレス\n戻り値 ROMのデータ\n*/\nfunction readData(addr) {\n    var i2c1 = i2c.openSync(1);\n    var buf=Buffer.alloc(2);\n    buf[0] = addr >> 8;         // アドレス上位ビット\n    buf[1] = addr & 0xff;       // アドレス下位ビット\n    i2c1.i2cWriteSync(EEPROM_ADDR,buf.length,buf);\n    i2c1.i2cReadSync(EEPROM_ADDR,1,buf);\n    i2c1.closeSync();\n    return buf[0];\n }\n\n\n//データの読み出し\nif(isNaN(msg.address)) return msg;\ndata = readData(msg.address);\nmsg.payload = data;\nreturn msg;","outputs":1,"noerr":0,"x":564,"y":505,"wires":[["46bb3ae9.e37b64"]]},{"id":"1d0cbd0d.78f423","type":"inject","z":"2c774522.8b3eba","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":177,"y":582,"wires":[["891f2646.e0c408"]]},{"id":"5cbcf688.2808e8","type":"function","z":"2c774522.8b3eba","name":"EEPROM Write","func":"i2c = global.get('i2c');\n\n//EEPROMのI2Cアドレス\nconst EEPROM_ADDR = 0x50;\n\n/*\n EEPROMへの書き込み\n引数 addr 書き込みたいアドレス\n data 書き込みデータ\n戻り値 なし\n*/\nfunction writeData(addr,data) {\n    var i2c1 = i2c.openSync(1);\n    var buf=Buffer.alloc(3);\n    buf[0] = addr >> 8;         // アドレス上位ビット\n    buf[1] = addr & 0xff;       // アドレス下位ビット\n    buf[2] = data;           // 書き込むデータ\n    i2c1.i2cWriteSync(EEPROM_ADDR,buf.length,buf);\n    i2c1.closeSync();\n }\n\n//データの書き込み\nif(isNaN(msg.address)) return msg;\nwriteData(msg.address,msg.payload);\nreturn msg;","outputs":1,"noerr":0,"x":571,"y":583,"wires":[["cd1f3e4d.7b86"]]},{"id":"cd1f3e4d.7b86","type":"debug","z":"2c774522.8b3eba","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":760,"y":583,"wires":[]},{"id":"891f2646.e0c408","type":"function","z":"2c774522.8b3eba","name":"データセット","func":"msg.address=0x12;   //書き込みたいアドレス\nmsg.payload = 0x5A; //書き込むデータ\nreturn msg;","outputs":1,"noerr":0,"x":367,"y":583,"wires":[["5cbcf688.2808e8"]]},{"id":"f8d420fe.fcfc9","type":"function","z":"2c774522.8b3eba","name":"データセット","func":"msg.address=0x12;   //読み込みたいアドレス\nmsg.payload = 0;    //ダミーデータ\nreturn msg;","outputs":1,"noerr":0,"x":368,"y":506,"wires":[["7e8ce1b2.d9dbf"]]}]

複数バイトをまとめて書き込み読み込みしたい場合のフロー

[{"id":"4d15b25.401994c","type":"inject","z":"9290778d.492218","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":300,"wires":[["dc52161e.aa3db8"]]},{"id":"83c67111.4054","type":"debug","z":"9290778d.492218","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":783,"y":299,"wires":[]},{"id":"831a3808.9788e8","type":"function","z":"9290778d.492218","name":"EEPROM read","func":"i2c = global.get('i2c');\n\n//EEPROMのI2Cアドレス\nconst EEPROM_ADDR = 0x50;\n\n/*\n EEPROMの読み出し\n引数 読み出したいアドレス\n戻り値 ROMのデータ\n*/\nfunction readData(addr,datasize) {\n    var i2c1 = i2c.openSync(1);\n    var buf=Buffer(2);\n    var readdata=Buffer(datasize);\n    buf[0] = addr >> 8;         // アドレス上位ビット\n    buf[1] = addr & 0xff;       // アドレス下位ビット\n    i2c1.i2cWriteSync(EEPROM_ADDR,buf.length,buf);\n    i2c1.i2cReadSync(EEPROM_ADDR,datasize,readdata);\n    i2c1.closeSync();\n    return readdata;\n }\n\n\n//データの読み出し\nif(isNaN(msg.address)) return msg;\nvar data=[]; \ndata = readData(msg.address,msg.datasize);\nmsg.payload = data \nreturn msg;","outputs":1,"noerr":0,"x":586,"y":298,"wires":[["83c67111.4054"]]},{"id":"b9a3c773.457878","type":"inject","z":"9290778d.492218","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":199,"y":375,"wires":[["944d2699.e859c8"]]},{"id":"9faf074a.8e1b78","type":"function","z":"9290778d.492218","name":"EEPROM Write","func":"i2c = global.get('i2c');\n\n//EEPROMのI2Cアドレス\nconst EEPROM_ADDR = 0x50;\n\n/*\n EEPROMへの書き込み\n引数 addr 書き込みたいアドレス\n data 書き込みデータ\n戻り値 なし\n*/\nfunction writeData(addr,data) {\n    var i2c1 = i2c.openSync(1);\n    var buf=Buffer(data.length+2);\n    var bufdata = Buffer(data);\n    buf[0] = addr >> 8;         // アドレス上位ビット\n    buf[1] = addr & 0xff;       // アドレス下位ビット\n    bufdata.copy(buf,2);           // 書き込むデータ\n    i2c1.i2cWriteSync(EEPROM_ADDR,buf.length,buf);\n    i2c1.closeSync();\n    msg.payload = buf;\n }\n\n//データの書き込み\nif(isNaN(msg.address)) return msg;\nwriteData(msg.address,msg.payload);\nreturn msg;","outputs":1,"noerr":0,"x":593,"y":376,"wires":[["475c1ec3.5a3f9"]]},{"id":"475c1ec3.5a3f9","type":"debug","z":"9290778d.492218","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":782,"y":376,"wires":[]},{"id":"944d2699.e859c8","type":"function","z":"9290778d.492218","name":"データセット","func":"msg.address=0x00;   //書き込みたいアドレス\nmsg.payload = [0xff,1,2,3,4,5,6,7,8,9,10,0xff]; //書き込むデータ\n//msg.datasize = msg.payload.length;    //データ数 書き込みには使用しない\nreturn msg;","outputs":1,"noerr":0,"x":389,"y":376,"wires":[["9faf074a.8e1b78"]]},{"id":"dc52161e.aa3db8","type":"function","z":"9290778d.492218","name":"データセット","func":"msg.address=0x00;   //読み込みたいアドレス\nmsg.datasize = 12;    //読み込みたいデータ数\nmsg.payload = 0;    //ダミーデータ\nreturn msg;","outputs":1,"noerr":0,"x":390,"y":299,"wires":[["831a3808.9788e8"]]}]
コメント

I2C接続のEEPROMへの読み込み書き込み

2018-12-03 20:21:00 | raspberry ...

raspberry Piに取り付けたI2C接続のEEPROMを読み書きするJavaScriptのプログラムです。

ライブラリのインストール

ローカルにライブラリをインストール

$ npm install i2c-bus

グローバルにライブラリをインストール

$ sudo npm –g install i2c-bus

JavaScriptコード

const i2c = require('i2c-bus');

//EEPROMのアドレス
const EEPROM_ADDR = 0x50;

/*
EEPROMの読み出し
引数 読み出したいアドレス
戻り値 ROMのデータ
*/
function readData(addr) {
    var i2c1 = i2c.openSync(1);
    var buf=Buffer.alloc(2);
    buf[0] = addr >> 8;         // アドレス上位ビット
    buf[1] = addr & 0xff;       // アドレス下位ビット
    i2c1.i2cWriteSync(EEPROM_ADDR,buf.length,buf);
    i2c1.i2cReadSync(EEPROM_ADDR,1,buf);

   i2c1.closeSync();
   return buf[0];
};

/*
EEPROMへの書き込み
引数 addr 書き込みたいアドレス
data 書き込みデータ
戻り値 なし
*/
function writeData(addr,data) {
    var i2c1 = i2c.openSync(1);
    var buf=Buffer.alloc(3);
    buf[0] = addr >> 8;         // アドレス上位ビット
    buf[1] = addr & 0xff;       // アドレス下位ビット
    buf[2] = data;           // 書き込むデータ
    i2c1.i2cWriteSync(EEPROM_ADDR,buf.length,buf);

    i2c1.closeSync();
};

//データの書き込み
writeData(0,0xAA);

//データの読み出し
data = readData(0);
console.log("read data "+data.toString(16));

参考

コメント