drivers: sensor: add I3G4250D Gyro driver
Implements a shim layer driver using st hal for I3G4250D gyro, mounted for example on stm32f3_disco_E. No support for triggers included yet. Signed-off-by: Jonathan Hahn <Jonathan.Hahn@t-online.de>
This commit is contained in:
parent
d5556cafed
commit
c9f87969c2
8 changed files with 396 additions and 0 deletions
|
@ -31,6 +31,7 @@ add_subdirectory_ifdef(CONFIG_TI_HDC20XX ti_hdc20xx)
|
||||||
add_subdirectory_ifdef(CONFIG_HMC5883L hmc5883l)
|
add_subdirectory_ifdef(CONFIG_HMC5883L hmc5883l)
|
||||||
add_subdirectory_ifdef(CONFIG_HP206C hp206c)
|
add_subdirectory_ifdef(CONFIG_HP206C hp206c)
|
||||||
add_subdirectory_ifdef(CONFIG_HTS221 hts221)
|
add_subdirectory_ifdef(CONFIG_HTS221 hts221)
|
||||||
|
add_subdirectory_ifdef(CONFIG_I3G4250D i3g4250d)
|
||||||
add_subdirectory_ifdef(CONFIG_ICM42605 icm42605)
|
add_subdirectory_ifdef(CONFIG_ICM42605 icm42605)
|
||||||
add_subdirectory_ifdef(CONFIG_IIS2DH iis2dh)
|
add_subdirectory_ifdef(CONFIG_IIS2DH iis2dh)
|
||||||
add_subdirectory_ifdef(CONFIG_IIS2DLPC iis2dlpc)
|
add_subdirectory_ifdef(CONFIG_IIS2DLPC iis2dlpc)
|
||||||
|
|
|
@ -100,6 +100,8 @@ source "drivers/sensor/hp206c/Kconfig"
|
||||||
|
|
||||||
source "drivers/sensor/hts221/Kconfig"
|
source "drivers/sensor/hts221/Kconfig"
|
||||||
|
|
||||||
|
source "drivers/sensor/i3g4250d/Kconfig"
|
||||||
|
|
||||||
source "drivers/sensor/icm42605/Kconfig"
|
source "drivers/sensor/icm42605/Kconfig"
|
||||||
|
|
||||||
source "drivers/sensor/iis2dh/Kconfig"
|
source "drivers/sensor/iis2dh/Kconfig"
|
||||||
|
|
5
drivers/sensor/i3g4250d/CMakeLists.txt
Normal file
5
drivers/sensor/i3g4250d/CMakeLists.txt
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
zephyr_library()
|
||||||
|
|
||||||
|
zephyr_library_sources(i3g4250d.c i3g4250d_spi.c)
|
12
drivers/sensor/i3g4250d/Kconfig
Normal file
12
drivers/sensor/i3g4250d/Kconfig
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
# I3G4250D three-axis digital output gyroscope
|
||||||
|
|
||||||
|
# Copyright (c) 2021 Jonathan Hahn
|
||||||
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
config I3G4250D
|
||||||
|
bool "I3G4250D three-axis digital output gyroscope"
|
||||||
|
depends on SPI
|
||||||
|
select HAS_STMEMSC
|
||||||
|
select USE_STDC_I3G4250D
|
||||||
|
help
|
||||||
|
Enable driver for I3G4250D SPI-based three-axis motion tracking device.
|
228
drivers/sensor/i3g4250d/i3g4250d.c
Normal file
228
drivers/sensor/i3g4250d/i3g4250d.c
Normal file
|
@ -0,0 +1,228 @@
|
||||||
|
/* ST Microelectronics I3G4250D gyro driver
|
||||||
|
*
|
||||||
|
* Copyright (c) 2021 Jonathan Hahn
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*
|
||||||
|
* Datasheet:
|
||||||
|
* https://www.st.com/resource/en/datasheet/i3g4250d.pdf
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define DT_DRV_COMPAT st_i3g4250d
|
||||||
|
|
||||||
|
#include <init.h>
|
||||||
|
#include <sys/__assert.h>
|
||||||
|
#include <sys/byteorder.h>
|
||||||
|
#include <drivers/sensor.h>
|
||||||
|
#include <logging/log.h>
|
||||||
|
|
||||||
|
#include "i3g4250d.h"
|
||||||
|
|
||||||
|
#define RAW_TO_MICRODEGREEPERSEC 8750
|
||||||
|
|
||||||
|
LOG_MODULE_REGISTER(i3g4250d, CONFIG_SENSOR_LOG_LEVEL);
|
||||||
|
|
||||||
|
static int i3g4250d_sample_fetch(const struct device *dev,
|
||||||
|
enum sensor_channel chan)
|
||||||
|
{
|
||||||
|
struct i3g4250d_data *i3g4250d = dev->data;
|
||||||
|
int ret;
|
||||||
|
uint8_t reg;
|
||||||
|
int16_t buf[3] = { 0 };
|
||||||
|
|
||||||
|
if ((chan != SENSOR_CHAN_ALL) && (chan != SENSOR_CHAN_GYRO_XYZ)) {
|
||||||
|
return -ENOTSUP;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = i3g4250d_flag_data_ready_get(i3g4250d->ctx, ®);
|
||||||
|
if (ret < 0 || reg != 1) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = i3g4250d_angular_rate_raw_get(i3g4250d->ctx, buf);
|
||||||
|
if (ret < 0) {
|
||||||
|
LOG_ERR("Failed to fetch raw data sample!");
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(i3g4250d->angular_rate, buf, sizeof(i3g4250d->angular_rate));
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void i3g4250d_convert(struct sensor_value *val, int16_t raw_value)
|
||||||
|
{
|
||||||
|
val->val1 = (int16_t)(raw_value * RAW_TO_MICRODEGREEPERSEC / 1000000LL);
|
||||||
|
val->val2 = (int16_t)(raw_value * RAW_TO_MICRODEGREEPERSEC) % 1000000LL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void i3g4250d_channel_convert(enum sensor_channel chan,
|
||||||
|
uint16_t *raw_xyz,
|
||||||
|
struct sensor_value *val)
|
||||||
|
{
|
||||||
|
uint8_t ofs_start, ofs_stop;
|
||||||
|
|
||||||
|
switch (chan) {
|
||||||
|
case SENSOR_CHAN_GYRO_X:
|
||||||
|
ofs_start = ofs_stop = 0U;
|
||||||
|
break;
|
||||||
|
case SENSOR_CHAN_GYRO_Y:
|
||||||
|
ofs_start = ofs_stop = 1U;
|
||||||
|
break;
|
||||||
|
case SENSOR_CHAN_GYRO_Z:
|
||||||
|
ofs_start = ofs_stop = 2U;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
ofs_start = 0U;
|
||||||
|
ofs_stop = 2U;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = ofs_start; i <= ofs_stop; i++) {
|
||||||
|
i3g4250d_convert(val++, raw_xyz[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int i3g4250d_channel_get(const struct device *dev,
|
||||||
|
enum sensor_channel chan,
|
||||||
|
struct sensor_value *val)
|
||||||
|
{
|
||||||
|
struct i3g4250d_data *i3g4250d = dev->data;
|
||||||
|
|
||||||
|
switch (chan) {
|
||||||
|
case SENSOR_CHAN_GYRO_X:
|
||||||
|
__fallthrough;
|
||||||
|
case SENSOR_CHAN_GYRO_Y:
|
||||||
|
__fallthrough;
|
||||||
|
case SENSOR_CHAN_GYRO_Z:
|
||||||
|
__fallthrough;
|
||||||
|
case SENSOR_CHAN_GYRO_XYZ:
|
||||||
|
i3g4250d_channel_convert(chan, i3g4250d->angular_rate, val);
|
||||||
|
return 0;
|
||||||
|
default:
|
||||||
|
return -ENOTSUP;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static i3g4250d_dr_t gyr_odr_to_reg(const struct sensor_value *val)
|
||||||
|
{
|
||||||
|
double odr = sensor_value_to_double(val);
|
||||||
|
i3g4250d_dr_t reg = I3G4250D_ODR_OFF;
|
||||||
|
|
||||||
|
if ((odr > 0.0) && (odr < 100.0)) {
|
||||||
|
reg = I3G4250D_ODR_SLEEP;
|
||||||
|
} else if ((odr >= 100.0) && (odr < 200.0)) {
|
||||||
|
reg = I3G4250D_ODR_100Hz;
|
||||||
|
} else if ((odr >= 200.0) && (odr < 400.0)) {
|
||||||
|
reg = I3G4250D_ODR_200Hz;
|
||||||
|
} else if ((odr >= 400.0) && (odr < 800.0)) {
|
||||||
|
reg = I3G4250D_ODR_400Hz;
|
||||||
|
} else if (odr >= 800.0) {
|
||||||
|
reg = I3G4250D_ODR_800Hz;
|
||||||
|
}
|
||||||
|
|
||||||
|
return reg;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int i3g4250d_config_gyro(const struct device *dev,
|
||||||
|
enum sensor_attribute attr,
|
||||||
|
const struct sensor_value *val)
|
||||||
|
{
|
||||||
|
struct i3g4250d_data *i3g4250d = dev->data;
|
||||||
|
i3g4250d_dr_t dr_reg;
|
||||||
|
|
||||||
|
switch (attr) {
|
||||||
|
case SENSOR_ATTR_SAMPLING_FREQUENCY:
|
||||||
|
dr_reg = gyr_odr_to_reg(val);
|
||||||
|
return i3g4250d_data_rate_set(i3g4250d->ctx, dr_reg);
|
||||||
|
default:
|
||||||
|
LOG_ERR("Gyro attribute not supported");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return -ENOTSUP;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int i3g4250d_attr_set(const struct device *dev, enum sensor_channel chan,
|
||||||
|
enum sensor_attribute attr,
|
||||||
|
const struct sensor_value *val)
|
||||||
|
{
|
||||||
|
switch (chan) {
|
||||||
|
case SENSOR_CHAN_GYRO_XYZ:
|
||||||
|
return i3g4250d_config_gyro(dev, attr, val);
|
||||||
|
default:
|
||||||
|
LOG_ERR("attr_set() not supported on this channel %d.", chan);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return -ENOTSUP;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const struct sensor_driver_api i3g4250d_driver_api = {
|
||||||
|
.attr_set = i3g4250d_attr_set,
|
||||||
|
.sample_fetch = i3g4250d_sample_fetch,
|
||||||
|
.channel_get = i3g4250d_channel_get,
|
||||||
|
};
|
||||||
|
|
||||||
|
static int i3g4250d_init(const struct device *dev)
|
||||||
|
{
|
||||||
|
struct i3g4250d_data *i3g4250d = dev->data;
|
||||||
|
uint8_t wai;
|
||||||
|
int ret = 0;
|
||||||
|
|
||||||
|
ret = i3g4250d_spi_init(dev);
|
||||||
|
if (ret != 0) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = i3g4250d_device_id_get(i3g4250d->ctx, &wai);
|
||||||
|
if (ret != 0) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (wai != I3G4250D_ID) {
|
||||||
|
LOG_ERR("Inavild chip ID: %02x", wai);
|
||||||
|
return -EIO;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Configure filtering chain - Gyroscope - High Pass */
|
||||||
|
ret = i3g4250d_filter_path_set(i3g4250d->ctx, I3G4250D_LPF1_HP_ON_OUT);
|
||||||
|
if (ret != 0) {
|
||||||
|
LOG_ERR("Failed setting filter path");
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = i3g4250d_hp_bandwidth_set(i3g4250d->ctx, I3G4250D_HP_LEVEL_3);
|
||||||
|
if (ret != 0) {
|
||||||
|
LOG_ERR("Failed setting high pass");
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Set Output data rate */
|
||||||
|
ret = i3g4250d_data_rate_set(i3g4250d->ctx, I3G4250D_ODR_100Hz);
|
||||||
|
if (ret != 0) {
|
||||||
|
LOG_ERR("Failed setting data rate");
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
#define I3G4250D_DEVICE_INIT(inst) \
|
||||||
|
static struct i3g4250d_data i3g4250d_data_##inst; \
|
||||||
|
static const struct i3g4250d_device_config i3g4250d_config_##inst = { \
|
||||||
|
.spi = SPI_DT_SPEC_INST_GET(inst, \
|
||||||
|
SPI_OP_MODE_MASTER | SPI_MODE_CPOL | \
|
||||||
|
SPI_MODE_CPHA | SPI_WORD_SET(8) | SPI_LINES_SINGLE, \
|
||||||
|
0), \
|
||||||
|
}; \
|
||||||
|
DEVICE_DT_INST_DEFINE(inst, \
|
||||||
|
i3g4250d_init, \
|
||||||
|
NULL, \
|
||||||
|
&i3g4250d_data_##inst, \
|
||||||
|
&i3g4250d_config_##inst, \
|
||||||
|
POST_KERNEL, \
|
||||||
|
CONFIG_SENSOR_INIT_PRIORITY, \
|
||||||
|
&i3g4250d_driver_api);
|
||||||
|
|
||||||
|
DT_INST_FOREACH_STATUS_OKAY(I3G4250D_DEVICE_INIT)
|
33
drivers/sensor/i3g4250d/i3g4250d.h
Normal file
33
drivers/sensor/i3g4250d/i3g4250d.h
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
/* ST Microelectronics I3G4250D gyro driver
|
||||||
|
*
|
||||||
|
* Copyright (c) 2021 Jonathan Hahn
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*
|
||||||
|
* Datasheet:
|
||||||
|
* https://www.st.com/resource/en/datasheet/i3g4250d.pdf
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef ZEPHYR_DRIVERS_SENSOR_I3G4250D_I3G4250D_H_
|
||||||
|
#define ZEPHYR_DRIVERS_SENSOR_I3G4250D_I3G4250D_H_
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <drivers/spi.h>
|
||||||
|
#include <drivers/gpio.h>
|
||||||
|
#include <drivers/sensor.h>
|
||||||
|
#include <sys/util.h>
|
||||||
|
#include "i3g4250d_reg.h"
|
||||||
|
|
||||||
|
struct i3g4250d_device_config {
|
||||||
|
struct spi_dt_spec spi;
|
||||||
|
};
|
||||||
|
|
||||||
|
/* sensor data */
|
||||||
|
struct i3g4250d_data {
|
||||||
|
int16_t angular_rate[3];
|
||||||
|
stmdev_ctx_t *ctx;
|
||||||
|
};
|
||||||
|
|
||||||
|
int i3g4250d_spi_init(const struct device *dev);
|
||||||
|
|
||||||
|
#endif /* __SENSOR_I3G4250D__ */
|
106
drivers/sensor/i3g4250d/i3g4250d_spi.c
Normal file
106
drivers/sensor/i3g4250d/i3g4250d_spi.c
Normal file
|
@ -0,0 +1,106 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2021 Jonathan Hahn
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define DT_DRV_COMPAT st_i3g4250d
|
||||||
|
|
||||||
|
#include <logging/log.h>
|
||||||
|
#include "i3g4250d.h"
|
||||||
|
|
||||||
|
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
|
||||||
|
|
||||||
|
#define I3G4250D_SPI_READM (3 << 6) /* 0xC0 */
|
||||||
|
#define I3G4250D_SPI_WRITEM (1 << 6) /* 0x40 */
|
||||||
|
|
||||||
|
LOG_MODULE_DECLARE(i3g4250d, CONFIG_SENSOR_LOG_LEVEL);
|
||||||
|
|
||||||
|
static int i3g4250d_spi_read(const struct device *dev, uint8_t reg,
|
||||||
|
uint8_t *data, uint16_t len)
|
||||||
|
{
|
||||||
|
int ret;
|
||||||
|
const struct i3g4250d_device_config *config = dev->config;
|
||||||
|
uint8_t buffer_tx[2] = { reg | I3G4250D_SPI_READM, 0 };
|
||||||
|
const struct spi_buf tx_buf = {
|
||||||
|
.buf = buffer_tx,
|
||||||
|
.len = 2,
|
||||||
|
};
|
||||||
|
const struct spi_buf_set tx = {
|
||||||
|
.buffers = &tx_buf,
|
||||||
|
.count = 1,
|
||||||
|
};
|
||||||
|
const struct spi_buf rx_buf[2] = {
|
||||||
|
{
|
||||||
|
.buf = NULL,
|
||||||
|
.len = 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
.buf = data,
|
||||||
|
.len = len,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const struct spi_buf_set rx = {
|
||||||
|
.buffers = rx_buf,
|
||||||
|
.count = 2,
|
||||||
|
};
|
||||||
|
|
||||||
|
ret = spi_transceive_dt(&config->spi, &tx, &rx);
|
||||||
|
if (ret < 0) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int i3g4250d_spi_write(const struct device *dev, uint8_t reg,
|
||||||
|
uint8_t *data, uint16_t len)
|
||||||
|
{
|
||||||
|
int ret;
|
||||||
|
const struct i3g4250d_device_config *config = dev->config;
|
||||||
|
uint8_t buffer_tx[2] = { reg | I3G4250D_SPI_WRITEM, 0 };
|
||||||
|
const struct spi_buf tx_buf[2] = {
|
||||||
|
{
|
||||||
|
.buf = buffer_tx,
|
||||||
|
.len = 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
.buf = data,
|
||||||
|
.len = len,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const struct spi_buf_set tx = {
|
||||||
|
.buffers = tx_buf,
|
||||||
|
.count = 2,
|
||||||
|
};
|
||||||
|
|
||||||
|
ret = spi_write_dt(&config->spi, &tx);
|
||||||
|
if (ret < 0) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
stmdev_ctx_t i3g4250d_spi_ctx = {
|
||||||
|
.read_reg = (stmdev_read_ptr) i3g4250d_spi_read,
|
||||||
|
.write_reg = (stmdev_write_ptr) i3g4250d_spi_write,
|
||||||
|
};
|
||||||
|
|
||||||
|
int i3g4250d_spi_init(const struct device *dev)
|
||||||
|
{
|
||||||
|
struct i3g4250d_data *i3g4250d = dev->data;
|
||||||
|
const struct i3g4250d_device_config *cfg = dev->config;
|
||||||
|
|
||||||
|
if (!spi_is_ready(&cfg->spi)) {
|
||||||
|
LOG_ERR("spi not ready");
|
||||||
|
return -ENODEV;
|
||||||
|
}
|
||||||
|
|
||||||
|
i3g4250d->ctx = &i3g4250d_spi_ctx;
|
||||||
|
i3g4250d->ctx->handle = (void *)dev;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(spi) */
|
9
dts/bindings/sensor/st,i3g4250d.yaml
Normal file
9
dts/bindings/sensor/st,i3g4250d.yaml
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
# Copyright (c) 2021 Jonathan Hahn
|
||||||
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
description: |
|
||||||
|
STMicroelectronics I3G4250D 3-axis gyrometer accessed through SPI bus
|
||||||
|
|
||||||
|
compatible: "st,i3g4250d"
|
||||||
|
|
||||||
|
include: spi-device.yaml
|
Loading…
Add table
Add a link
Reference in a new issue