ChibiOS/EX 1.3.0
adxl317.h
Go to the documentation of this file.
1/*
2 ChibiOS - Copyright (C) 2016..2023 Simona Di Domenico
3
4 This file is part of ChibiOS.
5
6 ChibiOS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 ChibiOS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19*/
20
21/**
22 * @file adxl317.h
23 * @brief ADXL317 MEMS interface module header.
24 *
25 * @addtogroup ADXL317
26 * @ingroup EX_ADI
27 * @{
28 */
29
30#ifndef _ADXL317_H_
31#define _ADXL317_H_
32
33#include "ex_accelerometer.h"
34
35/*===========================================================================*/
36/* Driver constants. */
37/*===========================================================================*/
38
39/**
40 * @name Version identification
41 * @{
42 */
43/**
44 * @brief ADXL317 driver version string.
45 */
46#define EX_ADXL317_VERSION "1.0.0"
47
48/**
49 * @brief ADXL317 driver version major number.
50 */
51#define EX_ADXL317_MAJOR 1
52
53/**
54 * @brief ADXL317 driver version minor number.
55 */
56#define EX_ADXL317_MINOR 0
57
58/**
59 * @brief ADXL317 driver version patch number.
60 */
61#define EX_ADXL317_PATCH 0
62/** @} */
63
64/**
65 * @brief ADXL317 accelerometer subsystem characteristics.
66 * @note Sensitivity is expressed as milli-G/LSB whereas
67 * 1 milli-G = 0.00980665 m/s^2.
68 * @note Bias is expressed as milli-G.
69 *
70 * @{
71 */
72#define ADXL317_ACC_NUMBER_OF_AXES 3U
73
74
75#define ADXL317_ACC_SENS 2.0f
76#define ADXL317_ACC_BIAS 0.0f
77
78/** @} */
79
80/**
81 * @name ADXL317 register addresses
82 * @{
83 */
84#define ADXL317_AD_DEVID_0 0x00
85#define ADXL317_AD_DEVID_1 0x01
86#define ADXL317_AD_USER_REG_KEY 0x80
87#define ADXL317_AD_I2C_CFGO 0x81
88#define ADXL317_AD_I2C_CFG1 0x82
89#define ADXL317_AD_CLK_RATE 0x83
90#define ADXL317_AD_X_ST 0x84
91#define ADXL317_AD_X_FILT 0x85
92#define ADXL317_AD_Y_ST 0x86
93#define ADXL317_AD_Y_FILT 0x87
94#define ADXL317_AD_Z_ST 0x88
95#define ADXL317_AD_Z_FILT 0x89
96#define ADXL317_AD_X_DATA_LO 0x8A
97#define ADXL317_AD_X_DATA_HI 0x8B
98#define ADXL317_AD_Y_DATA_LO 0x8C
99#define ADXL317_AD_Y_DATA_HI 0x8D
100#define ADXL317_AD_Z_DATA_LO 0x8E
101#define ADXL317_AD_Z_DATA_HI 0x8F
102/** @} */
103
104/**
105 * @name ADXL317 Device Identifier
106 * @{
107 */
108#define ADXL317_DEVID_0 0x22
109/** @} */
110
111/**
112 * @name ADXL317 Device User Reg Key for Register Write Enable
113 * @{
114 */
115#define ADXL317_USER_REG_KEY_0 0xBC
116#define ADXL317_USER_REG_KEY_1 0x43
117/** @} */
118
119/**
120 * @name ADXL317 Default filter
121 * @details LP (CIC and IIR cascade) with cut off at 4kHz
122 * HP disabled
123 * @{
124 */
125#define ADXL317_DEFAULT_FILTER 0x40
126/** @} */
127
128/**
129 * @name ADXL317_I2S_CFG0 register bits definitions
130 * @{
131 */
132#define ADXL317_I2S_CFG0_MASK 0xF3
133#define ADXL317_I2S_CFG0_TDMMODE_0 (1 << 0)
134#define ADXL317_I2S_CFG0_TDMMODE_1 (1 << 1)
135#define ADXL317_I2S_CFG0_TDMSS (1 << 4)
136#define ADXL317_I2S_CFG0_ALT (1 << 5)
137#define ADXL317_I2S_CFG0_EARLY (1 << 6)
138#define ADXL317_I2S_CFG0_INV (1 << 7)
139/** @} */
140
141/**
142 * @name ADXL317_I2S_CFG1 register bits definitions
143 * @{
144 */
145#define ADXL317_I2S_CFG1_MASK 0x07
146#define ADXL317_I2S_CFG1_TX0EN (1 << 0)
147#define ADXL317_I2S_CFG1_TX1EN (1 << 1)
148#define ADXL317_I2S_CFG1_TXBCLKINV (1 << 3)
149/** @} */
150
151/**
152 * @name ADXL317_CLOCK_RATE register bits definitions
153 * @{
154 */
155#define ADXL317_CLOCK_RATE_MASK 0x07
156#define ADXL317_CLOCK_RATE_A2B_CLK_RATE_0 (1 << 0)
157#define ADXL317_CLOCK_RATE_A2B_CLK_RATE_1 (1 << 1)
158#define ADXL317_CLOCK_RATE_A2B_CLK_RATE_2 (1 << 3)
159/** @} */
160
161/*===========================================================================*/
162/* Driver pre-compile time settings. */
163/*===========================================================================*/
164
165/**
166 * @name Configuration options
167 * @{
168 */
169/**
170 * @brief ADXL317 I2C interface switch.
171 * @details If set to @p TRUE the support for I2C is included.
172 * @note The default is @p FALSE.
173 */
174#if !defined(ADXL317_USE_I2C) || defined(__DOXYGEN__)
175#define ADXL317_USE_I2C TRUE
176#endif
177
178/**
179 * @brief ADXL317 shared I2C switch.
180 * @details If set to @p TRUE the device acquires I2C bus ownership
181 * on each transaction.
182 * @note The default is @p FALSE. Requires I2C_USE_MUTUAL_EXCLUSION.
183 */
184#if !defined(ADXL317_SHARED_I2C) || defined(__DOXYGEN__)
185#define ADXL317_SHARED_I2C FALSE
186#endif
187
188/**
189 * @brief ADXL317 advanced configurations switch.
190 * @details If set to @p TRUE more configurations are available.
191 * @note The default is @p FALSE.
192 */
193#if !defined(ADXL317_USE_ADVANCED) || defined(__DOXYGEN__)
194#define ADXL317_USE_ADVANCED FALSE
195#endif
196
197/**
198 * @brief ADXL317 internal maximum communication buffer sizes.
199 */
200#if !defined(ADXL317_COMM_BUFF_SIZE) || defined(__DOXYGEN__)
201#define ADXL317_MAX_BUFF_SIZE 6
202#endif
203/** @} */
204
205/*===========================================================================*/
206/* Derived constants and error checks. */
207/*===========================================================================*/
208
209#if ADXL317_USE_I2C && !HAL_USE_I2C
210#error "ADXL317_USE_I2C requires HAL_USE_I2C"
211#endif
212
213#if ADXL317_SHARED_I2C && !I2C_USE_MUTUAL_EXCLUSION
214#error "ADXL317_SHARED_I2C requires I2C_USE_MUTUAL_EXCLUSION"
215#endif
216
217/*===========================================================================*/
218/* Driver data structures and types. */
219/*===========================================================================*/
220
221/**
222 * @name ADXL317 data structures and types
223 * @{
224 */
225/**
226 * @brief Structure representing a ADXL317 driver.
227 */
229
230/**
231 * @brief ADXL317 Slave Address.
232 */
233typedef enum {
234 ADXL317_SAD_GND = 0x53, /**< SAD pin connected to GND. */
235 ADXL317_SAD_VCC = 0x1D /**< SAD pin connected to VCC. */
237
238/**
239 * @brief Driver state machine possible states.
240 */
241typedef enum {
242 ADXL317_UNINIT = 0, /**< Not initialized. */
243 ADXL317_STOP = 1, /**< Stopped. */
244 ADXL317_READY = 2, /**< Ready. */
246
247/**
248 * @brief ADXL317 X LP filter.
249 * @details The X LP filter is a cascade of the 2 LP filters: CIC, always
250 * enabled, and IIR, that can be disabled.
251 */
252typedef enum {
253 ADXL317_ACC_X_LP_DISABLED = 0x00, /**< LP disabled. */
254 ADXL317_ACC_X_LP_500 = 0x4F, /**< LP cut-off 506 Hz. */
255 ADXL317_ACC_X_LP_1000 = 0x4A, /**< LP cut-off 1012 Hz. */
256 ADXL317_ACC_X_LP_2000 = 0x45, /**< LP cut-off 2025 Hz. */
257 ADXL317_ACC_X_LP_4000 = 0x40, /**< LP cut-off 4051 Hz. */
259
260/**
261 * @brief ADXL317 Y LP filter.
262 * @details The Y LP filter is a cascade of the 2 LP filters: CIC, always
263 * enabled, and IIR, that can be disabled.
264 */
265typedef enum {
266 ADXL317_ACC_Y_LP_DISABLED = 0x00, /**< LP disabled. */
267 ADXL317_ACC_Y_LP_500 = 0x4F, /**< LP cut-off 506 Hz. */
268 ADXL317_ACC_Y_LP_1000 = 0x4A, /**< LP cut-off 1012 Hz. */
269 ADXL317_ACC_Y_LP_2000 = 0x45, /**< LP cut-off 2025 Hz. */
270 ADXL317_ACC_Y_LP_4000 = 0x40, /**< LP cut-off 4051 Hz. */
272
273/**
274 * @brief ADXL317 Z LP filter.
275 * @details The Z LP filter is a cascade of the 2 LP filters: CIC, always
276 * enabled, and IIR, that can be disabled.
277 */
278typedef enum {
279 ADXL317_ACC_Z_LP_DISABLED = 0x00, /**< LP disabled. */
280 ADXL317_ACC_Z_LP_500 = 0x4F, /**< LP cut-off 506 Hz. */
281 ADXL317_ACC_Z_LP_1000 = 0x4A, /**< LP cut-off 1012 Hz. */
282 ADXL317_ACC_Z_LP_2000 = 0x45, /**< LP cut-off 2025 Hz. */
283 ADXL317_ACC_Z_LP_4000 = 0x40, /**< LP cut-off 4051 Hz. */
285
286/**
287 * @brief ADXL317 LP filter configuration.
288 */
294
295/**
296 * @brief ADXL317 X HP filter.
297 * @details The X HP filter is HPF filter.
298 */
299typedef enum {
300 ADXL317_ACC_X_HP_DISABLED = 0x00, /**< HP disabled. */
301 ADXL317_ACC_X_HP_29 = 0x80, /**< HP enabled with cut-off 29.8 Hz. */
302 ADXL317_ACC_X_HP_7 = 0x90, /**< HP enabled with cut-off 7.46 Hz. */
303 ADXL317_ACC_X_HP_1 = 0xA0, /**< HP enabled with cut-off 1.85 Hz. */
304 ADXL317_ACC_X_HP_0_5 = 0xB0, /**< HP enabled with cut-off 0.46 Hz. */
306
307/**
308 * @brief ADXL317 Y HP filter.
309 * @details The Y HP filter is HPF filter.
310 */
311typedef enum {
312 ADXL317_ACC_Y_HP_DISABLED = 0x00, /**< HP disabled. */
313 ADXL317_ACC_Y_HP_29 = 0x80, /**< HP enabled with cut-off 29.8 Hz. */
314 ADXL317_ACC_Y_HP_7 = 0x90, /**< HP enabled with cut-off 7.46 Hz. */
315 ADXL317_ACC_Y_HP_1 = 0xA0, /**< HP enabled with cut-off 1.85 Hz. */
316 ADXL317_ACC_Y_HP_0_5 = 0xB0, /**< HP enabled with cut-off 0.46 Hz. */
318
319/**
320 * @brief ADXL317 Z HP filter.
321 * @details The Z HP filter is HPF filter.
322 */
323typedef enum {
324 ADXL317_ACC_Z_HP_DISABLED = 0x00, /**< HP disabled. */
325 ADXL317_ACC_Z_HP_29 = 0x80, /**< HP enabled with cut-off 29.8 Hz. */
326 ADXL317_ACC_Z_HP_7 = 0x90, /**< HP enabled with cut-off 7.46 Hz. */
327 ADXL317_ACC_Z_HP_1 = 0xA0, /**< HP enabled with cut-off 1.85 Hz. */
328 ADXL317_ACC_Z_HP_0_5 = 0xB0, /**< HP enabled with cut-off 0.46 Hz. */
330
331/**
332 * @brief ADXL317 HP filter configuration.
333 */
339
340/**
341 * @brief ADXL317 configuration structure.
342 */
343typedef struct {
344
345#if (ADXL317_USE_I2C) || defined(__DOXYGEN__)
346 /**
347 * @brief I2C driver associated to this ADXL317.
348 */
349 I2CDriver *i2cp;
350 /**
351 * @brief I2C configuration associated to this ADXL317.
352 */
353 const I2CConfig *i2ccfg;
354#endif /* ADXL317_USE_I2C */
355 /**
356 * @brief ADXL317 accelerometer subsystem initial sensitivity.
357 */
359 /**
360 * @brief ADXL317 accelerometer subsystem initial bias.
361 */
362 float *accbias;
363#if ADXL317_USE_ADVANCED || defined(__DOXYGEN__)
364 /**
365 * @brief ADXL317 LP filter bandwidth.
366 */
368 /**
369 * @brief ADXL317 HP filter bandwidth.
370 */
372#endif
374
375/**
376 * @brief @p ADXL317 specific methods.
377 */
378#define _adxl317_methods_alone
379
380
381/**
382 * @brief @p ADXL317 specific methods with inherited ones.
383 */
384#define _adxl317_methods \
385 _base_object_methods \
386 _adxl317_methods_alone
387
388/**
389 * @extends BaseObjectVMT
390 *
391 * @brief @p ADXL317 virtual methods table.
392 */
395};
396
397/**
398 * @brief @p ADXL317Driver specific data.
399 */
400#define _adxl317_data \
401 _base_sensor_data \
402 /* Driver state.*/ \
403 adxl317_state_t state; \
404 /* Current configuration data.*/ \
405 const ADXL317Config *config; \
406 /* Accelerometer subsystem axes number.*/ \
407 size_t accaxes; \
408 /* Accelerometer subsystem current sensitivity.*/ \
409 float accsensitivity[ADXL317_ACC_NUMBER_OF_AXES]; \
410 /* Accelerometer subsystem current bias .*/ \
411 float accbias[ADXL317_ACC_NUMBER_OF_AXES]; \
412 /* Accelerometer subsystem current full scale value.*/ \
413 float accfullscale; \
414 /* Accelerometer communication tx buffer pointer.*/ \
415 uint8_t* commtxp; \
416 /* Accelerometer communication rx buffer pointer.*/ \
417 uint8_t* commrxp;
418
419/**
420 * @brief ADXL317 3-axis accelerometer class.
421 */
423 /** @brief Virtual Methods Table.*/
424 const struct ADXL317VMT *vmt;
425 /** @brief Base accelerometer interface.*/
428};
429/** @} */
430
431/*===========================================================================*/
432/* Driver macros. */
433/*===========================================================================*/
434
435/**
436 * @brief Return the number of axes of the BaseAccelerometer.
437 *
438 * @param[in] devp pointer to @p ADXL317Driver.
439 *
440 * @return the number of axes.
441 *
442 * @api
443 */
444#define adxl317AccelerometerGetAxesNumber(devp) \
445 accelerometerGetAxesNumber(&((devp)->acc_if))
446
447/**
448 * @brief Retrieves raw data from the BaseAccelerometer.
449 * @note This data is retrieved from MEMS register without any algebraical
450 * manipulation.
451 * @note The axes array must be at least the same size of the
452 * BaseAccelerometer axes number.
453 *
454 * @param[in] devp pointer to @p ADXL317Driver.
455 * @param[out] axes a buffer which would be filled with raw data.
456 *
457 * @return The operation status.
458 * @retval MSG_OK if the function succeeded.
459 * @retval MSG_RESET if one or more I2C errors occurred, the errors can
460 * be retrieved using @p i2cGetErrors().
461 * @retval MSG_TIMEOUT if a timeout occurred before operation end.
462 *
463 * @api
464 */
465#define adxl317AccelerometerReadRaw(devp, axes) \
466 accelerometerReadRaw(&((devp)->acc_if), axes)
467
468/**
469 * @brief Retrieves cooked data from the BaseAccelerometer.
470 * @note This data is manipulated according to the formula
471 * cooked = (raw * sensitivity) - bias.
472 * @note Final data is expressed as milli-G.
473 * @note The axes array must be at least the same size of the
474 * BaseAccelerometer axes number.
475 *
476 * @param[in] devp pointer to @p ADXL317Driver.
477 * @param[out] axes a buffer which would be filled with cooked data.
478 *
479 * @return The operation status.
480 * @retval MSG_OK if the function succeeded.
481 * @retval MSG_RESET if one or more I2C errors occurred, the errors can
482 * be retrieved using @p i2cGetErrors().
483 * @retval MSG_TIMEOUT if a timeout occurred before operation end.
484 *
485 * @api
486 */
487#define adxl317AccelerometerReadCooked(devp, axes) \
488 accelerometerReadCooked(&((devp)->acc_if), axes)
489
490/*===========================================================================*/
491/* External declarations. */
492/*===========================================================================*/
493
494#ifdef __cplusplus
495extern "C" {
496#endif
497 void adxl317ObjectInit(ADXL317Driver *devp, uint8_t* txbp, uint8_t* rxbp);
498 msg_t adxl317Start(ADXL317Driver *devp, const ADXL317Config *config);
499 void adxl317Stop(ADXL317Driver *devp);
500#ifdef __cplusplus
501}
502#endif
503
504#endif /* _ADXL317_H_ */
505
506/** @} */
507
Generic accelerometer interface header.
adxl317_state_t
Driver state machine possible states.
Definition adxl317.h:241
adxl317_sad_t
ADXL317 Slave Address.
Definition adxl317.h:233
adxl317_acc_z_hp_t
ADXL317 Z HP filter.
Definition adxl317.h:323
#define _adxl317_data
ADXL317Driver specific data.
Definition adxl317.h:400
adxl317_acc_y_hp_t
ADXL317 Y HP filter.
Definition adxl317.h:311
#define _adxl317_methods
ADXL317 specific methods with inherited ones.
Definition adxl317.h:384
void adxl317ObjectInit(ADXL317Driver *devp, uint8_t *txbp, uint8_t *rxbp)
Initializes an instance.
Definition adxl317.c:353
void adxl317Stop(ADXL317Driver *devp)
Deactivates the ADXL317 Complex Driver peripheral.
Definition adxl317.c:474
adxl317_acc_x_lp_t
ADXL317 X LP filter.
Definition adxl317.h:252
adxl317_acc_y_lp_t
ADXL317 Y LP filter.
Definition adxl317.h:265
msg_t adxl317Start(ADXL317Driver *devp, const ADXL317Config *config)
Configures and activates ADXL317 Complex Driver peripheral.
Definition adxl317.c:374
adxl317_acc_z_lp_t
ADXL317 Z LP filter.
Definition adxl317.h:278
adxl317_acc_x_hp_t
ADXL317 X HP filter.
Definition adxl317.h:299
@ ADXL317_STOP
Definition adxl317.h:243
@ ADXL317_READY
Definition adxl317.h:244
@ ADXL317_UNINIT
Definition adxl317.h:242
@ ADXL317_SAD_GND
Definition adxl317.h:234
@ ADXL317_SAD_VCC
Definition adxl317.h:235
@ ADXL317_ACC_Z_HP_7
Definition adxl317.h:326
@ ADXL317_ACC_Z_HP_0_5
Definition adxl317.h:328
@ ADXL317_ACC_Z_HP_29
Definition adxl317.h:325
@ ADXL317_ACC_Z_HP_DISABLED
Definition adxl317.h:324
@ ADXL317_ACC_Z_HP_1
Definition adxl317.h:327
@ ADXL317_ACC_Y_HP_7
Definition adxl317.h:314
@ ADXL317_ACC_Y_HP_0_5
Definition adxl317.h:316
@ ADXL317_ACC_Y_HP_29
Definition adxl317.h:313
@ ADXL317_ACC_Y_HP_DISABLED
Definition adxl317.h:312
@ ADXL317_ACC_Y_HP_1
Definition adxl317.h:315
@ ADXL317_ACC_X_LP_2000
Definition adxl317.h:256
@ ADXL317_ACC_X_LP_DISABLED
Definition adxl317.h:253
@ ADXL317_ACC_X_LP_500
Definition adxl317.h:254
@ ADXL317_ACC_X_LP_1000
Definition adxl317.h:255
@ ADXL317_ACC_X_LP_4000
Definition adxl317.h:257
@ ADXL317_ACC_Y_LP_DISABLED
Definition adxl317.h:266
@ ADXL317_ACC_Y_LP_2000
Definition adxl317.h:269
@ ADXL317_ACC_Y_LP_1000
Definition adxl317.h:268
@ ADXL317_ACC_Y_LP_4000
Definition adxl317.h:270
@ ADXL317_ACC_Y_LP_500
Definition adxl317.h:267
@ ADXL317_ACC_Z_LP_500
Definition adxl317.h:280
@ ADXL317_ACC_Z_LP_2000
Definition adxl317.h:282
@ ADXL317_ACC_Z_LP_1000
Definition adxl317.h:281
@ ADXL317_ACC_Z_LP_DISABLED
Definition adxl317.h:279
@ ADXL317_ACC_Z_LP_4000
Definition adxl317.h:283
@ ADXL317_ACC_X_HP_1
Definition adxl317.h:303
@ ADXL317_ACC_X_HP_DISABLED
Definition adxl317.h:300
@ ADXL317_ACC_X_HP_7
Definition adxl317.h:302
@ ADXL317_ACC_X_HP_29
Definition adxl317.h:301
@ ADXL317_ACC_X_HP_0_5
Definition adxl317.h:304
ADXL317 configuration structure.
Definition adxl317.h:343
float * accbias
ADXL317 accelerometer subsystem initial bias.
Definition adxl317.h:362
adxl317_acc_hp_t acchighpass
ADXL317 HP filter bandwidth.
Definition adxl317.h:371
I2CDriver * i2cp
I2C driver associated to this ADXL317.
Definition adxl317.h:349
adxl317_acc_lp_t acclowpass
ADXL317 LP filter bandwidth.
Definition adxl317.h:367
const I2CConfig * i2ccfg
I2C configuration associated to this ADXL317.
Definition adxl317.h:353
float * accsensitivity
ADXL317 accelerometer subsystem initial sensitivity.
Definition adxl317.h:358
ADXL317 3-axis accelerometer class.
Definition adxl317.h:422
const struct ADXL317VMT * vmt
Virtual Methods Table.
Definition adxl317.h:424
BaseAccelerometer acc_if
Base accelerometer interface.
Definition adxl317.h:426
ADXL317 virtual methods table.
Definition adxl317.h:393
Base accelerometer class.
ADXL317 HP filter configuration.
Definition adxl317.h:334
adxl317_acc_y_hp_t y
Definition adxl317.h:336
adxl317_acc_z_hp_t z
Definition adxl317.h:337
adxl317_acc_x_hp_t x
Definition adxl317.h:335
ADXL317 LP filter configuration.
Definition adxl317.h:289
adxl317_acc_y_lp_t y
Definition adxl317.h:291
adxl317_acc_z_lp_t z
Definition adxl317.h:292
adxl317_acc_x_lp_t x
Definition adxl317.h:290