Skip to contentSkip to conversation

Reading a compass CMPS03

DISCUSSION Fred Labrosse 3 participants 7 messages May 19, 2005

lore
Share & tools
lore
#0 lore raw

Hi,

I'm trying to access data from a compass (CMPS03), the documentation of which being at <http://www.robot-electronics.co.uk/htm/cmps3doc.shtml>.
This device is supposed to be at address 0xC0. I read at
<http://lists.arm.linux.org.uk/pipermail/linux-arm-kernel/2003-May/015484.html>
that I should specify this as 0xC0 >> 1, which is 0x60 (0xC0 is clearly not supported anywhere anyway ;-).

I have written a small program that should to that, but it doesn't work. Here is what I did. I attached it to my parallel port as in i2c-2.8.4/doc/i2c-pport. I loaded all the modules:

pcffl root # lsmod

Module                  Size  Used by    Tainted: P
i2c-pport               1297   0
i2c-proc                6836   0  (unused)
i2c-dev                 4608   0
i2c-algo-bit            7720   0  [i2c-pport]

i2c-core 15460 0 [i2c-proc i2c-dev i2c-algo-bit]

and the parallel port is detected:

pcffl root # cat /proc/bus/i2c
i2c-0 i2c Primitive Parallel port adaptor Bit-shift algorithm

My program is attached below:

-------------- next part --------------
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <time.h>
#include <linux/i2c-dev.h>

/* #include <stdio.h> */
/* #include <sys/types.h> */
/* #include <sys/stat.h> */
/* #include <fcntl.h> */
/* #include <linux/i2c.h> */
/* #include <linux/i2c-dev.h> */

#define DEFAULT_I2C_BUS "/dev/i2c-0"
#define DEFAULT_ADDR 0xC0 /* the compass sits on i2c address 0xC0 */

int main(int argc, char **argv)
{
int addr;
int heading;

   int file;
   int adapter_nr = 0; /* probably dynamically determined */
   char filename[20];
  
   sprintf(filename,"/dev/i2c-%d",adapter_nr);
   if ((file = open(filename,O_RDWR)) < 0)
   {
      printf("Open failed for device %s!\n", filename);
      /* ERROR HANDLING; you can check errno to see what went wrong */
      exit(1);
   }

// tell the kernel which device we would like to talk to. addr = DEFAULT_ADDR >> 1;
/* The shift to the right is explained at

      <http://lists.arm.linux.org.uk/pipermail/linux-arm-kernel/2003-May/015484.html>

*/

   printf("Accessing device at 0x%X\n", addr); 
   if (ioctl(file, I2C_SLAVE, addr) < 0)
   {
      int errnumber = errno;
      printf("%s\n", strerror(errnumber));
      printf("I2C_SLAVE failed for 0x%X\n", addr); 
      /* ERROR HANDLING; you can check errno to see what went wrong */
      exit(2); 

}

   heading = i2c_smbus_read_byte_data(file, 1);
   if (heading < 0)
   {
      printf("write error\n"); 
      exit(3); 
   }
   else
   {
      printf("Heading %d\n", heading);
   }

// set all 8 legs to 1
/* if (i2c_smbus_write_byte(file, 0xff)<0) */
/* { */
/* printf("write error\n"); */
/* exit(3); */
/* } */

// and quit
close(file);
}
-------------- next part --------------

All it does is output 0, whatever register I read. Here is the output of i2cdetect and i2cdump:

pcffl root # i2cdetect 0
WARNING! This program can confuse your I2C bus, cause data loss and worse! I will probe file /dev/i2c-0
You have five seconds to reconsider and press CTRL-C!

0 1 2 3 4 5 6 7 8 9 a b c d e f
00: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
10: 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f
20: 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f
30: 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f
40: 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f
50: 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f
60: 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f
70: 70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f
pcffl root # i2cdump 0 0x60
No size specified (using byte-data access)
WARNING! This program can confuse your I2C bus, cause data loss and worse! I will probe file /dev/i2c-0, address 0x60, mode byte
You have five seconds to reconsider and press CTRL-C!

     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f    0123456789abcdef
00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
10: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
20: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
30: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
40: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
50: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
60: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
70: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
90: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................

Now, the funny thing is that i2cdetect and i2cdump produce exactly the same thing whether or not my device is plugged in!

One additional bit:

pcffl root # cat /proc/sys/dev/parport/parport0/modes PCSPP,TRISTATE

I hope someone can help me with that. Sorry for the long message.

Cheers,

Fred

#1 lore raw

According to Fred Labrosse:

Quoted 3 lines

This device is supposed to be at address 0xC0. I read at [URL] that I should specify this as 0xC0 >> 1, which is 0x60 (0xC0 is clearly not supported anywhere anyway ;-).

Correct. Bit 0 is for R/W, the address by itself being 7-bit. Some docs reference chip address as the pair N (write address) and N+1 (read address) but most people, including us, refer to the single address N>>1 (chip address).

Quoted 5 lines

