1
0
Fork 0
forked from nuttx/nuttx-update

sensors/bmi270: add support for the new sensor framework

bmi270 can be used with the new sensor framework
This commit is contained in:
raiden00pl 2024-02-27 19:05:03 +01:00 committed by Xiang Xiao
parent 3d765616ea
commit 5af805b2ef
8 changed files with 2135 additions and 1272 deletions

View file

@ -65,7 +65,12 @@ if(CONFIG_SENSORS)
endif()
if(CONFIG_SENSORS_BMI270)
list(APPEND SRCS bmi270.c)
list(APPEND SRCS bmi270_base.c)
if(CONFIG_SENSORS_BMI270_UORB)
list(APPEND SRCS bmi270_uorb.c)
else()
list(APPEND SRCS bmi270.c)
endif()
endif()
# These drivers depend on I2C support

View file

@ -223,6 +223,35 @@ config SENSORS_BMI270
if SENSORS_BMI270
config SENSORS_BMI270_UORB
bool "Bosch BMI270 UORB"
default n
if SENSORS_BMI270_UORB
config SENSORS_BMI270_POLL
bool "Enables polling sensor data"
default n
---help---
Enables polling of sensor.
config SENSORS_BMI270_POLL_INTERVAL
int "Polling interval in microseconds, default 1 sec"
depends on SENSORS_BMI270_POLL
default 1000000
range 0 4294967295
---help---
The interval until a new sensor measurement will be triggered.
config SENSORS_BMI270_THREAD_STACKSIZE
int "Worker thread stack size"
depends on SENSORS_BMI270_POLL
default 1024
---help---
The stack size for the worker thread.
endif #SENSORS_BMI270_UORB
choice
prompt "BMI270 Interface"
default SENSORS_BMI270_SPI

View file

