Page 2 of 2

Re: I2C crashes with BCM2835 library functions

Posted: Sun Apr 26, 2015 11:01 am
by lihakimpale
I haven't changed it so it is the default. I will try with that 50kbps.

Re: I2C crashes with BCM2835 library functions

Posted: Thu Apr 30, 2015 1:42 pm
by lihakimpale
"The standard 7-bit I2C slave address is 0x60 or 1100000. 8-bit read is 0xC1, 8-bit write is 0xC0". I am wondering for what reason are those read and write addresses for.

Re: I2C crashes with BCM2835 library functions

Posted: Thu Apr 30, 2015 1:52 pm
by joan
The I2C address is placed in the top 7 bits of the byte. The least significant bit encodes R/W.

Re: I2C crashes with BCM2835 library functions

Posted: Thu Apr 30, 2015 1:58 pm
by DougieLawson
0x60 == 0b01100000
0xC0 == 0x60 << 1 == 0b11000000
0xC1 == 0x60 << 1 | 0x1 == 0b11000001

The low bit is 1 for read, 0 for write and the 7-bit I2C device address is shifted one bit left to form the byte that's written to the I2C bus.

Re: I2C crashes with BCM2835 library functions

Posted: Sun May 03, 2015 1:37 pm
by lihakimpale
Can you say what is the reason for ioctl error 110: connection timed out?

Re: I2C crashes with BCM2835 library functions

Posted: Sat May 09, 2015 5:57 pm
by lihakimpale
I am able to read the registers but I think the problem is that I'm not able to write to the registers. First lines of my code checks the WhoamI register and it returns the right value and after that I set the right register values for Barometer reading but I think it isn't setting those values because I can't get any output. I have tried with two different write operations:

Code: Select all

int write_register(unsigned char reg, unsigned char value, int *p_fd)
{
	unsigned char buf[2] = { 0 };
	int fd = *p_fd;

	if(ioctl(fd, I2C_SLAVE, MPL3115A2_ADDR) < 0)
	{
			printf("Unable to get bus access to talk to slave\n");
			close(fd);
			return 1;
	}

	buf[0]=reg;
	buf[1]=value;

	if((write(fd, buf, 2)) != 2)
	{
		printf("Error writing to i2c slave\n");
		close(fd);
		return 1;
	}

	return 0;
}

Code: Select all

int write_register(unsigned char reg, unsigned char value, int *p_fd)
{
	int result = 0;
	unsigned char buf[2] = { 0 };
	int fd = *p_fd;

	struct i2c_msg rdwr_msg[1];
	struct i2c_rdwr_ioctl_data rdwr_data1;

    rdwr_data1.msgs = rdwr_msg;
    rdwr_data1.nmsgs = 1;

	if(ioctl(fd, I2C_SLAVE, MPL3115A2_ADDR) < 0)
	{
			printf("Unable to get bus access to talk to slave\n");
			close(fd);
			return 1;
	}

	buf[0]=reg;
	buf[1]=value;

		  rdwr_data1.msgs[0].addr = MPL3115A2_ADDR;
		  rdwr_data1.msgs[0].flags = 0;
		  rdwr_data1.msgs[0].len = 2;
		  rdwr_data1.msgs[0].buf = buf;

		  result = ioctl( fd, I2C_RDWR, &rdwr_data1 );

		  if ( result < 0 )
		  {
		    printf( "rdwr ioctl error: %d\n", errno );
		    perror( "reason" );
		  }

	return 0;

}

Re: I2C crashes with BCM2835 library functions

Posted: Tue May 12, 2015 3:59 pm
by lihakimpale
I am confused about if I should first read the current value of the register and after that set it or can I only write to the register without first reading the value of it? There is examples of both ways and I am wondering which is the proper one.

Re: I2C crashes with BCM2835 library functions

Posted: Mon May 25, 2015 2:38 pm
by lihakimpale
Ok, I am now able to read the temperature and it gives a correct value but I am not able to get right values of pressure and altitude. The pressure output on average is 160kPa and the altitude output on average is 7000m. It seems that the msb and lsb bytes of pressure and altitude output values are allways almost the same (change is about 0-2 in decimals) but the csb byte value varies a lot.

I also noticed that the combined transaction works better with 400k baudrate (at least with this sensor). Slower values like 50k and 100k screws up the combined transaction and the ioctl function call returns by error code "110 connection timed out".

Can I now assume that the I2C data transfer is working properly because I am able to read the temperature right and I need to look for the possible bug from my code?

Re: I2C crashes with BCM2835 library functions

Posted: Mon May 25, 2015 4:51 pm
by joan
Probably. May be something simple like byte order (especially if you have more than one I2C device).

Re: I2C crashes with BCM2835 library functions

Posted: Sun May 31, 2015 7:41 am
by hampi
My slow daemon code is here, but I need to still improve it. The structure is likely to change in near future.

https://github.com/oh7bf/Mpl3115a2d