L3GD20H driver added

This commit is contained in:
Gunar Schorcht 2017-12-21 19:04:00 +01:00
parent c3ae04c93f
commit 60427cf383
8 changed files with 3218 additions and 0 deletions

View file

@ -0,0 +1,3 @@
PROGRAM=L3GD20H
EXTRA_COMPONENTS = extras/i2c extras/l3gd20h
include ../../common.mk

View file

@ -0,0 +1,322 @@
/**
* Simple example with one sensor connected to I2C or SPI. It demonstrates the
* different approaches to fetch the data. Either one of the interrupt signals
* for axes movement wake up *INT1* and data ready interrupt *INT2* is used
* or the new data are fetched periodically.
*
* Harware configuration:
*
* I2C
*
* +-----------------+ +----------+
* | ESP8266 / ESP32 | | L3GD20H |
* | | | |
* | GPIO 14 (SCL) ----> SCL |
* | GPIO 13 (SDA) <---> SDA |
* | GPIO 5 <---- INT1 |
* | GPIO 4 <---- DRDY/INT2|
* +-----------------+ +----------+
*
* SPI
*
* +---------------+ +----------+ +---------------+ +----------+
* | ESP8266 | | L3GD20H | | ESP32 | | L3GD20H |
* | | | | | | | |
* | GPIO 14 (SCK) ----> SCK | | GPIO 16 (SCK) ----> SCK |
* | GPIO 13 (MOSI)----> SDI | | GPIO 17 (MOSI)----> SDI |
* | GPIO 12 (MISO)<---- SDO | | GPIO 18 (MISO)<---- SDO |
* | GPIO 2 (CS) ----> CS | | GPIO 19 (CS) ----> CS |
* | GPIO 5 <---- INT1 | | GPIO 5 <---- INT1 |
* | GPIO 4 <---- DRDY/INT2| | GPIO 4 <---- DRDY/INT2|
* +---------------+ +---------+ +---------------+ +----------+
*/
// use following constants to define the example mode
// #define SPI_USED // if defined SPI is used, otherwise I2C
// #define FIFO_MODE // multiple sample read mode
// #define INT_DATA // data interrupts used (data ready and FIFO status)
// #define INT_EVENT // event interrupts used (axis movement and wake up)
#if defined(INT_EVENT) || defined(INT_DATA)
#define INT_USED
#endif
/* -- includes -------------------------------------------------- */
#include "l3gd20h.h"
/* -- platform dependent definitions ---------------------------- */
#ifdef ESP_PLATFORM // ESP32 (ESP-IDF)
// user task stack depth
#define TASK_STACK_DEPTH 2048
// define SPI interface for L3GD20H sensors
#define SPI_BUS HSPI_HOST
#define SPI_SCK_GPIO 16
#define SPI_MOSI_GPIO 17
#define SPI_MISO_GPIO 18
#define SPI_CS_GPIO 19
#else // ESP8266 (esp-open-rtos)
// user task stack depth
#define TASK_STACK_DEPTH 256
// define SPI interface for L3GD20H sensors
#define SPI_BUS 1
#define SPI_SCK_GPIO 14
#define SPI_MOSI_GPIO 13
#define SPI_MISO_GPIO 12
#define SPI_CS_GPIO 2 // GPIO 15, the default CS of SPI bus 1, can't be used
#endif // ESP_PLATFORM
// define I2C interfaces for L3GD20H sensors
#define I2C_BUS 0
#define I2C_SCL_PIN 14
#define I2C_SDA_PIN 13
#define I2C_FREQ I2C_FREQ_100K
// define GPIOs for interrupt
#define INT1_PIN 5
#define INT2_PIN 4
/* -- user tasks ---------------------------------------------- */
static l3gd20h_sensor_t* sensor;
/**
* Common function used to get sensor data.
*/
void read_data (void)
{
#ifdef FIFO_MODE
l3gd20h_float_data_fifo_t data;
if (l3gd20h_new_data (sensor))
{
uint8_t num = l3gd20h_get_float_data_fifo (sensor, data);
printf("%.3f L3GD20H num=%d\n", (double)sdk_system_get_time()*1e-3, num);
for (int i = 0; i < num; i++)
// max. full scale is +-2000 dps and best sensitivity is 1 mdps, i.e. 7 digits
printf("%.3f L3GD20H (xyz)[dps]: %+9.3f %+9.3f %+9.3f\n",
(double)sdk_system_get_time()*1e-3, data[i].x, data[i].y, data[i].z);
}
#else // !FIFO_MODE
l3gd20h_float_data_t data;
while (l3gd20h_new_data (sensor) &&
l3gd20h_get_float_data (sensor, &data))
// max. full scale is +-2000 dps and best sensitivity is 1 mdps, i.e. 7 digits
printf("%.3f L3GD20H (xyz)[dps]: %+9.3f %+9.3f %+9.3f\n",
(double)sdk_system_get_time()*1e-3, data.x, data.y, data.z);
#endif // FIFO_MODE
}
#ifdef INT_USED
/**
* In this case, axes movement wake up interrupt *INT1* and/or data ready
* interrupt *INT2* are used. While data ready interrupt *INT2* is generated
* every time new data are available or the FIFO status changes, the axes
* movement wake up interrupt *INT1* is triggered when output data across
* defined thresholds.
*
* When interrupts are used, the user has to define interrupt handlers that
* either fetches the data directly or triggers a task which is waiting to
* fetch the data. In this example, the interrupt handler sends an event to
* a waiting task to trigger the data gathering.
*/
static QueueHandle_t gpio_evt_queue = NULL;
// User task that fetches the sensor values.
void user_task_interrupt (void *pvParameters)
{
uint8_t gpio_num;
while (1)
{
if (xQueueReceive(gpio_evt_queue, &gpio_num, portMAX_DELAY))
{
if (gpio_num == INT1_PIN)
{
l3gd20h_int_event_source_t source;
// get the source of INT1 reset INT1 signal
l3gd20h_get_int_event_source (sensor, &source);
// in case of data ready interrupt, get the results and do something with them
if (source.active)
read_data ();
}
else if (gpio_num == INT2_PIN)
{
l3gd20h_int_data_source_t source;
// get the source of INT2
l3gd20h_get_int_data_source (sensor, &source);
// if data ready interrupt, get the results and do something with them
read_data();
}
}
}
}
// Interrupt handler which resumes sends an event to the waiting user_task_interrupt
void IRAM int_signal_handler (uint8_t gpio)
{
// send an event with GPIO to the interrupt user task
xQueueSendFromISR(gpio_evt_queue, &gpio, NULL);
}
#else // !INT_USED
/*
* In this case, no interrupts are used and the user task fetches the sensor
* values periodically every seconds.
*/
void user_task_periodic(void *pvParameters)
{
vTaskDelay (100/portTICK_PERIOD_MS);
while (1)
{
// read sensor data
read_data ();
// passive waiting until 1 second is over
vTaskDelay (1000/portTICK_PERIOD_MS);
}
}
#endif // INT_USED
/* -- main program ---------------------------------------------- */
void user_init(void)
{
// Set UART Parameter.
uart_set_baud(0, 115200);
// Give the UART some time to settle
vTaskDelay(1);
/** -- MANDATORY PART -- */
#ifdef SPI_USED
// init the sensor connnected to SPI
spi_bus_init (SPI_BUS, SPI_SCK_GPIO, SPI_MISO_GPIO, SPI_MOSI_GPIO);
// init the sensor connected to SPI_BUS with SPI_CS_GPIO as chip select.
sensor = l3gd20h_init_sensor (SPI_BUS, 0, SPI_CS_GPIO);
#else // I2C
// init all I2C bus interfaces at which L3GD20H sensors are connected
i2c_init (I2C_BUS, I2C_SCL_PIN, I2C_SDA_PIN, I2C_FREQ);
// init the sensor with slave address L3GD20H_I2C_ADDRESS_2 connected to I2C_BUS.
sensor = l3gd20h_init_sensor (I2C_BUS, L3GD20H_I2C_ADDRESS_2, 0);
#endif // SPI_USED
if (sensor)
{
// --- SYSTEM CONFIGURATION PART ----
#if !defined (INT_USED)
// create a user task that fetches data from sensor periodically
xTaskCreate(user_task_periodic, "user_task_periodic", TASK_STACK_DEPTH, NULL, 2, NULL);
#else // INT_USED
// create a task that is triggered only in case of interrupts to fetch the data
xTaskCreate(user_task_interrupt, "user_task_interrupt", TASK_STACK_DEPTH, NULL, 2, NULL);
// create event queue
gpio_evt_queue = xQueueCreate(10, sizeof(uint8_t));
// configure interupt pins for *INT1* and *INT2* signals and set the interrupt handler
gpio_enable(INT1_PIN, GPIO_INPUT);
gpio_enable(INT2_PIN, GPIO_INPUT);
gpio_set_interrupt(INT1_PIN, GPIO_INTTYPE_EDGE_POS, int_signal_handler);
gpio_set_interrupt(INT2_PIN, GPIO_INTTYPE_EDGE_POS, int_signal_handler);
#endif // !defined(INT_USED)
// -- SENSOR CONFIGURATION PART ---
// Interrupt configuration has to be done before the sensor is set
// into measurement mode
// set polarity of INT signals if necessary
// l3gd20h_config_int_signals (dev, l3gd20h_high_active, l3gd20h_push_pull);
#ifdef INT_EVENT
// enable event interrupts (axis movement and wake up)
l3gd20h_int_event_config_t int_cfg;
l3gd20h_get_int_event_config (sensor, &int_cfg);
int_cfg.x_high_enabled = true;
int_cfg.y_high_enabled = true;
int_cfg.z_high_enabled = true;
int_cfg.x_low_enabled = false;
int_cfg.y_low_enabled = false;
int_cfg.z_low_enabled = false;
int_cfg.x_threshold = 1000;
int_cfg.y_threshold = 1000;
int_cfg.z_threshold = 1000;
int_cfg.filter = l3gd20h_hpf_only;
int_cfg.and_or = false;
int_cfg.duration = 0;
int_cfg.latch = true;
l3gd20h_set_int_event_config (sensor, &int_cfg);
l3gd20h_enable_int (sensor, l3gd20h_int_event, true);
#endif // INT_EVENT
#ifdef INT_DATA
// enable data ready (DRDY) and FIFO interrupt signal *INT2*
// NOTE: DRDY and FIFO interrupts must not be enabled at the same time
#ifdef FIFO_MODE
l3gd20h_enable_int (sensor, l3gd20h_int_fifo_overrun, true);
l3gd20h_enable_int (sensor, l3gd20h_int_fifo_threshold, true);
#else
l3gd20h_enable_int (sensor, l3gd20h_int_data_ready, true);
#endif
#endif // INT_DATA
#ifdef FIFO_MODE
// clear FIFO and activate FIFO mode if needed
l3gd20h_set_fifo_mode (sensor, l3gd20h_bypass, 0);
l3gd20h_set_fifo_mode (sensor, l3gd20h_stream, 10);
#endif
// select LPF/HPF, configure HPF and reset the reference by dummy read
l3gd20h_select_output_filter (sensor, l3gd20h_hpf_only);
l3gd20h_config_hpf (sensor, l3gd20h_hpf_normal, 0);
l3gd20h_get_hpf_ref (sensor);
// LAST STEP: Finally set scale and sensor mode to start measurements
l3gd20h_set_scale(sensor, l3gd20h_scale_245dps);
l3gd20h_set_mode (sensor, l3gd20h_normal_odr_12_5, 3, true, true, true);
// -- SENSOR CONFIGURATION PART ---
}
}