@ -65,8 +65,13 @@ ifeq ($(CONFIG_SENSORS_DHTXX),y)
endif
ifeq ($(CONFIG_SENSORS_BMI270),y)
CSRCS += bmi270_base.c
ifeq ($(CONFIG_SENSORS_BMI270_UORB),y)
CSRCS += bmi270_bmi.c
else
CSRCS += bmi270.c
endif
endif
# These drivers depend on I2C support

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,260 @@
/****************************************************************************
* drivers/sensors/bmi270_base.h
*
* 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.
*
****************************************************************************/
#ifndef __INCLUDE_NUTTX_SENSORS_BMI270_BASE_H
#define __INCLUDE_NUTTX_SENSORS_BMI270_BASE_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdint.h>
#include <stdlib.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/kmalloc.h>
#include <nuttx/fs/fs.h>
#include <nuttx/sensors/bmi270.h>
#ifdef CONFIG_SENSORS_BMI270_I2C
# include <nuttx/i2c/i2c_master.h>
#else
# include <nuttx/spi/spi.h>
#endif
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define BMI270_SPI_MAXFREQUENCY 10000000
#define BMI270_I2C_FREQ 400000
#define DEVID 0x24
#define BMI270_CHIP_ID (0x00) /* Chip ID */
#define BMI270_ERROR (0x02) /* Error register */
#define BMI270_PMU_STAT (0x03) /* Current power mode */
#define BMI270_DATA_0 (0x04) /* MAG X 7:0 (LSB) */
#define BMI270_DATA_1 (0x05) /* MAG X 15:8 (MSB) */
#define BMI270_DATA_2 (0x06) /* MAG Y 7:0 (LSB) */
#define BMI270_DATA_3 (0x07) /* MAG Y 15:8 (MSB) */
#define BMI270_DATA_4 (0x08) /* MAG Z 7:0 (LSB) */
#define BMI270_DATA_5 (0x09) /* MAG Z 15:8 (MSB) */
#define BMI270_DATA_6 (0x0a) /* RHALL 7:0 (LSB) */
#define BMI270_DATA_7 (0x0b) /* RHALL 15:8 (MSB) */
#define BMI270_DATA_8 (0x0c) /* ACC X 7:0 (LSB) */
#define BMI270_DATA_9 (0x0d) /* ACC X 15:8 (MSB) */
#define BMI270_DATA_10 (0x0e) /* ACC Y 7:0 (LSB) */
#define BMI270_DATA_11 (0x0f) /* ACC Y 15:8 (MSB) */
#define BMI270_DATA_12 (0x10) /* ACC Z 7:0 (LSB) */
#define BMI270_DATA_13 (0x11) /* ACC Z 15:8 (MSB) */
#define BMI270_DATA_14 (0x12) /* GYR X 7:0 (LSB) */
#define BMI270_DATA_15 (0x13) /* GYR X 15:8 (MSB) */
#define BMI270_DATA_16 (0x14) /* GYR Y 7:0 (LSB) */
#define BMI270_DATA_17 (0x15) /* GYR Y 15:8 (MSB) */
#define BMI270_DATA_18 (0x16) /* GYR Z 7:0 (LSB) */
#define BMI270_DATA_19 (0x17) /* GYR Z 15:8 (MSB) */
#define BMI270_SENSORTIME_0 (0x18) /* Sensor time 0 */
#define BMI270_SENSORTIME_1 (0x19) /* Sensor time 1 */
#define BMI270_SENSORTIME_2 (0x1A) /* Sensor time 2 */
#define BMI270_EVENT (0x1B) /* Sensor event flags */
#define BMI270_INTR_STAT_0 (0x1C) /* Interrupt status */
#define BMI270_INTR_STAT_1 (0x1D)
#define BMI270_SC_OUT_0 (0x1E) /* Step counting value */
#define BMI270_SC_OUT_1 (0x1f) /* Step counting value */
#define BMI270_WR_GEST_ACT (0x20) /* Wrist gesture and activity detection */
#define BMI270_INTERNAL_STAT (0x21) /* Internal status */
#define BMI270_TEMPERATURE_0 (0x22) /* Temperature */
#define BMI270_TEMPERATURE_1 (0x23)
#define BMI270_FIFO_LENGTH_0 (0x24) /* FIFO length */
#define BMI270_FIFO_LENGTH_1 (0x25)
#define BMI270_FIFO_DATA (0x26)
#define BMI270_FEAT_PAGE (0x2f) /* Page number for feature configuration and output registers */
/* TODO: Features 0x30-0x3f */
#define BMI270_ACC_CONFIG (0x40) /* ACCEL config for ODR, bandwidth and undersampling */
#define BMI270_ACC_RANGE (0x41) /* ACCEL range */
#define BMI270_GYR_CONFIG (0x42) /* GYRO config for ODR and bandwidth */
#define BMI270_GYR_RANGE (0x43) /* GYRO range */
#define BMI270_AUX_CONFIG (0x44) /* AUX config for ODR */
#define BMI270_FIFO_DOWN (0x45) /* GYRO and ACCEL downsampling rates for FIFO */
#define BMI270_FIFO_WTM_0 (0x46) /* FIFO Watermark level */
#define BMI270_FIFO_WTM_1 (0x47) /* FIFO Watermark level */
#define BMI270_FIFO_CONFIG_0 (0x48) /* FIFO config */
#define BMI270_FIFO_CONFIG_1 (0x49)
#define BMI270_SATURATION (0x4A) /* Saturation */
#define BMI270_AUX_DEV_ID (0x4B) /* Auxiliary interface device_id */
#define BMI270_AUX_IF_CONF (0x4C) /* Auxiliary interface configuration */
#define BMI270_AUX_RD_ADDR (0x4C) /* Auxiliary interface read address */
#define BMI270_AUX_WR_ADDR (0x4E) /* Auxiliary interface write address */
#define BMI270_AUX_WR_DATA (0x4F) /* Auxiliary interface write data */
#define BMI270_ERR_REG_MSK (0x52)
#define BMI270_INT1_IO_CTRL (0x53) /* INT pin configuration */
#define BMI270_INT2_IO_CTRL (0x54)
#define BMI270_INT_LATCH (0x55) /* Configure interrupt modes */
#define BMI270_INT1_MAP_FEAT (0x56) /* Interrupt/Feature mapping on INT1 */
#define BMI270_INT2_MAP_FEAT (0x57) /* Interrupt/Feature mapping on INT2 */
#define BMI270_INT_MAP_DATA (0x58) /* Data Interrupt mapping for both INT pins */
#define BMI270_INIT_CTRL (0x59) /* Start initialization */
#define BMI270_INIT_ADDR_0 (0x5b) /* Base address of the initialization data */
#define BMI270_INIT_ADDR_1 (0x5c)
#define BMI270_INIT_DATA (0x5e) /* Initialization register */
#define BMI270_INTERNAL_ERROR (0x5f)
#define BMI270_AUX_IF_TRIM (0x68) /* Auxiliary interface trim */
#define BMI270_GYR_CRT_CONF (0x69) /* Component Retrimming for Gyroscope */
#define BMI270_NMV_CONFIG (0x6A) /* NVM Configuration */
#define BMI270_IF_CONFIG (0x6B) /* Serial interface configuration */
#define BMI270_DRV (0x6C) /* Drive strength control */
#define BMI270_ACC_SELF_TEST (0x6D) /* Acc self test */
#define BMI270_GYR_SELF_TEST (0x6E) /* Gyro self test */
#define BMI270_NV_CONFIG (0x70) /* SPI/I2C selection */
#define BMI270_OFFSET_0 (0x71) /* ACCEL and GYRO offset */
#define BMI270_OFFSET_1 (0x72)
#define BMI270_OFFSET_2 (0x73)
#define BMI270_OFFSET_3 (0x74)
#define BMI270_OFFSET_4 (0x75)
#define BMI270_OFFSET_5 (0x76)
#define BMI270_OFFSET_6 (0x77)
#define BMI270_PWR_CONF (0x7C) /* Power mode configuration */
#define BMI270_PWR_CTRL (0x7D) /* Power mode control */
#define BMI270_CMD (0x7e) /* Command register */
/* Register 0x21 - INTERNAL_STATUS */
#define INTSTAT_MSG_MASK (7)
#define INTSTAT_MSG_NOTINIT (0x00)
#define INTSTAT_MSG_INITOK (0x01)
/* Register 0x40 - ACCEL_CONFIG accel bandwidth */
#define ACCEL_OSR4_AVG1 (0 << 4)
#define ACCEL_OSR2_AVG2 (1 << 4)
#define ACCEL_NORMAL_AVG4 (2 << 4)
#define ACCEL_CIC_AVG8 (3 << 4)
#define ACCEL_RES_AVG2 (4 << 4)
#define ACCEL_RES_AVG4 (5 << 4)
#define ACCEL_RES_AVG8 (6 << 4)
#define ACCEL_RES_AVG16 (7 << 4)
#define ACCEL_RES_AVG32 (8 << 4)
#define ACCEL_RES_AVG64 (9 << 4)
#define ACCEL_RES_AVG128 (10 << 4)
#define ACCEL_ODR_0_78HZ (0x01)
#define ACCEL_ODR_1_56HZ (0x02)
#define ACCEL_ODR_3_12HZ (0x03)
#define ACCEL_ODR_6_25HZ (0x04)
#define ACCEL_ODR_12_5HZ (0x05)
#define ACCEL_ODR_25HZ (0x06)
#define ACCEL_ODR_50HZ (0x07)
#define ACCEL_ODR_100HZ (0x08)
#define ACCEL_ODR_200HZ (0x09)
#define ACCEL_ODR_400HZ (0x0A)
#define ACCEL_ODR_800HZ (0x0B)
#define ACCEL_ODR_1600HZ (0x0C)
/* Register 0x41 - ACC_RANGE accel range */
#define ACCEL_RANGE_2G (0x00)
#define ACCEL_RANGE_4G (0x01)
#define ACCEL_RANGE_8G (0x02)
#define ACCEL_RANGE_16G (0x03)
/* Register 0x42 - GYRO_CONFIG accel bandwidth */
#define GYRO_OSR4_MODE (0 << 4)
#define GYRO_OSR2_MODE (1 << 4)
#define GYRO_NORMAL_MODE (2 << 4)
#define GYRO_CIC_MODE (3 << 4)
#define GYRO_ODR_25HZ (0x06)
#define GYRO_ODR_50HZ (0x07)
#define GYRO_ODR_100HZ (0x08)
#define GYRO_ODR_200HZ (0x09)
#define GYRO_ODR_400HZ (0x0A)
#define GYRO_ODR_800HZ (0x0B)
#define GYRO_ODR_1600HZ (0x0C)
#define GYRO_ODR_3200HZ (0x0D)
/* Register 0x43 - GYR_RANGE gyr range */
#define GYRO_RANGE_2000 (0x00)
#define GYRO_RANGE_1000 (0x01)
#define GYRO_RANGE_500 (0x02)
#define GYRO_RANGE_250 (0x03)
#define GYRO_RANGE_125 (0x04)
/* Register 0x7d - PWR_CONF */
#define PWRCONF_APS_ON (1 << 0)
#define PWRCONF_FSW_ON (1 << 1)
#define PWRCONF_FUP_ON (1 << 2)
/* Register 0x7d - PWR_CTRL */
#define PWRCTRL_AUX_EN (1 << 0)
#define PWRCTRL_GYR_EN (1 << 1)
#define PWRCTRL_ACC_EN (1 << 2)
#define PWRCTRL_TEMP_EN (1 << 3)
/* Register 0x7e - CMD */
#define CMD_SOFTRESET (0xB6)
/****************************************************************************
* Public Types
****************************************************************************/
struct bmi270_dev_s
{
#ifdef CONFIG_SENSORS_BMI270_I2C
FAR struct i2c_master_s *i2c; /* I2C interface */
uint8_t addr; /* I2C address */
int freq; /* Frequency <= 3.4MHz */
#else /* CONFIG_SENSORS_BMI270_SPI */
FAR struct spi_dev_s *spi; /* SPI interface */
#endif
};
/****************************************************************************
* Public Data
****************************************************************************/
extern const uint8_t g_bmi270_config_file[];
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
uint8_t bmi270_getreg8(FAR struct bmi270_dev_s *priv, uint8_t regaddr);
void bmi270_putreg8(FAR struct bmi270_dev_s *priv, uint8_t regaddr,
uint8_t regval);
void bmi270_getregs(FAR struct bmi270_dev_s *priv, uint8_t regaddr,
FAR uint8_t *regval, int len);
void bmi270_putregs(FAR struct bmi270_dev_s *priv, uint8_t regaddr,
FAR uint8_t *regval, int len);
void bmi270_set_normal_imu(FAR struct bmi270_dev_s *priv);
int bmi270_init_seq(FAR struct bmi270_dev_s *priv);
int bmi270_checkid(FAR struct bmi270_dev_s *priv);
#endif /* __INCLUDE_NUTTX_SENSORS_BMI270_BASE_H */

