2014-12-16 15:16:53 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/sensors/adxl345_spi.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014 Alan Carvalho de Assis. All rights reserved.
|
|
|
|
* Author: Alan Carvalho de Assis <acassis@gmail.com>
|
|
|
|
* Based on STME811 driver
|
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* 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 NuttX 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 OWNER 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
2015-08-10 20:59:11 +02:00
|
|
|
#include <nuttx/spi/spi.h>
|
2014-12-16 15:16:53 +01:00
|
|
|
#include <nuttx/sensors/adxl345.h>
|
|
|
|
|
|
|
|
#include "adxl345.h"
|
|
|
|
|
|
|
|
#if defined(CONFIG_SENSORS_ADXL345) && defined(CONFIG_ADXL345_SPI)
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: adxl345_configspi
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline void adxl345_configspi(FAR struct spi_dev_s *spi)
|
|
|
|
{
|
|
|
|
/* Configure SPI for the ADXL345 */
|
|
|
|
|
|
|
|
SPI_SETMODE(spi, SPIDEV_MODE3);
|
|
|
|
SPI_SETBITS(spi, 8);
|
2016-01-23 23:18:13 +01:00
|
|
|
(void)SPI_HWFEATURES(spi, 0);
|
|
|
|
(void)SPI_SETFREQUENCY(spi, ADXL345_SPI_MAXFREQUENCY);
|
2014-12-16 15:16:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: adxl345_getreg8
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read from an 8-bit ADXL345 register
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
uint8_t adxl345_getreg8(FAR struct adxl345_dev_s *priv, uint8_t regaddr)
|
|
|
|
{
|
|
|
|
uint8_t regval;
|
|
|
|
|
|
|
|
/* If SPI bus is shared then lock and configure it */
|
|
|
|
|
|
|
|
(void)SPI_LOCK(priv->spi, true);
|
|
|
|
adxl345_configspi(priv->spi);
|
|
|
|
|
|
|
|
/* Select the ADXL345 */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_ACCELEROMETER(0), true);
|
2015-10-04 23:28:54 +02:00
|
|
|
|
2014-12-16 15:16:53 +01:00
|
|
|
/* Send register to read and get the next byte */
|
|
|
|
|
|
|
|
(void)SPI_SEND(priv->spi, regaddr);
|
|
|
|
SPI_RECVBLOCK(priv->spi, ®val, 1);
|
|
|
|
|
|
|
|
/* Deselect the ADXL345 */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_ACCELEROMETER(0), false);
|
2014-12-16 15:16:53 +01:00
|
|
|
|
|
|
|
/* Unlock bus */
|
|
|
|
|
|
|
|
(void)SPI_LOCK(priv->spi, false);
|
|
|
|
|
|
|
|
#ifdef CONFIG_ADXL345_REGDEBUG
|
2016-06-16 20:33:32 +02:00
|
|
|
_err("%02x->%02x\n", regaddr, regval);
|
2014-12-16 15:16:53 +01:00
|
|
|
#endif
|
|
|
|
return regval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: adxl345_putreg8
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Write a value to an 8-bit ADXL345 register
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void adxl345_putreg8(FAR struct adxl345_dev_s *priv, uint8_t regaddr,
|
|
|
|
uint8_t regval)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_ADXL345_REGDEBUG
|
2016-06-16 20:33:32 +02:00
|
|
|
_err("%02x<-%02x\n", regaddr, regval);
|
2014-12-16 15:16:53 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* If SPI bus is shared then lock and configure it */
|
|
|
|
|
|
|
|
(void)SPI_LOCK(priv->spi, true);
|
|
|
|
adxl345_configspi(priv->spi);
|
|
|
|
|
|
|
|
/* Select the ADXL345 */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_ACCELEROMETER(0), true);
|
2015-10-04 23:28:54 +02:00
|
|
|
|
2014-12-16 15:16:53 +01:00
|
|
|
/* Send register address and set the value */
|
|
|
|
|
|
|
|
(void)SPI_SEND(priv->spi, regaddr);
|
|
|
|
(void)SPI_SEND(priv->spi, regval);
|
|
|
|
|
|
|
|
/* Deselect the ADXL345 */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_ACCELEROMETER(0), false);
|
2014-12-16 15:16:53 +01:00
|
|
|
|
|
|
|
/* Unlock bus */
|
2016-01-24 01:54:36 +01:00
|
|
|
|
2014-12-16 15:16:53 +01:00
|
|
|
(void)SPI_LOCK(priv->spi, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: adxl345_getreg16
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read 16-bits of data from an ADXL345 register
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
uint16_t adxl345_getreg16(FAR struct adxl345_dev_s *priv, uint8_t regaddr)
|
|
|
|
{
|
|
|
|
uint16_t regval;
|
|
|
|
|
|
|
|
/* If SPI bus is shared then lock and configure it */
|
|
|
|
|
|
|
|
(void)SPI_LOCK(priv->spi, true);
|
|
|
|
adxl345_configspi(priv->spi);
|
|
|
|
|
|
|
|
/* Select the ADXL345 */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_ACCELEROMETER(0), true);
|
2015-10-04 23:28:54 +02:00
|
|
|
|
2014-12-16 15:16:53 +01:00
|
|
|
/* Send register to read and get the next 2 bytes */
|
|
|
|
|
|
|
|
(void)SPI_SEND(priv->spi, regaddr);
|
|
|
|
SPI_RECVBLOCK(priv->spi, ®val, 2);
|
|
|
|
|
|
|
|
/* Deselect the ADXL345 */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_ACCELEROMETER(0), false);
|
2014-12-16 15:16:53 +01:00
|
|
|
|
|
|
|
/* Unlock bus */
|
|
|
|
|
|
|
|
(void)SPI_LOCK(priv->spi, false);
|
|
|
|
|
|
|
|
#ifdef CONFIG_ADXL345_REGDEBUG
|
2016-06-16 20:33:32 +02:00
|
|
|
_err("%02x->%04x\n", regaddr, regval);
|
2014-12-16 15:16:53 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return regval;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_SENSORS_ADXL345 && CONFIG_ADXL345_SPI*/
|