lihakimpale
Posts: 80
Joined: Tue Jul 22, 2014 4:12 pm

Re: I2C crashes with BCM2835 library functions

Sun Apr 26, 2015 11:01 am

I haven't changed it so it is the default. I will try with that 50kbps.

lihakimpale
Posts: 80
Joined: Tue Jul 22, 2014 4:12 pm

Re: I2C crashes with BCM2835 library functions

Thu Apr 30, 2015 1:42 pm

"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.

User avatar
joan
Posts: 14838
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: I2C crashes with BCM2835 library functions

Thu Apr 30, 2015 1:52 pm

The I2C address is placed in the top 7 bits of the byte. The least significant bit encodes R/W.

User avatar
DougieLawson
Posts: 38446
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: I2C crashes with BCM2835 library functions

Thu Apr 30, 2015 1:58 pm

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.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

I'll do your homework for you for a suitable fee.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

lihakimpale
Posts: 80
Joined: Tue Jul 22, 2014 4:12 pm

Re: I2C crashes with BCM2835 library functions

Sun May 03, 2015 1:37 pm

Can you say what is the reason for ioctl error 110: connection timed out?

lihakimpale
Posts: 80
Joined: Tue Jul 22, 2014 4:12 pm

Re: I2C crashes with BCM2835 library functions

Sat May 09, 2015 5:57 pm

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;

}

lihakimpale
Posts: 80
Joined: Tue Jul 22, 2014 4:12 pm

Re: I2C crashes with BCM2835 library functions

Tue May 12, 2015 3:59 pm

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.

lihakimpale
Posts: 80
Joined: Tue Jul 22, 2014 4:12 pm

Re: I2C crashes with BCM2835 library functions

Mon May 25, 2015 2:38 pm

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?

User avatar
joan
Posts: 14838
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: I2C crashes with BCM2835 library functions

Mon May 25, 2015 4:51 pm

Probably. May be something simple like byte order (especially if you have more than one I2C device).

hampi
Posts: 223
Joined: Fri May 31, 2013 11:29 am
Contact: Website

Re: I2C crashes with BCM2835 library functions

Sun May 31, 2015 7:41 am

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

Return to “Interfacing (DSI, CSI, I2C, etc.)”