2016-08-02 18:38:14 +02:00
|
|
|
/****************************************************************************
|
2017-02-18 15:08:41 +01:00
|
|
|
* drivers/sensors/kxtj9.c
|
2016-08-02 18:38:14 +02:00
|
|
|
*
|
2021-09-14 16:16:31 +02: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
|
2016-08-02 18:38:14 +02:00
|
|
|
*
|
2021-09-14 16:16:31 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2016-08-02 18:38:14 +02:00
|
|
|
*
|
2021-09-14 16:16:31 +02: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.
|
2016-08-02 18:38:14 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2016-08-02 18:38:14 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
2017-10-06 18:15:01 +02:00
|
|
|
#include <nuttx/signal.h>
|
2016-08-02 18:38:14 +02:00
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
#include <nuttx/i2c/i2c_master.h>
|
2020-02-01 08:17:32 +01:00
|
|
|
#include <nuttx/semaphore.h>
|
2017-02-18 15:08:41 +01:00
|
|
|
#include <nuttx/sensors/kxtj9.h>
|
2017-03-30 15:38:37 +02:00
|
|
|
#include <nuttx/random.h>
|
2016-08-02 18:38:14 +02:00
|
|
|
|
2017-08-24 17:54:23 +02:00
|
|
|
#if defined(CONFIG_I2C) && defined(CONFIG_SENSORS_KXTJ9)
|
2016-08-02 18:38:14 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Configuration ************************************************************/
|
|
|
|
|
2017-08-24 17:54:23 +02:00
|
|
|
#ifndef CONFIG_KXTJ9_I2C_BUS_SPEED
|
|
|
|
# define CONFIG_KXTJ9_I2C_BUS_SPEED 400000
|
2016-08-02 18:38:14 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Register Definitions *****************************************************/
|
|
|
|
|
|
|
|
/* Output registers */
|
|
|
|
|
|
|
|
#define XOUT_L 0x06
|
|
|
|
#define WHO_AM_I 0x0f
|
|
|
|
#define DCST_RESP 0x0c
|
|
|
|
|
|
|
|
/* Control registers */
|
|
|
|
|
|
|
|
#define INT_REL 0x1a
|
|
|
|
#define CTRL_REG1 0x1b
|
|
|
|
#define INT_CTRL1 0x1e
|
|
|
|
#define DATA_CTRL 0x21
|
|
|
|
#define CTRL_REG2 0x1d
|
|
|
|
|
|
|
|
/* Control register 1 bits */
|
|
|
|
|
|
|
|
#define PC1_OFF 0x7f
|
|
|
|
#define PC1_ON (1 << 7)
|
|
|
|
|
|
|
|
/* CTRL_REG1: set resolution, g-range, data ready enable */
|
2019-12-07 02:07:34 +01:00
|
|
|
|
2016-08-02 18:38:14 +02:00
|
|
|
/* Output resolution: 8-bit valid or 12-bit valid */
|
|
|
|
|
|
|
|
#define RES_8BIT 0
|
|
|
|
#define RES_12BIT (1 << 6)
|
|
|
|
|
2020-02-22 19:31:14 +01:00
|
|
|
/* Data ready function enable bit: set during probe if using irq mode */
|
2016-08-02 18:38:14 +02:00
|
|
|
|
|
|
|
#define DRDYE (1 << 5)
|
|
|
|
|
|
|
|
/* Output g-range: +/-2g, 4g, or 8g */
|
|
|
|
|
|
|
|
#define KXTJ9_G_2G 0
|
|
|
|
#define KXTJ9_G_4G (1 << 3)
|
|
|
|
#define KXTJ9_G_8G (1 << 4)
|
|
|
|
|
|
|
|
/* Interrupt control register 1 bits */
|
2019-12-07 02:07:34 +01:00
|
|
|
|
2016-08-02 18:38:14 +02:00
|
|
|
/* Set these during probe if using irq mode */
|
|
|
|
|
|
|
|
#define KXTJ9_IEL (1 << 3)
|
|
|
|
#define KXTJ9_IEA (1 << 4)
|
|
|
|
#define KXTJ9_IEN (1 << 5)
|
|
|
|
|
|
|
|
#define KXTJ9_SRST 0x80
|
|
|
|
#define WHO_AM_I_KXCJ9 0x0a
|
|
|
|
|
|
|
|
#define KXTJ9_CTRL1_CONFIG (RES_12BIT | KXTJ9_G_2G | DRDYE)
|
|
|
|
|
2020-02-23 09:50:23 +01:00
|
|
|
/* Misc. driver definitions *************************************************/
|
2016-08-02 18:38:14 +02:00
|
|
|
|
|
|
|
#define ACCEL_NUM_RETRIES 5
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* This structure describes the state of one KXTJ9 device */
|
|
|
|
|
2017-02-18 15:08:41 +01:00
|
|
|
struct kxtj9_dev_s
|
2016-08-02 18:38:14 +02:00
|
|
|
{
|
|
|
|
FAR struct i2c_master_s *i2c;
|
|
|
|
sem_t exclsem;
|
|
|
|
bool enable;
|
|
|
|
bool power_enabled;
|
|
|
|
uint8_t address;
|
|
|
|
uint8_t shift;
|
|
|
|
uint8_t ctrl_reg1;
|
|
|
|
uint8_t data_ctrl;
|
|
|
|
uint8_t int_ctrl;
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* I2C helpers */
|
|
|
|
|
2017-02-18 15:08:41 +01:00
|
|
|
static int kxtj9_reg_read(FAR struct kxtj9_dev_s *priv, uint8_t regaddr,
|
2022-04-12 12:16:13 +02:00
|
|
|
FAR uint8_t *regval, unsigned int len);
|
2017-02-18 15:08:41 +01:00
|
|
|
static int kxtj9_reg_write(FAR struct kxtj9_dev_s *priv,
|
2022-04-12 12:16:13 +02:00
|
|
|
uint8_t regaddr, uint8_t regval);
|
2016-08-02 18:38:14 +02:00
|
|
|
|
|
|
|
/* KXTJ9 helpers */
|
|
|
|
|
2017-02-18 15:08:41 +01:00
|
|
|
static int kxtj9_configure(FAR struct kxtj9_dev_s *priv, uint8_t odr);
|
|
|
|
static int kxtj9_enable(FAR struct kxtj9_dev_s *priv, bool on);
|
|
|
|
static int kxtj9_read_sensor_data(FAR struct kxtj9_dev_s *priv,
|
2016-08-02 18:38:14 +02:00
|
|
|
FAR struct kxtj9_sensor_data *sensor_data);
|
2017-02-18 15:08:41 +01:00
|
|
|
static void kxtj9_soft_reset(FAR struct kxtj9_dev_s *priv);
|
|
|
|
static void kxtj9_set_mode_standby(FAR struct kxtj9_dev_s *priv);
|
2016-08-02 18:38:14 +02:00
|
|
|
|
|
|
|
/* Character driver methods */
|
|
|
|
|
2017-02-18 15:08:41 +01:00
|
|
|
static ssize_t kxtj9_read(FAR struct file *filep, FAR char *buffer,
|
2022-04-12 12:16:13 +02:00
|
|
|
size_t buflen);
|
2017-02-18 15:08:41 +01:00
|
|
|
static ssize_t kxtj9_write(FAR struct file *filep, FAR const char *buffer,
|
2016-08-02 18:38:14 +02:00
|
|
|
size_t buflen);
|
2017-02-18 15:08:41 +01:00
|
|
|
static int kxtj9_ioctl(FAR struct file *filep, int cmd,
|
2022-04-12 12:16:13 +02:00
|
|
|
unsigned long arg);
|
2016-08-02 18:38:14 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static const struct file_operations g_fops =
|
|
|
|
{
|
2022-04-12 12:16:13 +02:00
|
|
|
NULL, /* open */
|
|
|
|
NULL, /* close */
|
2022-01-10 15:51:17 +01:00
|
|
|
kxtj9_read, /* read */
|
|
|
|
kxtj9_write, /* write */
|
|
|
|
NULL, /* seek */
|
|
|
|
kxtj9_ioctl, /* ioctl */
|
|
|
|
NULL /* poll */
|
2016-08-02 18:38:14 +02:00
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
2022-01-10 15:51:17 +01:00
|
|
|
, NULL /* unlink */
|
2016-08-02 18:38:14 +02:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: kxtj9_reg_read
|
|
|
|
*
|
|
|
|
* Description:
|
2016-08-02 18:46:19 +02:00
|
|
|
* Read from multiple KXTJ9 registers.
|
2016-08-02 18:38:14 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-02-18 15:08:41 +01:00
|
|
|
static int kxtj9_reg_read(FAR struct kxtj9_dev_s *priv, uint8_t regaddr,
|
2016-08-02 18:38:14 +02:00
|
|
|
FAR uint8_t *regval, unsigned int len)
|
|
|
|
{
|
|
|
|
struct i2c_msg_s msg[2];
|
|
|
|
uint8_t buf[1];
|
|
|
|
int retries = ACCEL_NUM_RETRIES;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* Format two messages: The first is a write containing the register
|
|
|
|
* address
|
|
|
|
*/
|
|
|
|
|
|
|
|
buf[0] = regaddr;
|
|
|
|
|
2017-08-24 17:54:23 +02:00
|
|
|
msg[0].frequency = CONFIG_KXTJ9_I2C_BUS_SPEED,
|
2016-08-02 18:38:14 +02:00
|
|
|
msg[0].addr = priv->address;
|
|
|
|
msg[0].flags = 0;
|
|
|
|
msg[0].buffer = buf;
|
|
|
|
msg[0].length = 1;
|
|
|
|
|
|
|
|
/* The second is a read with a restart containing the register data */
|
|
|
|
|
2017-08-24 17:54:23 +02:00
|
|
|
msg[1].frequency = CONFIG_KXTJ9_I2C_BUS_SPEED,
|
2016-08-02 18:38:14 +02:00
|
|
|
msg[1].addr = priv->address;
|
|
|
|
msg[1].flags = I2C_M_READ;
|
|
|
|
msg[1].buffer = regval;
|
|
|
|
msg[1].length = len;
|
|
|
|
|
|
|
|
/* Then perform the transfer. */
|
|
|
|
|
|
|
|
ret = I2C_TRANSFER(priv->i2c, msg, 2);
|
|
|
|
}
|
|
|
|
while (ret < 0 && retries-- > 0);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2016-08-02 18:46:19 +02:00
|
|
|
* Name: kxtj9_reg_write
|
2016-08-02 18:38:14 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2016-08-02 18:46:19 +02:00
|
|
|
* Write a value to a single KXTJ9 register
|
2016-08-02 18:38:14 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-02-18 15:08:41 +01:00
|
|
|
static int kxtj9_reg_write(FAR struct kxtj9_dev_s *priv, uint8_t regaddr,
|
2016-08-02 18:38:14 +02:00
|
|
|
uint8_t regval)
|
|
|
|
{
|
|
|
|
struct i2c_msg_s msg;
|
|
|
|
uint8_t buf[2];
|
|
|
|
int ret;
|
|
|
|
int retries = ACCEL_NUM_RETRIES;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* Setup for the transfer */
|
|
|
|
|
|
|
|
buf[0] = regaddr;
|
|
|
|
buf[1] = regval;
|
|
|
|
|
2017-08-24 17:54:23 +02:00
|
|
|
msg.frequency = CONFIG_KXTJ9_I2C_BUS_SPEED,
|
2016-08-02 18:38:14 +02:00
|
|
|
msg.addr = priv->address;
|
|
|
|
msg.flags = 0;
|
|
|
|
msg.buffer = buf;
|
|
|
|
msg.length = 2;
|
|
|
|
|
|
|
|
/* Then perform the transfer. */
|
|
|
|
|
|
|
|
ret = I2C_TRANSFER(priv->i2c, &msg, 1);
|
|
|
|
}
|
|
|
|
while (ret < 0 && retries-- > 0);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: kxtj9_soft_reset
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Configure the KXTJ9 device. Handler the SNIOC_CONFIGURE IOCTL command.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-02-18 15:08:41 +01:00
|
|
|
static void kxtj9_soft_reset(FAR struct kxtj9_dev_s *priv)
|
2016-08-02 18:38:14 +02:00
|
|
|
{
|
|
|
|
uint8_t wbuf[1];
|
|
|
|
|
|
|
|
/* Set accel into standby and known state by disabling PC1 */
|
|
|
|
|
|
|
|
wbuf[0] = KXTJ9_CTRL1_CONFIG;
|
|
|
|
kxtj9_reg_write(priv, CTRL_REG1, wbuf[0]);
|
|
|
|
|
|
|
|
/* Send the reset command */
|
|
|
|
|
|
|
|
kxtj9_reg_read(priv, CTRL_REG2, &wbuf[0], 1);
|
|
|
|
|
|
|
|
wbuf[0] |= KXTJ9_SRST;
|
|
|
|
kxtj9_reg_write(priv, CTRL_REG2, wbuf[0]);
|
|
|
|
|
|
|
|
/* Delay 10ms for the accel parts to re-initialize */
|
|
|
|
|
2017-10-06 18:15:01 +02:00
|
|
|
nxsig_usleep(10000);
|
2016-08-02 18:38:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: kxtj9_set_mode_standby
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Configure the KXTJ9 device. Handler the SNIOC_CONFIGURE IOCTL command.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-02-18 15:08:41 +01:00
|
|
|
static void kxtj9_set_mode_standby(FAR struct kxtj9_dev_s *priv)
|
2016-08-02 18:38:14 +02:00
|
|
|
{
|
|
|
|
uint8_t wbuf[1];
|
|
|
|
|
|
|
|
/* Set Accel into standby and known state by disabling PC1 */
|
|
|
|
|
|
|
|
wbuf[0] = KXTJ9_CTRL1_CONFIG;
|
|
|
|
kxtj9_reg_write(priv, CTRL_REG1, wbuf[0]);
|
|
|
|
|
|
|
|
/* Clear interrupts */
|
|
|
|
|
|
|
|
wbuf[0] = 0;
|
|
|
|
kxtj9_reg_write(priv, INT_CTRL1, wbuf[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: kxtj9_configure
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Configure the KXTJ9 device. Handler the SNIOC_CONFIGURE IOCTL command.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-02-18 15:08:41 +01:00
|
|
|
static int kxtj9_configure(FAR struct kxtj9_dev_s *priv, uint8_t odr)
|
2016-08-02 18:38:14 +02:00
|
|
|
{
|
|
|
|
uint8_t wbuf[0];
|
2020-03-30 23:06:15 +02:00
|
|
|
int ret;
|
2016-08-02 18:38:14 +02:00
|
|
|
|
2020-03-30 23:06:15 +02:00
|
|
|
ret = nxsem_wait_uninterruptible(&priv->exclsem);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2016-08-02 18:38:14 +02:00
|
|
|
|
|
|
|
kxtj9_soft_reset(priv);
|
|
|
|
kxtj9_set_mode_standby(priv);
|
|
|
|
|
|
|
|
/* Read WHO_AM_I register, should return 0x0a */
|
|
|
|
|
|
|
|
kxtj9_reg_read(priv, WHO_AM_I, &wbuf[0], 1);
|
|
|
|
if (wbuf[0] != WHO_AM_I_KXCJ9)
|
|
|
|
{
|
|
|
|
snerr("ERROR: Not KXCJ9 chipset, WHO_AM_I register is 0x%2x.\n",
|
|
|
|
wbuf[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ensure that PC1 is cleared before updating control registers */
|
|
|
|
|
|
|
|
kxtj9_reg_write(priv, CTRL_REG1, 0);
|
|
|
|
|
|
|
|
/* 12Bit Res and -2G~+2G range */
|
|
|
|
|
|
|
|
priv->ctrl_reg1 = KXTJ9_CTRL1_CONFIG;
|
|
|
|
kxtj9_reg_write(priv, CTRL_REG1, priv->ctrl_reg1);
|
|
|
|
|
|
|
|
priv->data_ctrl = odr;
|
|
|
|
kxtj9_reg_write(priv, DATA_CTRL, priv->data_ctrl);
|
|
|
|
|
|
|
|
/* In irq mode, populate INT_CTRL */
|
|
|
|
|
|
|
|
priv->int_ctrl = KXTJ9_IEN | KXTJ9_IEA | KXTJ9_IEL;
|
|
|
|
kxtj9_reg_write(priv, INT_CTRL1, priv->int_ctrl);
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&priv->exclsem);
|
2016-08-02 18:38:14 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: kxtj9_enable
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Enable or disable the KXTJ9 device. Handler the SNIOC_ENABLE and
|
|
|
|
* SNIOC_DISABLE IOCTL commands.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-02-18 15:08:41 +01:00
|
|
|
static int kxtj9_enable(FAR struct kxtj9_dev_s *priv, bool on)
|
2016-08-02 18:38:14 +02:00
|
|
|
{
|
|
|
|
uint8_t wbuf[1];
|
2020-03-30 23:06:15 +02:00
|
|
|
int ret;
|
2016-08-02 18:38:14 +02:00
|
|
|
|
2020-03-30 23:06:15 +02:00
|
|
|
ret = nxsem_wait_uninterruptible(&priv->exclsem);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2016-08-02 18:38:14 +02:00
|
|
|
|
|
|
|
if (!on && priv->power_enabled)
|
|
|
|
{
|
|
|
|
priv->ctrl_reg1 &= PC1_OFF;
|
|
|
|
kxtj9_reg_write(priv, CTRL_REG1, priv->ctrl_reg1);
|
|
|
|
|
|
|
|
priv->power_enabled = false;
|
|
|
|
sninfo("KXTJ9 in disabled mode\n");
|
|
|
|
}
|
|
|
|
else if (on && !priv->power_enabled)
|
|
|
|
{
|
|
|
|
/* Turn on outputs */
|
|
|
|
|
|
|
|
priv->ctrl_reg1 |= PC1_ON;
|
|
|
|
kxtj9_reg_write(priv, CTRL_REG1, priv->ctrl_reg1);
|
|
|
|
|
|
|
|
/* Clear initial interrupt if in irq mode */
|
|
|
|
|
|
|
|
kxtj9_reg_read(priv, INT_REL, wbuf, 1);
|
|
|
|
priv->power_enabled = true;
|
|
|
|
sninfo("KXTJ9 in operating mode\n");
|
|
|
|
}
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&priv->exclsem);
|
2016-08-02 18:38:14 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: kxtj9_read_sensor_data
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read sensor data. This supports the standard driver read() method.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-02-18 15:08:41 +01:00
|
|
|
static int kxtj9_read_sensor_data(FAR struct kxtj9_dev_s *priv,
|
2016-08-02 18:38:14 +02:00
|
|
|
FAR struct kxtj9_sensor_data *sensor_data)
|
|
|
|
{
|
|
|
|
int16_t acc_data[3];
|
|
|
|
uint8_t data;
|
2020-03-30 23:06:15 +02:00
|
|
|
int ret;
|
2016-08-02 18:38:14 +02:00
|
|
|
|
2020-03-30 23:06:15 +02:00
|
|
|
ret = nxsem_wait_uninterruptible(&priv->exclsem);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2016-08-02 18:38:14 +02:00
|
|
|
|
|
|
|
kxtj9_reg_read(priv, XOUT_L, (uint8_t *)acc_data, 6);
|
|
|
|
|
|
|
|
/* 12 bit resolution, get rid of the lowest 4 bits */
|
|
|
|
|
|
|
|
sensor_data->x = acc_data[0] >> 4;
|
|
|
|
sensor_data->y = acc_data[1] >> 4;
|
|
|
|
sensor_data->z = acc_data[2] >> 4;
|
|
|
|
|
|
|
|
/* Read INT_REL to clear interrupt status */
|
|
|
|
|
|
|
|
kxtj9_reg_read(priv, INT_REL, &data, 1);
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&priv->exclsem);
|
2017-03-30 15:38:37 +02:00
|
|
|
|
|
|
|
/* Feed sensor data to entropy pool */
|
|
|
|
|
|
|
|
add_sensor_randomness((acc_data[0] << 16) ^ (acc_data[1] << 8) ^
|
|
|
|
acc_data[2]);
|
|
|
|
|
2016-08-02 18:38:14 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-02-18 15:08:41 +01:00
|
|
|
* Name: kxtj9_read
|
2016-08-02 18:38:14 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The standard read method.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-02-18 15:08:41 +01:00
|
|
|
static ssize_t kxtj9_read(FAR struct file *filep, FAR char *buffer,
|
2016-08-03 01:32:47 +02:00
|
|
|
size_t buflen)
|
2016-08-02 18:38:14 +02:00
|
|
|
{
|
|
|
|
FAR struct inode *inode;
|
2017-02-18 15:08:41 +01:00
|
|
|
FAR struct kxtj9_dev_s *priv;
|
2016-08-03 01:32:47 +02:00
|
|
|
size_t nsamples;
|
|
|
|
size_t i;
|
2016-08-02 18:38:14 +02:00
|
|
|
int ret;
|
|
|
|
|
2016-08-03 01:32:47 +02:00
|
|
|
/* How many samples will fit in the buffer? */
|
|
|
|
|
|
|
|
nsamples = buflen / sizeof(struct kxtj9_sensor_data);
|
|
|
|
|
|
|
|
/* If the provided buffer is not large enough to return a single sample,
|
|
|
|
* then return an error.
|
2016-08-02 18:38:14 +02:00
|
|
|
*/
|
|
|
|
|
2016-08-03 01:32:47 +02:00
|
|
|
if (nsamples < 1)
|
2016-08-02 18:38:14 +02:00
|
|
|
{
|
2020-02-23 09:50:23 +01:00
|
|
|
snerr("ERROR: Buffer too small %lu < %u\n",
|
2016-08-02 18:38:14 +02:00
|
|
|
buflen, sizeof(struct kxtj9_sensor_data));
|
|
|
|
return (ssize_t)-EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUGASSERT(filep != NULL && filep->f_inode != NULL && buffer != NULL);
|
|
|
|
inode = filep->f_inode;
|
|
|
|
|
2017-02-18 15:08:41 +01:00
|
|
|
priv = (FAR struct kxtj9_dev_s *)inode->i_private;
|
2016-08-02 18:38:14 +02:00
|
|
|
DEBUGASSERT(priv != NULL && priv->i2c != NULL);
|
|
|
|
|
2016-08-03 01:32:47 +02:00
|
|
|
/* Return all of the samples that will fit in the user-provided buffer */
|
2016-08-02 18:38:14 +02:00
|
|
|
|
2016-08-03 01:32:47 +02:00
|
|
|
for (i = 0; i < nsamples; i++)
|
2016-08-02 18:38:14 +02:00
|
|
|
{
|
2016-08-03 01:32:47 +02:00
|
|
|
/* Get the next sample data */
|
|
|
|
|
2020-03-30 23:06:15 +02:00
|
|
|
ret = kxtj9_read_sensor_data(priv,
|
|
|
|
(FAR struct kxtj9_sensor_data *)buffer);
|
2016-08-03 01:32:47 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
snerr("ERROR: kxtj9_read_sensor_data failed: %d\n", ret);
|
|
|
|
return (ssize_t)ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set up for the next sample */
|
|
|
|
|
|
|
|
buffer += sizeof(struct kxtj9_sensor_data);
|
2016-08-02 18:38:14 +02:00
|
|
|
}
|
|
|
|
|
2016-08-03 01:32:47 +02:00
|
|
|
return (ssize_t)(nsamples * sizeof(struct kxtj9_sensor_data));
|
2016-08-02 18:38:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-02-18 15:08:41 +01:00
|
|
|
* Name: kxtj9_write
|
2016-08-02 18:38:14 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* A dummy write method.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-02-18 15:08:41 +01:00
|
|
|
static ssize_t kxtj9_write(FAR struct file *filep, FAR const char *buffer,
|
2016-08-02 18:38:14 +02:00
|
|
|
size_t buflen)
|
|
|
|
{
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-02-18 15:08:41 +01:00
|
|
|
* Name: kxtj9_ioctl
|
2016-08-02 18:38:14 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The standard ioctl method.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-02-18 15:08:41 +01:00
|
|
|
static int kxtj9_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
2016-08-02 18:38:14 +02:00
|
|
|
{
|
2016-08-02 18:46:19 +02:00
|
|
|
FAR struct inode *inode;
|
2017-02-18 15:08:41 +01:00
|
|
|
FAR struct kxtj9_dev_s *priv;
|
2016-08-02 18:46:19 +02:00
|
|
|
int ret;
|
2016-08-02 18:38:14 +02:00
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
|
2016-08-02 18:46:19 +02:00
|
|
|
DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
|
2016-08-02 18:38:14 +02:00
|
|
|
inode = filep->f_inode;
|
|
|
|
|
2017-02-18 15:08:41 +01:00
|
|
|
priv = (FAR struct kxtj9_dev_s *)inode->i_private;
|
2016-08-02 18:46:19 +02:00
|
|
|
DEBUGASSERT(priv != NULL && priv->i2c != NULL);
|
2016-08-02 18:38:14 +02:00
|
|
|
|
|
|
|
/* Handle ioctl commands */
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
/* Start converting. Arg: None. */
|
|
|
|
|
|
|
|
case SNIOC_ENABLE:
|
|
|
|
ret = kxtj9_enable(priv, true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Stop converting. Arg: None. */
|
|
|
|
|
|
|
|
case SNIOC_DISABLE:
|
|
|
|
ret = kxtj9_enable(priv, false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Configure the KXTJ9. Arg: enum kxtj9_odr_e value. */
|
|
|
|
|
|
|
|
case SNIOC_CONFIGURE:
|
|
|
|
{
|
2016-08-02 18:46:19 +02:00
|
|
|
DEBUGASSERT(arg <= UINT8_MAX);
|
2016-08-02 18:38:14 +02:00
|
|
|
ret = kxtj9_configure(priv, (uint8_t)arg);
|
|
|
|
sninfo("SNIOC_CONFIGURE: ODR=%u ret=%d\n",
|
|
|
|
(unsigned int)arg, ret);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Unrecognized commands */
|
|
|
|
|
|
|
|
default:
|
|
|
|
snerr("ERROR: Unrecognized cmd: %d arg: %lu\n", cmd, arg);
|
|
|
|
ret = -ENOTTY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-02-18 15:08:41 +01:00
|
|
|
* Name: kxtj9_register
|
2016-08-02 18:38:14 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2017-02-18 15:08:41 +01:00
|
|
|
* Register the KXTJ9 accelerometer device as 'devpath'.
|
2016-08-02 18:38:14 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* devpath - The full path to the driver to register, e.g., "/dev/accel0".
|
|
|
|
* i2c - An I2C driver instance.
|
2017-02-18 15:08:41 +01:00
|
|
|
* addr - The I2C address of the KXTJ9 accelerometer, gyroscope or
|
2016-08-02 18:38:14 +02:00
|
|
|
* magnetometer.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) on success; a negated errno value on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-02-18 15:08:41 +01:00
|
|
|
int kxtj9_register(FAR const char *devpath, FAR struct i2c_master_s *i2c,
|
2016-08-02 18:38:14 +02:00
|
|
|
uint8_t address)
|
|
|
|
{
|
2017-02-18 15:08:41 +01:00
|
|
|
FAR struct kxtj9_dev_s *priv;
|
2016-08-02 18:38:14 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
|
|
|
|
DEBUGASSERT(devpath != NULL && i2c != NULL);
|
|
|
|
|
|
|
|
/* Initialize the device's structure */
|
|
|
|
|
2017-02-18 15:08:41 +01:00
|
|
|
priv = (FAR struct kxtj9_dev_s *)kmm_zalloc(sizeof(struct kxtj9_dev_s));
|
2016-08-02 18:38:14 +02:00
|
|
|
if (priv == NULL)
|
|
|
|
{
|
|
|
|
snerr("ERROR: Failed to allocate driver instance\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->i2c = i2c;
|
|
|
|
priv->address = address;
|
2017-10-03 20:51:15 +02:00
|
|
|
nxsem_init(&priv->exclsem, 0, 1);
|
2016-08-02 18:38:14 +02:00
|
|
|
|
|
|
|
/* Register the character driver */
|
|
|
|
|
|
|
|
ret = register_driver(devpath, &g_fops, 0666, priv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
snerr("ERROR: Failed to register driver: %d\n", ret);
|
|
|
|
kmm_free(priv);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2017-08-24 17:54:23 +02:00
|
|
|
#endif /* CONFIG_I2C && CONFIG_SENSORS_KXTJ9 */
|