sumanharapanahalli
Posts: 20
Joined: Mon Oct 08, 2012 5:14 am
Location: Bangalore, India

I2C bus first byte MSB missing ..

Thu Oct 18, 2012 2:05 pm

Hi,

I am interacting my PI with a slave device [PC], with the same I am able to send data and receive data.

But the first byte that i receive in PI is missing the MSB bit every time it reads, i.e.; the first bit [MSB] is always considered as 0, below i have given some example that slave device is sending and what PI RX.

Slave[TX] PI[TX]
FF 7F
88 08
AA 2A

I did connect the logical analyzer and to check the data it sends correctly.


#include <stdio.h>
#include <stdlib.h>
#include <linux/i2c-dev.h>
#include <linux/i2c.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
int main()
{
int File;
char Device[] = "/dev/i2c-0";
char Touch = 0x32;
char Data[60];
int i,j=0;

if ((File = open("/dev/i2c-0", O_RDWR)) < 0) // I²C aktivieren
{
printf("I²C Module address can not be loaded!\n");
return -1;
}

if (ioctl(File, I2C_SLAVE, Touch) < 0) // Port und Adresse setzen
{
printf("Device adresse can not be found !\n");
exit(1);
}
while(j++ < 2)
{
/* SW version */
Data[0] = 0xff;
Data[1] = 0xff;
Data[2] = 0xff;
Data[3] = 0xff;

if(write(File, Data, 4) != 4)
{
printf("Error writing data!\n");
return -1;
}

if (read(File, Data, 4) != 4)
{
printf("Error reading data!\n");
return -1;
}
//else
{
for(i = 0;i < 4;i++)
printf(" %x ",Data);
printf("\n");
}
sleep(1);
}
close(File);
return 0;
}
Thanks & Regards
Sumanth

sumanharapanahalli
Posts: 20
Joined: Mon Oct 08, 2012 5:14 am
Location: Bangalore, India

Re: I2C bus first byte MSB missing ..

Thu Oct 18, 2012 2:06 pm

Can some one help me what i am missing plz ...

Thanks in advance ...
Thanks & Regards
Sumanth

Frank B
Posts: 61
Joined: Fri Sep 14, 2012 8:02 pm
Location: Germany

Re: I2C bus first byte MSB missing ..

Thu Oct 18, 2012 5:52 pm

sumanharapanahalli wrote:Hi,

But the first byte that i receive in PI is missing the MSB bit every time it reads
}
Hi,
you should read some docs about the i2c protocol.
I think the bit you are missing is the data direction which is included in the adress-byte.

You simply can't use i2c like rs232.

pepis
Posts: 4
Joined: Mon Oct 22, 2012 9:26 am
Location: Poland

Re: I2C bus first byte MSB missing ..

Mon Oct 22, 2012 9:33 am

If it would be due to R/W bit it should be LSB not MSB missing. What is more R/W bit is only from master side, after 7bits of slave address. I have the same situation, any suggestions more??

User avatar
Grumpy Mike
Posts: 936
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: I2C bus first byte MSB missing ..

Mon Oct 22, 2012 12:51 pm

I am interacting my PI with a slave device [PC],
Can you say more about what this slave device is? It is not just a PC it must be a PC with some sort of interface running some sort of code.

pepis
Posts: 4
Joined: Mon Oct 22, 2012 9:26 am
Location: Poland

Re: I2C bus first byte MSB missing ..

Tue Oct 23, 2012 8:15 am

OK, for me problem is solved :D
Have a look here: http://www.raspberrypi.org/phpBB3/viewt ... p?p=146272
I'm not very good on linux modules and stuff like that so I started to look for faults on my ATmega (I want to connect ATmega to RPi via I2C), and it looks like it is stretching the clock to slow down the transmission. So I tried to changed the oscillator from 8MHz to 16MHz and suddenly it starts working.
The same reason is why I couldn't make word and block transmission to work, and now it works fine.
So maybe you also should try to make your slave device respond faster so it won't stretch the clock.

I hope it will work for you and good luck

pepis
Posts: 4
Joined: Mon Oct 22, 2012 9:26 am
Location: Poland

Re: I2C bus first byte MSB missing ..

Wed Oct 24, 2012 5:32 am

I've found better idea. Hence it seems that making ATmega run faster didn't actually do the trick as I still get some errors from time to time. I've updated the firmware.
Here you will find update tool : http://www.raspberrypi.org/archives/1195
In kernel 3.2.27 there is baudrate parameter to control the bus clock. I've just added in /etc/modules:

Code: Select all

i2c-bcm2708 baudrate=50000 

sumanharapanahalli
Posts: 20
Joined: Mon Oct 08, 2012 5:14 am
Location: Bangalore, India

Re: I2C bus first byte MSB missing ..

Wed Oct 24, 2012 8:37 am

The error was because of the clock stretching I saw the data in the logical analyzer.

Thanks pals for the advise, my actual slave did not through up any errors.
It just worked fine.

Thanks
Suman
Thanks & Regards
Sumanth

sumanharapanahalli
Posts: 20
Joined: Mon Oct 08, 2012 5:14 am
Location: Bangalore, India

Re: I2C bus first byte MSB missing ..

Fri Oct 26, 2012 12:30 pm

More information -

I was using the Aardvark [USB to I2C and SPI bridge] from the PC as a slave device.

Probably I will attach a snapshot of the clock later.
Thanks & Regards
Sumanth

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