2014-12-16 15:16:53 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/sensors/adxl345_spi.c
|
|
|
|
*
|
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>
|
|
|
|
#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);
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_HWFEATURES(spi, 0);
|
|
|
|
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 */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_LOCK(priv->spi, true);
|
2014-12-16 15:16:53 +01:00
|
|
|
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 */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->spi, regaddr);
|
2014-12-16 15:16:53 +01:00
|
|
|
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 */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_LOCK(priv->spi, false);
|
2014-12-16 15:16:53 +01:00
|
|
|
|
|
|
|
#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 */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_LOCK(priv->spi, true);
|
2014-12-16 15:16:53 +01:00
|
|
|
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 */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->spi, regaddr);
|
|
|
|
SPI_SEND(priv->spi, regval);
|
2014-12-16 15:16:53 +01:00
|
|
|
|
|
|
|
/* 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
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_LOCK(priv->spi, false);
|
2014-12-16 15:16:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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 */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_LOCK(priv->spi, true);
|
2014-12-16 15:16:53 +01:00
|
|
|
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 */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->spi, regaddr);
|
2014-12-16 15:16:53 +01:00
|
|
|
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 */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_LOCK(priv->spi, false);
|
2014-12-16 15:16:53 +01:00
|
|
|
|
|
|
|
#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*/
|