1000
extras/l3gd20h/README.md Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,10 @@
# Component makefile for extras/l3gd20h
# expected anyone using L3GD20H driver includes it as 'l3gd20h/l3gd20h.h'
INC_DIRS += $(l3gd20h_ROOT)..
INC_DIRS += $(l3gd20h_ROOT)
# args for passing into compile rule generation
l3gd20h_SRC_DIR = $(l3gd20h_ROOT)
$(eval $(call component_compile_rules,l3gd20h))

1066
extras/l3gd20h/l3gd20h.c Normal file

File diff suppressed because it is too large Load diff

407
extras/l3gd20h/l3gd20h.h Normal file
View file

@ -0,0 +1,407 @@
/**
* Driver for L3GD20H 3-axes digital output gyroscope connected to I2C or SPI.
* It can also be used with L3GD20 and L3G4200D.
*
* 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 __L3GD20H_H__
#define __L3GD20H_H__
// Uncomment one of the following defines to enable debug output
// #define L3GD20H_DEBUG_LEVEL_1 // only error messages
// #define L3GD20H_DEBUG_LEVEL_2 // debug and error messages
// L3GD20H addresses
#define L3GD20H_I2C_ADDRESS_1 0x6a // SDO pin is low
#define L3GD20H_I2C_ADDRESS_2 0x6b // SDO pin is high
// L3GD20 addresses
#define L3GD20_I2C_ADDRESS_1 0x6a // SDO pin is low
#define L3GD20_I2C_ADDRESS_2 0x6b // SDO pin is high
// L3G4200D addresses
#define L3G4200D_I2C_ADDRESS_1 0x68 // SDO pin is low
#define L3G4200D_I2C_ADDRESS_2 0x69 // SDO pin is high
// L3GD20H chip id
#define L3GD20H_CHIP_ID 0xd7 // L3GD20H_REG_WHO_AM_I<7:0>
// L3GD20 chip id
#define L3GD20_CHIP_ID 0xd4 // L3GD20H_REG_WHO_AM_I<7:0>
// L3G4200D chip id
#define L3G4200D_CHIP_ID 0xd3 // L3GD20H_REG_WHO_AM_I<7:0>
// Definition of error codes
#define L3GD20H_OK 0
#define L3GD20H_NOK -1
#define L3GD20H_INT_ERROR_MASK 0x000f
#define L3GD20H_DRV_ERROR_MASK 0xfff0
// Error codes for I2C and SPI interfaces ORed with L3GD20H driver error codes
#define L3GD20H_I2C_READ_FAILED 1
#define L3GD20H_I2C_WRITE_FAILED 2
#define L3GD20H_I2C_BUSY 3
#define L3GD20H_SPI_WRITE_FAILED 4
#define L3GD20H_SPI_READ_FAILED 5
#define L3GD20H_SPI_BUFFER_OVERFLOW 6
#define L3GD20H_SPI_SET_PAGE_FAILED 7
// L3GD20H driver error codes ORed with error codes for I2C and SPI interfaces
#define L3GD20H_WRONG_CHIP_ID ( 1 << 8)
#define L3GD20H_WRONG_BANDWIDTH ( 2 << 8)
#define L3GD20H_GET_RAW_DATA_FAILED ( 3 << 8)
#define L3GD20H_GET_RAW_DATA_FIFO_FAILED ( 4 << 8)
#define L3GD20H_WRONG_INT_TYPE ( 5 << 8)
#define L3GD20H_CONFIG_INT_SIGNALS_FAILED ( 6 << 8)
#define L3GD20H_CONFIG_INT1_FAILED ( 7 << 8)
#define L3GD20H_CONFIG_INT2_FAILED ( 8 << 8)
#define L3GD20H_INT1_SOURCE_FAILED ( 9 << 8)
#define L3GD20H_INT2_SOURCE_FAILED (10 << 8)
#define L3GD20H_SEL_OUT_FILTER_FAILED (11 << 8)
#define L3GD20H_CONFIG_HPF_FAILED (12 << 8)
#define L3GD20H_ENABLE_HPF_FAILED (13 << 8)
#define L3GD20H_SENSOR_IN_BYPASS_MODE (14 << 8)
#define L3GD20H_SENSOR_IN_FIFO_MODE (15 << 8)
#define LG3GD20H_ODR_TOO_HIGH (16 << 8)
#include "l3gd20h_platform.h"
#include "l3gd20h_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 L3GD20H sensor is connected
* @param addr I2C addr of the L3GD20H sensor, 0 for using SPI
* @param cs SPI CS GPIO, ignored for I2C
* @return pointer to sensor data structure, or NULL on error
*/
l3gd20h_sensor_t* l3gd20h_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 mode sensor mode with certain output data rate
* @param bw bandwidth
* @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 l3gd20h_set_mode (l3gd20h_sensor_t* dev, l3gd20h_mode_t mode, uint8_t bw,
bool x, bool y, bool z);
/**
* @brief Set scale (full range range)
*
* @param dev pointer to the sensor device data structure
* @param scale range setting
* @return true on success, false on error
*/
bool l3gd20h_set_scale (l3gd20h_sensor_t* dev, l3gd20h_scale_t sens);
/**
* @brief Set FIFO mode
*
* @param dev pointer to the sensor device data structure
* @param mode FIFO mode
* @param thresh FIFO watermark (ignored in bypass mode)
* @return true on success, false on error
*/
bool l3gd20h_set_fifo_mode (l3gd20h_sensor_t* dev,
l3gd20h_fifo_mode_t mode, uint8_t thresh);
/**
* @brief Filter selection for raw data output values
*
* High pass filter (HPF) is configured with function *l3gd20h_config_hpf*. If
* HPF is selected, it is enabled implicitly.
*
* @param dev pointer to the sensor device data structure
* @param filter selected filters for output values
* @return true on success, false on error
*/
bool l3gd20h_select_output_filter (l3gd20h_sensor_t* dev,
l3gd20h_filter_t filter);
/**
* @brief Test whether new sets of data are available
*
* @param dev pointer to the sensor device data structure
* @return true on new data, otherwise false
*/
bool l3gd20h_new_data (l3gd20h_sensor_t* dev);
/**
* @brief Get one sample of floating point sensor data (unit degree)
*
* Function works only in bypass mode and fails in FIFO modes. In FIFO modes,
* function *l3gd20h_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 values
* @return true on success, false on error
*/
bool l3gd20h_get_float_data (l3gd20h_sensor_t* dev,
l3gd20h_float_data_t* data);
/**
* @brief Get all samples of sensor data stored in the FIFO (unit dps)
*
* 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
* @return number of data sets read from fifo on success or 0 on error
*/
uint8_t l3gd20h_get_float_data_fifo (l3gd20h_sensor_t* dev,
l3gd20h_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 *l3gd20h_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 l3gd20h_get_raw_data (l3gd20h_sensor_t* dev,
l3gd20h_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 l3gd20h_get_raw_data_fifo (l3gd20h_sensor_t* dev,
l3gd20h_raw_data_fifo_t raw);
/**
* @brief Enable / disable data or event interrupts on signal INT1/INT2
*
* @param dev pointer to the sensor device data structure
* @param type type of the interrupt to be enabled/disabled
* @param value true to enable or false to disable the interrupt
* @return true on success, false on error
*/
bool l3gd20h_enable_int (l3gd20h_sensor_t* dev,
l3gd20h_int_types_t type, bool value);
/**
* @brief Set the configuration of the event interrupt generator
*
* The event interrupt generator produces interrupts (axis movement and wake up)
* on signal INT1 whenever the angular rate of one or more axes becomes higher
* or lower than defined thresholds.
*
* @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 l3gd20h_set_int_event_config (l3gd20h_sensor_t* dev,
l3gd20h_int_event_config_t* config);
/**
* @brief Get the configuration of the event interrupt generator
*
* The event interrupt generator produces interrupts (axis movement and wake up)
* on signal INT1 whenever the angular rate of one or more axes becomes higher
* or lower than defined thresholds.
*
* @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 l3gd20h_get_int_event_config (l3gd20h_sensor_t* dev,
l3gd20h_int_event_config_t* config);
/**
* @brief Get the source of an event interrupt (axis movement and wake up)
*
* @param dev pointer to the sensor device data structure
* @param type pointer to the interrupt source
* @return true on success, false on error
*/
bool l3gd20h_get_int_event_source (l3gd20h_sensor_t* dev,
l3gd20h_int_event_source_t* source);
/**
* @brief Get the source of a data interrupt (data ready or FIFO status)
*
* @param dev pointer to the sensor device data structure
* @param source pointer to the interrupt source
* @return true on success, false on error
*/
bool l3gd20h_get_int_data_source (l3gd20h_sensor_t* dev,
l3gd20h_int_data_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
* @param type define interrupt signal as pushed/pulled or open drain
* @return true on success, false on error
*/
bool l3gd20h_config_int_signals (l3gd20h_sensor_t* dev,
l3gd20h_signal_level_t level,
l3gd20h_signal_type_t type);
/**
* @brief Config HPF (high pass filter)
*
* @param dev pointer to the sensor device data structure
* @param mode high pass filter mode
* @param cutoff cutoff frequency (depends on output data rate) [0 ... 15]
* @return true on success, false on error
*/
bool l3gd20h_config_hpf (l3gd20h_sensor_t* dev,
l3gd20h_hpf_mode_t mode, uint8_t cutoff);
/**
* @brief Set HPF (high pass filter) reference
*
* Used to set the reference of HPF in reference mode *l3gd20h_hpf_reference*.
* Used to reset the HPF in autoreset mode *l3gd20h_hpf_autoreset*.
* Reference is given as two's complement.
*
* @param dev pointer to the sensor device data structure
* @param ref reference *l3gd20h_hpf_reference* mode, otherwise ignored
* @return true on success, false on error
*/
bool l3gd20h_set_hpf_ref (l3gd20h_sensor_t* dev, int8_t ref);
/**
* @brief Get HPF (high pass filter) reference
*
* Used to reset the HPF in normal mode *l3gd20h_hpf_normal*.
*
* @param dev pointer to the sensor device data structure
* @return HPF reference as two's complement
*/
int8_t l3gd20h_get_hpf_ref (l3gd20h_sensor_t* dev);
/**
* @brief Get temperature
*
* @param dev pointer to the sensor device data structure
* @return temperature in degree as two's complement
*/
int8_t l3gd20h_get_temperature (l3gd20h_sensor_t* dev);
// ---- 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 l3gd20h_reg_write (l3gd20h_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 l3gd20h_reg_read (l3gd20h_sensor_t* dev,
uint8_t reg, uint8_t *data, uint16_t len);
#ifdef __cplusplus
}
#endif /* End of CPP guard */
#endif /* __L3GD20H_H__ */

View file

@ -0,0 +1,113 @@
/**
* Driver for L3GD20H 3-axes digital output gyroscope connected to I2C or SPI.
* It can also be used with L3GD20 and L3G4200D.
*
* 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 __L3GD20H_PLATFORM_H__
#define __L3GD20H_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 definitions
#define spi_semaphore_init()
#define spi_semaphore_take()
#define spi_semaphore_give()
// platform specific SPI functions
#define spi_bus_init(bus,sck,miso,mosi) // not needed on ESP8266
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
};
inline static bool spi_device_init (uint8_t bus, uint8_t cs)
{
gpio_enable(cs, GPIO_OUTPUT);
gpio_write (cs, true);
return true;
}
inline static 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;
}
#endif // ESP_OPEN_RTOS
#endif // __L3GD20H_PLATFORM_H__

View file

@ -0,0 +1,297 @@
/**
* Driver for L3GD20H 3-axes digital output gyroscope connected to I2C or SPI.
* It can also be used with L3GD20 and L3G4200D.
*
* 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 __L3GD20H_TYPES_H__
#define __L3GD20H_TYPES_H__
#include "stdint.h"
#include "stdbool.h"
#ifdef __cplusplus
extern "C"
{
#endif
/**
* @brief Output data rates (ODR)
*/
typedef enum
{
l3gd20h_power_down = 0, // power down mode
l3gd20h_normal_odr_12_5, // normal mode with low output data rate 12.5 Hz
l3gd20h_normal_odr_25, // normal mode with low output data rate 25 Hz
l3gd20h_normal_odr_50, // normal mode with low output data rate 50 Hz
l3gd20h_normal_odr_100, // normal mode with high output data rate 100 Hz
l3gd20h_normal_odr_200, // normal mode with high output data rate 200 Hz
l3gd20h_normal_odr_400, // normal mode with high output data rate 400 Hz
l3gd20h_normal_odr_800, // normal mode with high output data rate 800 Hz
} l3gd20h_mode_t;
/**
* @brief Sensitivity level
*/
typedef enum
{
l3gd20h_scale_245dps = 0, // default
l3gd20h_scale_500dps,
l3gd20h_scale_2000dps
} l3gd20h_scale_t;
/**
* @brief FIFO mode
*/
typedef enum
{
l3gd20h_bypass = 0, // default
l3gd20h_fifo = 1,
l3gd20h_stream = 2,
l3gd20h_stream_to_fifo = 3,
l3gd20h_bypass_to_stream = 5,
l3gd20h_dynamic_stream = 6,
l3gd20h_bypass_to_fifo = 7
} l3gd20h_fifo_mode_t;
/**
* @brief High pass filter (HPF) and low pass filter 2 (LPF2) modes
*/
typedef enum
{
l3gd20h_no_filter = 0, // HPF not used, LPF2 not used
l3gd20h_hpf_only, // HPF used, LPF2 not used
l3gd20h_lpf2_only, // HPF not used, LPF2 used
l3gd20h_hpf_and_lpf2 // HPF used, LPF2 used
} l3gd20h_filter_t;
/**
* @brief Interrupt types
*/
typedef enum {
l3gd20h_int_data_ready, // data are ready to read (INT2)
l3gd20h_int_fifo_threshold, // FIFO filling exceds FTH level (INT2)
l3gd20h_int_fifo_overrun, // FIFO is completely filled (INT2)
l3gd20h_int_fifo_empty, // FIFO becomes empty (INT2)
l3gd20h_int_event // angular rate of one or more axes becomes
// lower or higher than threshold (INT1)
} l3gd20h_int_types_t;
/**
* @brief Event interrupt generator configuration (axis movement and wake up)
*
* memset to 0 to disable all interrupt conditions (default)
*/
typedef struct
{
bool x_low_enabled; // x lower than threshold interrupt enabled
bool x_high_enabled; // x higher than threshold interrupt enabled
uint16_t x_threshold; // x threshold value
bool y_low_enabled; // y lower than threshold interrupt enabled
bool y_high_enabled; // y higher than threshold interrupt enabled
uint16_t y_threshold; // y threshold value
bool z_low_enabled; // z lower than threshold interrupt enabled
bool z_high_enabled; // z higher than threshold interrupt enabled
uint16_t z_threshold; // z threshold value
l3gd20h_filter_t filter; // HPF and LPF2 mode used for threshold comparison
bool and_or; // interrupt combination true - AND, false - OR
// AND - all enabled axes passed the treshold
// OR - at least one axes passed the threshold
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
bool wait; // when true, duration is also used when interrupt
// condition in no longer given before interrupt
// signal is reset
bool counter_mode; // DCRM is not documented and not used therefore
} l3gd20h_int_event_config_t;
/**
* @brief Event interrupt source (axis movement and wake up)
*/
typedef struct {
bool x_low :1; // true - x is lower event occured
bool x_high:1; // true - x is higher event occured
bool y_low :1; // true - z is lower event occured
bool y_high:1; // true - z is higher event occured
bool z_low :1; // true - z is lower event occured
bool z_high:1; // true - z is higher event occured
bool active:1; // true - one ore more have been generated
} l3gd20h_int_event_source_t;
/**
* @brief Data interrupt source type (data ready and FIFO status)
*/
typedef struct {
bool data_ready; // true when data are ready to read
bool fifo_threshold; // true when FIFO filling >= FTH level
bool fifo_overrun; // true when FIFO is completely filled
bool fifo_empty; // true when FIFO is empty
} l3gd20h_int_data_source_t;
/**
* @brief INT1, INT2 signal activity level
*/
typedef enum {
l3gd20h_high_active = 0,
l3gd20h_low_active
} l3gd20h_signal_level_t;
/**
* @brief INT1, INT2 signal type
*/
typedef enum {
l3gd20h_push_pull = 0,
l3gd20h_open_drain
} l3gd20h_signal_type_t;
/**
* @brief Raw data set as two complements
*/
typedef struct {
int16_t x;
int16_t y;
int16_t z;
} l3gd20h_raw_data_t;
/**
* @brief Raw data FIFO type
*/
typedef l3gd20h_raw_data_t l3gd20h_raw_data_fifo_t[32];
/**
* @brief Floating point output value set in degree
*/
typedef struct {
float x;
float y;
float z;
} l3gd20h_float_data_t;
/**
* @brief Floating point output value FIFO type
*/
typedef l3gd20h_float_data_t l3gd20h_float_data_fifo_t[32];
/**
* @brief HPF (high pass filter) modes
*/
typedef enum {
l3gd20h_hpf_normal = 0,
l3gd20h_hpf_reference,
l3gd20h_hpf_normal_x,
l3gd20h_hpf_autoreset
} l3gd20h_hpf_mode_t;
/**
* @brief L3GD20H sensor device data structure type
*/
typedef struct {
int error_code; // contains the 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
l3gd20h_scale_t scale; // fill range scale (default 245 dps)
l3gd20h_fifo_mode_t fifo_mode; // FIFO operation mode (default bypass)
} l3gd20h_sensor_t;
#ifdef __cplusplus
}
#endif /* End of CPP guard */
#endif /* __L3GD20H_TYPES_H__ */