Subversion Repositories svnkaklik

Rev

Rev 354 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log

Rev 354 Rev 358
1
/* compass.c
1
/* compass.c
2
 *
2
 *
3
 * Software to read from the CMPS03 magnetic compass. 
3
 * Software to read from the CMPS03 magnetic compass. 
4
 *
4
 *
5
 * By Josh Marshall, joshua dot marshall at studentmail dot newcastle dot edu dot au
5
 * By Josh Marshall, joshua dot marshall at studentmail dot newcastle dot edu dot au
6
 * October 15, 2004.
6
 * October 15, 2004.
7
 *
7
 *
8
 * Adapted from code for the SP03 speech synthesiser 
8
 * Adapted from code for the SP03 speech synthesiser 
9
 * By Bram Stolk, b.stolk at chello.nl
9
 * By Bram Stolk, b.stolk at chello.nl
10
 *
10
 *
11
 * Bram Stolk's notes:
11
 * Bram Stolk's notes:
12
 * In linux, you can do I2C stuff in user-space, if you enable
12
 * In linux, you can do I2C stuff in user-space, if you enable
13
 * the device-interface to i2c. You can use plain read() and 
13
 * the device-interface to i2c. You can use plain read() and 
14
 * write() calls on your /dev/i2c-0 file.
14
 * write() calls on your /dev/i2c-0 file.
15
 *
15
 *
16
 * NOTE: Check if you have an i2c adapter active by doing:
16
 * NOTE: Check if you have an i2c adapter active by doing:
17
 * cat /proc/bus/i2c
17
 * cat /proc/bus/i2c
18
 * If this comes up empty, then you have no adapters. If the
18
 * If this comes up empty, then you have no adapters. If the
19
 * file does not exist at all, you probably lack i2c kernel
19
 * file does not exist at all, you probably lack i2c kernel
20
 * support alltogether.
20
 * support alltogether.
21
 */
21
 */
22
 
22
 
23
#include <errno.h>
23
#include <errno.h>
24
#include <string.h>
24
#include <string.h>
25
#include <stdio.h>
25
#include <stdio.h>
26
#include <stdlib.h>
26
#include <stdlib.h>
27
#include <unistd.h>
27
#include <unistd.h>
28
#include <fcntl.h>
28
#include <fcntl.h>
29
#include <linux/i2c-dev.h>
29
#include <linux/i2c-dev.h>
30
 
30
 
31
/* Note that the documentation for the compass states its address as 0xC0.
31
/* Note that the documentation for the compass states its address as 0xC0.
32
 * However, this includes the low bit which specifies read or write.
32
 * However, this includes the low bit which specifies read or write.
33
 * Linux i2c does not include this bit in this address, so the actual
33
 * Linux i2c does not include this bit in this address, so the actual
34
 * address is 0xC0 shifted down, 0x60.
34
 * address is 0xC0 shifted down, 0x60.
35
 */
35
 */
36
#define CMPS03_ADDR 0x60
36
#define CMPS03_ADDR	0x60
-
 
37
#define I2C_SLAVE	0x0B	/* Change slave address	
37
 
38
 
38
/* The important registers on the compass. Internal/test registers omitted. */
39
/* The important registers on the compass. Internal/test registers omitted. */
39
#define CMPS03_SOFTWARE_REVISION 0x0
40
#define CMPS03_SOFTWARE_REVISION 0x0
40
#define CMPS03_BEARING_BYTE 0x1
41
#define CMPS03_BEARING_BYTE 0x1
41
#define CMPS03_BEARING_WORD_HIGH 0x2
42
#define CMPS03_BEARING_WORD_HIGH 0x2
42
#define CMPS03_BEARING_WORD_LOW 0x3
43
#define CMPS03_BEARING_WORD_LOW 0x3
43
#define CMPS03_CALIBRATE_CMD 0xF
44
#define CMPS03_CALIBRATE_CMD 0xF
44
 
45
 
