2017-02-19 16:45:47 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/sensors/l3gd20.c
|
|
|
|
* Character driver for the ST L3GD20 3-Axis gyroscope.
|
|
|
|
*
|
2020-10-10 14:28:05 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2017-02-19 18:49:42 +01:00
|
|
|
*
|
2020-10-10 14:28:05 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2017-02-19 16:45:47 +01:00
|
|
|
*
|
2020-10-10 14:28:05 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2017-02-19 16:45:47 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2020-11-17 07:02:06 +01:00
|
|
|
#include <math.h>
|
2017-02-19 16:45:47 +01:00
|
|
|
|
2020-11-17 07:02:06 +01:00
|
|
|
#include <nuttx/nuttx.h>
|
2017-02-19 16:45:47 +01:00
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/wqueue.h>
|
2017-03-30 15:38:37 +02:00
|
|
|
#include <nuttx/random.h>
|
2017-02-19 16:45:47 +01:00
|
|
|
|
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
|
2020-11-17 07:02:06 +01:00
|
|
|
#include <nuttx/sensors/sensor.h>
|
2017-02-19 16:45:47 +01:00
|
|
|
#include <nuttx/sensors/l3gd20.h>
|
|
|
|
|
|
|
|
#if defined(CONFIG_SPI) && defined(CONFIG_SENSORS_L3GD20)
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct l3gd20_dev_s
|
|
|
|
{
|
|
|
|
FAR struct l3gd20_dev_s *flink; /* Supports a singly linked list of
|
|
|
|
* drivers */
|
|
|
|
FAR struct spi_dev_s *spi; /* Pointer to the SPI instance */
|
|
|
|
FAR struct l3gd20_config_s *config; /* Pointer to the configuration of the
|
|
|
|
* L3GD20 sensor */
|
2020-11-17 07:02:06 +01:00
|
|
|
uint64_t timestamp; /* Units is microseconds */
|
|
|
|
struct sensor_lowerhalf_s lower; /* The struct of lower half driver */
|
|
|
|
#if CONFIG_SENSORS_L3GD20_BUFFER_SIZE > 0
|
2017-02-19 16:45:47 +01:00
|
|
|
struct work_s work; /* The work queue is responsible for
|
|
|
|
* retrieving the data from the sensor
|
|
|
|
* after the arrival of new data was
|
|
|
|
* signalled in an interrupt */
|
2020-11-17 07:02:06 +01:00
|
|
|
#endif
|
2017-02-19 16:45:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-05-12 22:19:26 +02:00
|
|
|
#ifdef CONFIG_DEBUG_SENSORS_INFO
|
2017-02-19 16:45:47 +01:00
|
|
|
static void l3gd20_read_register(FAR struct l3gd20_dev_s *dev,
|
2017-02-19 18:49:42 +01:00
|
|
|
uint8_t const reg_addr, uint8_t *reg_data);
|
2020-05-12 22:19:26 +02:00
|
|
|
#endif
|
2017-02-19 16:45:47 +01:00
|
|
|
static void l3gd20_write_register(FAR struct l3gd20_dev_s *dev,
|
|
|
|
uint8_t const reg_addr,
|
|
|
|
uint8_t const reg_data);
|
|
|
|
static void l3gd20_reset(FAR struct l3gd20_dev_s *dev);
|
2020-11-17 07:02:06 +01:00
|
|
|
static void l3gd20_read_measurement_data(FAR struct l3gd20_dev_s *dev,
|
|
|
|
FAR struct sensor_event_gyro *data);
|
2017-02-19 16:45:47 +01:00
|
|
|
static void l3gd20_read_gyroscope_data(FAR struct l3gd20_dev_s *dev,
|
2017-02-19 18:49:42 +01:00
|
|
|
uint16_t *x_gyr, uint16_t *y_gyr,
|
|
|
|
uint16_t *z_gyr);
|
2017-02-19 16:45:47 +01:00
|
|
|
static void l3gd20_read_temperature(FAR struct l3gd20_dev_s *dev,
|
|
|
|
uint8_t * temperature);
|
2020-05-12 22:19:26 +02:00
|
|
|
static int l3gd20_interrupt_handler(int irq, FAR void *context,
|
|
|
|
FAR void *arg);
|
2020-11-17 07:02:06 +01:00
|
|
|
static int l3gd20_activate(FAR struct sensor_lowerhalf_s *lower,
|
|
|
|
bool enable);
|
|
|
|
#if CONFIG_SENSORS_L3GD20_BUFFER_SIZE > 0
|
2017-02-19 16:45:47 +01:00
|
|
|
static void l3gd20_worker(FAR void *arg);
|
2020-11-17 07:02:06 +01:00
|
|
|
#else
|
|
|
|
static int l3gd20_fetch(FAR struct sensor_lowerhalf_s *lower,
|
|
|
|
FAR char *buffer, size_t buflen);
|
|
|
|
#endif
|
2017-02-19 16:45:47 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-11-17 07:02:06 +01:00
|
|
|
/* The lower half sensor driver operations for sensor register */
|
|
|
|
|
|
|
|
static const struct sensor_ops_s g_l2gd20_ops =
|
2017-02-19 18:49:42 +01:00
|
|
|
{
|
2020-11-17 07:02:06 +01:00
|
|
|
.activate = l3gd20_activate,
|
|
|
|
.set_interval = NULL,
|
|
|
|
.batch = NULL,
|
|
|
|
#if CONFIG_SENSORS_L3GD20_BUFFER_SIZE > 0
|
|
|
|
.fetch = NULL,
|
|
|
|
#else
|
|
|
|
.fetch = l3gd20_fetch,
|
2017-02-19 16:45:47 +01:00
|
|
|
#endif
|
2020-11-17 07:02:06 +01:00
|
|
|
.control = NULL
|
2017-02-19 18:49:42 +01:00
|
|
|
};
|
2017-02-19 16:45:47 +01:00
|
|
|
|
|
|
|
/* Single linked list to store instances of drivers */
|
|
|
|
|
|
|
|
static struct l3gd20_dev_s *g_l3gd20_list = NULL;
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-05-12 22:19:26 +02:00
|
|
|
#ifdef CONFIG_DEBUG_SENSORS_INFO
|
2017-02-19 16:45:47 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: l3gd20_read_register
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void l3gd20_read_register(FAR struct l3gd20_dev_s *dev,
|
2017-02-19 18:49:42 +01:00
|
|
|
uint8_t const reg_addr, uint8_t *reg_data)
|
2017-02-19 16:45:47 +01:00
|
|
|
{
|
2020-05-13 19:31:27 +02:00
|
|
|
/* Lock the SPI bus so that only one device can access it at the
|
|
|
|
* same time
|
|
|
|
*/
|
2017-02-19 16:45:47 +01:00
|
|
|
|
|
|
|
SPI_LOCK(dev->spi, true);
|
|
|
|
|
|
|
|
/* Set CS to low which selects the L3GD20 */
|
|
|
|
|
|
|
|
SPI_SELECT(dev->spi, dev->config->spi_devid, true);
|
|
|
|
|
|
|
|
/* Transmit the register address from where we want to read - the MSB needs
|
|
|
|
* to be set to indicate the read indication.
|
|
|
|
*/
|
|
|
|
|
|
|
|
SPI_SEND(dev->spi, reg_addr | 0x80);
|
|
|
|
|
|
|
|
/* Write an idle byte while receiving the required data */
|
|
|
|
|
|
|
|
*reg_data = (uint8_t) (SPI_SEND(dev->spi, 0));
|
|
|
|
|
|
|
|
/* Set CS to high which deselects the L3GD20 */
|
|
|
|
|
|
|
|
SPI_SELECT(dev->spi, dev->config->spi_devid, false);
|
|
|
|
|
|
|
|
/* Unlock the SPI bus */
|
|
|
|
|
|
|
|
SPI_LOCK(dev->spi, false);
|
|
|
|
}
|
2020-05-12 22:19:26 +02:00
|
|
|
#endif
|
2017-02-19 16:45:47 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: l3gd20_write_register
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void l3gd20_write_register(FAR struct l3gd20_dev_s *dev,
|
|
|
|
uint8_t const reg_addr,
|
|
|
|
uint8_t const reg_data)
|
|
|
|
{
|
2020-05-13 19:31:27 +02:00
|
|
|
/* Lock the SPI bus so that only one device can access it at the same
|
|
|
|
* time
|
|
|
|
*/
|
2017-02-19 16:45:47 +01:00
|
|
|
|
|
|
|
SPI_LOCK(dev->spi, true);
|
|
|
|
|
|
|
|
/* Set CS to low which selects the L3GD20 */
|
|
|
|
|
|
|
|
SPI_SELECT(dev->spi, dev->config->spi_devid, true);
|
|
|
|
|
|
|
|
/* Transmit the register address from where we want to read */
|
|
|
|
|
|
|
|
SPI_SEND(dev->spi, reg_addr);
|
|
|
|
|
|
|
|
/* Transmit the content which should be written in the register */
|
|
|
|
|
|
|
|
SPI_SEND(dev->spi, reg_data);
|
|
|
|
|
|
|
|
/* Set CS to high which deselects the L3GD20 */
|
|
|
|
|
|
|
|
SPI_SELECT(dev->spi, dev->config->spi_devid, false);
|
|
|
|
|
|
|
|
/* Unlock the SPI bus */
|
|
|
|
|
|
|
|
SPI_LOCK(dev->spi, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: l3gd20_reset
|
|
|
|
****************************************************************************/
|
2017-02-19 18:49:42 +01:00
|
|
|
|
2017-02-19 16:45:47 +01:00
|
|
|
static void l3gd20_reset(FAR struct l3gd20_dev_s *dev)
|
|
|
|
{
|
|
|
|
/* Reboot memory content */
|
|
|
|
|
2020-05-13 19:31:27 +02:00
|
|
|
l3gd20_write_register(dev, L3GD20_CTRL_REG_5, L3GD20_CTRL_REG_5_BOOT_BM);
|
2017-02-19 16:45:47 +01:00
|
|
|
|
|
|
|
up_mdelay(100);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: l3gd20_read_measurement_data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-11-17 07:02:06 +01:00
|
|
|
static void l3gd20_read_measurement_data(FAR struct l3gd20_dev_s *dev,
|
|
|
|
FAR struct sensor_event_gyro *data)
|
2017-02-19 16:45:47 +01:00
|
|
|
{
|
2017-02-19 18:49:42 +01:00
|
|
|
uint16_t x_gyr = 0;
|
|
|
|
uint16_t y_gyr = 0;
|
|
|
|
uint16_t z_gyr = 0;
|
2017-02-19 16:45:47 +01:00
|
|
|
uint8_t temperature = 0;
|
|
|
|
|
|
|
|
/* Read Gyroscope */
|
|
|
|
|
|
|
|
l3gd20_read_gyroscope_data(dev, &x_gyr, &y_gyr, &z_gyr);
|
|
|
|
|
|
|
|
/* Read Temperature */
|
|
|
|
|
|
|
|
l3gd20_read_temperature(dev, &temperature);
|
|
|
|
|
2020-11-17 07:02:06 +01:00
|
|
|
data->x = ((int16_t)x_gyr / 180.0f) * (float)M_PI;
|
|
|
|
data->y = ((int16_t)y_gyr / 180.0f) * (float)M_PI;
|
|
|
|
data->z = ((int16_t)z_gyr / 180.0f) * (float)M_PI;
|
|
|
|
data->temperature = temperature;
|
|
|
|
data->timestamp = dev->timestamp;
|
2017-03-30 15:38:37 +02:00
|
|
|
|
|
|
|
/* Feed sensor data to entropy pool */
|
|
|
|
|
|
|
|
add_sensor_randomness((x_gyr << 16) ^ (y_gyr << 8) ^ (z_gyr << 0));
|
2017-02-19 16:45:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: l3gd20_read_gyroscope_data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void l3gd20_read_gyroscope_data(FAR struct l3gd20_dev_s *dev,
|
|
|
|
uint16_t * x_gyr, uint16_t * y_gyr,
|
|
|
|
uint16_t * z_gyr)
|
|
|
|
{
|
2020-05-13 19:31:27 +02:00
|
|
|
/* Lock the SPI bus so that only one device can access it at the same
|
|
|
|
* time
|
|
|
|
*/
|
2017-02-19 16:45:47 +01:00
|
|
|
|
|
|
|
SPI_LOCK(dev->spi, true);
|
|
|
|
|
|
|
|
/* Set CS to low which selects the L3GD20 */
|
|
|
|
|
|
|
|
SPI_SELECT(dev->spi, dev->config->spi_devid, true);
|
|
|
|
|
|
|
|
/* Transmit the register address from where we want to start reading
|
|
|
|
* 0x80 -> MSB is set -> Read Indication
|
|
|
|
* 0x40 -> MSB-1 (MS-Bit) is set -> auto increment of address when reading
|
|
|
|
* multiple bytes.
|
|
|
|
*/
|
|
|
|
|
|
|
|
SPI_SEND(dev->spi, (L3GD20_OUT_X_L_REG | 0x80 | 0x40)); /* RX */
|
|
|
|
|
|
|
|
*x_gyr = ((uint16_t) (SPI_SEND(dev->spi, 0)) << 0); /* LSB */
|
|
|
|
*x_gyr |= ((uint16_t) (SPI_SEND(dev->spi, 0)) << 8); /* MSB */
|
|
|
|
|
|
|
|
*y_gyr = ((uint16_t) (SPI_SEND(dev->spi, 0)) << 0); /* LSB */
|
|
|
|
*y_gyr |= ((uint16_t) (SPI_SEND(dev->spi, 0)) << 8); /* MSB */
|
|
|
|
|
|
|
|
*z_gyr = ((uint16_t) (SPI_SEND(dev->spi, 0)) << 0); /* LSB */
|
|
|
|
*z_gyr |= ((uint16_t) (SPI_SEND(dev->spi, 0)) << 8); /* MSB */
|
|
|
|
|
|
|
|
/* Set CS to high which deselects the L3GD20 */
|
|
|
|
|
|
|
|
SPI_SELECT(dev->spi, dev->config->spi_devid, false);
|
|
|
|
|
|
|
|
/* Unlock the SPI bus */
|
|
|
|
|
|
|
|
SPI_LOCK(dev->spi, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: l3gd20_read_temperature
|
|
|
|
****************************************************************************/
|
2017-02-19 18:49:42 +01:00
|
|
|
|
2019-12-07 02:07:34 +01:00
|
|
|
static void l3gd20_read_temperature(FAR struct l3gd20_dev_s *dev,
|
|
|
|
FAR uint8_t *temperature)
|
2017-02-19 16:45:47 +01:00
|
|
|
{
|
2020-05-13 19:31:27 +02:00
|
|
|
/* Lock the SPI bus so that only one device can access it at the same
|
|
|
|
* time
|
|
|
|
*/
|
2017-02-19 16:45:47 +01:00
|
|
|
|
|
|
|
SPI_LOCK(dev->spi, true);
|
|
|
|
|
|
|
|
/* Set CS to low which selects the L3GD20 */
|
|
|
|
|
|
|
|
SPI_SELECT(dev->spi, dev->config->spi_devid, true);
|
|
|
|
|
|
|
|
/* Transmit the register address from where we want to start reading
|
|
|
|
* 0x80 MSB is set -> Read Indication
|
|
|
|
*/
|
|
|
|
|
|
|
|
SPI_SEND(dev->spi, (L3GD20_OUT_TEMP_REG | 0x80));
|
|
|
|
|
|
|
|
/* RX */
|
|
|
|
|
|
|
|
*temperature = (SPI_SEND(dev->spi, 0));
|
|
|
|
|
|
|
|
/* Set CS to high which deselects the L3GD20 */
|
|
|
|
|
|
|
|
SPI_SELECT(dev->spi, dev->config->spi_devid, false);
|
|
|
|
|
|
|
|
/* Unlock the SPI bus */
|
|
|
|
|
|
|
|
SPI_LOCK(dev->spi, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: l3gd20_interrupt_handler
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-05-12 22:19:26 +02:00
|
|
|
static int l3gd20_interrupt_handler(int irq, FAR void *context,
|
|
|
|
FAR void *arg)
|
2017-02-19 16:45:47 +01:00
|
|
|
{
|
|
|
|
/* This function should be called upon a rising edge on the L3GD20 new data
|
|
|
|
* interrupt pin since it signals that new data has been measured.
|
|
|
|
*/
|
|
|
|
|
|
|
|
FAR struct l3gd20_dev_s *priv = 0;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Find out which L3GD20 device caused the interrupt */
|
|
|
|
|
|
|
|
for (priv = g_l3gd20_list;
|
|
|
|
priv && priv->config->irq != irq;
|
|
|
|
priv = priv->flink)
|
|
|
|
{
|
|
|
|
DEBUGASSERT(priv != NULL);
|
|
|
|
}
|
|
|
|
|
2020-11-17 07:02:06 +01:00
|
|
|
/* Get the timestamp */
|
|
|
|
|
|
|
|
priv->timestamp = sensor_get_timestamp();
|
|
|
|
|
|
|
|
#if CONFIG_SENSORS_L3GD20_BUFFER_SIZE > 0
|
2017-02-19 16:45:47 +01:00
|
|
|
/* Task the worker with retrieving the latest sensor data. We should not do
|
2020-05-13 19:31:27 +02:00
|
|
|
* this in a interrupt since it might take too long. Also we cannot lock
|
|
|
|
* the SPI bus from within an interrupt.
|
2017-02-19 16:45:47 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(priv->work.worker == NULL);
|
|
|
|
ret = work_queue(HPWORK, &priv->work, l3gd20_worker, priv, 0);
|
2020-11-17 07:02:06 +01:00
|
|
|
|
2017-02-19 16:45:47 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
snerr("ERROR: Failed to queue work: %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
2020-11-17 07:02:06 +01:00
|
|
|
#else
|
2017-02-19 16:45:47 +01:00
|
|
|
|
2020-11-17 07:02:06 +01:00
|
|
|
/* notify event to upper half driver */
|
|
|
|
|
|
|
|
priv->lower.notify_event(priv->lower.priv);
|
|
|
|
|
|
|
|
#endif
|
2017-02-19 16:45:47 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2020-11-17 07:02:06 +01:00
|
|
|
#if CONFIG_SENSORS_L3GD20_BUFFER_SIZE > 0
|
2017-02-19 16:45:47 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: l3gd20_worker
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void l3gd20_worker(FAR void *arg)
|
|
|
|
{
|
2020-11-17 07:02:06 +01:00
|
|
|
struct sensor_event_gyro temp;
|
|
|
|
|
2017-02-19 16:45:47 +01:00
|
|
|
FAR struct l3gd20_dev_s *priv = (FAR struct l3gd20_dev_s *)(arg);
|
|
|
|
DEBUGASSERT(priv != NULL);
|
|
|
|
|
|
|
|
/* Read out the latest sensor data */
|
|
|
|
|
2020-11-17 07:02:06 +01:00
|
|
|
l3gd20_read_measurement_data(priv, &temp);
|
2017-02-19 16:45:47 +01:00
|
|
|
|
2020-11-17 07:02:06 +01:00
|
|
|
/* push data to upper half driver */
|
2017-02-19 16:45:47 +01:00
|
|
|
|
2020-11-17 07:02:06 +01:00
|
|
|
priv->lower.push_event(priv->lower.priv, &temp,
|
|
|
|
sizeof(struct sensor_event_gyro));
|
2017-02-19 16:45:47 +01:00
|
|
|
}
|
|
|
|
|
2020-11-17 07:02:06 +01:00
|
|
|
#else
|
|
|
|
|
2017-02-19 16:45:47 +01:00
|
|
|
/****************************************************************************
|
2020-11-17 07:02:06 +01:00
|
|
|
* Name: l3gd20_fetch
|
2017-02-19 16:45:47 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2020-11-17 07:02:06 +01:00
|
|
|
static int l3gd20_fetch(FAR struct sensor_lowerhalf_s *lower,
|
|
|
|
FAR char *buffer, size_t buflen)
|
2017-02-19 16:45:47 +01:00
|
|
|
{
|
2020-11-17 07:02:06 +01:00
|
|
|
FAR struct l3gd20_dev_s *priv = container_of(lower,
|
|
|
|
FAR struct l3gd20_dev_s,
|
|
|
|
lower);
|
|
|
|
|
|
|
|
if (buflen != sizeof(struct sensor_event_gyro))
|
|
|
|
return 0;
|
2017-02-19 16:45:47 +01:00
|
|
|
|
|
|
|
DEBUGASSERT(priv != NULL);
|
|
|
|
|
2020-11-17 07:02:06 +01:00
|
|
|
/* Read out the latest sensor data */
|
2017-02-19 16:45:47 +01:00
|
|
|
|
2020-11-17 07:02:06 +01:00
|
|
|
l3gd20_read_measurement_data(priv, (FAR struct sensor_event_gyro *)buffer);
|
|
|
|
|
|
|
|
return sizeof(struct sensor_event_gyro);
|
2017-02-19 16:45:47 +01:00
|
|
|
}
|
2020-11-17 07:02:06 +01:00
|
|
|
#endif
|
2017-02-19 16:45:47 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
2020-11-17 07:02:06 +01:00
|
|
|
* Name: l3gd20_activate
|
2017-02-19 16:45:47 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2020-11-17 07:02:06 +01:00
|
|
|
static int l3gd20_activate(FAR struct sensor_lowerhalf_s *lower,
|
|
|
|
bool enable)
|
2017-02-19 16:45:47 +01:00
|
|
|
{
|
2020-11-17 07:02:06 +01:00
|
|
|
FAR struct l3gd20_dev_s *priv = container_of(lower,
|
|
|
|
FAR struct l3gd20_dev_s,
|
|
|
|
lower);
|
|
|
|
struct sensor_event_gyro temp;
|
2017-02-19 16:45:47 +01:00
|
|
|
|
2020-11-17 07:02:06 +01:00
|
|
|
#ifdef CONFIG_DEBUG_SENSORS_INFO
|
|
|
|
uint8_t reg_content;
|
|
|
|
uint8_t reg_addr;
|
|
|
|
#endif
|
2017-02-19 16:45:47 +01:00
|
|
|
|
2020-11-17 07:02:06 +01:00
|
|
|
DEBUGASSERT(priv != NULL);
|
2017-02-19 16:45:47 +01:00
|
|
|
|
2020-11-17 07:02:06 +01:00
|
|
|
if (enable == true)
|
2017-02-19 16:45:47 +01:00
|
|
|
{
|
2020-11-17 07:02:06 +01:00
|
|
|
/* Perform a reset */
|
2017-02-19 16:45:47 +01:00
|
|
|
|
2020-11-17 07:02:06 +01:00
|
|
|
l3gd20_reset(priv);
|
2017-02-19 16:45:47 +01:00
|
|
|
|
2020-11-17 07:02:06 +01:00
|
|
|
/* Enable DRDY signal on INT 2 */
|
2017-02-19 16:45:47 +01:00
|
|
|
|
2020-11-17 07:02:06 +01:00
|
|
|
l3gd20_write_register(priv,
|
|
|
|
L3GD20_CTRL_REG_3,
|
|
|
|
L3GD20_CTRL_REG_3_I2_DRDY_BM);
|
2017-02-19 16:45:47 +01:00
|
|
|
|
2020-11-17 07:02:06 +01:00
|
|
|
/* Enable the maximum full scale mode.
|
|
|
|
* Enable block data update for gyro sensor data.
|
|
|
|
* This should prevent race conditions when reading sensor data.
|
|
|
|
*/
|
2017-02-19 16:45:47 +01:00
|
|
|
|
2020-11-17 07:02:06 +01:00
|
|
|
l3gd20_write_register(priv,
|
|
|
|
L3GD20_CTRL_REG_4,
|
|
|
|
L3GD20_CTRL_REG_4_BDU_BM |
|
|
|
|
L3GD20_CTRL_REG_4_FS_1_BM |
|
|
|
|
L3GD20_CTRL_REG_4_FS_0_BM);
|
2017-02-19 16:45:47 +01:00
|
|
|
|
2020-11-17 07:02:06 +01:00
|
|
|
/* Enable X,Y,Z axis
|
|
|
|
* DR=00 -> Output data rate = 95 Hz, Cut-off = 12.5
|
|
|
|
*/
|
2017-02-19 16:45:47 +01:00
|
|
|
|
2020-11-17 07:02:06 +01:00
|
|
|
l3gd20_write_register(priv,
|
|
|
|
L3GD20_CTRL_REG_1,
|
|
|
|
L3GD20_CTRL_REG_1_POWERDOWN_BM |
|
|
|
|
L3GD20_CTRL_REG_1_X_EN_BM |
|
|
|
|
L3GD20_CTRL_REG_1_Y_EN_BM |
|
|
|
|
L3GD20_CTRL_REG_1_Z_EN_BM);
|
2017-02-19 16:45:47 +01:00
|
|
|
|
2020-11-17 07:02:06 +01:00
|
|
|
/* Read measurement data to ensure DRDY is low */
|
2017-02-19 16:45:47 +01:00
|
|
|
|
2020-11-17 07:02:06 +01:00
|
|
|
l3gd20_read_measurement_data(priv, &temp);
|
2017-02-19 16:45:47 +01:00
|
|
|
|
2020-11-17 07:02:06 +01:00
|
|
|
/* Read back the content of all control registers for debug purposes */
|
2017-02-19 16:45:47 +01:00
|
|
|
|
2020-11-17 07:02:06 +01:00
|
|
|
#ifdef CONFIG_DEBUG_SENSORS_INFO
|
|
|
|
reg_content = 0;
|
2017-02-19 16:45:47 +01:00
|
|
|
|
2020-11-17 07:02:06 +01:00
|
|
|
l3gd20_read_register(priv, L3GD20_WHO_AM_I, ®_content);
|
|
|
|
sninfo("WHO_AM_I_REG = %04x\n", reg_content);
|
2017-02-19 16:45:47 +01:00
|
|
|
|
2020-11-17 07:02:06 +01:00
|
|
|
for (reg_addr = L3GD20_CTRL_REG_1;
|
|
|
|
reg_addr <= L3GD20_CTRL_REG_5;
|
|
|
|
reg_addr++)
|
|
|
|
{
|
|
|
|
l3gd20_read_register(priv, reg_addr, ®_content);
|
|
|
|
sninfo("R#%04x = %04x\n", reg_addr, reg_content);
|
|
|
|
}
|
2017-02-19 16:45:47 +01:00
|
|
|
|
2020-11-17 07:02:06 +01:00
|
|
|
l3gd20_read_register(priv, L3GD20_STATUS_REG, ®_content);
|
|
|
|
sninfo("STATUS_REG = %04x\n", reg_content);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Perform a reset */
|
2017-02-19 16:45:47 +01:00
|
|
|
|
2020-11-17 07:02:06 +01:00
|
|
|
l3gd20_reset(priv);
|
2017-02-19 16:45:47 +01:00
|
|
|
}
|
|
|
|
|
2020-11-17 07:02:06 +01:00
|
|
|
return 0;
|
2017-02-19 16:45:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: l3gd20_register
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Register the L3DF20 character device as 'devpath'.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2020-11-17 07:02:06 +01:00
|
|
|
* devno - The device number, used to build the device path
|
|
|
|
* as /dev/sensor/gyro_uncalN
|
2017-02-19 16:45:47 +01:00
|
|
|
* spi - An SPI driver instance.
|
|
|
|
* config - configuration for the L3GD20 driver.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) on success; a negated errno value on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-11-17 07:02:06 +01:00
|
|
|
int l3gd20_register(int devno, FAR struct spi_dev_s *spi,
|
2017-02-19 16:45:47 +01:00
|
|
|
FAR struct l3gd20_config_s *config)
|
|
|
|
{
|
|
|
|
FAR struct l3gd20_dev_s *priv;
|
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
|
|
|
|
DEBUGASSERT(spi != NULL);
|
|
|
|
DEBUGASSERT(config != NULL);
|
|
|
|
|
|
|
|
/* Initialize the L3GD20 device structure */
|
|
|
|
|
|
|
|
priv = (FAR struct l3gd20_dev_s *)kmm_malloc(sizeof(struct l3gd20_dev_s));
|
|
|
|
if (priv == NULL)
|
|
|
|
{
|
|
|
|
snerr("ERROR: Failed to allocate instance\n");
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2017-02-19 18:49:42 +01:00
|
|
|
priv->spi = spi;
|
|
|
|
priv->config = config;
|
2020-11-17 07:02:06 +01:00
|
|
|
#if CONFIG_SENSORS_L3GD20_BUFFER_SIZE > 0
|
2017-02-19 18:49:42 +01:00
|
|
|
priv->work.worker = NULL;
|
2020-11-17 07:02:06 +01:00
|
|
|
#endif
|
|
|
|
priv->timestamp = 0;
|
2017-02-19 16:45:47 +01:00
|
|
|
|
2020-11-17 07:02:06 +01:00
|
|
|
priv->lower.type = SENSOR_TYPE_GYROSCOPE;
|
2021-02-08 09:26:25 +01:00
|
|
|
priv->lower.buffer_number = CONFIG_SENSORS_L3GD20_BUFFER_SIZE;
|
2020-11-17 07:02:06 +01:00
|
|
|
priv->lower.ops = &g_l2gd20_ops;
|
|
|
|
priv->lower.uncalibrated = true;
|
2017-02-19 16:45:47 +01:00
|
|
|
|
|
|
|
/* Setup SPI frequency and mode */
|
|
|
|
|
|
|
|
SPI_SETFREQUENCY(spi, L3GD20_SPI_FREQUENCY);
|
|
|
|
SPI_SETMODE(spi, L3GD20_SPI_MODE);
|
|
|
|
|
|
|
|
/* Attach the interrupt handler */
|
|
|
|
|
|
|
|
ret = priv->config->attach(priv->config, &l3gd20_interrupt_handler);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
snerr("ERROR: Failed to attach interrupt\n");
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2020-11-17 07:02:06 +01:00
|
|
|
/* Register the sensor driver */
|
2017-02-19 16:45:47 +01:00
|
|
|
|
2020-11-17 07:02:06 +01:00
|
|
|
ret = sensor_register(&priv->lower, devno);
|
2017-02-19 16:45:47 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
snerr("ERROR: Failed to register driver: %d\n", ret);
|
|
|
|
kmm_free(priv);
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Since we support multiple L3GD20 devices, we will need to add this new
|
|
|
|
* instance to a list of device instances so that it can be found by the
|
2019-12-07 02:07:34 +01:00
|
|
|
* interrupt handler based on the received IRQ number.
|
|
|
|
*/
|
2017-02-19 16:45:47 +01:00
|
|
|
|
2017-02-19 18:49:42 +01:00
|
|
|
priv->flink = g_l3gd20_list;
|
2017-02-19 16:45:47 +01:00
|
|
|
g_l3gd20_list = priv;
|
|
|
|
|
2017-02-19 18:49:42 +01:00
|
|
|
errout:
|
2017-02-19 16:45:47 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_SPI && CONFIG_SENSORS_L3GD20 */
|