2014-12-16 15:16:53 +01:00
|
|
|
/****************************************************************************
|
2021-03-08 22:39:04 +01:00
|
|
|
* drivers/sensors/adxl345_base.c
|
2014-12-16 15:16:53 +01:00
|
|
|
*
|
2021-03-13 10:55:07 +01: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
|
2014-12-16 15:16:53 +01:00
|
|
|
*
|
2021-03-13 10:55:07 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-12-16 15:16:53 +01:00
|
|
|
*
|
2021-03-13 10:55:07 +01: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.
|
2014-12-16 15:16:53 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2014-12-16 15:16:53 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
2017-10-06 18:15:01 +02:00
|
|
|
#include <nuttx/signal.h>
|
2017-03-30 15:38:37 +02:00
|
|
|
#include <nuttx/random.h>
|
2017-10-06 18:15:01 +02:00
|
|
|
#include <nuttx/sensors/adxl345.h>
|
2014-12-16 15:16:53 +01:00
|
|
|
|
|
|
|
#include "adxl345.h"
|
|
|
|
|
|
|
|
#if defined(CONFIG_SENSORS_ADXL345)
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Character driver methods */
|
|
|
|
|
|
|
|
static ssize_t adxl345_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t len);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* This the vtable that supports the character driver interface */
|
|
|
|
|
|
|
|
static const struct file_operations g_adxl345fops =
|
|
|
|
{
|
2022-04-12 12:16:13 +02:00
|
|
|
NULL, /* open */
|
|
|
|
NULL, /* close */
|
2014-12-16 15:16:53 +01:00
|
|
|
adxl345_read, /* read */
|
2022-01-10 15:51:17 +01:00
|
|
|
NULL, /* write */
|
|
|
|
NULL, /* seek */
|
|
|
|
NULL, /* ioctl */
|
|
|
|
NULL /* poll */
|
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
, NULL /* unlink */
|
|
|
|
#endif
|
2014-12-16 15:16:53 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: adxl345_read
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Standard character driver read method.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-03-13 11:55:37 +01:00
|
|
|
static ssize_t adxl345_read(FAR struct file *filep,
|
|
|
|
FAR char *buffer, size_t len)
|
2014-12-16 15:16:53 +01:00
|
|
|
{
|
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct adxl345_dev_s *priv;
|
|
|
|
struct adxl345_sample_s sample;
|
|
|
|
int ret;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
sninfo("len=%d\n", len);
|
2014-12-16 15:16:53 +01:00
|
|
|
DEBUGASSERT(filep);
|
|
|
|
inode = filep->f_inode;
|
|
|
|
|
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
|
|
|
priv = (FAR struct adxl345_dev_s *)inode->i_private;
|
|
|
|
|
|
|
|
/* Verify that the caller has provided a buffer large enough to receive
|
|
|
|
* the accelerometer data.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (len < sizeof(struct adxl345_sample_s))
|
|
|
|
{
|
2018-08-02 19:59:59 +02:00
|
|
|
/* We could provide logic to break up a sample into segments and
|
2014-12-16 15:16:53 +01:00
|
|
|
* handle smaller reads... but why?
|
|
|
|
*/
|
|
|
|
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get exclusive access to the driver data structure */
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(&priv->exclsem);
|
2014-12-16 15:16:53 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
return ret;
|
2014-12-16 15:16:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Read accelerometer X Y Z axes */
|
|
|
|
|
|
|
|
sample.data_x = adxl345_getreg8(priv, ADXL345_DATAX1);
|
2021-03-13 11:55:37 +01:00
|
|
|
sample.data_x = (sample.data_x << 8) |
|
|
|
|
adxl345_getreg8(priv, ADXL345_DATAX0);
|
2014-12-16 15:16:53 +01:00
|
|
|
sample.data_y = adxl345_getreg8(priv, ADXL345_DATAY1);
|
2021-03-13 11:55:37 +01:00
|
|
|
sample.data_y = (sample.data_y << 8) |
|
|
|
|
adxl345_getreg8(priv, ADXL345_DATAY0);
|
2014-12-16 15:16:53 +01:00
|
|
|
sample.data_z = adxl345_getreg8(priv, ADXL345_DATAZ1);
|
2021-03-13 11:55:37 +01:00
|
|
|
sample.data_z = (sample.data_z << 8) |
|
|
|
|
adxl345_getreg8(priv, ADXL345_DATAZ0);
|
2014-12-16 15:16:53 +01:00
|
|
|
|
2017-03-30 15:38:37 +02:00
|
|
|
add_sensor_randomness(sample.data_x);
|
|
|
|
add_sensor_randomness((sample.data_z << 16) | sample.data_y);
|
|
|
|
|
2014-12-16 15:16:53 +01:00
|
|
|
/* Return read sample */
|
|
|
|
|
|
|
|
buffer = (FAR char *) &sample;
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&priv->exclsem);
|
2014-12-16 15:16:53 +01:00
|
|
|
return sizeof(struct adxl345_sample_s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: adxl345_register
|
|
|
|
*
|
|
|
|
* Description:
|
2021-03-13 11:55:37 +01:00
|
|
|
* This function will register the accelerometer driver as /dev/accelN where
|
|
|
|
* N is the minor device number
|
2014-12-16 15:16:53 +01:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* handle - The handle previously returned by adxl345_register
|
|
|
|
* minor - The input device minor number
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero is returned on success. Otherwise, a negated errno value is
|
|
|
|
* returned to indicate the nature of the failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int adxl345_register(ADXL345_HANDLE handle, int minor)
|
|
|
|
{
|
|
|
|
FAR struct adxl345_dev_s *priv = (FAR struct adxl345_dev_s *)handle;
|
|
|
|
char devname[DEV_NAMELEN];
|
|
|
|
int ret;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
sninfo("handle=%p minor=%d\n", handle, minor);
|
2014-12-16 15:16:53 +01:00
|
|
|
DEBUGASSERT(priv);
|
|
|
|
|
|
|
|
/* Get exclusive access to the device structure */
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(&priv->exclsem);
|
2014-12-16 15:16:53 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
snerr("ERROR: nxsem_wait failed: %d\n", ret);
|
|
|
|
return ret;
|
2014-12-16 15:16:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the structure fields to their default values */
|
|
|
|
|
|
|
|
priv->ofsx = 0;
|
|
|
|
priv->ofsy = 0;
|
|
|
|
priv->ofsz = 0;
|
|
|
|
|
|
|
|
/* Register the character driver */
|
|
|
|
|
2022-04-12 12:16:13 +02:00
|
|
|
snprintf(devname, sizeof(devname), DEV_FORMAT, minor);
|
2022-01-26 13:52:09 +01:00
|
|
|
ret = register_driver(devname, &g_adxl345fops, 0444, priv);
|
2014-12-16 15:16:53 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
snerr("ERROR: Failed to register driver %s: %d\n", devname, ret);
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&priv->exclsem);
|
2014-12-16 15:16:53 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Indicate that the accelerometer was successfully initialized */
|
|
|
|
|
|
|
|
priv->status |= ADXL345_STAT_INITIALIZED; /* Accelerometer is initialized */
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&priv->exclsem);
|
2014-12-16 15:16:53 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: adxl345_worker
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This is the "bottom half" of the ADXL345 interrupt handler
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void adxl345_worker(FAR void *arg)
|
|
|
|
{
|
|
|
|
FAR struct adxl345_dev_s *priv = (FAR struct adxl345_dev_s *)arg;
|
|
|
|
uint8_t regval;
|
|
|
|
|
|
|
|
DEBUGASSERT(priv && priv->config);
|
|
|
|
|
|
|
|
/* Get the global interrupt status */
|
|
|
|
|
|
|
|
regval = adxl345_getreg8(priv, ADXL345_INT_SOURCE);
|
|
|
|
|
|
|
|
/* Check for a data ready interrupt */
|
|
|
|
|
|
|
|
if ((regval & INT_DATA_READY) != 0)
|
|
|
|
{
|
|
|
|
/* Read accelerometer data to sample */
|
|
|
|
|
2019-12-07 02:07:34 +01:00
|
|
|
priv->sample.data_x = adxl345_getreg8(priv, ADXL345_DATAX1);
|
|
|
|
priv->sample.data_x = (priv->sample.data_x << 8) |
|
|
|
|
adxl345_getreg8(priv, ADXL345_DATAX0);
|
|
|
|
priv->sample.data_y = adxl345_getreg8(priv, ADXL345_DATAY1);
|
|
|
|
priv->sample.data_y = (priv->sample.data_y << 8) |
|
|
|
|
adxl345_getreg8(priv, ADXL345_DATAY0);
|
|
|
|
priv->sample.data_z = adxl345_getreg8(priv, ADXL345_DATAZ1);
|
|
|
|
priv->sample.data_z = (priv->sample.data_z << 8) |
|
|
|
|
adxl345_getreg8(priv, ADXL345_DATAZ0);
|
2014-12-16 15:16:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Re-enable the ADXL345 GPIO interrupt */
|
|
|
|
|
|
|
|
priv->config->enable(priv->config, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: adxl345_interrupt
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The ADXL345 interrupt handler
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-03-13 11:55:37 +01:00
|
|
|
static void adxl345_interrupt(FAR struct adxl345_config_s *config,
|
|
|
|
FAR void *arg)
|
2014-12-16 15:16:53 +01:00
|
|
|
{
|
2014-12-16 18:45:28 +01:00
|
|
|
FAR struct adxl345_dev_s *priv = (FAR struct adxl345_dev_s *)arg;
|
|
|
|
int ret;
|
2014-12-16 15:16:53 +01:00
|
|
|
|
2014-12-16 18:45:28 +01:00
|
|
|
DEBUGASSERT(priv && priv->config == config);
|
2014-12-16 15:16:53 +01:00
|
|
|
|
|
|
|
/* Disable further interrupts */
|
|
|
|
|
|
|
|
config->enable(config, false);
|
|
|
|
|
|
|
|
/* Check if interrupt work is already queue. If it is already busy, then
|
|
|
|
* we already have interrupt processing in the pipeline and we need to do
|
|
|
|
* nothing more.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (work_available(&priv->work))
|
|
|
|
{
|
|
|
|
/* Yes.. Transfer processing to the worker thread. Since ADXL345
|
|
|
|
* interrupts are disabled while the work is pending, no special
|
|
|
|
* action should be required to protect the work queue.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = work_queue(HPWORK, &priv->work, adxl345_worker, priv, 0);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2016-06-20 20:44:38 +02:00
|
|
|
snerr("ERROR: Failed to queue work: %d\n", ret);
|
2014-12-16 15:16:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear any pending interrupts and return success */
|
|
|
|
|
|
|
|
config->clear(config);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: adxl345_checkid
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read and verify the ADXL345 chip ID
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int adxl345_checkid(FAR struct adxl345_dev_s *priv)
|
|
|
|
{
|
|
|
|
uint8_t devid = 0;
|
|
|
|
|
|
|
|
/* Read device ID */
|
|
|
|
|
|
|
|
devid = adxl345_getreg8(priv, ADXL345_DEVID);
|
2016-06-11 19:59:51 +02:00
|
|
|
sninfo("devid: %04x\n", devid);
|
2014-12-16 15:16:53 +01:00
|
|
|
|
|
|
|
if (devid != (uint16_t) DEVID)
|
|
|
|
{
|
|
|
|
/* ID is not Correct */
|
|
|
|
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: adxl345_reset
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Reset the ADXL345
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void adxl345_reset(FAR struct adxl345_dev_s *priv)
|
|
|
|
{
|
|
|
|
/* ADXL345 doesn't have software reset */
|
|
|
|
|
|
|
|
/* Wait a bit to make the GOD of TIME happy */
|
|
|
|
|
2019-12-07 02:07:34 +01:00
|
|
|
nxsig_usleep(20 * 1000);
|
2014-12-16 15:16:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: adxl345_instantiate
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Instantiate and configure the ADXL345 device driver to use the provided
|
|
|
|
* I2C or SPIdevice instance.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dev - An I2C or SPI driver instance
|
|
|
|
* config - Persistent board configuration data
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* A non-zero handle is returned on success. This handle may then be used
|
|
|
|
* to configure the ADXL345 driver as necessary. A NULL handle value is
|
|
|
|
* returned on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_ADXL345_SPI
|
|
|
|
ADXL345_HANDLE adxl345_instantiate(FAR struct spi_dev_s *dev,
|
2014-12-16 18:45:28 +01:00
|
|
|
FAR struct adxl345_config_s *config)
|
2014-12-16 15:16:53 +01:00
|
|
|
#else
|
2016-01-30 15:36:47 +01:00
|
|
|
ADXL345_HANDLE adxl345_instantiate(FAR struct i2c_master_s *dev,
|
2014-12-16 18:45:28 +01:00
|
|
|
FAR struct adxl345_config_s *config)
|
2014-12-16 15:16:53 +01:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
FAR struct adxl345_dev_s *priv;
|
|
|
|
uint8_t regval;
|
|
|
|
int ret;
|
|
|
|
|
2014-12-16 18:45:28 +01:00
|
|
|
/* Allocate the ADXL345 driver instance */
|
2014-12-16 15:16:53 +01:00
|
|
|
|
2021-03-13 11:55:37 +01:00
|
|
|
priv = (FAR struct adxl345_dev_s *)
|
|
|
|
kmm_zalloc(sizeof(struct adxl345_dev_s));
|
2014-12-16 18:45:28 +01:00
|
|
|
if (!priv)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: Failed to allocate the device structure!\n");
|
2014-12-16 18:45:28 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2014-12-16 15:16:53 +01:00
|
|
|
|
|
|
|
/* Initialize the device state structure */
|
|
|
|
|
2017-10-03 20:51:15 +02:00
|
|
|
nxsem_init(&priv->exclsem, 0, 1);
|
2014-12-16 15:16:53 +01:00
|
|
|
priv->config = config;
|
|
|
|
|
|
|
|
#ifdef CONFIG_ADXL345_SPI
|
|
|
|
priv->spi = dev;
|
|
|
|
|
|
|
|
#else
|
|
|
|
priv->i2c = dev;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Read and verify the ADXL345 device ID */
|
|
|
|
|
|
|
|
ret = adxl345_checkid(priv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: Wrong Device ID!\n");
|
2014-12-17 00:27:09 +01:00
|
|
|
kmm_free(priv);
|
2014-12-16 15:16:53 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Generate ADXL345 Software reset */
|
|
|
|
|
|
|
|
adxl345_reset(priv);
|
|
|
|
|
2021-03-13 11:55:37 +01:00
|
|
|
/* Configure the interrupt output pin to generate interrupts on high or low
|
|
|
|
* level.
|
|
|
|
*/
|
2014-12-16 15:16:53 +01:00
|
|
|
|
|
|
|
regval = adxl345_getreg8(priv, ADXL345_DATA_FORMAT);
|
|
|
|
#ifdef CONFIG_ADXL345_ACTIVELOW
|
|
|
|
regval |= DATA_FMT_INT_INVERT; /* Pin polarity: Active low / falling edge */
|
|
|
|
#else
|
|
|
|
regval &= ~DATA_FMT_INT_INVERT; /* Pin polarity: Active high / rising edge */
|
|
|
|
#endif
|
|
|
|
adxl345_putreg8(priv, ADXL345_DATA_FORMAT, regval);
|
|
|
|
|
|
|
|
/* Attach the ADXL345 interrupt handler. */
|
|
|
|
|
2021-03-13 11:55:37 +01:00
|
|
|
config->attach(config,
|
|
|
|
(adxl345_handler_t)adxl345_interrupt,
|
|
|
|
(FAR void *)priv);
|
2014-12-16 15:16:53 +01:00
|
|
|
|
|
|
|
/* Leave standby mode */
|
|
|
|
|
|
|
|
adxl345_putreg8(priv, ADXL345_POWER_CTL, POWER_CTL_MEASURE);
|
|
|
|
|
|
|
|
config->clear(config);
|
|
|
|
config->enable(config, true);
|
|
|
|
|
|
|
|
/* Enable interrupts */
|
|
|
|
|
|
|
|
adxl345_putreg8(priv, ADXL345_INT_ENABLE, INT_DATA_READY);
|
|
|
|
|
|
|
|
/* Return our private data structure as an opaque handle */
|
2014-12-16 18:45:28 +01:00
|
|
|
|
2014-12-16 15:16:53 +01:00
|
|
|
return (ADXL345_HANDLE)priv;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_SENSORS_ADXL345 */
|