upm  1.7.1
Sensor/Actuator repository for libmraa (v2.0.0)
lis3dh.h
Go to the documentation of this file.
1 /*
2  * Author: Alex Tereschenko <alext.mkrs@gmail.com>
3  * Copyright (c) 2018 Alex Tereschenko.
4  *
5  * Based on LIS2DS12 module by
6  * Author: Jon Trulson <jtrulson@ics.com>
7  * Copyright (c) 2017 Intel Corporation.
8  *
9  * The MIT License
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining
12  * a copy of this software and associated documentation files (the
13  * "Software"), to deal in the Software without restriction, including
14  * without limitation the rights to use, copy, modify, merge, publish,
15  * distribute, sublicense, and/or sell copies of the Software, and to
16  * permit persons to whom the Software is furnished to do so, subject to
17  * the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be
20  * included in all copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29  */
30 #pragma once
31 
32 #include <string.h>
33 #include <unistd.h>
34 
35 #include <mraa/gpio.h>
36 #include <mraa/i2c.h>
37 #include <mraa/spi.h>
38 
39 #include "upm.h"
40 
41 #include "lis3dh_defs.h"
42 
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 
58 typedef struct _lis3dh_context {
59  mraa_i2c_context i2c;
60  mraa_spi_context spi;
61 
62  mraa_gpio_context gpioCS; // SPI CS pin
63  mraa_gpio_context gpioINT1; // Interrupt 1
64  mraa_gpio_context gpioINT2; // Interrupt 2
65 
66  // Uncompensated temperature data
67  float temperature;
68 
69  // Temperature scaling factor. It accounts for storing 8b or 10b actual
70  // data in 16b register pair.
71  float temperatureFactor;
72 
73  // Uncompensated acceleration data
74  float accX;
75  float accY;
76  float accZ;
77 
78  // Acceleration scaling - used to calculate actual acceleration,
79  // depending on sensor working mode (low power/normal/high resolution)
80  float accScale;
81 } * lis3dh_context;
82 
99 lis3dh_context lis3dh_init(int bus, int addr, int cs);
100 
106 void lis3dh_close(lis3dh_context dev);
107 
119 upm_result_t
120 lis3dh_devinit(const lis3dh_context dev, LIS3DH_ODR_T odr, LIS3DH_FS_T fs, bool high_res);
121 
131 upm_result_t lis3dh_enable_axes(const lis3dh_context dev,
132  bool x_axis_enable,
133  bool y_axis_enable,
134  bool z_axis_enable);
135 
143 upm_result_t lis3dh_enable_bdu_mode(const lis3dh_context dev, bool bdu_enable);
144 
153 upm_result_t lis3dh_enable_lp_mode(const lis3dh_context dev, bool lp_enable);
154 
163 upm_result_t lis3dh_enable_hr_mode(const lis3dh_context dev, bool hr_enable);
164 
173 upm_result_t lis3dh_enable_normal_mode(const lis3dh_context dev);
174 
184 upm_result_t lis3dh_enable_hp_filtering(const lis3dh_context dev, bool filter);
185 
193 upm_result_t lis3dh_enable_adc(const lis3dh_context dev, bool adc_enable);
194 
203 upm_result_t lis3dh_enable_temperature(const lis3dh_context dev, bool temperature_enable);
204 
214 upm_result_t
215 lis3dh_enable_interrupt_latching(const lis3dh_context dev, bool int1_latch, bool int2_latch);
216 
224 upm_result_t lis3dh_set_odr(const lis3dh_context dev, LIS3DH_ODR_T odr);
225 
234 upm_result_t lis3dh_set_full_scale(const lis3dh_context dev, LIS3DH_FS_T fs);
235 
244 upm_result_t lis3dh_set_interrupt_active_high(const lis3dh_context dev, bool high);
245 
253 upm_result_t lis3dh_set_int1_config(const lis3dh_context dev, uint8_t cfg);
254 
262 upm_result_t lis3dh_set_int2_config(const lis3dh_context dev, uint8_t cfg);
263 
271 uint8_t lis3dh_read_reg(const lis3dh_context dev, uint8_t reg);
272 
282 int lis3dh_read_regs(const lis3dh_context dev, uint8_t reg, uint8_t* buffer, int len);
283 
292 upm_result_t lis3dh_write_reg(const lis3dh_context dev, uint8_t reg, uint8_t val);
293 
300 upm_result_t lis3dh_update(const lis3dh_context dev);
301 
308 uint8_t lis3dh_get_chip_id(const lis3dh_context dev);
309 
322 void lis3dh_get_accelerometer(const lis3dh_context dev, float* x, float* y, float* z);
323 
332 float lis3dh_get_temperature(const lis3dh_context dev);
333 
340 uint8_t lis3dh_get_status(const lis3dh_context dev);
341 
348 uint8_t lis3dh_get_status_aux(const lis3dh_context dev);
349 
364 upm_result_t lis3dh_install_isr(const lis3dh_context dev,
365  LIS3DH_INTERRUPT_PINS_T intr,
366  int gpio,
367  mraa_gpio_edge_t level,
368  void (*isr)(void*),
369  void* arg);
370 
378 void lis3dh_uninstall_isr(const lis3dh_context dev, LIS3DH_INTERRUPT_PINS_T intr);
379 
380 #ifdef __cplusplus
381 }
382 #endif
void lis3dh_get_accelerometer(const lis3dh_context dev, float *x, float *y, float *z)
Definition: lis3dh.c:704
upm_result_t lis3dh_write_reg(const lis3dh_context dev, uint8_t reg, uint8_t val)
Definition: lis3dh.c:629
void lis3dh_close(lis3dh_context dev)
Definition: lis3dh.c:150
upm_result_t lis3dh_devinit(const lis3dh_context dev, LIS3DH_ODR_T odr, LIS3DH_FS_T fs, bool high_res)
Definition: lis3dh.c:173
struct _lis3dh_context * lis3dh_context
upm_result_t lis3dh_enable_temperature(const lis3dh_context dev, bool temperature_enable)
Definition: lis3dh.c:420
upm_result_t lis3dh_enable_bdu_mode(const lis3dh_context dev, bool bdu_enable)
Definition: lis3dh.c:239
uint8_t lis3dh_get_status(const lis3dh_context dev)
Definition: lis3dh.c:730
upm_result_t lis3dh_update(const lis3dh_context dev)
Definition: lis3dh.c:657
upm_result_t lis3dh_set_interrupt_active_high(const lis3dh_context dev, bool high)
Definition: lis3dh.c:522
upm_result_t lis3dh_enable_interrupt_latching(const lis3dh_context dev, bool int1_latch, bool int2_latch)
Definition: lis3dh.c:366
upm_result_t lis3dh_enable_axes(const lis3dh_context dev, bool x_axis_enable, bool y_axis_enable, bool z_axis_enable)
Definition: lis3dh.c:200
int lis3dh_read_regs(const lis3dh_context dev, uint8_t reg, uint8_t *buffer, int len)
Definition: lis3dh.c:593
uint8_t lis3dh_get_status_aux(const lis3dh_context dev)
Definition: lis3dh.c:738
upm_result_t lis3dh_enable_adc(const lis3dh_context dev, bool adc_enable)
Definition: lis3dh.c:393
void lis3dh_uninstall_isr(const lis3dh_context dev, LIS3DH_INTERRUPT_PINS_T intr)
Definition: lis3dh.c:788
upm_result_t lis3dh_enable_hr_mode(const lis3dh_context dev, bool hr_enable)
Definition: lis3dh.c:297
upm_result_t lis3dh_install_isr(const lis3dh_context dev, LIS3DH_INTERRUPT_PINS_T intr, int gpio, mraa_gpio_edge_t level, void(*isr)(void *), void *arg)
Definition: lis3dh.c:746
lis3dh_context lis3dh_init(int bus, int addr, int cs)
Definition: lis3dh.c:73
upm_result_t lis3dh_enable_normal_mode(const lis3dh_context dev)
Definition: lis3dh.c:331
float lis3dh_get_temperature(const lis3dh_context dev)
Definition: lis3dh.c:722
upm_result_t lis3dh_set_int1_config(const lis3dh_context dev, uint8_t cfg)
Definition: lis3dh.c:543
upm_result_t lis3dh_set_int2_config(const lis3dh_context dev, uint8_t cfg)
Definition: lis3dh.c:556
upm_result_t lis3dh_enable_lp_mode(const lis3dh_context dev, bool lp_enable)
Definition: lis3dh.c:260
upm_result_t lis3dh_set_full_scale(const lis3dh_context dev, LIS3DH_FS_T fs)
Definition: lis3dh.c:480
uint8_t lis3dh_read_reg(const lis3dh_context dev, uint8_t reg)
Definition: lis3dh.c:569
upm_result_t lis3dh_enable_hp_filtering(const lis3dh_context dev, bool filter)
Definition: lis3dh.c:345
upm_result_t lis3dh_set_odr(const lis3dh_context dev, LIS3DH_ODR_T odr)
Definition: lis3dh.c:448
Definition: lis3dh.h:58
uint8_t lis3dh_get_chip_id(const lis3dh_context dev)
Definition: lis3dh.c:696