I have written a small program that should to that, but it doesn't work.
Here is what I did. I attached it to my parallel port as in i2c-2.8.4/doc/i2c-pport. I loaded all the modules:

Quoted 7 lines

pcffl root # lsmod

Module                  Size  Used by    Tainted: P
i2c-pport               1297   0
i2c-proc                6836   0  (unused)
i2c-dev                 4608   0
i2c-algo-bit            7720   0  [i2c-pport]

i2c-core 15460 0 [i2c-proc i2c-dev i2c-algo-bit]

That looks good.

Quoted 5 lines

and the parallel port is detected:

pcffl root # cat /proc/bus/i2c
i2c-0 i2c Primitive Parallel port adaptor
Bit-shift algorithm

This is correct too.

Quoted 1 line

My program is attached below:

As long as i2cdetect and i2cdump don't output the expected result, your program doesn't have a chance to work.

Quoted 11 lines

pcffl root # i2cdetect 0
(...)
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10: 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20: 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30: 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f 40: 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50: 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f 60: 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70: 70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f

This is bad. Only 60 should answer, other addresses should show "XX".

Quoted 1 line

pcffl root # i2cdump 0 0x60

Bad too, of course :(

Quoted 2 lines

Now, the funny thing is that i2cdetect and i2cdump produce exactly the same thing whether or not my device is plugged in!

Well, it simply looks like i2c-parport isn't working as intended.

Quoted 4 lines

One additional bit:

pcffl root # cat /proc/sys/dev/parport/parport0/modes PCSPP,TRISTATE

That may be a problem. I think I remember the i2c-pport doc stating that the i2c-pport.c driver takes benefit of the features of modern parallel ports (usually known as EPP or ECP, as opposed to SPP). Looks like your parallel port doesn't support these modes, which may be why i2c-pport doesn't work. Maybe you need to enable it through the BIOS setup screen?

Second, i2c-pport.c doesn't use the standard parport interface, i.e. it doesn't depend on the parport module - and is not even compatible with it AFAIK. This means that you should *not* load the parport module when using i2c-pport, because both modules will try to access the same I/O ports and that may be the problem.

You may consider using i2c-philips-par (or i2c-parport in the 2.6 kernel tree) instead, but the electronics needed to interface with your chip are slighly different in this case, so you would have to change that.

Quoted 1 line

I hope someone can help me with that. Sorry for the long message.

No problem, I hope you'll find a solution. Let us know!

Signature · 2 lines
Jean Delvare
http://www.ensicaen.ismra.fr/~delvare/
#2 lore raw
In reply to Fred Labrosse
Quoted 13 lines · Jean Delvare
Quoted 12 lines

pcffl root # i2cdetect 0
(...)
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10: 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20: 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30: 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f 40: 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50: 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f 60: 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70: 70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f

Quoted 2 lines

This is bad. Only 60 should answer, other addresses should show "XX".

This is solved. Somebody (and I did not verify before hand) had configured the parallel port to be on some silly address. It is now back to normal.

Quoted 13 lines
Quoted 5 lines

One additional bit:

pcffl root # cat /proc/sys/dev/parport/parport0/modes PCSPP,TRISTATE

That may be a problem. I think I remember the i2c-pport doc stating that the i2c-pport.c driver takes benefit of the features of modern parallel ports (usually known as EPP or ECP, as opposed to SPP). Looks like your parallel port doesn't support these modes, which may be why i2c-pport doesn't work. Maybe you need to enable it through the BIOS setup screen?

Quoted 5 lines

Second, i2c-pport.c doesn't use the standard parport interface, i.e. it doesn't depend on the parport module - and is not even compatible with it AFAIK. This means that you should *not* load the parport module when using i2c-pport, because both modules will try to access the same I/O ports and that may be the problem.

I indeed had to unload parport to make it work. Now i2cdetect doesn't detect anything. I have been playing with a scope during the last hour or 2 to try to see what was happening. It seems that the ACK signal is not pulled down enough by the CMPS03 to be detected as such (I read 1V which is probably a bit high for a 0V). This is confirmed in the messages:

Apr  5 17:32:49 pcffl i2c-algo-bit.o: Used 4 tries to write client at 0x60 : no ack
Apr  5 17:32:49 pcffl i2c-algo-bit.o: NAK from device addr 60 msg #0
Quoted 5 lines

You may consider using i2c-philips-par (or i2c-parport in the 2.6 kernel tree) instead, but the electronics needed to interface with your chip are slighly different in this case, so you would have to change that.

Yes, but that indeed involves a bit more of hardware, thus time and money ;-). I wanted to try a fast quick and dirty way. It proves to be not that fast. ;-).

Quoted 4 lines
Quoted 1 line

I hope someone can help me with that. Sorry for the long message.

No problem, I hope you'll find a solution. Let us know!

So any input as to why the device is not pulling down enough would be much appreciated. Note however that I'm yet not sure this is indeed the problem. I'll do more experiments tomorrow with a better scope.

Thanks anyway.

