Contiki 2.5
gyro-l3g4200d.c
Go to the documentation of this file.
1 /* Copyright (c) 2010, Ulf Kulau
2  *
3  * Permission is hereby granted, free of charge, to any person
4  * obtaining a copy of this software and associated documentation
5  * files (the "Software"), to deal in the Software without
6  * restriction, including without limitation the rights to use,
7  * copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following
10  * conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 /**
26  * \addtogroup Device Interfaces
27  * @{
28  *
29  * \addtogroup L3G4200D_interface
30  * @{
31  */
32 
33 /**
34  * \file
35  * ST L3G4200D 3-axis Gyroscope interface implementation
36  * \author
37  * Ulf Kulau <kulau@ibr.cs.tu-bs.de>
38  */
39 
40 #include "gyro-l3g4200d.h"
41 
42 int8_t l3g4200d_init(void) {
43  uint8_t i = 0;
44 
45  i2c_init();
46  while (l3g4200d_read8bit(L3G4000D_WHO_I_AM_REG) != 0xD3) {
47  _delay_ms(10);
48  if (i++ > 10) {
49  return -1;
50  }
51  }
52  l3g4200d_write8bit(L3G4000D_CTRL_REG1, 0x0F);
53  return 0;
54 }
55 
56 angle_data_t l3g4200d_get_angle(void) {
57  angle_data_t this_angle;
58 
59  this_angle.angle_x_value = l3g4200d_get_x_angle();
60  this_angle.angle_y_value = l3g4200d_get_y_angle();
61  this_angle.angle_z_value = l3g4200d_get_z_angle();
62  return this_angle;
63 }
64 
65 uint16_t l3g4200d_get_x_angle(void) {
66  return l3g4200d_read16bit(L3G4000D_OUT_X_L);
67 }
68 
69 uint16_t l3g4200d_get_y_angle(void) {
70  return l3g4200d_read16bit(L3G4000D_OUT_Y_L);
71 }
72 
73 uint16_t l3g4200d_get_z_angle(void) {
74  return l3g4200d_read16bit(L3G4000D_OUT_Z_L);
75 }
76 
77 int8_t l3g4200d_get_temp(void){
78  return 45-(int8_t)l3g4200d_read8bit(L3G4000D_OUT_TEMP);
79 }
80 
81 uint8_t l3g4200d_read8bit(uint8_t addr) {
82  uint8_t lsb = 0;
83  i2c_start(L3G4200D_DEV_ADDR_W);
84  i2c_write(addr);
85  i2c_rep_start(L3G4200D_DEV_ADDR_R);
86  i2c_read_nack(&lsb);
87  i2c_stop();
88  return lsb;
89 }
90 
91 uint16_t l3g4200d_read16bit(uint8_t addr) {
92  uint8_t lsb = 0, msb = 0;
93  i2c_start(L3G4200D_DEV_ADDR_W);
94  i2c_write((addr | 0x80));
95  i2c_rep_start(L3G4200D_DEV_ADDR_R);
96  i2c_read_ack(&lsb);
97  i2c_read_nack(&msb);
98  i2c_stop();
99  return (uint16_t) ((msb << 8) + lsb);
100 }
101 
102 void l3g4200d_write8bit(uint8_t addr, uint8_t data){
103  i2c_start(L3G4200D_DEV_ADDR_W);
104  i2c_write(addr);
105  i2c_write(data);
106  i2c_stop();
107 }