LIS3DH 3-axes accelerometer driver added (#546)
This commit is contained in:
parent
812794f7d9
commit
a4de9dd4f1
9 changed files with 3665 additions and 0 deletions
1039
extras/lis3dh/README.md
Normal file
1039
extras/lis3dh/README.md
Normal file
File diff suppressed because it is too large
Load diff
10
extras/lis3dh/component.mk
Normal file
10
extras/lis3dh/component.mk
Normal file
|
@ -0,0 +1,10 @@
|
|||
# Component makefile for extras/lis3dh
|
||||
|
||||
# expected anyone using SHT3x driver includes it as 'lis3dh/lis3dh.h'
|
||||
INC_DIRS += $(lis3dh_ROOT)..
|
||||
INC_DIRS += $(lis3dh_ROOT)
|
||||
|
||||
# args for passing into compile rule generation
|
||||
lis3dh_SRC_DIR = $(lis3dh_ROOT)
|
||||
|
||||
$(eval $(call component_compile_rules,lis3dh))
|
1274
extras/lis3dh/lis3dh.c
Normal file
1274
extras/lis3dh/lis3dh.c
Normal file
File diff suppressed because it is too large
Load diff
456
extras/lis3dh/lis3dh.h
Normal file
456
extras/lis3dh/lis3dh.h
Normal file
|
@ -0,0 +1,456 @@
|
|||
/**
|
||||
* Driver for LIS3DH 3-axes digital accelerometer connected to I2C or SPI.
|
||||
*
|
||||
* This driver is for the usage with the ESP8266 and FreeRTOS (esp-open-rtos)
|
||||
* [https://github.com/SuperHouse/esp-open-rtos]. It is also working with ESP32
|
||||
* and ESP-IDF [https://github.com/espressif/esp-idf.git] as well as Linux
|
||||
* based systems using a wrapper library for ESP8266 functions.
|
||||
*
|
||||
* ---------------------------------------------------------------------------
|
||||
*
|
||||
* The BSD License (3-clause license)
|
||||
*
|
||||
* Copyright (c) 2017 Gunar Schorcht (https://github.com/gschorcht)
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef __LIS3DH_H__
|
||||
#define __LIS3DH_H__
|
||||
|
||||
// Uncomment one of the following defines to enable debug output
|
||||
// #define LIS3DH_DEBUG_LEVEL_1 // only error messages
|
||||
// #define LIS3DH_DEBUG_LEVEL_2 // debug and error messages
|
||||
|
||||
// LIS3DH addresses (also used for LIS2DH, LIS2DH12 and LIS2DE12)
|
||||
#define LIS3DH_I2C_ADDRESS_1 0x18 // SDO pin is low
|
||||
#define LIS3DH_I2C_ADDRESS_2 0x19 // SDO pin is high
|
||||
|
||||
// LIS3DE addresse (also used for LIS2DE)
|
||||
#define LIS3DE_I2C_ADDRESS_1 0x28 // SDO pin is low
|
||||
#define LIS3DE_I2C_ADDRESS_2 0x29 // SDO pin is high
|
||||
|
||||
// LIS3DH chip id
|
||||
#define LIS3DH_CHIP_ID 0x33 // LIS3DH_REG_WHO_AM_I<7:0>
|
||||
|
||||
// Definition of error codes
|
||||
#define LIS3DH_OK 0
|
||||
#define LIS3DH_NOK -1
|
||||
|
||||
#define LIS3DH_INT_ERROR_MASK 0x000f
|
||||
#define LIS3DH_DRV_ERROR_MASK 0xfff0
|
||||
|
||||
// Error codes for I2C and SPI interfaces ORed with LIS3DH driver error codes
|
||||
#define LIS3DH_I2C_READ_FAILED 1
|
||||
#define LIS3DH_I2C_WRITE_FAILED 2
|
||||
#define LIS3DH_I2C_BUSY 3
|
||||
#define LIS3DH_SPI_WRITE_FAILED 4
|
||||
#define LIS3DH_SPI_READ_FAILED 5
|
||||
#define LIS3DH_SPI_BUFFER_OVERFLOW 6
|
||||
|
||||
// LIS3DH driver error codes ORed with error codes for I2C and SPI interfaces
|
||||
#define LIS3DH_WRONG_CHIP_ID ( 1 << 8)
|
||||
#define LIS3DH_WRONG_BANDWIDTH ( 2 << 8)
|
||||
#define LIS3DH_GET_RAW_DATA_FAILED ( 3 << 8)
|
||||
#define LIS3DH_GET_RAW_DATA_FIFO_FAILED ( 4 << 8)
|
||||
#define LIS3DH_WRONG_INT_TYPE ( 5 << 8)
|
||||
#define LIS3DH_CONFIG_INT_SIGNALS_FAILED ( 6 << 8)
|
||||
#define LIS3DH_CONFIG_INT_FAILED ( 7 << 8)
|
||||
#define LIS3DH_INT_SOURCE_FAILED ( 8 << 8)
|
||||
#define LIS3DH_CONFIG_HPF_FAILED ( 9 << 8)
|
||||
#define LIS3DH_ENABLE_HPF_FAILED (10 << 8)
|
||||
#define LIS3DH_CONFIG_CLICK_FAILED (11 << 8)
|
||||
#define LIS3DH_CLICK_SOURCE_FAILED (12 << 8)
|
||||
#define LIS3DH_GET_ADC_DATA_FAILED (13 << 8)
|
||||
#define LIS3DH_SENSOR_IN_BYPASS_MODE (14 << 8)
|
||||
#define LIS3DH_SENSOR_IN_FIFO_MODE (15 << 8)
|
||||
#define LIS3DH_ODR_TOO_HIGH (16 << 8)
|
||||
|
||||
#include "lis3dh_platform.h"
|
||||
#include "lis3dh_types.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
|
||||
/**
|
||||
* @brief Initialize the sensor
|
||||
*
|
||||
* Reset the sensor and switch to power down mode. All registers are reset to
|
||||
* default values. FIFO is cleared.
|
||||
*
|
||||
* @param bus I2C or SPI bus at which LIS3DH sensor is connected
|
||||
* @param addr I2C addr of the LIS3DH sensor, 0 for using SPI
|
||||
* @param cs SPI CS GPIO, ignored for I2C
|
||||
* @return pointer to sensor data structure, or NULL on error
|
||||
*/
|
||||
lis3dh_sensor_t* lis3dh_init_sensor (uint8_t bus, uint8_t addr, uint8_t cs);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Set sensor mode
|
||||
*
|
||||
* @param dev pointer to the sensor device data structure
|
||||
* @param odr sensor output data rate (ODR)
|
||||
* @param res sensor resolution
|
||||
* @param x true enable x-axis, false disable x-axis
|
||||
* @param y true enable y-axis, false disable y-axis
|
||||
* @param z true enable z-axis, false disable z-axis
|
||||
* @return true on success, false on error
|
||||
*/
|
||||
bool lis3dh_set_mode (lis3dh_sensor_t* dev,
|
||||
lis3dh_odr_mode_t odr, lis3dh_resolution_t res,
|
||||
bool x, bool y, bool z);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Set scale (full scale range)
|
||||
*
|
||||
* @param dev pointer to the sensor device data structure
|
||||
* @param scale full range scale
|
||||
* @return true on success, false on error
|
||||
*/
|
||||
bool lis3dh_set_scale (lis3dh_sensor_t* dev, lis3dh_scale_t scale);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Set FIFO mode
|
||||
*
|
||||
* FIFO watermark can be used to generate an interrupt when FIFO content
|
||||
* exceeds the value. It is ignored in bypass mode.
|
||||
*
|
||||
*
|
||||
* @param dev pointer to the sensor device data structure
|
||||
* @param mode FIFO mode
|
||||
* @param thresh FIFO watermark (ignored in bypass mode)
|
||||
* @param trigger interrupt signal used as trigger (only in Stream-to-FIFO)
|
||||
* @return true on success, false on error
|
||||
*/
|
||||
bool lis3dh_set_fifo_mode (lis3dh_sensor_t* dev, lis3dh_fifo_mode_t mode,
|
||||
uint8_t thresh, lis3dh_int_signal_t trigger);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Test whether new data samples are available
|
||||
*
|
||||
* @param dev pointer to the sensor device data structure
|
||||
* @return true on new data, otherwise false
|
||||
*/
|
||||
bool lis3dh_new_data (lis3dh_sensor_t* dev);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Get one sample of sensor data as floating point values (unit g)
|
||||
*
|
||||
* Function works only in bypass mode and fails in FIFO modes. In FIFO modes,
|
||||
* function *lis3dh_get_float_data_fifo* has to be used instead to get data.
|
||||
*
|
||||
* @param dev pointer to the sensor device data structure
|
||||
* @param data pointer to float data structure filled with g values
|
||||
* @return true on success, false on error
|
||||
*/
|
||||
bool lis3dh_get_float_data (lis3dh_sensor_t* dev,
|
||||
lis3dh_float_data_t* data);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Get all samples of sensor data stored in the FIFO (unit g)
|
||||
*
|
||||
* In bypass mode, it returns only one sensor data sample.
|
||||
*
|
||||
* @param dev pointer to the sensor device data structure
|
||||
* @param data array of 32 float data structures filled with g values
|
||||
* @return number of data sets read from fifo on success or 0 on error
|
||||
*/
|
||||
uint8_t lis3dh_get_float_data_fifo (lis3dh_sensor_t* dev,
|
||||
lis3dh_float_data_fifo_t data);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Get one sample of raw sensor data as 16 bit two's complements
|
||||
*
|
||||
* Function works only in bypass mode and fails in FIFO modes. In FIFO modes,
|
||||
* function *lis3dh_get_raw_data_fifo* has to be used instead to get data.
|
||||
*
|
||||
* @param dev pointer to the sensor device data structure
|
||||
* @param raw pointer to raw data structure filled with values
|
||||
* @return true on success, false on error
|
||||
*/
|
||||
bool lis3dh_get_raw_data (lis3dh_sensor_t* dev, lis3dh_raw_data_t* raw);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Get all samples of raw sensor data stored in the FIFO
|
||||
*
|
||||
* In bypass mode, it returns only one raw data sample.
|
||||
*
|
||||
* @param dev pointer to the sensor device data structure
|
||||
* @param raw array of 32 raw data structures
|
||||
* @return number of data sets read from fifo on success or 0 on error
|
||||
*/
|
||||
uint8_t lis3dh_get_raw_data_fifo (lis3dh_sensor_t* dev,
|
||||
lis3dh_raw_data_fifo_t raw);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Enable / disable an interrupt on signal INT1 or INT2
|
||||
*
|
||||
* @param dev pointer to the sensor device data structure
|
||||
* @param type interrupt to be enabled or disabled
|
||||
* @param signal interrupt signal that is activated for the interrupt
|
||||
* @param value true to enable or false to disable the interrupt
|
||||
* @return true on success, false on error
|
||||
*/
|
||||
bool lis3dh_enable_int (lis3dh_sensor_t* dev,
|
||||
lis3dh_int_type_t type,
|
||||
lis3dh_int_signal_t signal, bool value);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Get the source of data ready and FIFO interrupts on INT1
|
||||
*
|
||||
* @param dev pointer to the sensor device data structure
|
||||
* @param source pointer to the interrupt source
|
||||
* @return true on success, false on error
|
||||
*/
|
||||
bool lis3dh_get_int_data_source (lis3dh_sensor_t* dev,
|
||||
lis3dh_int_data_source_t* source);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Set the configuration of an inertial event interrupt generator
|
||||
*
|
||||
* Inertial interrupt generators produce interrupts when certain inertial event
|
||||
* occures (event interrupts), that is, the acceleration of defined axes is
|
||||
* higher or lower than a defined threshold and one of the following event is
|
||||
* recognized: axis movement / wake up, free fall, 6D/4D orientation detection.
|
||||
*
|
||||
* @param dev pointer to the sensor device data structure
|
||||
* @param config pointer to the interrupt generator configuration
|
||||
* @param gen interrupt generator to which the function is applied
|
||||
* @return true on success, false on error
|
||||
*/
|
||||
bool lis3dh_set_int_event_config (lis3dh_sensor_t* dev,
|
||||
lis3dh_int_event_config_t* config,
|
||||
lis3dh_int_event_gen_t gen);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Get the configuration of an inertial event interrupt generator
|
||||
*
|
||||
* Inertial interrupt generators produce interrupts when certain inertial event
|
||||
* occures (event interrupts), that is, the acceleration of defined axes is
|
||||
* higher or lower than a defined threshold and one of the following event is
|
||||
* recognized: axis movement / wake up, free fall, 6D/4D orientation detection.
|
||||
*
|
||||
* @param dev pointer to the sensor device data structure
|
||||
* @param config pointer to the interrupt generator configuration
|
||||
* @param gen interrupt generator to which the function is applied
|
||||
* @return true on success, false on error
|
||||
*/
|
||||
bool lis3dh_get_int_event_config (lis3dh_sensor_t* dev,
|
||||
lis3dh_int_event_config_t* config,
|
||||
lis3dh_int_event_gen_t gen);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Get the source of an inertial event interrupt INT1/INT2
|
||||
*
|
||||
* Returns a byte with flags that indicate the event which triggered
|
||||
* the interrupt signal (see INTx_SRC register in datasheet for details)
|
||||
*
|
||||
* @param dev pointer to the sensor device data structure
|
||||
* @param source pointer to the interrupt source data structure
|
||||
* @param gen interrupt generator to which the function is applied
|
||||
* @return true on success, false on error
|
||||
*/
|
||||
bool lis3dh_get_int_event_source (lis3dh_sensor_t* dev,
|
||||
lis3dh_int_event_source_t* source,
|
||||
lis3dh_int_event_gen_t gen);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Set the configuration of the click detection interrupt generator
|
||||
*
|
||||
* Set the configuration for interrupts that are generated when single or
|
||||
* double clicks are detected.
|
||||
*
|
||||
* @param dev pointer to the sensor device data structure
|
||||
* @param config pointer to the interrupt generator configuration
|
||||
* @return true on success, false on error
|
||||
*/
|
||||
bool lis3dh_set_int_click_config (lis3dh_sensor_t* dev,
|
||||
lis3dh_int_click_config_t* config);
|
||||
|
||||
/**
|
||||
* @brief Get the configuration of the click detection interrupt generator
|
||||
*
|
||||
* Set the configuration for interrupts that are generated when single or
|
||||
* double clicks are detected.
|
||||
*
|
||||
* @param dev pointer to the sensor device data structure
|
||||
* @param config pointer to the interrupt generator configuration
|
||||
* @return true on success, false on error
|
||||
*/
|
||||
bool lis3dh_get_int_click_config (lis3dh_sensor_t* dev,
|
||||
lis3dh_int_click_config_t* config);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Get the source of the click detection interrupt on signal INT1/INT2
|
||||
*
|
||||
* Returns a byte with flags that indicate the activity which triggered
|
||||
* the interrupt signal (see CLICK_SRC register in datasheet for details)
|
||||
*
|
||||
* @param dev pointer to the sensor device data structure
|
||||
* @param source pointer to the interrupt source
|
||||
* @return true on success, false on error
|
||||
*/
|
||||
bool lis3dh_get_int_click_source (lis3dh_sensor_t* dev,
|
||||
lis3dh_int_click_source_t* source);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Set signal configuration for INT1 and INT2 signals
|
||||
*
|
||||
* @param dev pointer to the sensor device data structure
|
||||
* @param level define interrupt signal as low or high active
|
||||
* @return true on success, false on error
|
||||
*/
|
||||
bool lis3dh_config_int_signals (lis3dh_sensor_t* dev,
|
||||
lis3dh_int_signal_level_t level);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Config HPF (high pass filter)
|
||||
*
|
||||
* @param dev pointer to the sensor device data structure
|
||||
* @param mode filter mode
|
||||
* @param cutoff filter cutoff frequency (depends on ODR) [0 ... 3]
|
||||
* @param data if true, use filtered data as sensor output
|
||||
* @param click if true, use filtered data for CLICK function
|
||||
* @param int1 if true, use filtered data for interrupt INT1 generation
|
||||
* @param int2 if true, use filtered data for interrupt INT2 generation
|
||||
* @return true on success, false on error
|
||||
*/
|
||||
bool lis3dh_config_hpf (lis3dh_sensor_t* dev,
|
||||
lis3dh_hpf_mode_t mode, uint8_t cutoff,
|
||||
bool data, bool click, bool int1, bool int2);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Set HPF (high pass filter) reference
|
||||
*
|
||||
* Used to set the reference of HPF in reference mode *lis3dh_hpf_reference*.
|
||||
* Used to reset the HPF in autoreset mode *lis3dh_hpf_autoreset*.
|
||||
* Reference is given as two's complement.
|
||||
*
|
||||
* @param dev pointer to the sensor device data structure
|
||||
* @param ref reference *lis3dh_hpf_reference* mode, otherwise ignored
|
||||
* @return true on success, false on error
|
||||
*/
|
||||
bool lis3dh_set_hpf_ref (lis3dh_sensor_t* dev, int8_t ref);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Get HPF (high pass filter) reference
|
||||
*
|
||||
* Used to reset the HPF in normal mode *lis3dh_hpf_normal*.
|
||||
*
|
||||
* @param dev pointer to the sensor device data structure
|
||||
* @return HPF reference as two's complement
|
||||
*/
|
||||
int8_t lis3dh_get_hpf_ref (lis3dh_sensor_t* dev);
|
||||
|
||||
/**
|
||||
* @brief Enable / disable ADC or temperature sensor
|
||||
*
|
||||
* @param dev pointer to the sensor device data structure
|
||||
* @param enable if true, ADC inputs are enabled
|
||||
* @param temp if true, ADC input 3 is the output of temperature sensor
|
||||
* @return true on success, false on error
|
||||
*/
|
||||
int8_t lis3dh_enable_adc (lis3dh_sensor_t* dev, bool enable, bool temp);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Get ADC input or temperature
|
||||
*
|
||||
* @param dev pointer to the sensor device data structure
|
||||
* @param adc1 ADC input 1
|
||||
* @param adc2 ADC input 2
|
||||
* @param adc3 ADC input 3 or temperature in degree if enabled
|
||||
* @return true on success, false on error
|
||||
*/
|
||||
bool lis3dh_get_adc (lis3dh_sensor_t* dev,
|
||||
uint16_t* adc1, uint16_t* adc2, uint16_t* adc3);
|
||||
|
||||
|
||||
// ---- Low level interface functions -----------------------------
|
||||
|
||||
/**
|
||||
* @brief Direct write to register
|
||||
*
|
||||
* PLEASE NOTE: This function should only be used to do something special that
|
||||
* is not covered by the high level interface AND if you exactly know what you
|
||||
* do and what effects it might have. Please be aware that it might affect the
|
||||
* high level interface.
|
||||
*
|
||||
* @param dev pointer to the sensor device data structure
|
||||
* @param reg address of the first register to be changed
|
||||
* @param data pointer to the data to be written to the register
|
||||
* @param len number of bytes to be written to the register
|
||||
* @return true on success, false on error
|
||||
*/
|
||||
bool lis3dh_reg_write (lis3dh_sensor_t* dev,
|
||||
uint8_t reg, uint8_t *data, uint16_t len);
|
||||
|
||||
/**
|
||||
* @brief Direct read from register
|
||||
*
|
||||
* PLEASE NOTE: This function should only be used to do something special that
|
||||
* is not covered by the high level interface AND if you exactly know what you
|
||||
* do and what effects it might have. Please be aware that it might affect the
|
||||
* high level interface.
|
||||
*
|
||||
* @param dev pointer to the sensor device data structure
|
||||
* @param reg address of the first register to be read
|
||||
* @param data pointer to the data to be read from the register
|
||||
* @param len number of bytes to be read from the register
|
||||
* @return true on success, false on error
|
||||
*/
|
||||
bool lis3dh_reg_read (lis3dh_sensor_t* dev,
|
||||
uint8_t reg, uint8_t *data, uint16_t len);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif /* End of CPP guard */
|
||||
|
||||
#endif /* __LIS3DH_H__ */
|
81
extras/lis3dh/lis3dh_platform.c
Normal file
81
extras/lis3dh/lis3dh_platform.c
Normal file
|
@ -0,0 +1,81 @@
|
|||
/**
|
||||
* Driver for LIS3DH 3-axes digital accelerometer connected to I2C or SPI.
|
||||
*
|
||||
* This driver is for the usage with the ESP8266 and FreeRTOS (esp-open-rtos)
|
||||
* [https://github.com/SuperHouse/esp-open-rtos]. It is also working with ESP32
|
||||
* and ESP-IDF [https://github.com/espressif/esp-idf.git] as well as Linux
|
||||
* based systems using a wrapper library for ESP8266 functions.
|
||||
*
|
||||
* ---------------------------------------------------------------------------
|
||||
*
|
||||
* The BSD License (3-clause license)
|
||||
*
|
||||
* Copyright (c) 2017 Gunar Schorcht (https://github.com/gschorcht)
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Platform file: platform specific definitions, includes and functions
|
||||
*/
|
||||
|
||||
#include "lis3dh_platform.h"
|
||||
|
||||
// platform specific SPI functions
|
||||
|
||||
static const spi_settings_t bus_settings = {
|
||||
.mode = SPI_MODE0,
|
||||
.freq_divider = SPI_FREQ_DIV_1M,
|
||||
.msb = true,
|
||||
.minimal_pins = false,
|
||||
.endianness = SPI_LITTLE_ENDIAN
|
||||
};
|
||||
|
||||
bool spi_device_init (uint8_t bus, uint8_t cs)
|
||||
{
|
||||
gpio_enable(cs, GPIO_OUTPUT);
|
||||
gpio_write (cs, true);
|
||||
return true;
|
||||
}
|
||||
|
||||
size_t spi_transfer_pf(uint8_t bus, uint8_t cs, const uint8_t *mosi, uint8_t *miso, uint16_t len)
|
||||
{
|
||||
spi_settings_t old_settings;
|
||||
|
||||
spi_get_settings(bus, &old_settings);
|
||||
spi_set_settings(bus, &bus_settings);
|
||||
gpio_write(cs, false);
|
||||
|
||||
size_t transfered = spi_transfer (bus, (const void*)mosi, (void*)miso, len, SPI_8BIT);
|
||||
|
||||
gpio_write(cs, true);
|
||||
spi_set_settings(bus, &old_settings);
|
||||
|
||||
return transfered;
|
||||
}
|
||||
|
80
extras/lis3dh/lis3dh_platform.h
Normal file
80
extras/lis3dh/lis3dh_platform.h
Normal file
|
@ -0,0 +1,80 @@
|
|||
/**
|
||||
* Driver for LIS3DH 3-axes digital accelerometer connected to I2C or SPI.
|
||||
*
|
||||
* This driver is for the usage with the ESP8266 and FreeRTOS (esp-open-rtos)
|
||||
* [https://github.com/SuperHouse/esp-open-rtos]. It is also working with ESP32
|
||||
* and ESP-IDF [https://github.com/espressif/esp-idf.git] as well as Linux
|
||||
* based systems using a wrapper library for ESP8266 functions.
|
||||
*
|
||||
* ---------------------------------------------------------------------------
|
||||
*
|
||||
* The BSD License (3-clause license)
|
||||
*
|
||||
* Copyright (c) 2017 Gunar Schorcht (https://github.com/gschorcht)
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Platform file: platform specific definitions, includes and functions
|
||||
*/
|
||||
|
||||
#ifndef __LIS3DH_PLATFORM_H__
|
||||
#define __LIS3DH_PLATFORM_H__
|
||||
|
||||
#if !defined(ESP_OPEN_RTOS)
|
||||
#define ESP_OPEN_RTOS 1
|
||||
#endif
|
||||
|
||||
#ifdef ESP_OPEN_RTOS // ESP8266
|
||||
|
||||
// platform specific includes
|
||||
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
#include "queue.h"
|
||||
|
||||
#include "espressif/esp_common.h"
|
||||
#include "espressif/sdk_private.h"
|
||||
|
||||
#include "esp/uart.h"
|
||||
#include "esp/spi.h"
|
||||
#include "i2c/i2c.h"
|
||||
|
||||
// platform specific SPI functions
|
||||
|
||||
#define spi_bus_init(bus,sck,miso,mosi) // not needed on ESP8266
|
||||
|
||||
extern bool spi_device_init (uint8_t bus, uint8_t cs);
|
||||
extern size_t spi_transfer_pf (uint8_t bus, uint8_t cs,
|
||||
const uint8_t *mosi, uint8_t *miso,
|
||||
uint16_t len);
|
||||
|
||||
#endif // ESP_OPEN_RTOS
|
||||
|
||||
#endif // __LIS3DH_PLATFORM_H__
|
355
extras/lis3dh/lis3dh_types.h
Normal file
355
extras/lis3dh/lis3dh_types.h
Normal file
|
@ -0,0 +1,355 @@
|
|||
/**
|
||||
* Driver for LIS3DH 3-axes digital accelerometer connected to I2C or SPI.
|
||||
*
|
||||
* This driver is for the usage with the ESP8266 and FreeRTOS (esp-open-rtos)
|
||||
* [https://github.com/SuperHouse/esp-open-rtos]. It is also working with ESP32
|
||||
* and ESP-IDF [https://github.com/espressif/esp-idf.git] as well as Linux
|
||||
* based systems using a wrapper library for ESP8266 functions.
|
||||
*
|
||||
* ---------------------------------------------------------------------------
|
||||
*
|
||||
* The BSD License (3-clause license)
|
||||
*
|
||||
* Copyright (c) 2017 Gunar Schorcht (https://github.com/gschorcht)
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO Activity SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef __LIS3DH_TYPES_H__
|
||||
#define __LIS3DH_TYPES_H__
|
||||
|
||||
#include "stdint.h"
|
||||
#include "stdbool.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
|
||||
/**
|
||||
* @brief Output data rates (ODR), related to resolution modes
|
||||
*/
|
||||
typedef enum {
|
||||
|
||||
lis3dh_power_down = 0, // power down mode
|
||||
lis3dh_odr_1, // high resolution / normal / low power 1 Hz
|
||||
lis3dh_odr_10, // high resolution / normal / low power 10 Hz
|
||||
lis3dh_odr_25, // high resolution / normal / low power 25 Hz
|
||||
lis3dh_odr_50, // high resolution / normal / low power 50 Hz
|
||||
lis3dh_odr_100, // high resolution / normal / low power 100 Hz
|
||||
lis3dh_odr_200, // high resolution / normal / low power 200 Hz
|
||||
lis3dh_odr_400, // high resolution / normal / low power 400 Hz
|
||||
lis3dh_odr_1600, // low power mode 1.6 kHz
|
||||
lis3dh_odr_5000, // normal 1.25 kHz / low power 5 kHz
|
||||
|
||||
} lis3dh_odr_mode_t;
|
||||
|
||||
/**
|
||||
* @brief Resolution modes, related to output data rates (ODR)
|
||||
*/
|
||||
typedef enum {
|
||||
|
||||
lis3dh_low_power, // low power mode resolution ( 8 bit data)
|
||||
lis3dh_normal, // normal mode resolution (10 bit data)
|
||||
lis3dh_high_res // high resolution mode (12 bit data)
|
||||
|
||||
} lis3dh_resolution_t;
|
||||
|
||||
/**
|
||||
* @brief Full scale measurement range
|
||||
*/
|
||||
typedef enum {
|
||||
|
||||
lis3dh_scale_2_g = 0, // default
|
||||
lis3dh_scale_4_g,
|
||||
lis3dh_scale_8_g,
|
||||
lis3dh_scale_16_g
|
||||
|
||||
} lis3dh_scale_t;
|
||||
|
||||
|
||||
/**
|
||||
* @brief FIFO mode
|
||||
*/
|
||||
typedef enum {
|
||||
|
||||
lis3dh_bypass = 0, // default
|
||||
lis3dh_fifo = 1,
|
||||
lis3dh_stream = 2,
|
||||
lis3dh_trigger= 3
|
||||
|
||||
} lis3dh_fifo_mode_t;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Interrupt signals
|
||||
*/
|
||||
typedef enum {
|
||||
|
||||
lis3dh_int1_signal = 0,
|
||||
lis3dh_int2_signal = 1
|
||||
|
||||
} lis3dh_int_signal_t;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Inertial event interrupt generators
|
||||
*/
|
||||
typedef enum {
|
||||
|
||||
lis3dh_int_event1_gen = 0,
|
||||
lis3dh_int_event2_gen = 1
|
||||
|
||||
} lis3dh_int_event_gen_t;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Interrupt types for interrupt signals INT1/INT2
|
||||
*/
|
||||
typedef enum {
|
||||
|
||||
lis3dh_int_data_ready, // data ready for read interrupt (only INT1)
|
||||
|
||||
lis3dh_int_fifo_watermark, // FIFO exceeds the threshold (only INT1)
|
||||
lis3dh_int_fifo_overrun, // FIFO is completely filled (only INT1)
|
||||
|
||||
lis3dh_int_event1, // inertial event interrupt 1
|
||||
lis3dh_int_event2, // inertial event interrupt 2
|
||||
|
||||
lis3dh_int_click // click detection interrupt
|
||||
|
||||
} lis3dh_int_type_t;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Data ready and FIFO status interrupt source for INT1
|
||||
*/
|
||||
typedef struct {
|
||||
|
||||
bool data_ready; // true when acceleration data are ready to read
|
||||
|
||||
bool fifo_watermark; // true when FIFO exceeds the FIFO threshold
|
||||
bool fifo_overrun; // true when FIFO is completely filled
|
||||
|
||||
} lis3dh_int_data_source_t;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Inertial interrupt generator configuration for INT1/INT2
|
||||
*
|
||||
* Inertial events are: wake-up, free-fall, 6D/4D detection.
|
||||
*/
|
||||
typedef struct {
|
||||
|
||||
enum { // interrupt mode
|
||||
|
||||
lis3dh_wake_up, // AOI = 0, 6D = 0
|
||||
lis3dh_free_fall, // AOI = 1, 6D = 0
|
||||
|
||||
lis3dh_6d_movement, // AOI = 0, 6D = 1, D4D = 0
|
||||
lis3dh_6d_position, // AOI = 1, 6D = 1, D4D = 0
|
||||
|
||||
lis3dh_4d_movement, // AOI = 0, 6D = 1, D4D = 1
|
||||
lis3dh_4d_position, // AOI = 1, 6D = 1, D4D = 1
|
||||
|
||||
} mode;
|
||||
|
||||
uint8_t threshold; // threshold used for comparison for all axes
|
||||
|
||||
bool x_low_enabled; // x lower than threshold interrupt enabled
|
||||
bool x_high_enabled; // x higher than threshold interrupt enabled
|
||||
|
||||
bool y_low_enabled; // y lower than threshold interrupt enabled
|
||||
bool y_high_enabled; // y higher than threshold interrupt enabled
|
||||
|
||||
bool z_low_enabled; // z lower than threshold interrupt enabled
|
||||
bool z_high_enabled; // z higher than threshold interrupt enabled
|
||||
|
||||
bool latch; // latch the interrupt when true until the
|
||||
// interrupt source has been read
|
||||
|
||||
uint8_t duration; // duration in 1/ODR an interrupt condition has
|
||||
// to be given before the interrupt is generated
|
||||
} lis3dh_int_event_config_t;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Inertial event source type for interrupt generator INT1/INT2
|
||||
*/
|
||||
typedef struct {
|
||||
|
||||
bool active:1; // true - one ore more events occured
|
||||
|
||||
bool x_low :1; // true - x lower than threshold event
|
||||
bool x_high:1; // true - x higher than threshold event
|
||||
|
||||
bool y_low :1; // true - z lower than threshold event
|
||||
bool y_high:1; // true - z higher than threshold event
|
||||
|
||||
bool z_low :1; // true - z lower than threshold event
|
||||
bool z_high:1; // true - z higher than threshold event
|
||||
|
||||
} lis3dh_int_event_source_t;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Click interrupt configuration for interrupt signals INT1/INT2
|
||||
*/
|
||||
typedef struct {
|
||||
|
||||
bool x_single; // x-axis single tap interrupt enabled
|
||||
bool x_double; // x-axis double tap interrupt enabled
|
||||
|
||||
bool y_single; // y-axis single tap interrupt enabled
|
||||
bool y_double; // y-axis double tap interrupt enabled
|
||||
|
||||
bool z_single; // z-axis single tap interrupt enabled
|
||||
bool z_double; // z-axis double tap interrupt enabled
|
||||
|
||||
uint8_t threshold; // threshold used for comparison for all axes
|
||||
|
||||
bool latch; // latch the interrupt when true until the
|
||||
// interrupt source has been read
|
||||
|
||||
uint8_t time_limit; // maximum time interval between the start and the
|
||||
// end of a cick (accel increases and falls back)
|
||||
uint8_t time_latency; // click detection is disabled for that time after
|
||||
// a was click detected (in 1/ODR)
|
||||
uint8_t time_window; // time interval in which the second click has to
|
||||
// to be detected in double clicks (in 1/ODR)
|
||||
|
||||
} lis3dh_int_click_config_t;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Click interrupt source for interrupt signals INT1/INT2
|
||||
*/
|
||||
typedef struct {
|
||||
|
||||
bool x_click:1; // click detected in x direction
|
||||
bool y_click:1; // click detected in y direction
|
||||
bool z_click:1; // click detected in z direction
|
||||
|
||||
bool sign :1; // click sign (0 - posisitive, 1 - negative)
|
||||
|
||||
bool s_click:1; // single click detected
|
||||
bool d_click:1; // double click detected
|
||||
|
||||
bool active :1; // true - one ore more event occured
|
||||
|
||||
} lis3dh_int_click_source_t;
|
||||
|
||||
|
||||
/**
|
||||
* @brief INT1, INT2 signal activity level
|
||||
*/
|
||||
typedef enum {
|
||||
|
||||
lis3dh_high_active = 0,
|
||||
lis3dh_low_active
|
||||
|
||||
} lis3dh_int_signal_level_t;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Raw data set as two complements
|
||||
*/
|
||||
typedef struct {
|
||||
|
||||
int16_t ax; // acceleration on x axis
|
||||
int16_t ay; // acceleration on y axis
|
||||
int16_t az; // acceleration on z axis
|
||||
|
||||
} lis3dh_raw_data_t;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Raw data FIFO type
|
||||
*/
|
||||
typedef lis3dh_raw_data_t lis3dh_raw_data_fifo_t[32];
|
||||
|
||||
|
||||
/**
|
||||
* @brief Floating point output value set in g
|
||||
*/
|
||||
typedef struct {
|
||||
|
||||
float ax; // acceleration on x axis
|
||||
float ay; // acceleration on y axis
|
||||
float az; // acceleration on z axis
|
||||
|
||||
} lis3dh_float_data_t;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Floating point output value FIFO type
|
||||
*/
|
||||
typedef lis3dh_float_data_t lis3dh_float_data_fifo_t[32];
|
||||
|
||||
|
||||
/**
|
||||
* @brief HPF (high pass filter) modes
|
||||
*/
|
||||
typedef enum {
|
||||
|
||||
lis3dh_hpf_normal = 0, // normal mode (reset by reading reference)
|
||||
lis3dh_hpf_reference, // reference signal for filtering
|
||||
lis3dh_hpf_normal_x, // normal mode
|
||||
lis3dh_hpf_autoreset // autoreset on interrupt Activity
|
||||
|
||||
} lis3dh_hpf_mode_t;
|
||||
|
||||
|
||||
/**
|
||||
* @brief LIS3DH sensor device data structure type
|
||||
*/
|
||||
typedef struct {
|
||||
|
||||
int error_code; // error code of last operation
|
||||
|
||||
uint8_t bus; // I2C = x, SPI = 1
|
||||
uint8_t addr; // I2C = slave address, SPI = 0
|
||||
|
||||
uint8_t cs; // ESP8266, ESP32: GPIO used as SPI CS
|
||||
// __linux__: device index
|
||||
|
||||
lis3dh_scale_t scale; // full range scale (default 2 g)
|
||||
lis3dh_resolution_t res; // resolution used
|
||||
|
||||
lis3dh_fifo_mode_t fifo_mode; // FIFO operation mode (default bypass)
|
||||
bool fifo_first; // first FIFO access
|
||||
|
||||
} lis3dh_sensor_t;
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif /* End of CPP guard */
|
||||
|
||||
#endif /* __LIS3DH_TYPES_H__ */
|
Loading…
Add table
Add a link
Reference in a new issue