lis3dh.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Eistec AB
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
25 #ifndef LIS3DH_H
26 #define LIS3DH_H
27 
28 #include <stdint.h>
29 
30 #include "periph/spi.h"
31 #include "periph/gpio.h"
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
43 #define LIS3DH_WHO_AM_I_RESPONSE (0x33)
44 
49 #define LIS3DH_REG_STATUS_AUX (0x07)
50 #define LIS3DH_REG_OUT_AUX_ADC1_L (0x08)
51 #define LIS3DH_REG_OUT_AUX_ADC1_H (0x09)
52 #define LIS3DH_REG_OUT_AUX_ADC2_L (0x0A)
53 #define LIS3DH_REG_OUT_AUX_ADC2_H (0x0B)
54 #define LIS3DH_REG_OUT_AUX_ADC3_L (0x0C)
55 #define LIS3DH_REG_OUT_AUX_ADC3_H (0x0D)
56 #define LIS3DH_REG_INT_COUNTER_REG (0x0E)
57 #define LIS3DH_REG_WHO_AM_I (0x0F)
58 #define LIS3DH_REG_TEMP_CFG_REG (0x1F)
59 #define LIS3DH_REG_CTRL_REG1 (0x20)
60 #define LIS3DH_REG_CTRL_REG2 (0x21)
61 #define LIS3DH_REG_CTRL_REG3 (0x22)
62 #define LIS3DH_REG_CTRL_REG4 (0x23)
63 #define LIS3DH_REG_CTRL_REG5 (0x24)
64 #define LIS3DH_REG_CTRL_REG6 (0x25)
65 #define LIS3DH_REG_REFERENCE (0x26)
66 #define LIS3DH_REG_STATUS_REG (0x27)
67 #define LIS3DH_REG_OUT_X_L (0x28)
68 #define LIS3DH_REG_OUT_X_H (0x29)
69 #define LIS3DH_REG_OUT_Y_L (0x2A)
70 #define LIS3DH_REG_OUT_Y_H (0x2B)
71 #define LIS3DH_REG_OUT_Z_L (0x2C)
72 #define LIS3DH_REG_OUT_Z_H (0x2D)
73 #define LIS3DH_REG_FIFO_CTRL_REG (0x2E)
74 #define LIS3DH_REG_FIFO_SRC_REG (0x2F)
75 #define LIS3DH_REG_INT1_CFG (0x30)
76 #define LIS3DH_REG_INT1_SOURCE (0x31)
77 #define LIS3DH_REG_INT1_THS (0x32)
78 #define LIS3DH_REG_INT1_DURATION (0x33)
79 #define LIS3DH_REG_CLICK_CFG (0x38)
80 #define LIS3DH_REG_CLICK_SRC (0x39)
81 #define LIS3DH_REG_CLICK_THS (0x3A)
82 #define LIS3DH_REG_TIME_LIMIT (0x3B)
83 #define LIS3DH_REG_TIME_LATENCY (0x3C)
84 #define LIS3DH_REG_TIME_WINDOW (0x3D)
85 
87 /*
88  * Bit offsets within the individual registers
89  * source: LIS3DH datasheet
90  */
91 
103 #define LIS3DH_TEMP_CFG_REG_ADC_PD_MASK (1 << 7)
104 
111 #define LIS3DH_TEMP_CFG_REG_TEMP_EN_MASK (1 << 6)
112  /* TEMP_CFG_REG bitfield macros */
113 
121 #define LIS3DH_CTRL_REG1_ODR_SHIFT (4)
122 
125 #define LIS3DH_CTRL_REG1_ODR3_MASK (1 << (LIS3DH_CTRL_REG1_ODR_SHIFT + 3))
126 
129 #define LIS3DH_CTRL_REG1_ODR2_MASK (1 << (LIS3DH_CTRL_REG1_ODR_SHIFT + 2))
130 
133 #define LIS3DH_CTRL_REG1_ODR1_MASK (1 << (LIS3DH_CTRL_REG1_ODR_SHIFT + 1))
134 
137 #define LIS3DH_CTRL_REG1_ODR0_MASK (1 << LIS3DH_CTRL_REG1_ODR_SHIFT)
138 
147 #define LIS3DH_CTRL_REG1_ODR_MASK (LIS3DH_CTRL_REG1_ODR3_MASK | \
148  LIS3DH_CTRL_REG1_ODR2_MASK | \
149  LIS3DH_CTRL_REG1_ODR1_MASK | \
150  LIS3DH_CTRL_REG1_ODR0_MASK)
151 
159 #define LIS3DH_CTRL_REG1_LPEN_MASK (1 << 3)
160 
163 #define LIS3DH_CTRL_REG1_ZEN_SHIFT (2)
164 
172 #define LIS3DH_CTRL_REG1_ZEN_MASK (1 << LIS3DH_CTRL_REG1_ZEN_SHIFT)
173 
176 #define LIS3DH_CTRL_REG1_YEN_SHIFT (1)
177 
185 #define LIS3DH_CTRL_REG1_YEN_MASK (1 << LIS3DH_CTRL_REG1_YEN_SHIFT)
186 
189 #define LIS3DH_CTRL_REG1_XEN_SHIFT (0)
190 
198 #define LIS3DH_CTRL_REG1_XEN_MASK (1 << LIS3DH_CTRL_REG1_XEN_SHIFT)
199 
202 #define LIS3DH_CTRL_REG1_XYZEN_SHIFT (0)
203 
206 #define LIS3DH_CTRL_REG1_XYZEN_MASK (LIS3DH_CTRL_REG1_XEN_MASK | \
207  LIS3DH_CTRL_REG1_YEN_MASK | LIS3DH_CTRL_REG1_ZEN_MASK)
208 
212 #define LIS3DH_AXES_X (LIS3DH_CTRL_REG1_XEN_MASK)
213 
216 #define LIS3DH_AXES_Y (LIS3DH_CTRL_REG1_YEN_MASK)
217 
220 #define LIS3DH_AXES_Z (LIS3DH_CTRL_REG1_ZEN_MASK)
221  /* CTRL_REG1 bitfield macros */
222 
226 #define LIS3DH_AXES_XYZ (LIS3DH_CTRL_REG1_XYZEN_MASK)
227 
239 #define LIS3DH_CTRL_REG2_HPM1_MASK (1 << 7)
240 
247 #define LIS3DH_CTRL_REG2_HPM0_MASK (1 << 6)
248 
251 #define LIS3DH_CTRL_REG2_HPCF2_MASK (1 << 5)
252 
255 #define LIS3DH_CTRL_REG2_HPCF1_MASK (1 << 4)
256 
264 #define LIS3DH_CTRL_REG2_FDS_MASK (1 << 3)
265 
271 #define LIS3DH_CTRL_REG2_HPCLICK_MASK (1 << 2)
272 
278 #define LIS3DH_CTRL_REG2_HPIS2_MASK (1 << 1)
279 
285 #define LIS3DH_CTRL_REG2_HPIS1_MASK (1 << 0)
286  /* CTRL_REG2 bitfield macros */
287 
300 #define LIS3DH_CTRL_REG3_I1_CLICK_MASK (1 << 7)
301 
309 #define LIS3DH_CTRL_REG3_I1_AOI1_MASK (1 << 6)
310 
318 #define LIS3DH_CTRL_REG3_I1_AOI2_MASK (1 << 5)
319 
327 #define LIS3DH_CTRL_REG3_I1_DRDY1_MASK (1 << 4)
328 
336 #define LIS3DH_CTRL_REG3_I1_DRDY2_MASK (1 << 3)
337 
345 #define LIS3DH_CTRL_REG3_I1_WTM_MASK (1 << 2)
346 
354 #define LIS3DH_CTRL_REG3_I1_OVERRUN_MASK (1 << 1)
355  /* CTRL_REG3 bitfield macros */
356 
369 #define LIS3DH_CTRL_REG4_BDU_MASK (1 << 7)
370 
373 #define LIS3DH_CTRL_REG4_BDU_ENABLE (LIS3DH_CTRL_REG4_BDU_MASK)
374 
377 #define LIS3DH_CTRL_REG4_BDU_DISABLE (0)
378 
386 #define LIS3DH_CTRL_REG4_BLE_MASK (1 << 6)
387 
390 #define LIS3DH_CTRL_REG4_BLE_LITTLE_ENDIAN (0)
391 
394 #define LIS3DH_CTRL_REG4_BLE_BIG_ENDIAN (LIS3DH_CTRL_REG4_BLE_MASK)
395 
398 #define LIS3DH_CTRL_REG4_FS1_MASK (1 << 5)
399 
402 #define LIS3DH_CTRL_REG4_FS0_MASK (1 << 4)
403 
406 #define LIS3DH_CTRL_REG4_FS_MASK (LIS3DH_CTRL_REG4_FS1_MASK | \
407  LIS3DH_CTRL_REG4_FS0_MASK)
408 
411 #define LIS3DH_CTRL_REG4_SCALE_2G (0)
412 
415 #define LIS3DH_CTRL_REG4_SCALE_4G (LIS3DH_CTRL_REG4_FS0_MASK)
416 
419 #define LIS3DH_CTRL_REG4_SCALE_8G (LIS3DH_CTRL_REG4_FS1_MASK)
420 
423 #define LIS3DH_CTRL_REG4_SCALE_16G (LIS3DH_CTRL_REG4_FS1_MASK | LIS3DH_CTRL_REG4_FS0_MASK)
424 
432 #define LIS3DH_CTRL_REG4_HR_MASK (1 << 3)
433 
443 #define LIS3DH_CTRL_REG4_ST1_MASK (1 << 2)
444 
447 #define LIS3DH_CTRL_REG4_ST0_MASK (1 << 1)
448 
456 #define LIS3DH_CTRL_REG4_SIM_MASK (1 << 0)
457 
465 #define LIS3DH_CTRL_REG5_REBOOT_MASK (1 << 7)
466 
474 #define LIS3DH_CTRL_REG5_FIFO_EN_MASK (1 << 6)
475 
486 #define LIS3DH_CTRL_REG5_LIR_I1_MASK (1 << 3)
487 
492 #define LIS3DH_CTRL_REG5_D4D_I1_MASK (1 << 2)
493  /* CTRL_REG4 bitfield macros */
494 
507 #define LIS3DH_STATUS_REG_ZYXOR_MASK (1 << 7)
508 
516 #define LIS3DH_STATUS_REG_ZOR_MASK (1 << 6)
517 
525 #define LIS3DH_STATUS_REG_YOR_MASK (1 << 5)
526 
534 #define LIS3DH_STATUS_REG_XOR_MASK (1 << 4)
535 
543 #define LIS3DH_STATUS_REG_ZYXDA_MASK (1 << 3)
544 
552 #define LIS3DH_STATUS_REG_ZDA_MASK (1 << 2)
553 
561 #define LIS3DH_STATUS_REG_YDA_MASK (1 << 1)
562 
570 #define LIS3DH_STATUS_REG_XDA_MASK (1 << 0)
571  /* STATUS_REG bitfield macros */
572 
577 #define LIS3DH_FIFO_CTRL_REG_FM_SHIFT (6)
578 #define LIS3DH_FIFO_CTRL_REG_FM1_MASK (1 << 7)
579 #define LIS3DH_FIFO_CTRL_REG_FM0_MASK (1 << 6)
580 #define LIS3DH_FIFO_CTRL_REG_FM_MASK (LIS3DH_FIFO_CTRL_REG_FM1_MASK | \
581  LIS3DH_FIFO_CTRL_REG_FM0_MASK)
582 #define LIS3DH_FIFO_CTRL_REG_TR_MASK (1 << 5)
583 #define LIS3DH_FIFO_CTRL_REG_FTH4_MASK (1 << 4)
584 #define LIS3DH_FIFO_CTRL_REG_FTH3_MASK (1 << 3)
585 #define LIS3DH_FIFO_CTRL_REG_FTH2_MASK (1 << 2)
586 #define LIS3DH_FIFO_CTRL_REG_FTH1_MASK (1 << 1)
587 #define LIS3DH_FIFO_CTRL_REG_FTH0_MASK (1 << 0)
588 #define LIS3DH_FIFO_CTRL_REG_FTH_SHIFT (0)
589 #define LIS3DH_FIFO_CTRL_REG_FTH_MASK (LIS3DH_FIFO_CTRL_REG_FTH0_MASK | \
590  LIS3DH_FIFO_CTRL_REG_FTH1_MASK | \
591  LIS3DH_FIFO_CTRL_REG_FTH2_MASK | \
592  LIS3DH_FIFO_CTRL_REG_FTH3_MASK | \
593  LIS3DH_FIFO_CTRL_REG_FTH4_MASK)
594  /* FIFO_CTRL_REG bitfield macros */
595 
600 #define LIS3DH_FIFO_SRC_REG_WTM_MASK (1 << 7)
601 #define LIS3DH_FIFO_SRC_REG_OVRN_FIFO_MASK (1 << 6)
602 #define LIS3DH_FIFO_SRC_REG_EMPTY_MASK (1 << 5)
603 #define LIS3DH_FIFO_SRC_REG_FSS4_MASK (1 << 4)
604 #define LIS3DH_FIFO_SRC_REG_FSS3_MASK (1 << 3)
605 #define LIS3DH_FIFO_SRC_REG_FSS2_MASK (1 << 2)
606 #define LIS3DH_FIFO_SRC_REG_FSS1_MASK (1 << 1)
607 #define LIS3DH_FIFO_SRC_REG_FSS0_MASK (1 << 0)
608 #define LIS3DH_FIFO_SRC_REG_FSS_SHIFT (0)
609 #define LIS3DH_FIFO_SRC_REG_FSS_MASK (LIS3DH_FIFO_SRC_REG_FSS0_MASK | \
610  LIS3DH_FIFO_SRC_REG_FSS1_MASK | \
611  LIS3DH_FIFO_SRC_REG_FSS2_MASK | \
612  LIS3DH_FIFO_SRC_REG_FSS3_MASK | \
613  LIS3DH_FIFO_SRC_REG_FSS4_MASK)
614  /* FIFO_CTRL_REG bitfield macros */
615 
623 #define LIS3DH_SPI_WRITE_MASK (0 << 7)
624 
627 #define LIS3DH_SPI_READ_MASK (1 << 7)
628 
631 #define LIS3DH_SPI_MULTI_MASK (1 << 6)
632 
635 #define LIS3DH_SPI_SINGLE_MASK (0 << 6)
636 
639 #define LIS3DH_SPI_ADDRESS_MASK (0x3F)
640  /* Register address bitfield macros */
641 
645 #define LIS3DH_ADC_DATA_SIZE (2U)
646 
656 #define LIS3DH_FIFO_MODE_BYPASS (0x00 << LIS3DH_FIFO_CTRL_REG_FM_SHIFT)
657 
660 #define LIS3DH_FIFO_MODE_FIFO (0x01 << LIS3DH_FIFO_CTRL_REG_FM_SHIFT)
661 
664 #define LIS3DH_FIFO_MODE_STREAM (0x02 << LIS3DH_FIFO_CTRL_REG_FM_SHIFT)
665 
668 #define LIS3DH_FIFO_MODE_STREAM_TO_FIFO (0x03 << LIS3DH_FIFO_CTRL_REG_FM_SHIFT)
669 
680 #define LIS3DH_ODR_POWERDOWN (0x00 << LIS3DH_CTRL_REG1_ODR_SHIFT)
681 
684 #define LIS3DH_ODR_1Hz (0x01 << LIS3DH_CTRL_REG1_ODR_SHIFT)
685 
688 #define LIS3DH_ODR_10Hz (0x02 << LIS3DH_CTRL_REG1_ODR_SHIFT)
689 
692 #define LIS3DH_ODR_25Hz (0x03 << LIS3DH_CTRL_REG1_ODR_SHIFT)
693 
696 #define LIS3DH_ODR_50Hz (0x04 << LIS3DH_CTRL_REG1_ODR_SHIFT)
697 
700 #define LIS3DH_ODR_100Hz (0x05 << LIS3DH_CTRL_REG1_ODR_SHIFT)
701 
704 #define LIS3DH_ODR_200Hz (0x06 << LIS3DH_CTRL_REG1_ODR_SHIFT)
705 
708 #define LIS3DH_ODR_400Hz (0x07 << LIS3DH_CTRL_REG1_ODR_SHIFT)
709 
712 #define LIS3DH_ODR_LP1600Hz (0x08 << LIS3DH_CTRL_REG1_ODR_SHIFT)
713 
717 #define LIS3DH_ODR_NP1250Hz (0x09 << LIS3DH_CTRL_REG1_ODR_SHIFT)
718 
722 #define LIS3DH_ODR_LP5000HZ (0x09 << LIS3DH_CTRL_REG1_ODR_SHIFT)
723 
728 typedef struct {
731  gpio_t cs;
732  gpio_t int1;
733  gpio_t int2;
734  uint8_t scale;
735  uint8_t odr;
737 
741 typedef struct {
743  uint16_t scale;
744 } lis3dh_t;
745 
749 typedef struct __attribute__((packed))
750 {
751  int16_t acc_x;
752  int16_t acc_y;
753  int16_t acc_z;
754 } lis3dh_data_t;
755 
765 int lis3dh_init(lis3dh_t *dev, const lis3dh_params_t *params);
766 
776 int lis3dh_read_xyz(const lis3dh_t *dev, lis3dh_data_t *acc_data);
777 
787 int lis3dh_read_aux_adc1(const lis3dh_t *dev, int16_t *out);
788 
798 int lis3dh_read_aux_adc2(const lis3dh_t *dev, int16_t *out);
799 
812 int lis3dh_read_aux_adc3(const lis3dh_t *dev, int16_t *out);
813 
828 int lis3dh_set_aux_adc(const lis3dh_t *dev, const uint8_t enable, const uint8_t temperature);
829 
842 int lis3dh_set_axes(const lis3dh_t *dev, const uint8_t axes);
843 
854 int lis3dh_set_fifo(const lis3dh_t *dev, const uint8_t mode, const uint8_t watermark);
855 
865 int lis3dh_set_odr(const lis3dh_t *dev, const uint8_t odr);
866 
879 int lis3dh_set_scale(lis3dh_t *dev, const uint8_t scale);
880 
892 int lis3dh_set_int1(const lis3dh_t *dev, const uint8_t mode);
893 
902 int lis3dh_get_fifo_level(const lis3dh_t *dev);
903 
904 #ifdef __cplusplus
905 }
906 #endif
907 
908 #endif /* LIS3DH_H */
909 
lis3dh_data_t
Result vector for accelerometer measurement.
Definition: lis3dh.h:749
lis3dh_set_odr
int lis3dh_set_odr(const lis3dh_t *dev, const uint8_t odr)
Set the output data rate of the sensor.
spi.h
Low-level SPI peripheral driver interface definition.
lis3dh_t::scale
uint16_t scale
Internal sensor scale.
Definition: lis3dh.h:743
lis3dh_params_t::odr
uint8_t odr
Default sensor ODR setting: LIS3DH_ODR_xxxHz.
Definition: lis3dh.h:735
lis3dh_data_t::acc_y
int16_t acc_y
Acceleration in the Y direction in milli-G.
Definition: lis3dh.h:752
lis3dh_data_t::acc_z
int16_t acc_z
Acceleration in the Z direction in milli-G.
Definition: lis3dh.h:753
lis3dh_set_fifo
int lis3dh_set_fifo(const lis3dh_t *dev, const uint8_t mode, const uint8_t watermark)
Enable/disable the FIFO.
lis3dh_t
Device descriptor for LIS3DH sensors.
Definition: lis3dh.h:741
lis3dh_set_axes
int lis3dh_set_axes(const lis3dh_t *dev, const uint8_t axes)
Enable/disable accelerometer axes.
lis3dh_params_t::int2
gpio_t int2
INT2 (DRDY) pin.
Definition: lis3dh.h:733
lis3dh_set_scale
int lis3dh_set_scale(lis3dh_t *dev, const uint8_t scale)
Set the full scale range of the sensor.
lis3dh_set_aux_adc
int lis3dh_set_aux_adc(const lis3dh_t *dev, const uint8_t enable, const uint8_t temperature)
Turn on/off power to the auxiliary ADC in LIS3DH.
lis3dh_params_t::scale
uint8_t scale
Default sensor scale: 2, 4, 8, or 16 (G)
Definition: lis3dh.h:734
lis3dh_set_int1
int lis3dh_set_int1(const lis3dh_t *dev, const uint8_t mode)
Set INT1 pin function.
spi_clk_t
spi_clk_t
Available SPI clock speeds.
Definition: spi.h:173
lis3dh_params_t::spi
spi_t spi
SPI device the sensor is connected to.
Definition: lis3dh.h:729
lis3dh_read_aux_adc2
int lis3dh_read_aux_adc2(const lis3dh_t *dev, int16_t *out)
Read auxiliary ADC channel 2 data from the accelerometer.
lis3dh_init
int lis3dh_init(lis3dh_t *dev, const lis3dh_params_t *params)
Initialize a LIS3DH sensor instance.
lis3dh_read_aux_adc3
int lis3dh_read_aux_adc3(const lis3dh_t *dev, int16_t *out)
Read auxiliary ADC channel 3 data from the accelerometer.
gpio.h
Low-level GPIO peripheral driver interface definitions.
lis3dh_t::params
lis3dh_params_t params
Device initialization parameters.
Definition: lis3dh.h:742
lis3dh_params_t::int1
gpio_t int1
INT1 pin.
Definition: lis3dh.h:732
lis3dh_data_t::acc_x
int16_t acc_x
Acceleration in the X direction in milli-G.
Definition: lis3dh.h:751
lis3dh_read_aux_adc1
int lis3dh_read_aux_adc1(const lis3dh_t *dev, int16_t *out)
Read auxiliary ADC channel 1 data from the accelerometer.
lis3dh_params_t::clk
spi_clk_t clk
designated clock speed of the SPI bus
Definition: lis3dh.h:730
lis3dh_read_xyz
int lis3dh_read_xyz(const lis3dh_t *dev, lis3dh_data_t *acc_data)
Read 3D acceleration data from the accelerometer.
lis3dh_params_t
Configuration parameters for LIS3DH devices.
Definition: lis3dh.h:728
lis3dh_get_fifo_level
int lis3dh_get_fifo_level(const lis3dh_t *dev)
Get the current number of elements in the FIFO.
spi_t
unsigned int spi_t
Default type for SPI devices.
Definition: spi.h:118
lis3dh_params_t::cs
gpio_t cs
Chip select pin.
Definition: lis3dh.h:731