Blame view

RIOT/drivers/include/mag3110.h 7.94 KB
a752c7ab   elopes   add first test an...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
  /*
   * Copyright (C) 2014 PHYTEC Messtechnik GmbH
   *               2017 HAW Hamburg
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @defgroup    drivers_mag3110 MAG3110 3-Axis Digital Magnetometer
   * @ingroup     drivers_sensors
   * @brief       Driver for the Freescale MAG3110 magnetometer.
   *              After initialization and set activ the magnetometer
   *              will make measurements at periodic times.
   *              The output rate and over sample ratio
   *              can be determined by magnetometer initialization.
   *              The measured values of magnetic field strength and
   *              die temperature have uncalibrated offsets.
   *              To get correct measurement values, the individual
   *              offsets must be measured and set accordingly.
   *
   * @{
   *
   * @file
   * @brief       Interface definition for the MAG3110 magnetometer driver.
   *
   * @author      Johann Fischer <j.fischer@phytec.de>
   * @author      Sebastian Meiling <s@mlng.net>
   */
  
  #ifndef MAG3110_H
  #define MAG3110_H
  
  #include <stdint.h>
  #include <stdbool.h>
  #include "periph/i2c.h"
  
  #ifdef __cplusplus
  extern "C"
  {
  #endif
  
  /**
   * @brief   Named return values
   */
  enum {
      MAG3110_OK,                 /**< all good */
      MAG3110_ERROR_I2C,          /**< I2C communication failed */
      MAG3110_ERROR_DEV,          /**< Device MAG3110 not found */
      MAG3110_ERROR_CNF,          /**< Device configuration failed */
  };
  
  #ifndef MAG3110_I2C_ADDRESS
  #define MAG3110_I2C_ADDRESS             0x0E /**< Magnetometer Default Address */
  #endif
  
  /**
   * @name    Output data rate macros
   * @{
   */
  #define MAG3110_DROS_8000_16    0 /**< Output Rate 80 Hz, Over Sample Ratio 16 */
  #define MAG3110_DROS_4000_32    1 /**< Output Rate 40 Hz, Over Sample Ratio 32 */
  #define MAG3110_DROS_2000_64    2 /**< Output Rate 20 Hz, Over Sample Ratio 64 */
  #define MAG3110_DROS_1000_128   3 /**< Output Rate 10 Hz, Over Sample Ratio 128 */
  #define MAG3110_DROS_4000_16    4 /**< Output Rate 40 Hz, Over Sample Ratio 16 */
  #define MAG3110_DROS_2000_32    5 /**< Output Rate 20 Hz, Over Sample Ratio 32 */
  #define MAG3110_DROS_1000_64    6 /**< Output Rate 10 Hz, Over Sample Ratio 64 */
  #define MAG3110_DROS_0500_128   7 /**< Output Rate 5 Hz, Over Sample Ratio 128 */
  #define MAG3110_DROS_2000_16    8 /**< Output Rate 20 Hz, Over Sample Ratio 16 */
  #define MAG3110_DROS_1000_32    9 /**< Output Rate 10 Hz, Over Sample Ratio 32 */
  #define MAG3110_DROS_0500_64    10 /**< Output Rate 5 Hz, Over Sample Ratio 64 */
  #define MAG3110_DROS_0250_128   11 /**< Output Rate 2.5 Hz, Over Sample Ratio 128 */
  #define MAG3110_DROS_1000_16    12 /**< Output Rate 10 Hz, Over Sample Ratio 16 */
  #define MAG3110_DROS_0500_32    13 /**< Output Rate 5 Hz, Over Sample Ratio 32 */
  #define MAG3110_DROS_0250_64    14 /**< Output Rate 2.5 Hz, Over Sample Ratio 64 */
  #define MAG3110_DROS_0125_128   15 /**< Output Rate 1.25 Hz, Over Sample Ratio 128 */
  #define MAG3110_DROS_0500_16    16 /**< Output Rate 5 Hz, Over Sample Ratio 16 */
  #define MAG3110_DROS_0250_32    17 /**< Output Rate 2.5 Hz, Over Sample Ratio 32 */
  #define MAG3110_DROS_0125_64    18 /**< Output Rate 1.25 Hz, Over Sample Ratio 64 */
  #define MAG3110_DROS_0063_128   19 /**< Output Rate 0.63 Hz, Over Sample Ratio 128 */
  #define MAG3110_DROS_0250_16    20 /**< Output Rate 2.5 Hz, Over Sample Ratio 16 */
  #define MAG3110_DROS_0125_32    21 /**< Output Rate 1.25 Hz, Over Sample Ratio 32 */
  #define MAG3110_DROS_0063_64    22 /**< Output Rate 0.63 Hz, Over Sample Ratio 64 */
  #define MAG3110_DROS_0031_128   23 /**< Output Rate 0.31 Hz, Over Sample Ratio 128 */
  #define MAG3110_DROS_0125_16    24 /**< Output Rate 1.25 Hz, Over Sample Ratio 16 */
  #define MAG3110_DROS_0063_32    25 /**< Output Rate 0.63 Hz, Over Sample Ratio 32 */
  #define MAG3110_DROS_0031_64    26 /**< Output Rate 0.31 Hz, Over Sample Ratio 64 */
  #define MAG3110_DROS_0016_128   27 /**< Output Rate 0.16 Hz, Over Sample Ratio 128 */
  #define MAG3110_DROS_0063_16    28 /**< Output Rate 0.63 Hz, Over Sample Ratio 16 */
  #define MAG3110_DROS_0031_32    29 /**< Output Rate 0.31 Hz, Over Sample Ratio 32 */
  #define MAG3110_DROS_0016_64    30 /**< Output Rate 0.16 Hz, Over Sample Ratio 64 */
  #define MAG3110_DROS_0008_128   31 /**< Output Rate 0.08 Hz, Over Sample Ratio 128 */
  #define MAG3110_DROS_DEFAULT    MAG3110_DROS_0125_128 /**< Default Setting for testing */
  /** @} */
  
  /**
   * @brief   Configuration parameters
   */
  typedef struct {
      i2c_t i2c;                  /**< I2C bus the device is connected to */
      uint8_t addr;               /**< I2C bus address of the device */
      uint8_t type;               /**< device type */
      uint8_t dros;               /**< sampling rate to use */
      int16_t offset[3];          /**< data offset in X, Y, and Z direction */
  } mag3110_params_t;
  
  /**
   * @brief   Device descriptor for MAG3110 magnetometer.
   */
  typedef struct {
      mag3110_params_t params;    /**< device configuration parameters */
  } mag3110_t;
  
  /**
   * @brief   Data type for the result data
   */
  typedef struct {
      int16_t x;                  /**< acceleration in X direction */
      int16_t y;                  /**< acceleration in Y direction */
      int16_t z;                  /**< acceleration in Z direction */
  } mag3110_data_t;
  
  /**
   * @brief   Initialise the MAG3110 magnetometer driver.
   *
   * @param[out] dev          device descriptor of magnetometer to initialize
   * @param[in]  params       configuration parameters
   *
   * @return                  0 on success
   * @return                  -1 if I2C communication failed
   * @return                  -2 if magnetometer test failed
   * @return                  -3 if magnetometer configuration failed
   */
  int mag3110_init(mag3110_t *dev, const mag3110_params_t *params);
  
  /**
   * @brief   Set user offset correction.
   *
   * Offset correction register will be erased after accelerometer reset.
   *
   * @param[out] dev          device descriptor of magnetometer
   * @param[in]  x            offset offset correction value for x-axis
   * @param[in]  y            offset offset correction value for y-axis
   * @param[in]  z            offset offset correction value for z-axis
   *
   * @return                  0 on success
   * @return                  -1 on error
   */
  int mag3110_set_user_offset(const mag3110_t *dev, int16_t x, int16_t y, int16_t z);
  
  /**
   * @brief   Set active mode, this enables periodic measurements.
   *
   * @param[out] dev          device descriptor of magnetometer
   *
   * @return                  0 on success
   * @return                  -1 on error
   */
  int mag3110_set_active(const mag3110_t *dev);
  
  /**
   * @brief   Set standby mode.
   *
   * @param[in]  dev          device descriptor of magnetometer
   *
   * @return                  0 on success
   * @return                  -1 on error
   */
  int mag3110_set_standby(const mag3110_t *dev);
  
  /**
   * @brief   Check for new set of measurement data.
   *
   * @param[in]  dev          device descriptor of magnetometer
   *
   * @return                  >0 if x-, y- and z-axis new sample is ready
   * @return                  0 if measurement is in progress
   * @return                  -1 on error
   */
  int mag3110_is_ready(const mag3110_t *dev);
  
  /**
   * @brief   Read magnetometer's data.
   *
   * To get the actual values for the magnetic field in \f$\mu T\f$,
   * one have to divide the returned values from the magnetometer by 10.
   *
   * @param[in]  dev          device descriptor of accelerometer
   * @param[out] data         the current magnetic field strength
   *
   * @return                  0 on success
   * @return                  -1 on error
   */
  int mag3110_read(const mag3110_t *dev, mag3110_data_t *data);
  
  /**
   * @brief   Read die temperature.
   *
   * @param[in]  dev          device descriptor of magnetometer
   * @param[out] dtemp        die temperature
   *
   * @return                  0 on success
   * @return                  -1 on error
   */
  int mag3110_read_dtemp(const mag3110_t *dev, int8_t *dtemp);
  
  #ifdef __cplusplus
  }
  #endif
  
  #endif /* MAG3110_H */
  /** @} */