View file

@ -0,0 +1,706 @@
/****************************************************************************
* drivers/sensors/bmi270_uorb.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 <nuttx/config.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <fcntl.h>
#include <inttypes.h>
#include <sys/param.h>
#include <nuttx/mutex.h>
#include <nuttx/signal.h>
#include <nuttx/compiler.h>
#include <nuttx/nuttx.h>
#include <nuttx/kthread.h>
#include <nuttx/sensors/sensor.h>
#include <nuttx/sensors/ioctl.h>
#include "bmi270_base.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define CONSTANTS_ONE_G 9.8f
/****************************************************************************
* Private Types
****************************************************************************/
enum bmi270_idx_e
{
BMI270_ACCEL_IDX = 0,
BMI270_GYRO_IDX,
BMI270_MAX_IDX
};
struct bmi270_sensor_s
{
struct sensor_lowerhalf_s lower;
uint64_t last_update;
float scale;
FAR void *dev;
bool enabled;
#ifdef CONFIG_SENSORS_BMI270_POLL
unsigned long interval;
#endif
struct bmi270_dev_s base;
};
struct bmi270_sensor_dev_s
{
struct bmi270_sensor_s priv[BMI270_MAX_IDX];
mutex_t lock;
#ifdef CONFIG_SENSORS_BMI270_POLL
sem_t run;
#endif
};
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/* Sensor methods */
static int bmi270_activate(FAR struct sensor_lowerhalf_s *lower,
FAR struct file *filep,
bool enable);
static int bmi270_set_interval(FAR struct sensor_lowerhalf_s *lower,
FAR struct file *filep,
FAR unsigned long *period_us);
#ifndef CONFIG_SENSORS_BMI270_POLL
static int bmi270_fetch(FAR struct sensor_lowerhalf_s *lower,
FAR struct file *filep,
FAR char *buffer, size_t buflen);
#endif
static int bmi270_control(FAR struct sensor_lowerhalf_s *lower,
FAR struct file *filep,
int cmd, unsigned long arg);
/* Helpers */
static int bmi270_accel_scale(FAR struct bmi270_sensor_s *priv,
uint8_t scale);
static int bmi270_gyro_scale(FAR struct bmi270_sensor_s *priv,
uint16_t scale);
/****************************************************************************
* Private Data
****************************************************************************/
static const struct sensor_ops_s g_sensor_ops =
{
NULL, /* open */
NULL, /* close */
bmi270_activate,
bmi270_set_interval,
NULL, /* batch */
#ifdef CONFIG_SENSORS_BMI270_POLL
NULL, /* fetch */
#else
bmi270_fetch,
#endif
NULL, /* selftest */
NULL, /* set_calibvalue */
NULL, /* calibrate */
bmi270_control
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: bmi270_activate
****************************************************************************/
static int bmi270_activate(FAR struct sensor_lowerhalf_s *lower,
FAR struct file *filep, bool enable)
{
FAR struct bmi270_sensor_s *priv = NULL;
FAR struct bmi270_sensor_dev_s *dev = NULL;
bool start = false;
bool stop = false;
int ret = OK;
int tmp = 0;
priv = (FAR struct bmi270_sensor_s *)lower;
dev = priv->dev;
nxmutex_lock(&dev->lock);
tmp = (dev->priv[BMI270_ACCEL_IDX].enabled +
dev->priv[BMI270_GYRO_IDX].enabled);
if (enable && tmp == 0)
{
/* One time start */
start = true;
}
else if (tmp == 1)
{
/* One time stop */
stop = true;
}
priv->enabled = enable;
nxmutex_unlock(&dev->lock);
if (start)
{
/* Initialization sequence */
ret = bmi270_init_seq(&priv->base);
if (ret != 0)
{
return ret;
}
/* Set normal mode */
bmi270_set_normal_imu(&priv->base);
#ifdef CONFIG_SENSORS_BMI270_POLL
priv->last_update = sensor_get_timestamp();
/* Wake up the thread */
nxsem_post(&dev->run);
#endif
}
else if (stop)
{
/* Disable acquisition of acc and gyro */
bmi270_putreg8(&priv->base, BMI270_PWR_CTRL, 0);
up_mdelay(30);
}
return ret;
}
/****************************************************************************
* Name: bmi270_set_interval
****************************************************************************/
static int bmi270_set_interval(FAR struct sensor_lowerhalf_s *lower,
FAR struct file *filep,
FAR unsigned long *interval)
{
#ifdef CONFIG_SENSORS_BMI270_POLL
FAR struct bmi270_sensor_s *priv = NULL;
priv = (FAR struct bmi270_sensor_s *)lower;
priv->interval = *interval;
#endif
return OK;
}
#ifndef CONFIG_SENSORS_BMI270_POLL
/****************************************************************************
* Name: bmi270_set_interval
****************************************************************************/
static int bmi270_fetch(FAR struct sensor_lowerhalf_s *lower,
FAR struct file *filep, FAR char *buffer,
size_t buflen)
{
FAR struct bmi270_sensor_s *priv = NULL;
int16_t data[3];
int ret = OK;
priv = (FAR struct bmi270_sensor_s *)lower;
switch (lower->type)
{
case SENSOR_TYPE_ACCELEROMETER:
{
struct sensor_accel accel;
bmi270_getregs(&priv->base, BMI270_DATA_8,
(FAR uint8_t *)data, 6);
accel.timestamp = sensor_get_timestamp();
accel.x = data[0] * priv->scale;
accel.y = data[1] * priv->scale;
accel.z = data[2] * priv->scale;
memcpy(buffer, &accel, sizeof(accel));
ret = sizeof(accel);
break;
}
case SENSOR_TYPE_GYROSCOPE:
{
struct sensor_gyro gyro;
bmi270_getregs(&priv->base, BMI270_DATA_14,
(FAR uint8_t *)data, 6);
gyro.timestamp = sensor_get_timestamp();
gyro.x = data[0] * priv->scale;
gyro.y = data[1] * priv->scale;
gyro.z = data[2] * priv->scale;
memcpy(buffer, &gyro, sizeof(gyro));
ret = sizeof(gyro);
break;
}
default:
{
ret = -EINVAL;
break;
}
}
return ret;
}
#endif
/****************************************************************************
* Name: bmi270_cotrol
****************************************************************************/
static int bmi270_control(FAR struct sensor_lowerhalf_s *lower,
FAR struct file *filep, int cmd,
unsigned long arg)
{
FAR struct bmi270_sensor_s *priv = NULL;
int ret = OK;
priv = (FAR struct bmi270_sensor_s *)lower;
switch (cmd)
{
/* Set full scale command */
case SNIOC_SET_SCALE_XL:
{
if (priv->lower.type == SENSOR_TYPE_GYROSCOPE)
{
ret = bmi270_gyro_scale(priv, arg);
}
else if (priv->lower.type == SENSOR_TYPE_ACCELEROMETER)
{
ret = bmi270_accel_scale(priv, arg);
}
break;
}
default:
{
snerr("ERROR: Unrecognized cmd: %d\n", cmd);
ret = -ENOTTY;
break;
}
}
return ret;
}
/****************************************************************************
* Name: bmi270_midpoint
*
* Description:
* Find the midpoint between two numbers.
*
****************************************************************************/
static uint32_t bmi270_midpoint(uint32_t a, uint32_t b)
{
return (uint32_t)(((uint64_t)a +
(uint64_t)b + (uint64_t)1) / (uint64_t)2);
}
/****************************************************************************
* Name: bmi270_accel_scale
****************************************************************************/
static int bmi270_accel_scale(FAR struct bmi270_sensor_s *priv,
uint8_t scale)
{
int ret = OK;
if (scale < bmi270_midpoint(2, 4))
{
bmi270_putreg8(&priv->base, BMI270_ACC_RANGE, ACCEL_RANGE_2G);
priv->scale = CONSTANTS_ONE_G / 16384.f;
}
else if (scale < bmi270_midpoint(4, 8))
{
bmi270_putreg8(&priv->base, BMI270_ACC_RANGE, ACCEL_RANGE_4G);
priv->scale = CONSTANTS_ONE_G / 8192.f;
}
else if (scale < bmi270_midpoint(8, 16))
{
bmi270_putreg8(&priv->base, BMI270_ACC_RANGE, ACCEL_RANGE_8G);
priv->scale = CONSTANTS_ONE_G / 4096.f;
}
else
{
bmi270_putreg8(&priv->base, BMI270_ACC_RANGE, ACCEL_RANGE_16G);
priv->scale = CONSTANTS_ONE_G / 2048.f;
}
return ret;
}
/****************************************************************************
* Name: bmi270_gyro_scale
****************************************************************************/
static int bmi270_gyro_scale(FAR struct bmi270_sensor_s *priv,
uint16_t scale)
{
int ret = OK;
if (scale < bmi270_midpoint(125, 250))
{
bmi270_putreg8(&priv->base, BMI270_GYR_RANGE, GYRO_RANGE_125);
priv->scale = (M_PI / 180.0f) * 125.f / 32768.f;
}
else if (scale < bmi270_midpoint(250, 500))
{
bmi270_putreg8(&priv->base, BMI270_GYR_RANGE, GYRO_RANGE_250);
priv->scale = (M_PI / 180.0f) * 250.f / 32768.f;
}
else if (scale < bmi270_midpoint(500, 1000))
{
bmi270_putreg8(&priv->base, BMI270_GYR_RANGE, GYRO_RANGE_500);
priv->scale = (M_PI / 180.0f) * 500.f / 32768.f;
}
else if (scale < bmi270_midpoint(1000, 2000))
{
bmi270_putreg8(&priv->base, BMI270_GYR_RANGE, GYRO_RANGE_1000);
priv->scale = (M_PI / 180.0f) * 1000.f / 32768.f;
}
else
{
bmi270_putreg8(&priv->base, BMI270_GYR_RANGE, GYRO_RANGE_2000);
priv->scale = (M_PI / 180.0f) * 2000.f / 32768.f;
}
return ret;
}
#ifdef CONFIG_SENSORS_BMI270_POLL
/****************************************************************************
* Name: bmi270_accel_data
*
* Description:
* Get and push accel data from struct sensor_data_s
*
* Parameter:
* priv - Internal private lower half driver instance
* buf - Point to data
*
* Return:
* OK - on success
*
****************************************************************************/
static void bmi270_accel_data(FAR struct bmi270_sensor_s *priv,
FAR int16_t *buf)
{
FAR struct sensor_lowerhalf_s *lower = &priv->lower;
struct sensor_accel accel;
uint64_t now = sensor_get_timestamp();
if (!priv->enabled || now - priv->last_update < priv->interval)
{
return;
}
priv->last_update = now;
accel.timestamp = now;
accel.x = buf[0] * priv->scale;
accel.y = buf[1] * priv->scale;
accel.z = buf[2] * priv->scale;
accel.temperature = 0;
lower->push_event(lower->priv, &accel, sizeof(accel));
}
/****************************************************************************
* Name: bmi270_gyro_data
*
* Description:
* Get and push gyro data from struct sensor_data_s
*
* Parameter:
* priv - Internal private lower half driver instance
* buf - Point to data
*
* Return:
* OK - on success
*
****************************************************************************/
static void bmi270_gyro_data(FAR struct bmi270_sensor_s *priv,
FAR int16_t *buf)
{
FAR struct sensor_lowerhalf_s *lower = &priv->lower;
struct sensor_gyro gyro;
uint64_t now = sensor_get_timestamp();
if (!priv->enabled || now - priv->last_update < priv->interval)
{
return;
}
priv->last_update = now;
gyro.timestamp = now;
gyro.x = buf[0] * priv->scale;
gyro.y = buf[1] * priv->scale;
gyro.z = buf[2] * priv->scale;
gyro.temperature = 0;
lower->push_event(lower->priv, &gyro, sizeof(gyro));
}
/****************************************************************************
* Name: bmi270_thread
*
* Description:
* Thread for performing interval measurement cycle and data read.
*
* Parameter:
* argc - Number opf arguments
* argv - Pointer to argument list
*
****************************************************************************/
static int bmi270_thread(int argc, FAR char **argv)
{
FAR struct bmi270_sensor_dev_s *dev
= (FAR struct bmi270_sensor_dev_s *)((uintptr_t)strtoul(argv[1], NULL,
16));
FAR struct bmi270_sensor_s *accel = &dev->priv[BMI270_ACCEL_IDX];
FAR struct bmi270_sensor_s *gyro = &dev->priv[BMI270_GYRO_IDX];
unsigned long min_interval;
int16_t data[6];
int ret;
while (true)
{
if ((!accel->enabled) && (!gyro->enabled))
{
/* Waiting to be woken up */
ret = nxsem_wait(&dev->run);
if (ret < 0)
{
continue;
}
}
/* Get data */
bmi270_getregs(&gyro->base, BMI270_DATA_8, (uint8_t *)data, 12);
/* Read accel */
if (accel->enabled)
{
bmi270_accel_data(accel, data);
}
/* Read gyro */
if (gyro->enabled)
{
bmi270_gyro_data(gyro, &data[3]);
}
/* Sleeping thread before fetching the next sensor data */
min_interval = MIN(accel->interval, gyro->interval);
nxsig_usleep(min_interval);
}
return OK;
}
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: bmi270_register_uorb
*
* Description:
* Register the BMI270 IMU as sensor device
*
* Input Parameters:
* devno - Instance number for driver
*
* Returned Value:
* Zero (OK) on success; a negated errno value on failure.
*
****************************************************************************/
#ifdef CONFIG_SENSORS_BMI270_I2C
int bmi270_register_uorb(int devno, FAR struct i2c_master_s *i2c,
uint8_t addr)
#else /* CONFIG_SENSORS_BMI270_SPI */
int bmi270_register_uorb(int devno, FAR struct spi_dev_s *spi)
#endif
{
FAR struct bmi270_sensor_dev_s *dev = NULL;
FAR struct bmi270_sensor_s *tmp = NULL;
#ifdef CONFIG_SENSORS_BMI270_POLL
FAR char *argv[2];
char arg1[32];
#endif
int ret = OK;
/* Initialize the device structure. */
dev = (FAR struct bmi270_sensor_dev_s *)kmm_malloc(sizeof(*dev));
if (dev == NULL)
{
snerr("ERROR: Failed to allocate instance\n");
return -ENOMEM;
}
memset(dev, 0, sizeof(*dev));
nxmutex_init(&dev->lock);
#ifdef CONFIG_SENSORS_BMI270_POLL
nxsem_init(&dev->run, 0, 0);
#endif
/* Accelerometer register */
tmp = &dev->priv[BMI270_ACCEL_IDX];
tmp->dev = dev;
#ifdef CONFIG_SENSORS_BMI270_I2C
tmp->base.i2c = i2c;
tmp->base.addr = addr;
#else
tmp->base.spi = spi;
#endif
tmp->lower.ops = &g_sensor_ops;
tmp->lower.type = SENSOR_TYPE_ACCELEROMETER;
tmp->lower.nbuffer = 1;
#ifdef CONFIG_SENSORS_BMI270_POLL
tmp->enabled = false;
tmp->interval = CONFIG_SENSORS_BMI270_POLL_INTERVAL;
#endif
ret = sensor_register(&tmp->lower, devno);
if (ret < 0)
{
snerr("sensor_register failed: %d\n", ret);
goto gyro_err;
}
bmi270_accel_scale(tmp, 2);
/* Gyroscope register */
tmp = &dev->priv[BMI270_GYRO_IDX];
tmp->dev = dev;
#ifdef CONFIG_SENSORS_BMI270_I2C
tmp->base.i2c = i2c;
tmp->base.addr = addr;
#else
tmp->base.spi = spi;
#endif
tmp->lower.ops = &g_sensor_ops;
tmp->lower.type = SENSOR_TYPE_GYROSCOPE;
tmp->lower.nbuffer = 1;
#ifdef CONFIG_SENSORS_BMI270_POLL
tmp->enabled = false;
tmp->interval = CONFIG_SENSORS_BMI270_POLL_INTERVAL;
#endif
ret = sensor_register(&tmp->lower, devno);
if (ret < 0)
{
snerr("sensor_register failed: %d\n", ret);
goto gyro_err;
}
bmi270_gyro_scale(tmp, 2000);
#ifdef CONFIG_SENSORS_BMI270_SPI
/* BMI270 detects communication bus is SPI by rising edge of CS. */
bmi270_getreg8(&tmp->base, 0x00);
bmi270_getreg8(&tmp->base, 0x00);
up_udelay(200);
#endif
#ifdef CONFIG_SENSORS_BMI270_POLL
/* Create thread for polling sensor data */
snprintf(arg1, 16, "%p", dev);
argv[0] = arg1;
argv[1] = NULL;
ret = kthread_create("bmi270_thread", SCHED_PRIORITY_DEFAULT,
CONFIG_SENSORS_BMI270_THREAD_STACKSIZE,
bmi270_thread,
argv);
if (ret < 0)
{
goto thr_err;
}
#endif
return ret;
#ifdef CONFIG_SENSORS_BMI270_POLL
thr_err:
#endif
#ifdef AUX_MAG_SUPPORTED
sensor_unregister(&dev->priv[BMI270_MAG_IDX].lower, devno);
mag_err:
#endif
sensor_unregister(&dev->priv[BMI270_GYRO_IDX].lower, devno);
gyro_err:
sensor_unregister(&dev->priv[BMI270_ACCEL_IDX].lower, devno);
kmm_free(dev);
return ret;
}

View file

@ -57,6 +57,9 @@ struct accel_gyro_st_s
uint32_t sensor_time;
};
struct i2c_master_s;
struct spi_dev_s;
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
@ -86,12 +89,15 @@ extern "C"
*
****************************************************************************/
#ifdef CONFIG_SENSORS_BMI270_I2C
struct i2c_master_s;
#if defined(CONFIG_SENSORS_BMI270_I2C) && defined(CONFIG_SENSORS_BMI270_UORB)
int bmi270_register_uorb(int devno, FAR struct i2c_master_s *dev,
uint8_t addr);
#elif defined(CONFIG_SENSORS_BMI270_I2C) && !defined(CONFIG_SENSORS_BMI270_UORB)
int bmi270_register(FAR const char *devpath, FAR struct i2c_master_s *dev,
uint8_t addr);
#else /* CONFIG_BMI270_SPI */
struct spi_dev_s;
#elif !defined(CONFIG_SENSORS_BMI270_I2C) && defined(CONFIG_SENSORS_BMI270_UORB)
int bmi270_register_uorb(int devno, FAR struct spi_dev_s *dev);
#elif !defined(CONFIG_SENSORS_BMI270_I2C) && !defined(CONFIG_SENSORS_BMI270_UORB)
int bmi270_register(FAR const char *devpath, FAR struct spi_dev_s *dev);
#endif