1 /*
2  * Author: Brendan Le Foll <brendan.le.foll@intel.com>
3  * Copyright (c) 2014 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 #include "mraa.h"
26 #include "math.h"
27 
28 #define MAX_BUFFER_LENGTH 6
29 #define HMC5883L_I2C_ADDR 0x1E
30 
31 // configuration registers
32 #define HMC5883L_CONF_REG_A 0x00
33 #define HMC5883L_CONF_REG_B 0x01
34 
35 // mode register
36 #define HMC5883L_MODE_REG 0x02
37 
38 // data register
39 #define HMC5883L_X_MSB_REG 0
40 #define HMC5883L_X_LSB_REG 1
41 #define HMC5883L_Z_MSB_REG 2
42 #define HMC5883L_Z_LSB_REG 3
43 #define HMC5883L_Y_MSB_REG 4
44 #define HMC5883L_Y_LSB_REG 5
45 #define DATA_REG_SIZE 6
46 
47 // status register
48 #define HMC5883L_STATUS_REG 0x09
49 
50 // ID registers
51 #define HMC5883L_ID_A_REG 0x0A
52 #define HMC5883L_ID_B_REG 0x0B
53 #define HMC5883L_ID_C_REG 0x0C
54 
55 #define HMC5883L_CONT_MODE 0x00
56 #define HMC5883L_DATA_REG 0x03
57 
58 // scales
59 #define GA_0_88_REG 0x00 << 5
60 #define GA_1_3_REG 0x01 << 5
61 #define GA_1_9_REG 0x02 << 5
62 #define GA_2_5_REG 0x03 << 5
63 #define GA_4_0_REG 0x04 << 5
64 #define GA_4_7_REG 0x05 << 5
65 #define GA_5_6_REG 0x06 << 5
66 #define GA_8_1_REG 0x07 << 5
67 
68 // digital resolutions
69 #define SCALE_0_73_MG 0.73
70 #define SCALE_0_92_MG 0.92
71 #define SCALE_1_22_MG 1.22
72 #define SCALE_1_52_MG 1.52
73 #define SCALE_2_27_MG 2.27
74 #define SCALE_2_56_MG 2.56
75 #define SCALE_3_03_MG 3.03
76 #define SCALE_4_35_MG 4.35
77 
78 int
main(int argc,char ** argv)79 main(int argc, char** argv)
80 {
81     mraa_init();
82     float direction = 0;
83     int16_t x = 0, y = 0, z = 0;
84     uint8_t rx_tx_buf[MAX_BUFFER_LENGTH];
85 
86     //! [Interesting]
87     mraa_i2c_context i2c;
88     i2c = mraa_i2c_init(0);
89 
90     mraa_i2c_address(i2c, HMC5883L_I2C_ADDR);
91     rx_tx_buf[0] = HMC5883L_CONF_REG_B;
92     rx_tx_buf[1] = GA_1_3_REG;
93     mraa_i2c_write(i2c, rx_tx_buf, 2);
94     //! [Interesting]
95 
96     mraa_i2c_address(i2c, HMC5883L_I2C_ADDR);
97     rx_tx_buf[0] = HMC5883L_MODE_REG;
98     rx_tx_buf[1] = HMC5883L_CONT_MODE;
99     mraa_i2c_write(i2c, rx_tx_buf, 2);
100 
101     for (;;) {
102 #if 0
103         int i = 0;
104         //alternative, equivalent method which helps to understand exactly what
105         //the below does
106         mraa_i2c_address(i2c, HMC5883L_I2C_ADDR);
107         for (i = 0; i < DATA_REG_SIZE; i++) {
108             mraa_i2c_read_byte_data(i2c, HMC5883L_DATA_REG+i);
109         }
110 #endif
111         // first 'select' the register we want to read from
112         mraa_i2c_address(i2c, HMC5883L_I2C_ADDR);
113         mraa_i2c_write_byte(i2c, HMC5883L_DATA_REG);
114 
115         // then we read from that register incrementing with every read the
116         // chosen register
117         mraa_i2c_address(i2c, HMC5883L_I2C_ADDR);
118         // this call behaves very similarly to the Wire receive() call
119         mraa_i2c_read(i2c, rx_tx_buf, DATA_REG_SIZE);
120 
121         x = (rx_tx_buf[HMC5883L_X_MSB_REG] << 8) | rx_tx_buf[HMC5883L_X_LSB_REG];
122         z = (rx_tx_buf[HMC5883L_Z_MSB_REG] << 8) | rx_tx_buf[HMC5883L_Z_LSB_REG];
123         y = (rx_tx_buf[HMC5883L_Y_MSB_REG] << 8) | rx_tx_buf[HMC5883L_Y_LSB_REG];
124 
125         // scale and calculate direction
126         direction = atan2(y * SCALE_0_92_MG, x * SCALE_0_92_MG);
127 
128         // check if the signs are reversed
129         if (direction < 0)
130             direction += 2 * M_PI;
131 
132         printf("Compass scaled data x : %f, y : %f, z : %f\n", x * SCALE_0_92_MG, y * SCALE_0_92_MG,
133                z * SCALE_0_92_MG);
134         printf("Heading : %f\n", direction * 180 / M_PI);
135     }
136 }
137