Blame view

RIOT/drivers/include/adxl345.h 7.61 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
  /*
   * Copyright (C) 2017 Mesotic SAS
   *
   * 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_adxl345 ADXL345 3-Axis accelerometer
   * @ingroup     drivers_sensors
   * @brief       Device driver interface for the ADXL345
   * @{
   *
   * @file
   * @brief       Interface definition for the ADXL345
   *
   * @author     Dylan Laduranty <dylan.laduranty@mesotic.com>
   */
  
  #ifndef ADXL345_H
  #define ADXL345_H
  
  #ifdef __cplusplus
  extern "C" {
  #endif
  
  #include "periph/i2c.h"
  #include "periph/gpio.h"
  
  /**
   * @brief   Possible ADXL345 hardware addresses (wiring specific)
   */
  enum {
      ADXL345_ADDR_1D = 0x1D, /**< I2C device address if Alt addr pin is high */
      ADXL345_ADDR_53 = 0x53, /**< I2C device address if Alt addr pin is low */
  };
  
  /**
   * @brief   List ADXL345 power mode
   */
  enum {
      ADXL345_MEASURE_MODE,
      ADXL345_STANDBY_MODE,
      ADXL345_SLEEP_MODE,
      ADXL345_AUTOSLEEP_MODE,
  };
  
  /**
   * @brief   Define ADXL345 sensitivity
   */
  enum {
      ADXL345_RANGE_2G    = 1,     /**< +/- 2 g Full Scale Rang */
      ADXL345_RANGE_4G    = 2,     /**< +/- 4 g Full Scale Rang */
      ADXL345_RANGE_8G    = 4,     /**< +/- 8 g Full Scale Rang */
      ADXL345_RANGE_16G   = 8      /**< +/- 16 g Full Scale Rang */
  };
  
  /**
   * @brief   List bandwidth rate
   */
  enum {
      ADXL345_RATE_0HZ1   = 0,   /**< 0.1 Hz Output Data Rate */
      ADXL345_RATE_0HZ2   = 1,   /**< 0.2 Hz Output Data Rate */
      ADXL345_RATE_0HZ39  = 2,   /**< 0.39 Hz Output Data Rate */
      ADXL345_RATE_0HZ78  = 3,   /**< 0.78 Hz Output Data Rate */
      ADXL345_RATE_1HZ56  = 4,   /**< 1.56 Hz Output Data Rate */
      ADXL345_RATE_3HZ13  = 5,   /**< 3.13 Hz Output Data Rate */
      ADXL345_RATE_6HZ25  = 6,   /**< 6.25 Hz Output Data Rate */
      ADXL345_RATE_12HZ50 = 7,   /**< 12.5 Hz Output Data Rate */
      ADXL345_RATE_25HZ   = 8,   /**< 25 Hz Output Data Rate */
      ADXL345_RATE_50HZ   = 9,   /**< 50 Hz Output Data Rate */
      ADXL345_RATE_100HZ  = 10,  /**< 100 Hz Output Data Rate */
      ADXL345_RATE_200HZ  = 11,  /**< 200 Hz Output Data Rate */
      ADXL345_RATE_400HZ  = 12,  /**< 400 Hz Output Data Rate */
      ADXL345_RATE_800HZ  = 13,  /**< 800 Hz Output Data Rate */
      ADXL345_RATE_1600HZ = 14,  /**< 1600 Hz Output Data Rate */
      ADXL345_RATE_3200HZ = 15   /**< 3200 Hz Output Data Rate */
  };
  
  /**
   * @brief   List fifo mode
   */
  enum {
      BYPASS  = 0,          /**< FIFO bypass mode */
      FIFO    = 1,          /**< FIFO mode */
      STREAM  = 2,          /**< FIFO stream mode */
      TRIGGER = 3           /**< FIFO trigger mode */
  };
  
  /**
   * @brief   Output Interrupt selection
   */
  enum {
      INT1,  /**< Output interrupt on INT1 pin */
      INT2   /**< Output interrupt on INT2 pin */
  };
  
  /**
   * @brief   Named return values
   */
  enum {
      ADXL345_OK          =  0,       /**< everything was fine */
      ADXL345_DATA_READY  =  1,       /**< new data ready to be read */
      ADXL345_NOI2C       = -1,       /**< I2C communication failed */
      ADXL345_NODEV       = -2,       /**< no ADXL345 device found on the bus */
      ADXL345_NODATA      = -3        /**< no data available */
  };
  
  /**
   * @brief   ADXL345 result vector struct
   */
  typedef struct {
      int16_t x;             /**< X-Axis measurement result */
      int16_t y;             /**< Y-Axis measurement result */
      int16_t z;             /**< Z-Axis measurement result */
  } adxl345_data_t;
  
  /**
   * @brief   Interrupt configuration struct for the ADXL345 sensor
   */
  typedef struct {
      uint8_t source;       /**< Source of interrupts */
      uint8_t map;          /**< Interrupt mapping control */
      uint8_t enable;       /**< Interrupt enable control */
      uint8_t thres_tap;    /**< Tap threshold */
      uint8_t thres_dur;    /**< Tap duration */
      uint8_t thres_latent; /**< Tap latency */
      uint8_t thres_window; /**< Tap window */
      uint8_t thres_act;    /**< Activity threshold */
      uint8_t thres_inact;  /**< Inactivity threshold */
      uint8_t time_inact;   /**< Inactivity time */
      uint8_t thres_ff;     /**< Free-fall threshold */
      uint8_t time_ff;      /**< Time threshold */
      uint8_t act_inact;    /**< Enable ctrl for activity/inactivity detection */
      uint8_t tap_axes;     /**< Axis control for single tap/double tap */
  } adxl345_interrupt_t;
  
  /**
   * @brief   Configuration struct for the ADXL345 sensor
   */
  typedef struct {
      gpio_t int1;              /**< accelerometer int1 pin */
      gpio_t int2;              /**< accelerometer int2 pin */
      uint8_t offset[3];        /**< offset axis */
      uint8_t range;            /**< Sensitivity configuration */
      uint8_t rate;             /**< Configured sample rate for accel  */
      uint8_t full_res;         /**< Resolution bit */
  } adxl345_params_t;
  
  /**
   * @brief   Device descriptor for the ADXL345 sensor
   */
  typedef struct {
      i2c_t i2c;                      /**< I2C device which is used */
      uint8_t addr;                   /**< I2C address */
      adxl345_params_t *params;       /**< Device configuration */
      adxl345_interrupt_t interrupt;  /**< Interrupts configuration  */
      float scale_factor;             /**< Scale factor for converting value to mg */
  } adxl345_t;
  
  /**
   * @brief   Initialize the ADXL345 accelerometer driver.
   *
   * @param[out] dev          device descriptor of accelerometer to initialize
   * @param[in]  params       configuration parameters
   *
   * @return                  ADXL345_OK on success
   * @return                  ADXL345_NOI2C if initialization of I2C bus failed
   * @return                  ADXL345_NODEV if accelerometer test failed
   */
  int adxl345_init(adxl345_t *dev, const adxl345_params_t* params);
  /**
   * @brief   Read accelerometer's data
   *
   * Acceleration will be calculated as:<br>
   *     \f$ accel = {value \times 3.9} \f$ if full scale is set to 2g<br>
   *     \f$ accel = {value \times 7.8} \f$ if full scale is set to 4g<br>
   *     \f$ accel = {value \times 15.6} \f$ if full scale is set to 8g<br>
   *     \f$ accel = {value \times 31.2} \f$ if full scale is set to 16g<br>
   *
   * @param[in]  dev          device descriptor of accelerometer
   * @param[out] data         the current acceleration data [in mg]
   */
  void adxl345_read(const adxl345_t *dev, adxl345_data_t *data);
  
  /**
   * @brief   set ADXL345's interrupts configuration
   *
   * @param[in]  dev          device descriptor of accelerometer
   */
  void adxl345_set_interrupt(const adxl345_t *dev);
  
  /**
   * @brief   set ADXL345's measure mode
   *
   * @param[in]  dev          device descriptor of accelerometer
   */
  void adxl345_set_measure(const adxl345_t *dev);
  
  /**
   * @brief   Set standby mode
   *
   * @param[in]  dev          device descriptor of accelerometer
   */
  void adxl345_set_standby(const adxl345_t *dev);
  
  /**
   * @brief   Set sleep mode
   *
   * @param[in]  dev          device descriptor of accelerometer
   */
  void adxl345_set_sleep(const adxl345_t *dev);
  
  /**
   * @brief   Set autosleep mode
   *
   * @param[in]  dev          device descriptor of accelerometer
   */
  void adxl345_set_autosleep(const adxl345_t *dev);
  
  /**
   * @brief   Set bandwidth rate
   *
   * @param[in]  dev          device descriptor of accelerometer
   * @param[in]  bw_rate      new datarate
   */
  void adxl345_set_bandwidth_rate(const adxl345_t *dev, uint8_t bw_rate);
  
  /**
   * @brief   Set fifo mode with its configuration.
   *
   * @param[in]  dev          device descriptor of accelerometer
   * @param[in]  mode         fifo mode configuration
   * @param[in]  output       set trigger output
   * @param[in]  value        set trigger's value
   */
  void adxl345_set_fifo_mode(const adxl345_t *dev, uint8_t mode,
                             uint8_t output, uint8_t value);
  
  #ifdef __cplusplus
  }
  #endif
  
  #endif /* ADXL345_H */
  /** @} */