Statistics
| Branch: | Tag: | Revision:

amiro-os / include / amiro / gyro / l3g4200d.hpp @ 3f899f5d

History | View | Annotate | Download (4.52 KB)

1
#ifndef L3G4200D_HPP_
2
#define L3G4200D_HPP_
3

    
4
#include <ch.hpp>
5

    
6
namespace amiro {
7

    
8
class HWSPIDriver;
9

    
10
class L3G4200D : public chibios_rt::BaseStaticThread<256> {
11

    
12
 public:
13
  enum {
14
    DR_100_HZ   = 0x00,
15
    DR_200_HZ   = 0x40,
16
    DR_400_HZ   = 0x80,
17
    DR_800_HZ   = 0xC0,
18
  };
19
  enum {
20
    BW_12_5  = 0x00,
21
    BW_20    = 0x00,
22
    BW_25    = 0x10,
23
    BW_30    = 0x00,
24
    BW_35    = 0x10,
25
    BW_50    = 0x20,
26
    BW_70    = 0x30,
27
    BW_110   = 0x30,
28
  };
29
  enum {
30
    PD  = 0x08,
31
    ZEN = 0x04,
32
    YEN = 0x02,
33
    XEN = 0x01,
34
  };
35
  enum {
36
    HPM_NORMAL_RST = 0x00,
37
    HPM_REFERENCE  = 0x10,
38
    HPM_NORMAL     = 0x20,
39
    HPM_AUTO_RST   = 0x30,
40
  };
41
  enum {
42
    HPCF_2    = 0x00,
43
    HPCF_4    = 0x01,
44
    HPCF_8    = 0x02,
45
    HPCF_16   = 0x03,
46
    HPCF_32   = 0x04,
47
    HPCF_64   = 0x05,
48
    HPCF_128  = 0x06,
49
    HPCF_256  = 0x07,
50
    HPCF_512  = 0x08,
51
    HPCF_1024 = 0x09,
52
  };
53
  enum {
54
    I1_INT1   = 0x80,
55
    I1_BOOT   = 0x40,
56
    H_IACTIVE = 0x20,
57
    PP_OD     = 0x10,
58
    I2_DRDY   = 0x08,
59
    I2_WTM    = 0x04,
60
    I2_ORUN   = 0x02,
61
    I2_EMPTY  = 0x01,
62
  };
63
  enum {
64
    BDU_CONT    = 0x00,
65
    BDU_SINGLE  = 0x80,
66
    BLE_MSB     = 0x40,
67
    BLE_LSB     = 0x00,
68
    FS_250_DPS  = 0x00,
69
    FS_500_DPS  = 0x10,
70
    FS_2000_DPS = 0x20,
71
    ST_SIGN_M   = 0x04,
72
    ST_SIGN_P   = 0x00,
73
    ST_EN       = 0x02,
74
    SIM_3W      = 0x01,
75
    SIM_4W      = 0x00,
76
  };
77
  enum {
78
    BOOT          = 0x80,
79
    FIFO_EN       = 0x40,
80
    HP_EN         = 0x10,
81
    INT1_SEL_NOHP = 0x00,
82
    INT1_SEL_HP   = 0x04,
83
    INT1_SEL_LP   = 0x08,
84
    OUT_SEL_NOHP  = 0x00,
85
    OUT_SEL_HP    = 0x01,
86
    OUT_SEL_LP    = 0x02,
87
  };
88
  enum {
89
    ZYXOR = 0x80,
90
    ZOR   = 0x40,
91
    YOR   = 0x20,
92
    XOR   = 0x10,
93
    ZYXDA = 0x08,
94
    ZDA   = 0x04,
95
    YDA   = 0x02,
96
    XDA   = 0x01,
97
  };
98
  enum {
99
    FM_BYPASS        = 0x00,
100
    FM_FMMODE        = 0x20,
101
    FM_STREAM        = 0x40,
102
    FM_STREAM2FIFO   = 0x60,
103
    FM_BYPASS2STREAM = 0x80,
104
    WTM_MASK         = 0x1F,
105
  };
106
  enum {
107
    WTM      = 0x80,
108
    OVRN     = 0x40,
109
    EMPTY    = 0x20,
110
    FSS_MASK = 0x1F,
111
  };
112
  enum {
113
    ANDOR = 0x80,
114
    LIR   = 0x40,
115
    ZHIE  = 0x20,
116
    ZLIE  = 0x10,
117
    YHIE  = 0x08,
118
    YLIE  = 0x04,
119
    XHIE  = 0x02,
120
    XLIE  = 0x01,
121
  };
122
  enum {
123
    IA = 0x40,
124
    ZH = 0x20,
125
    ZL = 0x10,
126
    YH = 0x08,
127
    YL = 0x04,
128
    XH = 0x02,
129
    XL = 0x01,
130
  };
131
  enum {
132
    THS_L_MASK = 0x7F
133
  };
134
  enum {
135
    INT1_WAIT          = 0x80,
136
    INT1_DURATION_MASK = 0x7F,
137
  };
138

    
139
  /**
140
   * Return types of getCheck()
141
   */
142
  enum {
143
    CHECK_OK = 0x00u,
144
    CHECK_FAIL = 0x01u,
145
  };
146

    
147
  /**
148
   * Top view of the AMiRo with charger in the back (F:Front, B:Back).
149
   * Z is pointing into the ground (Apply right-hand-rule):
150
   *    ___________
151
   *   /    F      \
152
   *  /     X       \
153
   * |      |        |
154
   * |      Z---Y    |
155
   * |               |
156
   *  \             /
157
   *   \____B______/
158
   */
159
  enum {
160
    AXIS_X = 0x00u,
161
    AXIS_Y = 0x01u,
162
    AXIS_Z = 0x02u,
163
  };
164

    
165
  //private:
166

    
167
  struct registers {
168
    uint8_t RESERVED_0x00_0x0E[0x0Fu];
169
    uint8_t WHO_AM_I;
170
    uint8_t RESERVED_0x10_0x1F[0x10u];
171
    uint8_t CTRL_REG1;
172
    uint8_t CTRL_REG2;
173
    uint8_t CTRL_REG3;
174
    uint8_t CTRL_REG4;
175
    uint8_t CTRL_REG5;
176
    uint8_t REFERENCE;
177

    
178
    uint8_t OUT_TEMP;
179

    
180
    uint8_t STATUS_REG;
181

    
182
    uint16_t OUT_X; /* LE */
183
    uint16_t OUT_Y; /* LE */
184
    uint16_t OUT_Z; /* LE */
185

    
186
    uint8_t FIFO_CTRL_REG;
187
    uint8_t FIFO_SRC_REG;
188

    
189
    uint8_t INT1_CFG;
190
    uint8_t INT1_SRC;
191
    uint8_t INT1_THS_XH;
192
    uint8_t INT1_THS_XL;
193
    uint8_t INT1_THS_YH;
194
    uint8_t INT1_THS_YL;
195
    uint8_t INT1_THS_ZH;
196
    uint8_t INT1_THS_ZL;
197
    uint8_t INT1_DURATION;
198
    uint8_t RESERVED_0x38_0x3F[0x08u];
199
  }__attribute__((packed));
200

    
201
  enum {
202
    L3G4200D_ID = 0xD3u
203
  };
204

    
205
  enum {
206
    SPI_MULT = 0x40u,
207
    SPI_READ = 0x80u,
208
    SPI_WRITE = 0x00u,
209
  };
210

    
211
 public:
212

    
213
  struct L3G4200DConfig {
214
    uint8_t         ctrl1;
215
    uint8_t         ctrl2;
216
    uint8_t         ctrl3;
217
    uint8_t         ctrl4;
218
    uint8_t         ctrl5;
219
  };
220

    
221
  L3G4200D(HWSPIDriver* driver);
222
  virtual ~L3G4200D();
223

    
224
  chibios_rt::EvtSource* getEventSource();
225
  msg_t configure(const L3G4200DConfig* config);
226
  int16_t angularRate[AXIS_Z - AXIS_X + 1];
227

    
228
  int16_t getAngularRate(const uint8_t axis);
229

    
230

    
231
  /**
232
   * Check the presence of the accelerometer by reading
233
   * the identifier register and comparing it to the standard
234
   * value
235
   */
236
  uint8_t getCheck();
237

    
238
 protected:
239
  virtual msg_t main();
240

    
241
 private:
242
  inline void updateSensorData();
243

    
244
 private:
245

    
246
  HWSPIDriver* driver;
247
  chibios_rt::EvtSource eventSource;
248

    
249
};
250

    
251
} /* amiro */
252

    
253
#endif /* L3G4200D_HPP_ */