sensor/bmi160.c:provides work in character device mode and UORB communication mode.

Create bmi160_base.h, bmi160_base.c bmi160_uorb.c files, the bmi160_base file stores
public function interfaces, and the bmi160_uorb file stores functions related to the
uorb framework. Switch the character interface and UORB interface through the
macro CONFIG_SENSORS_BMI160_UORB.

Signed-off-by: likun17 <likun17@xiaomi.com>
This commit is contained in:
likun17
2023-09-04 08:17:44 +02:00
committed by Mateusz Szafoni
parent bc066af50a
commit ad643fe7a3
6 changed files with 1090 additions and 278 deletions
+1
View File
@@ -137,6 +137,7 @@ ifeq ($(CONFIG_SENSORS_BMG160),y)
endif
ifeq ($(CONFIG_SENSORS_BMI160),y)
CSRCS += bmi160_base.c
ifeq ($(CONFIG_SENSORS_BMI160_UORB),y)
CSRCS += bmi160_uorb.c
else
+1 -266
View File
@@ -22,7 +22,7 @@
* Included Files
****************************************************************************/
#include "bmi160.h"
#include "bmi160_base.h"
#if defined(CONFIG_SENSORS_BMI160)
@@ -229,271 +229,6 @@ static int bmi160_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: bmi160_checkid
*
* Description:
* Read and verify the BMI160 chip ID
*
****************************************************************************/
int bmi160_checkid(FAR struct bmi160_dev_s *priv)
{
uint8_t devid = 0;
/* Read device ID */
devid = bmi160_getreg8(priv, BMI160_CHIP_ID);
sninfo("devid: %04x\n", devid);
if (devid != (uint16_t) DEVID)
{
/* ID is not Correct */
return -ENODEV;
}
return OK;
}
/****************************************************************************
* Name: bmi160_getreg8
*
* Description:
* Read from an 8-bit BMI160 register
*
****************************************************************************/
uint8_t bmi160_getreg8(FAR struct bmi160_dev_s *priv, uint8_t regaddr)
{
uint8_t regval = 0;
#ifdef CONFIG_SENSORS_BMI160_I2C
struct i2c_msg_s msg[2];
int ret;
msg[0].frequency = priv->freq;
msg[0].addr = priv->addr;
msg[0].flags = I2C_M_NOSTOP;
msg[0].buffer = &regaddr;
msg[0].length = 1;
msg[1].frequency = priv->freq;
msg[1].addr = priv->addr;
msg[1].flags = I2C_M_READ;
msg[1].buffer = &regval;
msg[1].length = 1;
ret = I2C_TRANSFER(priv->i2c, msg, 2);
if (ret < 0)
{
snerr("I2C_TRANSFER failed: %d\n", ret);
}
#else /* CONFIG_SENSORS_BMI160_SPI */
/* If SPI bus is shared then lock and configure it */
SPI_LOCK(priv->spi, true);
bmi160_configspi(priv->spi);
/* Select the BMI160 */
SPI_SELECT(priv->spi, SPIDEV_ACCELEROMETER(0), true);
/* Send register to read and get the next byte */
SPI_SEND(priv->spi, regaddr | 0x80);
SPI_RECVBLOCK(priv->spi, &regval, 1);
/* Deselect the BMI160 */
SPI_SELECT(priv->spi, SPIDEV_ACCELEROMETER(0), false);
/* Unlock bus */
SPI_LOCK(priv->spi, false);
#endif
return regval;
}
/****************************************************************************
* Name: bmi160_putreg8
*
* Description:
* Write a value to an 8-bit BMI160 register
*
****************************************************************************/
void bmi160_putreg8(FAR struct bmi160_dev_s *priv, uint8_t regaddr,
uint8_t regval)
{
#ifdef CONFIG_SENSORS_BMI160_I2C
struct i2c_msg_s msg[2];
int ret;
uint8_t txbuffer[2];
txbuffer[0] = regaddr;
txbuffer[1] = regval;
msg[0].frequency = priv->freq;
msg[0].addr = priv->addr;
msg[0].flags = 0;
msg[0].buffer = txbuffer;
msg[0].length = 2;
ret = I2C_TRANSFER(priv->i2c, msg, 1);
if (ret < 0)
{
snerr("I2C_TRANSFER failed: %d\n", ret);
}
#else /* CONFIG_SENSORS_BMI160_SPI */
/* If SPI bus is shared then lock and configure it */
SPI_LOCK(priv->spi, true);
bmi160_configspi(priv->spi);
/* Select the BMI160 */
SPI_SELECT(priv->spi, SPIDEV_ACCELEROMETER(0), true);
/* Send register address and set the value */
SPI_SEND(priv->spi, regaddr);
SPI_SEND(priv->spi, regval);
/* Deselect the BMI160 */
SPI_SELECT(priv->spi, SPIDEV_ACCELEROMETER(0), false);
/* Unlock bus */
SPI_LOCK(priv->spi, false);
#endif
}
/****************************************************************************
* Name: bmi160_getreg16
*
* Description:
* Read 16-bits of data from an BMI160 register
*
****************************************************************************/
uint16_t bmi160_getreg16(FAR struct bmi160_dev_s *priv, uint8_t regaddr)
{
uint16_t regval = 0;
#ifdef CONFIG_SENSORS_BMI160_I2C
struct i2c_msg_s msg[2];
int ret;
msg[0].frequency = priv->freq;
msg[0].addr = priv->addr;
msg[0].flags = I2C_M_NOSTOP;
msg[0].buffer = &regaddr;
msg[0].length = 1;
msg[1].frequency = priv->freq;
msg[1].addr = priv->addr;
msg[1].flags = I2C_M_READ;
msg[1].buffer = (uint8_t *)&regval;
msg[1].length = 2;
ret = I2C_TRANSFER(priv->i2c, msg, 2);
if (ret < 0)
{
snerr("I2C_TRANSFER failed: %d\n", ret);
}
#else /* CONFIG_SENSORS_BMI160_SPI */
/* If SPI bus is shared then lock and configure it */
SPI_LOCK(priv->spi, true);
bmi160_configspi(priv->spi);
/* Select the BMI160 */
SPI_SELECT(priv->spi, SPIDEV_ACCELEROMETER(0), true);
/* Send register to read and get the next 2 bytes */
SPI_SEND(priv->spi, regaddr | 0x80);
SPI_RECVBLOCK(priv->spi, &regval, 2);
/* Deselect the BMI160 */
SPI_SELECT(priv->spi, SPIDEV_ACCELEROMETER(0), false);
/* Unlock bus */
SPI_LOCK(priv->spi, false);
#endif
return regval;
}
/****************************************************************************
* Name: bmi160_getregs
*
* Description:
* Read cnt bytes from specified dev_addr and reg_addr
*
****************************************************************************/
void bmi160_getregs(FAR struct bmi160_dev_s *priv, uint8_t regaddr,
uint8_t *regval, int len)
{
#ifdef CONFIG_SENSORS_BMI160_I2C
struct i2c_msg_s msg[2];
int ret;
msg[0].frequency = priv->freq;
msg[0].addr = priv->addr;
msg[0].flags = I2C_M_NOSTOP;
msg[0].buffer = &regaddr;
msg[0].length = 1;
msg[1].frequency = priv->freq;
msg[1].addr = priv->addr;
msg[1].flags = I2C_M_READ;
msg[1].buffer = regval;
msg[1].length = len;
ret = I2C_TRANSFER(priv->i2c, msg, 2);
if (ret < 0)
{
snerr("I2C_TRANSFER failed: %d\n", ret);
}
#else /* CONFIG_SENSORS_BMI160_SPI */
/* If SPI bus is shared then lock and configure it */
SPI_LOCK(priv->spi, true);
bmi160_configspi(priv->spi);
/* Select the BMI160 */
SPI_SELECT(priv->spi, SPIDEV_ACCELEROMETER(0), true);
/* Send register to read and get the next 2 bytes */
SPI_SEND(priv->spi, regaddr | 0x80);
SPI_RECVBLOCK(priv->spi, regval, len);
/* Deselect the BMI160 */
SPI_SELECT(priv->spi, SPIDEV_ACCELEROMETER(0), false);
/* Unlock bus */
SPI_LOCK(priv->spi, false);
#endif
}
/****************************************************************************
* Name: bmi160_register
*
+314
View File
@@ -0,0 +1,314 @@
/****************************************************************************
* drivers/sensors/bmi160_base.c
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include "bmi160_base.h"
#if defined(CONFIG_SENSORS_BMI160)
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Private Types
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: bmi160_getreg8
*
* Description:
* Read from an 8-bit BMI160 register
*
****************************************************************************/
uint8_t bmi160_getreg8(FAR struct bmi160_dev_s *priv, uint8_t regaddr)
{
uint8_t regval = 0;
#ifdef CONFIG_SENSORS_BMI160_I2C
struct i2c_msg_s msg[2];
int ret;
msg[0].frequency = priv->freq;
msg[0].addr = priv->addr;
msg[0].flags = I2C_M_NOSTOP;
msg[0].buffer = &regaddr;
msg[0].length = 1;
msg[1].frequency = priv->freq;
msg[1].addr = priv->addr;
msg[1].flags = I2C_M_READ;
msg[1].buffer = &regval;
msg[1].length = 1;
ret = I2C_TRANSFER(priv->i2c, msg, 2);
if (ret < 0)
{
snerr("I2C_TRANSFER failed: %d\n", ret);
}
#else /* CONFIG_SENSORS_BMI160_SPI */
/* If SPI bus is shared then lock and configure it */
SPI_LOCK(priv->spi, true);
bmi160_configspi(priv->spi);
/* Select the BMI160 */
SPI_SELECT(priv->spi, SPIDEV_ACCELEROMETER(0), true);
/* Send register to read and get the next byte */
SPI_SEND(priv->spi, regaddr | 0x80);
SPI_RECVBLOCK(priv->spi, &regval, 1);
/* Deselect the BMI160 */
SPI_SELECT(priv->spi, SPIDEV_ACCELEROMETER(0), false);
/* Unlock bus */
SPI_LOCK(priv->spi, false);
#endif
return regval;
}
/****************************************************************************
* Name: bmi160_putreg8
*
* Description:
* Write a value to an 8-bit BMI160 register
*
****************************************************************************/
void bmi160_putreg8(FAR struct bmi160_dev_s *priv, uint8_t regaddr,
uint8_t regval)
{
#ifdef CONFIG_SENSORS_BMI160_I2C
struct i2c_msg_s msg[2];
int ret;
uint8_t txbuffer[2];
txbuffer[0] = regaddr;
txbuffer[1] = regval;
msg[0].frequency = priv->freq;
msg[0].addr = priv->addr;
msg[0].flags = 0;
msg[0].buffer = txbuffer;
msg[0].length = 2;
ret = I2C_TRANSFER(priv->i2c, msg, 1);
if (ret < 0)
{
snerr("I2C_TRANSFER failed: %d\n", ret);
}
#else /* CONFIG_SENSORS_BMI160_SPI */
/* If SPI bus is shared then lock and configure it */
SPI_LOCK(priv->spi, true);
bmi160_configspi(priv->spi);
/* Select the BMI160 */
SPI_SELECT(priv->spi, SPIDEV_ACCELEROMETER(0), true);
/* Send register address and set the value */
SPI_SEND(priv->spi, regaddr);
SPI_SEND(priv->spi, regval);
/* Deselect the BMI160 */
SPI_SELECT(priv->spi, SPIDEV_ACCELEROMETER(0), false);
/* Unlock bus */
SPI_LOCK(priv->spi, false);
#endif
}
/****************************************************************************
* Name: bmi160_getreg16
*
* Description:
* Read 16-bits of data from an BMI160 register
*
****************************************************************************/
uint16_t bmi160_getreg16(FAR struct bmi160_dev_s *priv, uint8_t regaddr)
{
uint16_t regval = 0;
#ifdef CONFIG_SENSORS_BMI160_I2C
struct i2c_msg_s msg[2];
int ret;
msg[0].frequency = priv->freq;
msg[0].addr = priv->addr;
msg[0].flags = I2C_M_NOSTOP;
msg[0].buffer = &regaddr;
msg[0].length = 1;
msg[1].frequency = priv->freq;
msg[1].addr = priv->addr;
msg[1].flags = I2C_M_READ;
msg[1].buffer = (uint8_t *)&regval;
msg[1].length = 2;
ret = I2C_TRANSFER(priv->i2c, msg, 2);
if (ret < 0)
{
snerr("I2C_TRANSFER failed: %d\n", ret);
}
#else /* CONFIG_SENSORS_BMI160_SPI */
/* If SPI bus is shared then lock and configure it */
SPI_LOCK(priv->spi, true);
bmi160_configspi(priv->spi);
/* Select the BMI160 */
SPI_SELECT(priv->spi, SPIDEV_ACCELEROMETER(0), true);
/* Send register to read and get the next 2 bytes */
SPI_SEND(priv->spi, regaddr | 0x80);
SPI_RECVBLOCK(priv->spi, &regval, 2);
/* Deselect the BMI160 */
SPI_SELECT(priv->spi, SPIDEV_ACCELEROMETER(0), false);
/* Unlock bus */
SPI_LOCK(priv->spi, false);
#endif
return regval;
}
/****************************************************************************
* Name: bmi160_getregs
*
* Description:
* Read cnt bytes from specified dev_addr and reg_addr
*
****************************************************************************/
void bmi160_getregs(FAR struct bmi160_dev_s *priv, uint8_t regaddr,
uint8_t *regval, int len)
{
#ifdef CONFIG_SENSORS_BMI160_I2C
struct i2c_msg_s msg[2];
int ret;
msg[0].frequency = priv->freq;
msg[0].addr = priv->addr;
msg[0].flags = I2C_M_NOSTOP;
msg[0].buffer = &regaddr;
msg[0].length = 1;
msg[1].frequency = priv->freq;
msg[1].addr = priv->addr;
msg[1].flags = I2C_M_READ;
msg[1].buffer = regval;
msg[1].length = len;
ret = I2C_TRANSFER(priv->i2c, msg, 2);
if (ret < 0)
{
snerr("I2C_TRANSFER failed: %d\n", ret);
}
#else /* CONFIG_SENSORS_BMI160_SPI */
/* If SPI bus is shared then lock and configure it */
SPI_LOCK(priv->spi, true);
bmi160_configspi(priv->spi);
/* Select the BMI160 */
SPI_SELECT(priv->spi, SPIDEV_ACCELEROMETER(0), true);
/* Send register to read and get the next 2 bytes */
SPI_SEND(priv->spi, regaddr | 0x80);
SPI_RECVBLOCK(priv->spi, regval, len);
/* Deselect the BMI160 */
SPI_SELECT(priv->spi, SPIDEV_ACCELEROMETER(0), false);
/* Unlock bus */
SPI_LOCK(priv->spi, false);
#endif
}
/****************************************************************************
* Name: bmi160_checkid
*
* Description:
* Read and verify the BMI160 chip ID
*
****************************************************************************/
int bmi160_checkid(FAR struct bmi160_dev_s *priv)
{
uint8_t devid = 0;
/* Read device ID */
devid = bmi160_getreg8(priv, BMI160_CHIP_ID);
sninfo("devid: %04x\n", devid);
if (devid != (uint16_t) DEVID)
{
/* ID is not Correct */
return -ENODEV;
}
return OK;
}
#endif /* CONFIG_SENSORS_BMI160 */
@@ -1,5 +1,5 @@
/****************************************************************************
* include/nuttx/sensors/bmi160.h
* drivers/sensors/bmi160_base.h
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
@@ -27,18 +27,18 @@
#include <nuttx/config.h>
#include <stdlib.h>
#include <fixedmath.h>
#include <assert.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/kmalloc.h>
#include <nuttx/fs/fs.h>
#include <nuttx/kmalloc.h>
#include <nuttx/spi/spi.h>
#include <nuttx/i2c/i2c_master.h>
#include <nuttx/sensors/bmi160.h>
#include <stdlib.h>
#include <assert.h>
#include <errno.h>
#include <debug.h>
#include <fixedmath.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
@@ -211,11 +211,9 @@
#define MAG_PM_LOWPOWER (0x1A)
/****************************************************************************
* Private Types
* Public Types
****************************************************************************/
/* Device struct */
struct bmi160_dev_s
{
#ifdef CONFIG_SENSORS_BMI160_I2C
@@ -230,7 +228,7 @@ struct bmi160_dev_s
};
/****************************************************************************
* Private Functions
* Public Function Prototypes
****************************************************************************/
uint8_t bmi160_getreg8(FAR struct bmi160_dev_s *priv, uint8_t regaddr);
File diff suppressed because it is too large Load Diff
+8
View File
@@ -134,9 +134,17 @@ extern "C"
#ifndef CONFIG_SENSORS_BMI160_SCU
# ifdef CONFIG_SENSORS_BMI160_I2C
# ifdef CONFIG_SENSORS_BMI160_UORB
int bmi160_register_uorb(int devno, FAR struct i2c_master_s *dev);
# else
int bmi160_register(FAR const char *devpath, FAR struct i2c_master_s *dev);
# endif /* CONFIG_SENSORS_BMI160_UORB */
# else /* CONFIG_BMI160_SPI */
# ifdef CONFIG_SENSORS_BMI160_UORB
int bmi160_register_uorb(int devno, FAR struct spi_dev_s *dev);
# else
int bmi160_register(FAR const char *devpath, FAR struct spi_dev_s *dev);
# endif /* CONFIG_SENSORS_BMI160_UORB */
# endif
#else /* CONFIG_SENSORS_BMI160_SCU */