Contiki-Inga 3.x
l3g4200d.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, TU Braunschweig.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 /**
31  * \file
32  * ST L3G4200D 3-axis Gyroscope interface definitions
33  * \author
34  * Ulf Kulau <kulau@ibr.cs.tu-bs.de>
35  * Enrico Jörns <e.joerns@tu-bs.de>
36  */
37 
38 /**
39  * \addtogroup inga_sensors_driver
40  * @{
41  */
42 
43 /** \defgroup l3g4200d_interface ST L3G4200D 3-axis Gyroscope interface
44  *
45  * Details about sensors functionality can be found
46  * in corresponding datasheet.
47  * Registers and bit positions are named according to their datasheet names.
48  *
49  * @{
50  */
51 
52 #ifndef GYROL3G4200D_H_
53 #define GYROL3G4200D_H_
54 
55 #include "../dev/i2c.h"
56 #include <util/delay.h>
57 
58 /* Divisior for converting raw value to dps. */
59 //#define L3G4200D_DPSDIV_250G 35
60 //#define L3G4200D_DPSDIV_500G 70
61 //#define L3G4200D_DPSDIV_2000G 280
62 
63 extern uint16_t l3g4200d_dps_scale;
64 /** Convert raw values to dps */
65 #define l3g4200d_raw_to_dps(raw) (int16_t) (((int32_t) raw * l3g4200d_dps_scale) / 4000)
66 
67 #define L3G4200D_WHO_AM_I 0xD3
68 /* Gyroscope device address*/
69 #define L3G4200D_DEV_ADDR_R 0xD3
70 #define L3G4200D_DEV_ADDR_W 0xD2
71 
72 #define L3G4200D_FIFO_SIZE 32
73 
74 /** \name Register addresses.
75  *
76  * Naming convention of the datasheet with prefix L3G4200D_ is used.
77  *
78  * @{ */
79 /// Device identification register
80 #define L3G4200D_WHO_AM_I_REG 0x0F
81 /// Control register 1
82 #define L3G4200D_CTRL_REG1 0x20
83 /// Control register 2
84 #define L3G4200D_CTRL_REG2 0x21
85 /// Control register 3
86 #define L3G4200D_CTRL_REG3 0x22
87 /// Control register 4
88 #define L3G4200D_CTRL_REG4 0x23
89 /// Control register 5
90 #define L3G4200D_CTRL_REG5 0x24
91 /// Reference value for Interrupt generation
92 #define L3G4200D_REFERENCE 0x25
93 /// Temperature data
94 #define L3G4200D_OUT_TEMP 0x26
95 /// Status register
96 #define L3G4200D_STATUS_REG 0x27
97 /// X-axis angular rate data. (low)
98 #define L3G4200D_OUT_X_L 0x28
99 /// X-axis angular rate data. (high)
100 #define L3G4200D_OUT_X_H 0x29
101 /// Y-axis angular rate data. (low)
102 #define L3G4200D_OUT_Y_L 0x2A
103 /// Y-axis angular rate data. (high)
104 #define L3G4200D_OUT_Y_H 0x2B
105 /// Z-axis angular rate data. (low)
106 #define L3G4200D_OUT_Z_L 0x2C
107 /// Z-axis angular rate data. (high)
108 #define L3G4200D_OUT_Z_H 0x2D
109 /// Fifo control register
110 #define L3G4200D_FIFO_CTRL_REG 0x2E
111 /// Fifo source register
112 #define L3G4200D_FIFO_SRC_REG 0x2F
113 /** @} */
114 
115 /** \name Bit addresses for CTRL_REG1
116  * @{ */
117 /// Output Data Rate selection (higher)
118 #define L3G4200D_DR1 7
119 /// Output Data Rate selection (lower)
120 #define L3G4200D_DR0 6
121 /// Bandwidth selection (higher)
122 #define L3G4200D_BW1 5
123 /// Bandwidth selection (lower)
124 #define L3G4200D_BW0 4
125 /// Power down mode enable.
126 #define L3G4200D_PD 3
127 /// Z axis enable.
128 #define L3G4200D_ZEN 2
129 /// Y axis enable.
130 #define L3G4200D_YEN 1
131 /// X axis enable
132 #define L3G4200D_XEN 0
133 /** @} */
134 
135 /** \name Bit addresses for CTRL_REG2.
136  * @{ */
137 /// High Pass filter Mode Selection (higher)
138 #define L3G4200D_HPM1 5
139 /// High Pass filter Mode Selection (lower)
140 #define L3G4200D_HPM0 4
141 /// High Pass filter Cutt Off frequency selection (highest)
142 #define L3G4200D_HPCF3 3
143 /// High Pass filter Cutt Off frequency selection
144 #define L3G4200D_HPCF2 2
145 /// High Pass filter Cutt Off frequency selection
146 #define L3G4200D_HPCF1 1
147 /// High Pass filter Cutt Off frequency selection (lowest)
148 #define L3G4200D_HPCF0 0
149 /** @} */
150 
151 /** \name Bit addresses for CTRL_REG3.
152  * @{ */
153 /// Interrupt enable on INT1 pin
154 #define L3G4200D_I1_INT1 7
155 /// Boot status available on INT1
156 #define L3G4200D_I1_BOOT 6
157 /// Interrupt active configuration on INT1
158 #define L3G4200D_H_LACTIVE 5
159 /// Push-Pull / Open drain
160 #define L3G4200D_PP_OD 4
161 /// Data Ready on DRDY/INT2
162 #define L3G4200D_I2_DRDY 3
163 /// FIFO Watermark interrupt on DRDY/INT2
164 #define L3G4200D_I2_WTM 2
165 /// FIFO Overrun interrupt on DRDY/INT2
166 #define L3G4200D_I2_ORUN 1
167 /// FIFO Empty interrupt on DRDY/INT2
168 #define L3G4200D_I2_EMPTY 0
169 /** @} */
170 
171 /** \name Bit addresses for CTRL_REG4.
172  * @{ */
173 /// Block Data Update
174 #define L3G4200D_BDU 7
175 /// Big/Little Endian Data Selection
176 #define L3G4200D_BLE 6
177 /// Full Scale Selection (higher)
178 #define L3G4200D_FS1 5
179 /// Full Scale Selection (lower)
180 #define L3G4200D_FS0 4
181 /// Self Test Enable (higher)
182 #define L3G4200D_ST1 2
183 /// Self Test Enable (lower)
184 #define L3G4200D_ST0 1
185 /// SPI Serial Interface mode selection
186 #define L3G4200D_SIM 0
187 /** @} */
188 
189 /** \name Bit addresses for CTRL_REG5.
190  * @{ */
191 /// Reboot memory content
192 #define L3G4200D_BOOT 7
193 /// FIFO enable
194 #define L3G4200D_FIFO_EN 6
195 /// High Pass filter Enable
196 #define L3G4200D_HPEN 4
197 /// INT1 selection configuration (higher)
198 #define L3G4200D_INT1_SEL1 3
199 /// INT1 selection configuration (lower)
200 #define L3G4200D_INT1_SEL0 2
201 /// Out selection configuration (higher)
202 #define L3G4200D_OUT_SEL1 1
203 /// Out selection configuration (lower)
204 #define L3G4200D_OUT_SEL0 0
205 /** @} */
206 
207 /** \name Bit addresses for STATUS_REG.
208  * @{ */
209 /// X, Y, Z-axis data overrun
210 #define L3G4200D_ZYXOR 7
211 /// Z axis data overrun
212 #define L3G4200D_ZOR 6
213 /// Y axis data overrun
214 #define L3G4200D_YOR 5
215 /// X axis data overrun
216 #define L3G4200D_XOR 4
217 /// X, Y, Z-axis new data available
218 #define L3G4200D_ZYXDA 3
219 /// Z axis new data available
220 #define L3G4200D_ZDA 2
221 /// Y axis new data available
222 #define L3G4200D_YDA 1
223 /// X axis new data available
224 #define L3G4200D_XDA 0
225 /** @} */
226 
227 /** \name Bit addresses for FIFO_CTRL_REG.
228  * @{ */
229 /// FIFO mode selection
230 #define L3G4200D_FM2 7
231 /// FIFO mode selection
232 #define L3G4200D_FM1 6
233 /// FIFO mode selection
234 #define L3G4200D_FM0 5
235 /// FIFO threshold. Watermark level setting (highest)
236 #define L3G4200D_WTM4 4
237 /// FIFO threshold. Watermark level setting
238 #define L3G4200D_WTM3 3
239 /// FIFO threshold. Watermark level setting
240 #define L3G4200D_WTM2 2
241 /// FIFO threshold. Watermark level setting
242 #define L3G4200D_WTM1 1
243 /// FIFO threshold. Watermark level setting (lowest)
244 #define L3G4200D_WTM0 0
245 /** @} */
246 
247 /** \name Bit addresses for FIFO_SRC_REG.
248  * @{ */
249 /// Watermark status
250 #define L3G4200D_WTM 7
251 /// Overrun bit status
252 #define L3G4200D_OVRN 6
253 /// FIFO empty bit
254 #define L3G4200D_EMPTY 5
255 /// FIFO stored data level (highest)
256 #define L3G4200D_FSS4 4
257 /// FIFO stored data level
258 #define L3G4200D_FSS3 3
259 /// FIFO stored data level
260 #define L3G4200D_FSS2 2
261 /// FIFO stored data level
262 #define L3G4200D_FSS1 1
263 /// FIFO stored data level (lowest)
264 #define L3G4200D_FSS0 0
265 /** @} */
266 
267 
268 /** \name Resolution Settings.
269  * @{ */
270 #define L3G4200D_250DPS (0x00 << L3G4200D_FS0)
271 #define L3G4200D_500DPS (0x01 << L3G4200D_FS0)
272 #define L3G4200D_2000DPS (0x02 << L3G4200D_FS0)
273 /** @} */
274 
275 /** \name Data rate Settings.
276  * @{ */
277 /** ODR: 100Hz */
278 #define L3G4200D_ODR_100HZ (0x0 << L3G4200D_DR0)
279 /** ODR: 200Hz */
280 #define L3G4200D_ODR_200HZ (0x1 << L3G4200D_DR0)
281 /** ODR: 400Hz */
282 #define L3G4200D_ODR_400HZ (0x2 << L3G4200D_DR0)
283 /** ODR: 800Hz */
284 #define L3G4200D_ODR_800HZ (0x3 << L3G4200D_DR0)
285 /** @} */
286 
287 /** \name Mode Settings.
288  * @{ */
289 #define L3G4200D_BYPASS (0x00 << L3G4200D_FM0)
290 #define L3G4200D_FIFO (0x01 << L3G4200D_FM0)
291 #define L3G4200D_STREAM (0x02 << L3G4200D_FM0)
292 #define L3G4200D_STREAM_TO_FIFO (0x03 << L3G4200D_FM0)
293 #define L3G4200D_BYPASS_TO_STREAM (0x04 << L3G4200D_FM0)
294 /** @} */
295 
296 
297 //--- types
298 
299 /** Angle data type. */
300 typedef struct {
301  int16_t x;
302  int16_t y;
303  int16_t z;
304 } angle_data_t;
305 
306 
307 //--- functions
308 /** Checks if l3g4200d is avialable.
309  * \retval 1 is available
310  * \retval 0 not available
311  */
312 int8_t l3g4200d_available(void);
313 
314 /** Inits the gyroscope.
315  * \retval 0 if init succeeded
316  * \retval 1 if init failed
317  */
318 int8_t l3g4200d_init(void);
319 
320 /** Deinit the gyroscope. */
321 int8_t l3g4200d_deinit(void);
322 
323 /** Sets the sensitivity value [dps]
324  *
325  * @param set One of L3G4200D_250DPS, L3G4200D_500DPS or L3G4200D_2000DBS
326  */
327 uint8_t l3g4200d_set_dps(uint8_t set);
328 
329 /**
330  * Sets the data rate [Hz]
331  *
332  * @param set one of L3G4200D_ODR_100HZ, L3G4200D_ODR_200HZ, L3G4200D_ODR_400HZ, L3G4200D_ODR_800HZ
333  * @return
334  */
335 uint8_t l3g4200d_set_data_rate(uint8_t set);
336 
337 /** Sets the fifo mode.
338  *
339  * @note To use stream or fifo mode, the fifo has to be enabled
340  * with l3g4200d_fifo_enable().
341  *
342  * @param set One of L3G4200D_BYPASS, L3G4200D_FIFO, L3G4200D_STREAM,
343  * L3G4200D_STREAM_TO_FIFO or L3G4200D_BYPASS_TO_STREAM
344  */
345 void l3g4200d_set_fifomode(uint8_t set);
346 
347 
348 /** Enables fifo mode.
349  */
350 void l3g4200d_fifo_enable(void);
351 
352 
353 /** Checks for fifo overrun.
354  *
355  * @return 0 = no overrun, else overrun
356  */
357 int8_t l3g4200d_fifo_overrun(void);
358 
359 
360 /** Reads data for x,y and z angle.
361  *
362  * @note If all channels are required, this function is faster than reading values with l3g4200d_get_x/y/z_angle, because only a single readout is performed!
363  */
365 
366 
367 /** Reads angle values from fifo.
368  *
369  * @note This will only work if fifo/stream mode is enabled.
370  *
371  * @param ret Output data is written to this address
372  * @return Number of data sets read (max. L3G4200D_FIFO_SIZE)
373  */
375 
376 
377 /** Reads x angle value
378  * @return x angle value
379  */
380 int16_t l3g4200d_get_x_angle(void);
381 
382 /** Reads y angle value
383  * @return y angle value
384  */
385 int16_t l3g4200d_get_y_angle(void);
386 
387 
388 /** Reads z angle value
389  * @return z angle value
390  */
391 int16_t l3g4200d_get_z_angle(void);
392 
393 
394 /** Reads temperature value
395  * @return 1 degree/digit
396  */
397 int8_t l3g4200d_get_temp(void);
398 
399 
400 /** Reads 2x8 bit register from gyroscopes
401  * @param addr address to read from (lower byte)
402  * @return register content
403  */
404 uint16_t l3g4200d_read16bit(uint8_t addr);
405 
406 
407 /** Reads 8 bit register from gyroscopes
408  * @param addr address to read from
409  * @return register content
410  */
411 uint8_t l3g4200d_read8bit(uint8_t addr);
412 
413 
414 /** Writes 8 bit to gyroscope register
415  * @param addr address to write to
416  * @param data data to write
417  */
418 void l3g4200d_write8bit(uint8_t addr, uint8_t data);
419 
420 #endif /* GYROL3G4200D_H_ */
421 
422 /** @} */
423 /** @} */