45
int main(int argc, char *argv[]) {
46
int main(int argc, char *argv[]) {
46
   char *end;
47
   char *end;
47
   int res,file;
48
   int res,file;
48
   int e1;
49
   int error;
49
   char filename[20] ;
50
   char filename[20] ;
50
   long funcs;
51
   long funcs;
51
    
52
    
52
   int heading_byte, heading_word_h, heading_word_l;
53
   int heading_byte, heading_word_h, heading_word_l;
53
   int bearing_long, bearing_degrees;
54
   int bearing_long, bearing_degrees;
54
   
55
   
55
   sprintf(filename,"/dev/i2c-%d");
56
   sprintf(filename,"/dev/i2c-0");
56
   if ((file = open(filename,O_RDWR)) < 0) {
57
   file = open(filename,O_RDWR);
-
 
58
   if (errno != 0)
-
 
59
   {
57
     e1 = errno;
60
     switch (errno)
-
 
61
     {
58
     if (e1 != ENOENT) {
62
     case EACCES:
59
       fprintf(stderr,"Error: Could not open file '%s' : %sn",
63
         fprintf(stderr,"Run as root? \n");
60
               filename,strerror(e1));
64
         break;
61
       if(e1 == EACCES)
65
     default:
62
         fprintf(stderr,"Run as root?n");
66
         fprintf(stderr,"Error: Could not open file '%s' : %s \n", filename, strerror(errno));
-
 
67
         break;
63
     }
68
     }
64
   } 
69
   } 
65
      
70
      
66
   if (ioctl(file,I2C_SLAVE,CMPS03_ADDR) < 0) {
71
   if (ioctl(file,I2C_SLAVE,CMPS03_ADDR) < 0) {
-
 
72
     switch (errno) 
-
 
73
     {
67
     if (errno == EBUSY) {
74
     case EBUSY:  
68
       printf("device is busyn");
75
         printf("device is busy \n");
-
 
76
         break;
-
 
77
 
69
     }
78
     default:
70
     printf("Got error: %d\n", errno);
79
         printf("Got error: %s \n", strerror(errno));
71
     exit(0);
80
         exit(0);
-
 
81
      }
72
   }
82
   }
73
   
83
   
74
   /* Get software revision number */
84
   /* Get software revision number */
75
   res = i2c_smbus_read_byte_data(file, CMPS03_SOFTWARE_REVISION);
85
   res = read(file, CMPS03_SOFTWARE_REVISION,1);
76
   if (res < 0) {
86
   if (res < 0) {
77
     printf("Cannot read software revision leveln");
87
     printf("Cannot read software revision level \n");
78
   } else {
88
   } else {
79
     printf("Software revision level: %02xn", res);
89
     printf("Software revision level: %02x \n", res);
80
   }
90
   }
81
   
91
   
82
   /* Loop and read from the compass. */
92
   /* Loop and read from the compass. */
83
   while (1) {
93
   while (1) {
84
     /* The heading byte is 0-255 for the 360 degrees. */
94
     /* The heading byte is 0-255 for the 360 degrees. */
85
     heading_byte = i2c_smbus_read_byte_data(file, CMPS03_BEARING_BYTE);
95
     heading_byte = read(file, CMPS03_BEARING_BYTE,1);
86
     if (heading_byte < 0) { printf("Error reading from compass."); exit(1);}
96
     if (heading_byte < 0) { printf("Error reading from compass. \n"); exit(1);}
87
     
97
     
88
     /* The high resolution heading is given in two registers, and is 10 * the 
98
     /* The high resolution heading is given in two registers, and is 10 * the 
89
      * heading in degrees, ie 359.9 degrees reads as 3599. */
99
      * heading in degrees, ie 359.9 degrees reads as 3599. */
90
     heading_word_h = i2c_smbus_read_byte_data(file, CMPS03_BEARING_WORD_HIGH);
100
     heading_word_h = read(file, CMPS03_BEARING_WORD_HIGH,1);
91
     if (heading_word_h < 0) { printf("Error reading from compass."); exit(1);}
101
     if (heading_word_h < 0) { printf("Error reading from compass. \n"); exit(1);}
92
     heading_word_l = i2c_smbus_read_byte_data(file, CMPS03_BEARING_WORD_LOW);
102
     heading_word_l = read(file, CMPS03_BEARING_WORD_LOW,1);
93
     if (heading_word_l < 0) { printf("Error reading from compass."); exit(1);}
103
     if (heading_word_l < 0) { printf("Error reading from compass. \n"); exit(1);}
94
     
104
     
95
     /* Combine the two bytes, and get the heading in degrees. */
105
     /* Combine the two bytes, and get the heading in degrees. */
96
     bearing_long = heading_word_h * 256 + heading_word_l;
106
     bearing_long = heading_word_h * 256 + heading_word_l;
97
     bearing_degrees = bearing_long / 10;
107
     bearing_degrees = bearing_long / 10;
98
     
108
     
99
     printf("Bearing: %d \n", bearing_degrees);
109
     printf("Bearing: %d \n", bearing_degrees);
100
     
110
     
101
     /* Wait for a while. */
111
     /* Wait for a while. */
102
     usleep(200000);
112
     usleep(200000);
103
   }
113
   }
104
}
114
}