Fred

#3 lore raw
In reply to Fred Labrosse
Quoted 4 lines · Mark Studebaker

What value of pullup resistor are you using?
5K is probably about right. Definitely not less than 1K. I note that doc/i2c-pport doesn't make a recommendation.

Well it actually says: "no pullups requied" (sic), which I believed because of the resistor on the "out pin" on the diagram in the same file. Moreover, a pullup resistor would make it even harder for the device to pull down the signal, which seems to be what causes the noack problem. Right?

Cheers,

Fred

#4 lore raw
In reply to Fred Labrosse
Quoted 4 lines · Mark Studebaker

What value of pullup resistor are you using?
5K is probably about right. Definitely not less than 1K. I note that doc/i2c-pport doesn't make a recommendation.

We just did some measurements using a digital scope on the clock and data lines on the compass.

Just to remind people, the compass sits on address 0x60 (1100000 in binary) and I'm trying to use i2c-pport.

When I send the address on the i2c bus (trying to read a register of the device), I do read, synchronised with the clock, the address (1100000), followed by a 0 (write) followed by something at about 1.2V that should be the 0 for the acknowledgement. So clearly, the device is not pulling down enough.

Anybody got any idea?

TIA,

Fred

#5 lore raw
In reply to Fred Labrosse

What value of pullup resistor are you using?
5K is probably about right. Definitely not less than 1K. I note that doc/i2c-pport doesn't make a recommendation.

Quoted 73 lines · Fred Labrosse
Quoted 13 lines · Jean Delvare
Quoted 12 lines

pcffl root # i2cdetect 0
(...)
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10: 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20: 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30: 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f 40: 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50: 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f 60: 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70: 70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f

Quoted 2 lines

This is bad. Only 60 should answer, other addresses should show "XX".

This is solved. Somebody (and I did not verify before hand) had configured the parallel port to be on some silly address. It is now back to normal.

Quoted 13 lines
Quoted 5 lines

One additional bit:

pcffl root # cat /proc/sys/dev/parport/parport0/modes PCSPP,TRISTATE

That may be a problem. I think I remember the i2c-pport doc stating that the i2c-pport.c driver takes benefit of the features of modern parallel ports (usually known as EPP or ECP, as opposed to SPP). Looks like your parallel port doesn't support these modes, which may be why i2c-pport doesn't work. Maybe you need to enable it through the BIOS setup screen?

Quoted 5 lines

Second, i2c-pport.c doesn't use the standard parport interface, i.e. it doesn't depend on the parport module - and is not even compatible with it AFAIK. This means that you should *not* load the parport module when using i2c-pport, because both modules will try to access the same I/O ports and that may be the problem.

I indeed had to unload parport to make it work. Now i2cdetect doesn't detect anything. I have been playing with a scope during the last hour or 2 to try to see what was happening. It seems that the ACK signal is not pulled down enough by the CMPS03 to be detected as such (I read 1V which is probably a bit high for a 0V). This is confirmed in the messages:

Apr  5 17:32:49 pcffl i2c-algo-bit.o: Used 4 tries to write client at 0x60 : no ack
Apr  5 17:32:49 pcffl i2c-algo-bit.o: NAK from device addr 60 msg #0
Quoted 5 lines

You may consider using i2c-philips-par (or i2c-parport in the 2.6 kernel tree) instead, but the electronics needed to interface with your chip are slighly different in this case, so you would have to change that.

Yes, but that indeed involves a bit more of hardware, thus time and money ;-). I wanted to try a fast quick and dirty way. It proves to be not that fast. ;-).

Quoted 4 lines
Quoted 1 line

I hope someone can help me with that. Sorry for the long message.

No problem, I hope you'll find a solution. Let us know!

So any input as to why the device is not pulling down enough would be much appreciated. Note however that I'm yet not sure this is indeed the problem. I'll do more experiments tomorrow with a better scope.

Thanks anyway.

Fred

#6 lore raw
In reply to Fred Labrosse

right.
If parallel ports have integrated pullups, hopefully they aren't too strong for the external i2c chip.
Two other possibilities:
1) the i2c chip doesn't have a good ground
2) the parallel port is driving high on that pin rather than changing to an input, so you're getting contention. This could be caused by a driver bug, or your connecting to the wrong pin.

Quoted 25 lines · Fred Labrosse
Quoted 4 lines · Mark Studebaker

What value of pullup resistor are you using?
5K is probably about right. Definitely not less than 1K. I note that doc/i2c-pport doesn't make a recommendation.

We just did some measurements using a digital scope on the clock and data lines on the compass.

Just to remind people, the compass sits on address 0x60 (1100000 in binary) and I'm trying to use i2c-pport.

When I send the address on the i2c bus (trying to read a register of the device), I do read, synchronised with the clock, the address (1100000), followed by a 0 (write) followed by something at about 1.2V that should be the 0 for the acknowledgement. So clearly, the device is not pulling down enough.

Anybody got any idea?

TIA,

Fred