upm  1.7.1
Sensor/Actuator repository for libmraa (v2.0.0)
mag3110.hpp
1 /*
2  * Author: Norbert Wesp <nwesp@phytec.de>
3  * Copyright (c) 2017 Phytec Messtechnik GmbH.
4  *
5  * based on: RIOT-driver mag3110 by Johann Fischer <j.fischer@phytec.de>
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining
8  * a copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sublicense, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  */
26 
27 #pragma once
28 
29 #include <string>
30 #include <mraa/i2c.hpp>
31 #include <stdint.h>
32 #include <stdbool.h>
33 
34 #define MAG3110_NAME "MAG3110"
35 #define MAG3110_I2C_ADDRESS 0x0E
36 #define MAG3110_DEVICE_ID 0xC4
37 #define MAG3110_DEVICE_ID_REG 0x07
38 
39 /* MAG3110 Register Map */
40 #define MAG3110_DR_STATUS 0x00
41 #define MAG3110_OUT_X_MSB 0x01
42 #define MAG3110_OUT_X_LSB 0x02
43 #define MAG3110_OUT_Y_MSB 0x03
44 #define MAG3110_OUT_Y_LSB 0x04
45 #define MAG3110_OUT_Z_MSB 0x05
46 #define MAG3110_OUT_Z_LSB 0x06
47 #define MAG3110_SYSMOD 0x08
48 #define MAG3110_OFF_X_MSB 0x09
49 #define MAG3110_OFF_X_LSB 0x0A
50 #define MAG3110_OFF_Y_MSB 0x0B
51 #define MAG3110_OFF_Y_LSB 0x0C
52 #define MAG3110_OFF_Z_MSB 0x0D
53 #define MAG3110_OFF_Z_LSB 0x0E
54 #define MAG3110_DIE_TEMP 0x0F
55 #define MAG3110_CTRL_REG1 0x10
56 #define MAG3110_CTRL_REG2 0x11
58 /* MAG3110 DR-STATUS Register */
59 #define MAG3110_DR_STATUS_ZYXOW (1 << 7)
60 #define MAG3110_DR_STATUS_ZOW (1 << 6)
61 #define MAG3110_DR_STATUS_YOW (1 << 5)
62 #define MAG3110_DR_STATUS_XOW (1 << 4)
63 #define MAG3110_DR_STATUS_ZYXDR (1 << 3)
64 #define MAG3110_DR_STATUS_ZDR (1 << 2)
65 #define MAG3110_DR_STATUS_YDR (1 << 1)
66 #define MAG3110_DR_STATUS_XDR (1 << 0)
67 
68 /* MAG3110 SYSMOD Register */
69 #define MAG3110_SYSMOD_STANDBY 0
70 #define MAG3110_SYSMOD_ACTIVE_RAW 1
71 #define MAG3110_SYSMOD_ACTIVE 2
72 
73 /* MAG3110 Control Register 1 */
74 #define MAG3110_CTRL_REG1_DROS_SHIFT 3
75 #define MAG3110_CTRL_REG1_DROS_MASK 0xF8
76 #define MAG3110_CTRL_REG1_DROS(x) (((uint8_t)(((uint8_t)(x))<<\
77  MAG3110_CTRL_REG1_DROS_SHIFT))\
78  &MAG3110_CTRL_REG1_DROS_MASK)
79 #define MAG3110_CTRL_REG1_FR (1 << 2)
80 #define MAG3110_CTRL_REG1_TM (1 << 1)
81 #define MAG3110_CTRL_REG1_AC (1 << 0)
82 
83 /* MAG3110 Control Register 2 */
84 #define MAG3110_CTRL_REG2_AUTO_MRST_EN (1 << 7)
85 #define MAG3110_CTRL_REG2_RAW (1 << 5)
86 #define MAG3110_CTRL_REG2_MAG_RST (1 << 4)
87 
88 /* MAG3110 Output Rate (DR) and Over Sample (OS) Ratio for CTRL_REG1 */
89 #define MAG3110_DROS_8000_16 0 /* DR 80 Hz, OS Ratio 16 */
90 #define MAG3110_DROS_4000_32 1 /* DR 40 Hz, OS Ratio 32 */
91 #define MAG3110_DROS_2000_64 2 /* DR 20 Hz, OS Ratio 64 */
92 #define MAG3110_DROS_1000_128 3 /* DR 10 Hz, OS Ratio 128 */
93 #define MAG3110_DROS_4000_16 4 /* DR 40 Hz, OS Ratio 16 */
94 #define MAG3110_DROS_2000_32 5 /* DR 20 Hz, OS Ratio 32 */
95 #define MAG3110_DROS_1000_64 6 /* DR 10 Hz, OS Ratio 64 */
96 #define MAG3110_DROS_0500_128 7 /* DR 5 Hz, OS Ratio 128 */
97 #define MAG3110_DROS_2000_16 8 /* DR 20 Hz, OS Ratio 16 */
98 #define MAG3110_DROS_1000_32 9 /* DR 10 Hz, OS Ratio 32 */
99 #define MAG3110_DROS_0500_64 10 /* DR 5 Hz, OS Ratio 64 */
100 #define MAG3110_DROS_0250_128 11 /* DR 2.5 Hz, OS Ratio 128 */
101 #define MAG3110_DROS_1000_16 12 /* DR 10 Hz, OS Ratio 16 */
102 #define MAG3110_DROS_0500_32 13 /* DR 5 Hz, OS Ratio 32 */
103 #define MAG3110_DROS_0250_64 14 /* DR 2.5 Hz, OS Ratio 64 */
104 #define MAG3110_DROS_0125_128 15 /* DR 1.25 Hz, OS Ratio 128 */
105 #define MAG3110_DROS_0500_16 16 /* DR 5 Hz, OS Ratio 16 */
106 #define MAG3110_DROS_0250_32 17 /* DR 2.5 Hz, OS Ratio 32 */
107 #define MAG3110_DROS_0125_64 18 /* DR 1.25 Hz, OS Ratio 64 */
108 #define MAG3110_DROS_0063_128 19 /* DR 0.63 Hz, OS Ratio 128 */
109 #define MAG3110_DROS_0250_16 20 /* DR 2.5 Hz, OS Ratio 16 */
110 #define MAG3110_DROS_0125_32 21 /* DR 1.25 Hz, OS Ratio 32 */
111 #define MAG3110_DROS_0063_64 22 /* DR 0.63 Hz, OS Ratio 64 */
112 #define MAG3110_DROS_0031_128 23 /* DR 0.31 Hz, OS Ratio 128 */
113 #define MAG3110_DROS_0125_16 24 /* DR 1.25 Hz, OS Ratio 16 */
114 #define MAG3110_DROS_0063_32 25 /* DR 0.63 Hz, OS Ratio 32 */
115 #define MAG3110_DROS_0031_64 26 /* DR 0.31 Hz, OS Ratio 64 */
116 #define MAG3110_DROS_0016_128 27 /* DR 0.16 Hz, OS Ratio 128 */
117 #define MAG3110_DROS_0063_16 28 /* DR 0.63 Hz, OS Ratio 16 */
118 #define MAG3110_DROS_0031_32 29 /* DR 0.31 Hz, OS Ratio 32 */
119 #define MAG3110_DROS_0016_64 30 /* DR 0.16 Hz, OS Ratio 64 */
120 #define MAG3110_DROS_0008_128 31 /* DR 0.08 Hz, OS Ratio 128 */
121 #define MAG3110_DROS_DEFAULT MAG3110_DROS_0125_128
122  /* Default Setting for testing*/
123 
124 namespace upm {
125 
126 typedef struct {
127  int16_t x;
128  int16_t y;
129  int16_t z;
130  uint8_t status;
131  int8_t dtemp;
133 
165 class MAG3110 {
166  public:
175  MAG3110 (int bus, uint8_t dros=MAG3110_DROS_DEFAULT,
176  int devAddr=MAG3110_I2C_ADDRESS);
177 
184  int checkID(void);
185 
197  int setUserOffset(int16_t x, int16_t y, int16_t z);
198 
205  int setActive(void);
206 
213  int setStandby(void);
214 
221  int isReady(void);
222 
232  int sampleData(void);
233 
241  int16_t getX(int bSampleData = 0);
242 
250  int16_t getY(int bSampleData = 0);
251 
259  int16_t getZ(int bSampleData = 0);
260 
266  uint8_t getStatus(void);
267 
273  int8_t getDieTemperature(void);
274 
283  int getData(mag3110_data_t* data, int bSampleData = 0);
284 
285  private:
286 
287  std::string m_name;
288 
289  int m_controlAddr;
290  int m_bus;
291  mraa::I2c m_i2ControlCtx;
292 
293  mag3110_data_t s_data[1];
294 };
295 
296 }
int getData(mag3110_data_t *data, int bSampleData=0)
Definition: mag3110.cpp:253
int checkID(void)
Definition: mag3110.cpp:87
int16_t getX(int bSampleData=0)
Definition: mag3110.cpp:202
int16_t getY(int bSampleData=0)
Definition: mag3110.cpp:215
int8_t getDieTemperature(void)
Definition: mag3110.cpp:247
MAG3110(int bus, uint8_t dros=MAG3110_DROS_DEFAULT, int devAddr=MAG3110_I2C_ADDRESS)
Definition: mag3110.cpp:39
Definition: mag3110.hpp:126
C++ API wrapper for the bh1749 driver.
Definition: a110x.hpp:29
int setUserOffset(int16_t x, int16_t y, int16_t z)
Definition: mag3110.cpp:101
uint8_t getStatus(void)
Definition: mag3110.cpp:241
int16_t getZ(int bSampleData=0)
Definition: mag3110.cpp:228
API for the MAG3110 Three-Axis Digital Magnetometer.
Definition: mag3110.hpp:165
int setStandby(void)
Definition: mag3110.cpp:148
int setActive(void)
Definition: mag3110.cpp:129
int sampleData(void)
Definition: mag3110.cpp:177
int isReady(void)
Definition: mag3110.cpp:167