kelly / node-i2c

Node.js native bindings for i2c-dev. Plays well with Raspberry Pi and Beaglebone.
Other
216 stars 91 forks source link

Returned data is incorrect #8

Closed jbaldwinroberts closed 11 years ago

jbaldwinroberts commented 11 years ago

Hi,

Thanks for this.

Im trying to get communications working between a raspberry pi and an mBed.

My raspberry pi code is:

var i2c = require('i2c');
var address = 0x08;
var wire = new i2c(address, {debug:false, device:'/dev/i2c-1'});

wire.readBytes(49, 10, function(err, res) {
    // result contains a buffer of bytes
    var counter = 0;
    for (counter=0; counter<res.length; counter++)
    console.log(res[counter] + "\r\n");
});

My mBed code is:

 #include <mbed.h>

 I2CSlave slave(p9, p10);

 int main() {
     char buf[10];
     char msg[] = "1234567890";

     slave.address(0x10);
     while (1) {
         int i = slave.receive();
         switch (i) {
             case I2CSlave::ReadAddressed:
                 slave.write(msg, strlen(msg) + 1); // Includes null char
                 printf("Read Addressed: %s\n\r", msg);
                 break;
             case I2CSlave::WriteGeneral:
                 slave.read(buf, 10);
                 printf("Read G: %s\n", buf);
                 break;
             case I2CSlave::WriteAddressed:
                 slave.read(buf, 10);
                 printf("Write Addressed: %s\n\r", buf);
                 break;
         }
         for(int i = 0; i < 10; i++) buf[i] = 0;    // Clear buffer
     }
}

For some reason my mBed shows on my raspberry pi as 0x08 even though its address is set to 0x10, although i dont think this matters?

When i run the raspberry pi code my mBed outputs:

Write Addressed: 1 // This is equal to 49 converted to ASCII Read Addressed: 1234567890

and my raspberry pi outputs:

test 60 23 0 0 164 163 148 190 172 23

These numbers don't match what I'm sending and don't change when i change what I'm sending.

Any ideas?

Thanks

Joe

kelly commented 11 years ago

Do you have pull-up resistors on the SDA and SCL?

jbaldwinroberts commented 11 years ago

I have tried both with and without and i get the same result. I don't think their needed as the pi has internal pull ups on the I2C lines.

Sending data works, just not receiving

Thanks

kelly commented 11 years ago

hmm, I'm not sure what's wrong. If I had an mbed device, i'd test it out, but a little difficult without one. I've had success reading from an Arduino and other devices.

Does anything change if you use readByte?

jbaldwinroberts commented 11 years ago

I have an Arduino, would you be able to post the code you tested it with and i'll see if it works. Thanks

jbaldwinroberts commented 11 years ago

Hi, so i tried readByte and it does not get into the callback, have I done something wrong?

var i2c = require('i2c');
var address = 0x08;
var wire = new i2c(address, {debug:false, device:'/dev/i2c-1'});

wire.readByte(function(err, res) {
console.log(res);
console.log("test");
});

Also my mBed is getting read twice when i run the above code not once?

Thanks

kelly commented 11 years ago

Here is a really basic example:

#include "Wire.h"

byte data[3] = {1, 2, 3};

void requestEvent()
{
  Serial.println('requested');
  Wire.write(data, 3);
}

void setup() {
Serial.begin(115200);
Wire.begin(4);
Wire.onRequest(requestEvent);  // register event
}

void loop() {
  delay(100);
}
Wire = require('i2c');

wire = new Wire(0x04, {
  device: '/dev/i2c-0'
});

//0x00 cmd isn't used, but required
wire.readBytes(0x00, 3, function(err, res) {
  return console.log(res);
});
ajitam commented 10 years ago

Hi, I'm having some problem with wire.readByteS to. wire.readByte works on but wire.readByteS never triggers requestEvent.

Any ideas?