Go Down

Topic: Arduino "Wire" i2c not compatible with SMBus Block Read ... is it? (Read 3 times) previous topic - next topic

amundsen

The ic2master library won't work for me neither.

I have installed the library in Arduino.app/Contents/Resources/Java/Libraries/i2cmaster (I am on a Mac).
I have renamed twimaster.c as twimaster.cpp and test_i2cmaster.c as test_i2cmaster.cpp.
I have modified the F_CPU with value 16000000UL and the SCL_CLOCK with value 50000L in twimaster.cpp.
I have created a keywords file (see it at the bottom of this message) and put it within the library's folder.

However there is something wrong because the code doesn't run. Even my basic Serial.print() instructions are not executed.

What am I doing wrong ???


Here's the sketch:

Code: [Select]
/*

  i2cmaster library test with an AD7746
 
*/

#include <i2cmaster.h>

#define AD7746  0x48    // device address of AD7746 without 8th bit
unsigned char ret;


void setup() {   
                               
     Serial.begin(9600);
     Serial.println("Hello!"); // just to see if any code is executed
     i2c_init();   // initialize I2C library
}

void loop() {

     // read
     i2c_start_wait(AD7746+I2C_WRITE);     // set device address and write mode

     i2c_write(0x00);                        // read address = 0
     i2c_rep_start(AD7746+I2C_READ);       // set device address and read mode

     ret = i2c_readNak();        // read one byte
     i2c_stop();
     Serial.print("RET = ");
     Serial.println(ret);
}


Code: [Select]
#######################################
# Syntax Coloring Map For i2cmaster
#######################################

#######################################
# Datatypes (KEYWORD1)
#######################################

#######################################
# Methods and Functions (KEYWORD2)
#######################################

i2c_init KEYWORD2
i2c_start_wait KEYWORD2
i2c_write KEYWORD2
i2c_stop KEYWORD2
i2c_rep_start KEYWORD2
i2c_cursor KEYWORD2
i2c_readNak KEYWORD2
i2c_readAck KEYWORD2
i2c_read KEYWORD2

#######################################
# Constants (LITERAL1)
#######################################

I2C_WRITE LITERAL1
I2C_READ LITERAL1

amundsen

if I remove the test_i2cmaster.cpp (previously test_i2cmaster.c) the code doesn't hang immediately but still does very soon.

Trough the insertion of Serial.print("dummy text") instructions, I could find where it hangs. It's just after this line:
Code: [Select]
ret = i2c_readNak();        // read one byte

So, is it my code, the installation of the library or the library itself ?

amundsen

It seems the point where the code hangs depends on the I2D device address used.

With address 0x00 the code hangs after this:

Code: [Select]
ret = i2c_readNak();        // read one byte

With a random address it hangs after this:

Code: [Select]
i2c_write(0x00);                        // read address = 0

And with the supposed correct address (0x90) almost all the code is executed excepted the last line:

Code: [Select]
i2c_stop();

Also, no value is returned by the I2C slave anyway (the ret variable remains without value).

Nick Gammon

Code: [Select]
#define AD7746  0x48    // device address of AD7746 without 8th bit
...
i2c_start_wait(AD7746+I2C_WRITE);     // set device address and write mode


That doesn't look right. According to the datasheet for the AD7746:

Quote
The start byte address for the AD7745/AD7746 is 0x90 for a write and 0x91 for a read.


But you are doing 0x48 for a write and 0x49 for a read.

i2c_start_wait does not appear to shift the address left 1 bit, and if it did, then when you use:

Code: [Select]
i2c_rep_start(AD7746+I2C_READ)

... then the 1 which is I2C_READ would be doubled to make it 2, which would be wrong.

Code: [Select]
#define I2C_READ    1

amundsen

#9
Jun 11, 2011, 11:48 am Last Edit: Jun 11, 2011, 11:51 am by amundsen Reason: 1
Nick Gammon wrote :

Quote
That doesn't look right. According to the datasheet for the AD7746:


Quote
The start byte address for the AD7745/AD7746 is 0x90 for a write and 0x91 for a read.


You're right but I have tried 0x48 as well as 0x90 because 0x48 =  0x90 >> 1 and I wasn't sure 0x90 included the read/write bit or not. Finally 0x90 seems indeed the right address because 0x48 hangs the program and 0x90 doesn't.

However the problem was indeed that I2C_WRITE and I2C_READ were not defined. I thought they were constants defined by the library hence I didn't define them myself.

Now the program doesnt' hang anymore but nothing seems to be read.

If I code like this
Code: [Select]
    ret = i2c_readNak();        // read one byte
    Serial.println(ret);

nothing appears on the terminal window

but if I convert the value supposed to be returned on the I2C bus into another format like this
Code: [Select]
    ret = i2c_readNak();        // read one byte
    Serial.println(ret, DEC);

the printed value is always 144 whatever the registrer address chosen.

Anyway, thanks for the help Nick !

Here is the complete sketch:
Code: [Select]
/*

 i2cmaster library test with an AD7746
 
*/

#include <i2cmaster.h>

#define AD7746  0x90    // device address of AD7746
#define I2C_READ 0
#define I2C_WRITE 1
unsigned char ret = 0;


void setup() {    
                             
    Serial.begin(9600);
    Serial.println();
    i2c_init();   // initialize I2C library
}

void loop() {

    // read
    i2c_start_wait(AD7746+I2C_WRITE);     // set device address and write mode
    i2c_write(0x00);                                  // read address = 0
    i2c_rep_start(AD7746+I2C_READ);       // set device address and read mode

    ret = i2c_readNak();        // read one byte
    Serial.print("RET = ");
    Serial.println(ret, DEC);
    i2c_stop();
    delay(500);
}


Go Up