Statistics
| Branch: | Tag: | Revision:

amiro-os / include / amiro / accel / lis331dlh.hpp @ a3c54343

History | View | Annotate | Download (5.662 KB)

1
#ifndef LIS331DLH_HPP_
2
#define LIS331DLH_HPP_
3

    
4
#include <ch.hpp>
5

    
6
namespace amiro {
7

    
8
  class HWSPIDriver;
9

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

    
12
  public:
13

    
14
  struct InterruptConfig {
15

    
16
    uint8_t config;
17
    uint8_t ths;
18
    uint8_t duration;
19

    
20
  } __attribute__((packed));
21

    
22
  struct LIS331DLHConfig {
23
    uint8_t         ctrl1;
24
    uint8_t         ctrl2;
25
    uint8_t         ctrl3;
26
    uint8_t         ctrl4;
27
    uint8_t         ctrl5;
28
    InterruptConfig int1_cfg;
29
    InterruptConfig int2_cfg;
30
  };
31

    
32
  enum {
33
    AOI_OR_INT  = 0x00u,
34
    AOI_6MOVE   = 0x40u,
35
    AOI_AND_INT = 0x80u,
36
    AOI_6POS    = 0xC0u,
37
    ZHIE        = 0x20u,
38
    ZLIE        = 0x10u,
39
    YHIE        = 0x08u,
40
    YLIE        = 0x04u,
41
    XHIE        = 0x02u,
42
    XLIE        = 0x01u,
43
  };
44

    
45
  enum {
46
    IA          = 0x40u,
47
    ZH          = 0x20u,
48
    ZL          = 0x10u,
49
    YH          = 0x08u,
50
    YL          = 0x04u,
51
    XH          = 0x02u,
52
    XL          = 0x01u,
53
  };
54

    
55
  enum {
56
    THS_MASK      = 0x7Fu,
57
  };
58

    
59
  enum {
60
    DURATION_MASK = 0x7Fu,
61
  };
62

    
63
  enum {
64
    PM_OFF          = 0x00u,
65
    PM_ODR          = 0x20u,
66
    PM_0_5_HZ       = 0x40u,
67
    PM_1_HZ         = 0x60u,
68
    PM_2_HZ         = 0x80u,
69
    PM_5_HZ         = 0xA0u,
70
    PM_10_HZ        = 0xC0u,
71
    DR_50HZ_37LP    = 0x00u,
72
    DR_100HZ_74LP   = 0x08u,
73
    DR_400HZ_292LP  = 0x10u,
74
    DR_1000HZ_780LP = 0x18u,
75
    ZEN             = 0x04u,
76
    YEN             = 0x02u,
77
    XEN             = 0x01u,
78
  };
79

    
80
  enum {
81
    BOOT_REBOOT  = 0x80u,
82
    HPM_NORMAL0  = 0x00u,
83
    HPM_REF      = 0x20u,
84
    HPM_NORMAL1  = 0x40u,
85
    FDS_FILTER   = 0x10u,
86
    HPEN_INT2    = 0x08u,
87
    HPEN_INT1    = 0x04u,
88
    HPCF_8       = 0x00u,
89
    HPCF_16      = 0x01u,
90
    HPCF_32      = 0x02u,
91
    HPCF_64      = 0x03u,
92
  };
93

    
94
  enum {
95
    INT_HIGH     = 0x00u,
96
    INT_LOW      = 0x80u,
97
    PUSH_PULL    = 0x00u,
98
    OPEN_DRAIN   = 0x40u,
99
    LIR2_LATCH   = 0x20u,
100
    I2_CFG_I2    = 0x00u,
101
    I2_CFG_I1_I2 = 0x08u,
102
    I2_CFG_DRY   = 0x10u,
103
    I2_CFG_BOOT  = 0x18u,
104
    LIR1_LATCH   = 0x04u,
105
    I1_CFG_I1    = 0x00u,
106
    I1_CFG_I1_I2 = 0x01u,
107
    I1_CFG_DRY   = 0x02u,
108
    I1_CFG_BOOT  = 0x03u,
109
  };
110

    
111
  enum {
112
    BDU_CONT = 0x00u,
113
    BDU_STOP = 0x80u,
114
    BLE_LE   = 0x00u,
115
    BLE_BE   = 0x40u,
116
    FS_2G    = 0x00u,
117
    FS_4G    = 0x10u,
118
    FS_8G    = 0x30u,
119
    STSIGN_POS = 0x00u,
120
    STSIGN_NEG = 0x08u,
121
    ST_DISABLE = 0x00u,
122
    ST_ENABLE  = 0x02u,
123
    SIM_4WI    = 0x00u,
124
    SIM_3WI    = 0x01u,
125
  };
126

    
127
  enum {
128
    SLEEP_TO_WAKE_OFF = 0x00u,
129
    SLEEP_TO_WAKE_ON  = 0x03u,
130
  };
131

    
132
  /**
133
   * Return types of getCheck()
134
   */
135
  enum {
136
    CHECK_OK   = 0x00,
137
    CHECK_FAIL = 0x01,
138
  };
139

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

    
158
  //private:
159

    
160
  struct registers {
161
    uint8_t  reserved_0x00_0x0E[0x0Fu];
162
    uint8_t  who_am_i;
163
    uint8_t  reserved_0x10_0x1F[0x10u];
164
    uint8_t  ctrl_reg1;
165
    uint8_t  ctrl_reg2;
166
    uint8_t  ctrl_reg3;
167
    uint8_t  ctrl_reg4;
168
    uint8_t  ctrl_reg5;
169
    uint8_t  hp_filter_reset;
170
    uint8_t  reference;
171
    uint8_t  status_reg;
172
    uint16_t out_x; /* LE */
173
    uint16_t out_y; /* LE */
174
    uint16_t out_z; /* LE */
175
    uint8_t  reserved_0x2E_0x2F[0x02u];
176
    uint8_t  int1_cfg;
177
    uint8_t  int1_src;
178
    uint8_t  int1_ths;
179
    uint8_t  int1_duration;
180
    uint8_t  int2_cfg;
181
    uint8_t  int2_src;
182
    uint8_t  int2_ths;
183
    uint8_t  int2_duration;
184
    uint8_t  reserved_0x38_0x3F[0x08u];
185
  } __attribute__((packed));
186

    
187
  enum {
188
    LIS331DLH_ID = 0x32u
189
  };
190

    
191
  enum {
192
    ZYXOR = 0x80,
193
    ZOR   = 0x40,
194
    YOR   = 0x20,
195
    XOR   = 0x10,
196
    ZYXDA = 0x08,
197
    ZDA   = 0x04,
198
    YDA   = 0x02,
199
    XDA   = 0x01,
200
  };
201

    
202
  enum {
203
    SPI_MULT  = 0x40u,
204
    SPI_READ  = 0x80u,
205
    SPI_WRITE = 0x00u,
206
  };
207

    
208
  public:
209
    LIS331DLH(HWSPIDriver* driver);
210
    virtual ~LIS331DLH();
211

    
212
    chibios_rt::EvtSource* getEventSource();
213
    msg_t configure(LIS331DLHConfig* config);
214

    
215
    /**
216
    * Return the accelaration in LSB for the given axis.
217
    *
218
    * @param axis can be one of the axis [AXIS_X | AXIS_Y | AXIS_Z]
219
    *
220
    * @return Measured accelaration in in LSB for the given axis
221
    */
222
    int16_t getAcceleration(const uint8_t axis);
223

    
224
    /**
225
    * Return the accelaration in milli g for the given axis
226
    *
227
    * @param axis can be one of the axis [AXIS_X | AXIS_Y | AXIS_Z]
228
    *
229
    * @return Measured accelaration in in milli g for the given axis
230
    */
231
    int16_t getAccelerationForce(const uint8_t axis);
232

    
233
    /**
234
     * Check the presence of the accelerometer by reading
235
     * the identifier register and comparing it to the standard
236
     * value
237
     *
238
     * @return [CHECK_OK | CHECK_FAIL]
239
     */
240
    uint8_t getCheck();
241

    
242
    /**
243
     * Do the build in self-test of the device by applying
244
     * an electrostatic force and measuring the amplitude.
245
     * The result is then checked against the standard values
246
     * of the manual
247
     * TODO Check if it works correct
248
     *
249
     * @param config A working config as template (if NULL, predefined config is taken)
250
     */
251
    void printSelfTest(LIS331DLHConfig* config);
252

    
253
  protected:
254
    virtual msg_t main();
255

    
256
  private:
257
    inline void updateSensorData();
258

    
259
  private:
260

    
261
    HWSPIDriver* driver;
262
    chibios_rt::EvtSource eventSource;
263
    int16_t accelerations[AXIS_Z - AXIS_X + 1];
264
    /**
265
    * Stores the configuration [FS_2G | FS_4G | FS_8G] of
266
    * the current setup
267
    */
268
    uint8_t currentFullScaleConfiguration;
269

    
270

    
271
  };
272

    
273
} /* amiro */
274

    
275
#endif /* LIS331DLH_HPP_ */