[SOLVED] I2CMaster Lib hang at start

Hi,

I’m using an Arduino Nano.

I am using I2CMaster Lib (http://homepage.hispeed.ch/peterfleury/i2cmaster.zip), see manual, and I have problems using it.

I can do an i2c_init(), without any problem but when I try to start a communication the program hang.

I do have pull-up resistors.

Here is my code :

extern "C" {
#include <i2cmaster.h>
}

#define deviceAddress 0x0b
#define I2C_WRITE 0 

unsigned char i2c_result;

void setup() {
  Serial.begin(115200);
  i2c_init();
  while (!Serial);
  //PORTC = (1 << PORTC4) | (1 << PORTC5); //enable pullups
  Serial.print("Hello");
  //i2c_stop();
}

void loop() {

 i2c_result = i2c_start((deviceAddress<<1) + I2C_WRITE);

 if(!i2c_result) Serial.print("Error I2C");

}

And the function in the lib hanging :

unsigned char i2c_start(unsigned char address)
{
    uint8_t   twst;

 // send START condition
 TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
 // wait until transmission completed
 while(!(TWCR & (1<<TWINT)));

 // check value of TWI Status Register. Mask prescaler bits.
 twst = TW_STATUS & 0xF8;
 if ( (twst != TW_START) && (twst != TW_REP_START)) return 1;

 // send device address
 TWDR = address;
 TWCR = (1<<TWINT) | (1<<TWEN);

 // wail until transmission completed and ACK/NACK has been received
 while(!(TWCR & (1<<TWINT)));

 // check value of TWI Status Register. Mask prescaler bits.
 twst = TW_STATUS & 0xF8;
 if ( (twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK) ) return 1;

 return 0;

}/* i2c_start */

I know my i2c device works because when I do an I2C scan with the I2C Wire Lib sketch it can detect it.

Even if my device was not working I should have in my term “Error I2C”.

I think the problem is here :

while(!(TWCR & (1<<TWINT)));

But I checked in the datasheet page 272 and it seems correct. Apart the fact that in the datasheet they said “TWCR0”, so I tried changing this in the function but it said it’s not declared.

Thank you for your help.

You're doing repeated start sequences without actually doing anything. I don't have such an in-depth knowledge of the hardware registers but I doubt that this procedure was foreseen by the hardware designers.

Why don't you just use the Wire library? There you do complete transactions so this stupid repetition of part-transaction isn't possible by design.

pylon:
You're doing repeated start sequences without actually doing anything. I don't have such an in-depth knowledge of the hardware registers but I doubt that this procedure was foreseen by the hardware designers.

Why don't you just use the Wire library? There you do complete transactions so this stupid repetition of part-transaction isn't possible by design.

Thank you for your help but my module uses SMBus protocol transfer which I is impossible to do with the Wire lib.

BUT !

Tip to myself : Properly soldering pull-up resistors, helps your I2C to work perfectly.

Thank you for your help but my module uses SMBus protocol transfer which I is impossible to do with the Wire lib.

Says who? Please define which part of the SMBus protocol (which is a subset of the I2C protocol) you think is not supported by the Wire library.

My bad, when I tried it was when my pull-up resistors were badly soldered so I thought it was the Wire lib. Then I tried hard using I2Cmaster and never retried with wire.

I wrote some basic Wire functions and it works perfectly. I still have sometimes hanging problems on my I2C but this is something out of this topic.

Thank you for your help.