fix nxstyle
This commit is contained in:
parent
c7cda607f4
commit
60b6199120
@ -1,43 +1,33 @@
|
||||
/****************************************************************************
|
||||
* drivers/sensors/msa301.c
|
||||
* Author: <davie08@qq.com>
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
* 3. Neither the name NuttX nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
* 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
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#include <errno.h>
|
||||
#include <assert.h>
|
||||
#include <debug.h>
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <nuttx/kmalloc.h>
|
||||
#include <nuttx/signal.h>
|
||||
#include <nuttx/fs/fs.h>
|
||||
@ -46,6 +36,10 @@
|
||||
|
||||
#if defined(CONFIG_I2C) && defined(CONFIG_SENSORS_MSA301)
|
||||
|
||||
/****************************************************************************
|
||||
* Private Types
|
||||
****************************************************************************/
|
||||
|
||||
struct msa301_dev_s
|
||||
{
|
||||
FAR struct i2c_master_s * i2c; /* I2C interface */
|
||||
@ -114,7 +108,8 @@ static int msa301_register(FAR const char * devpath,
|
||||
* Private Data
|
||||
****************************************************************************/
|
||||
|
||||
static const struct file_operations g_fops = {
|
||||
static const struct file_operations g_fops =
|
||||
{
|
||||
msa301_open,
|
||||
msa301_close,
|
||||
msa301_read,
|
||||
@ -128,7 +123,8 @@ static const struct file_operations g_fops = {
|
||||
# endif
|
||||
};
|
||||
|
||||
static const struct msa301_ops_s g_msa301_sensor_ops = {
|
||||
static const struct msa301_ops_s g_msa301_sensor_ops =
|
||||
{
|
||||
msa301_sensor_config,
|
||||
msa301_sensor_start,
|
||||
msa301_sensor_stop,
|
||||
@ -176,6 +172,7 @@ static int msa301_readreg(FAR struct msa301_dev_s *priv,
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: msa301_readreg8
|
||||
*
|
||||
@ -268,7 +265,8 @@ static int msa301_writereg8(FAR struct msa301_dev_s *priv,
|
||||
return OK;
|
||||
}
|
||||
|
||||
static int msa301_set_range(FAR struct msa301_dev_s *priv, msa301_range_t range)
|
||||
static int msa301_set_range(FAR struct msa301_dev_s *priv,
|
||||
msa301_range_t range)
|
||||
{
|
||||
uint8_t ctl;
|
||||
|
||||
@ -299,7 +297,8 @@ static int msa301_set_rate(FAR struct msa301_dev_s *priv, msa301_rate_t rate)
|
||||
return OK;
|
||||
}
|
||||
|
||||
static int msa301_set_powermode(FAR struct msa301_dev_s *priv, msa301_powermode_t mode)
|
||||
static int msa301_set_powermode(FAR struct msa301_dev_s *priv,
|
||||
msa301_powermode_t mode)
|
||||
{
|
||||
uint8_t ctl;
|
||||
|
||||
@ -311,7 +310,8 @@ static int msa301_set_powermode(FAR struct msa301_dev_s *priv, msa301_powermode_
|
||||
return OK;
|
||||
}
|
||||
|
||||
static int msa301_set_resolution(FAR struct msa301_dev_s *priv, msa301_resolution_t resolution)
|
||||
static int msa301_set_resolution(FAR struct msa301_dev_s *priv,
|
||||
msa301_resolution_t resolution)
|
||||
{
|
||||
uint8_t ctl;
|
||||
|
||||
@ -346,6 +346,7 @@ static int msa301_set_axis(FAR struct msa301_dev_s *priv, uint8_t enable)
|
||||
static int msa301_sensor_config(FAR struct msa301_dev_s *priv)
|
||||
{
|
||||
/* Sanity check */
|
||||
|
||||
DEBUGASSERT(priv != NULL);
|
||||
|
||||
msa301_set_resolution(priv, MSA301_RESOLUTION_14);
|
||||
@ -367,12 +368,15 @@ static int msa301_sensor_config(FAR struct msa301_dev_s *priv)
|
||||
static int msa301_sensor_start(FAR struct msa301_dev_s *priv)
|
||||
{
|
||||
/* Sanity check */
|
||||
|
||||
DEBUGASSERT(priv != NULL);
|
||||
|
||||
// Power normal
|
||||
/* Power normal */
|
||||
|
||||
msa301_set_powermode(priv, MSA301_NORMALMODE);
|
||||
|
||||
/* Enable the accelerometer */
|
||||
|
||||
msa301_set_axis(priv, 1);
|
||||
|
||||
up_mdelay(5);
|
||||
@ -393,12 +397,15 @@ static int msa301_sensor_start(FAR struct msa301_dev_s *priv)
|
||||
static int msa301_sensor_stop(FAR struct msa301_dev_s *priv)
|
||||
{
|
||||
/* Sanity check */
|
||||
|
||||
DEBUGASSERT(priv != NULL);
|
||||
|
||||
/* Disable the accelerometer */
|
||||
|
||||
msa301_set_axis(priv, 0);
|
||||
|
||||
// Power suspend
|
||||
/* Power suspend */
|
||||
|
||||
msa301_set_powermode(priv, MSA301_SUSPENDMODE);
|
||||
|
||||
sninfo("Stoping....");
|
||||
@ -420,9 +427,11 @@ static int msa301_sensor_stop(FAR struct msa301_dev_s *priv)
|
||||
|
||||
static int msa301_sensor_read(FAR struct msa301_dev_s *priv)
|
||||
{
|
||||
uint8_t xyz_value[6];
|
||||
float scale = 1;
|
||||
|
||||
DEBUGASSERT(priv != NULL);
|
||||
|
||||
uint8_t xyz_value[6] = { 0 };
|
||||
if (msa301_readreg(priv, MSA301_REG_OUT_X_L, xyz_value, 6) < 0)
|
||||
{
|
||||
return -EIO;
|
||||
@ -433,11 +442,11 @@ static int msa301_sensor_read(FAR struct msa301_dev_s *priv)
|
||||
priv->sensor_data.z_data = xyz_value[5] << 8 | xyz_value[4];
|
||||
|
||||
/* 14 bit resolution */
|
||||
|
||||
priv->sensor_data.x_data >>= 2;
|
||||
priv->sensor_data.y_data >>= 2;
|
||||
priv->sensor_data.z_data >>= 2;
|
||||
|
||||
float scale = 1;
|
||||
if (priv->range == MSA301_RANGE_2_G)
|
||||
{
|
||||
scale = 4096;
|
||||
@ -531,6 +540,7 @@ static ssize_t msa301_read(FAR struct file *filep,
|
||||
{
|
||||
FAR struct inode * inode;
|
||||
FAR struct msa301_dev_s *priv;
|
||||
int datalen = sizeof(struct msa301_sensor_data_s);
|
||||
|
||||
/* Sanity check */
|
||||
|
||||
@ -553,7 +563,6 @@ static ssize_t msa301_read(FAR struct file *filep,
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
int datalen = sizeof(struct msa301_sensor_data_s);
|
||||
memcpy(buffer, &priv->sensor_data, datalen);
|
||||
|
||||
return datalen;
|
||||
@ -618,6 +627,7 @@ static int msa301_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
||||
break;
|
||||
|
||||
/* Unrecognized commands */
|
||||
|
||||
default:
|
||||
snerr("ERROR: Unrecognized cmd: %d arg: %lu\n", cmd, arg);
|
||||
ret = -ENOTTY;
|
||||
@ -652,7 +662,8 @@ static int msa301_register(FAR const char * devpath,
|
||||
FAR const struct msa301_ops_s *ops)
|
||||
{
|
||||
FAR struct msa301_dev_s *priv;
|
||||
int ret;
|
||||
int ret;
|
||||
uint8_t id = 0;
|
||||
|
||||
/* Sanity check */
|
||||
|
||||
@ -673,7 +684,7 @@ static int msa301_register(FAR const char * devpath,
|
||||
priv->ops = ops;
|
||||
|
||||
/* ID check */
|
||||
uint8_t id = 0;
|
||||
|
||||
msa301_readreg8(priv, MSA301_REG_PARTID, &id);
|
||||
if (id != 0x13)
|
||||
{
|
||||
@ -683,6 +694,7 @@ static int msa301_register(FAR const char * devpath,
|
||||
}
|
||||
|
||||
/* Configure the device */
|
||||
|
||||
ret = priv->ops->config(priv);
|
||||
if (ret < 0)
|
||||
{
|
||||
@ -694,6 +706,7 @@ static int msa301_register(FAR const char * devpath,
|
||||
nxsem_init(&priv->exclsem, 0, 1);
|
||||
|
||||
/* Register the character driver */
|
||||
|
||||
ret = register_driver(devpath, &g_fops, 0666, priv);
|
||||
if (ret < 0)
|
||||
{
|
||||
@ -730,6 +743,8 @@ static int msa301_register(FAR const char * devpath,
|
||||
int msa301_sensor_register(FAR const char * devpath,
|
||||
FAR struct i2c_master_s *i2c)
|
||||
{
|
||||
return msa301_register(devpath, i2c, MSA301_ACCEL_ADDR0, &g_msa301_sensor_ops);
|
||||
return msa301_register(devpath, i2c,
|
||||
MSA301_ACCEL_ADDR0, &g_msa301_sensor_ops);
|
||||
}
|
||||
|
||||
#endif /* CONFIG_I2C && CONFIG_SENSORS_MSA301 */
|
||||
|
@ -1,10 +1,39 @@
|
||||
#ifndef __MSA301_H
|
||||
#define __MSA301_H
|
||||
/****************************************************************************
|
||||
* include/nuttx/sensors/msa301.h
|
||||
* msa301 Driver declaration
|
||||
*
|
||||
* 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_MSA301
|
||||
#define __INCLUDE_NUTTX_SENSORS_MSA301
|
||||
|
||||
#if defined(CONFIG_I2C) && defined(CONFIG_SENSORS_MSA301)
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#include <nuttx/sensors/ioctl.h>
|
||||
|
||||
#if defined(CONFIG_I2C) && defined(CONFIG_SENSORS_MSA301)
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
#define MSA301_ACCEL_ADDR0 0x26
|
||||
|
||||
@ -33,30 +62,38 @@
|
||||
#define MSA301_REG_TAPDUR 0x2A
|
||||
#define MSA301_REG_TAPTH 0x2B
|
||||
|
||||
/** The accelerometer ranges */
|
||||
typedef enum {
|
||||
MSA301_RANGE_2_G = 0x0, ///< +/- 2g
|
||||
MSA301_RANGE_4_G = 0x1, ///< +/- 4g
|
||||
MSA301_RANGE_8_G = 0x2, ///< +/- 8g
|
||||
MSA301_RANGE_16_G = 0x3, ///< +/- 16g
|
||||
/****************************************************************************
|
||||
* Public Types
|
||||
****************************************************************************/
|
||||
|
||||
/* The accelerometer ranges */
|
||||
|
||||
typedef enum
|
||||
{
|
||||
MSA301_RANGE_2_G = 0x0, /* +/- 2g */
|
||||
MSA301_RANGE_4_G = 0x1, /* +/- 4g */
|
||||
MSA301_RANGE_8_G = 0x2, /* +/- 8g */
|
||||
MSA301_RANGE_16_G = 0x3, /* +/- 16g */
|
||||
} msa301_range_t;
|
||||
|
||||
#define MSA301_CTL_RANGE_SHIFT (0x0)
|
||||
#define MSA301_CTL_RANGE_MASK (0x3<<0)
|
||||
|
||||
/** The accelerometer data rate */
|
||||
typedef enum {
|
||||
MSA301_RATE_1_HZ = 0, ///< 1 Hz
|
||||
MSA301_RATE_1_95_HZ = 1, ///< 1.95 Hz
|
||||
MSA301_RATE_3_9_HZ = 2, ///< 3.9 Hz
|
||||
MSA301_RATE_7_81_HZ = 3, ///< 7.81 Hz
|
||||
MSA301_RATE_15_63_HZ = 4, ///< 15.63 Hz
|
||||
MSA301_RATE_31_25_HZ = 5, ///< 31.25 Hz
|
||||
MSA301_RATE_62_5_HZ = 6, ///< 62.5 Hz
|
||||
MSA301_RATE_125_HZ = 7, ///< 125 Hz
|
||||
MSA301_RATE_250_HZ = 8, ///< 250 Hz
|
||||
MSA301_RATE_500_HZ = 9, ///< 500 Hz
|
||||
MSA301_RATE_1000_HZ = 10, ///< 1000 Hz
|
||||
/* The accelerometer data rate */
|
||||
|
||||
typedef enum
|
||||
{
|
||||
MSA301_RATE_1_HZ = 0, /* 1 Hz */
|
||||
MSA301_RATE_1_95_HZ = 1, /* 1.95 Hz */
|
||||
MSA301_RATE_3_9_HZ = 2, /* 3.9 Hz */
|
||||
MSA301_RATE_7_81_HZ = 3, /* 7.81 Hz */
|
||||
MSA301_RATE_15_63_HZ = 4, /* 15.63 Hz */
|
||||
MSA301_RATE_31_25_HZ = 5, /* 31.25 Hz */
|
||||
MSA301_RATE_62_5_HZ = 6, /* 62.5 Hz */
|
||||
MSA301_RATE_125_HZ = 7, /* 125 Hz */
|
||||
MSA301_RATE_250_HZ = 8, /* 250 Hz */
|
||||
MSA301_RATE_500_HZ = 9, /* 500 Hz */
|
||||
MSA301_RATE_1000_HZ = 10, /* 1000 Hz */
|
||||
} msa301_rate_t;
|
||||
|
||||
#define MSA301_CTL_RATE_SHIFT (0x0)
|
||||
@ -67,22 +104,26 @@ typedef enum {
|
||||
#define MSA301_CTL_AXIS_SHIFT (0x05)
|
||||
#define MSA301_CTL_AXIS_MASK (0x7<<5)
|
||||
|
||||
/** The accelerometer power mode */
|
||||
typedef enum {
|
||||
MSA301_NORMALMODE = 0x00, ///< Normal (high speed) mode
|
||||
MSA301_LOWPOWERMODE = 0x01, ///< Low power (slow speed) mode
|
||||
MSA301_SUSPENDMODE = 0x10, ///< Suspend (sleep) mode
|
||||
/* The accelerometer power mode */
|
||||
|
||||
typedef enum
|
||||
{
|
||||
MSA301_NORMALMODE = 0x00, /* Normal (high speed) mode */
|
||||
MSA301_LOWPOWERMODE = 0x01, /* Low power (slow speed) mode */
|
||||
MSA301_SUSPENDMODE = 0x10, /* Suspend (sleep) mode */
|
||||
} msa301_powermode_t;
|
||||
|
||||
#define MSA301_CTL_POWERMODE_SHIFT (0x6)
|
||||
#define MSA301_CTL_POWERMODE_MASK (0x3<<6)
|
||||
|
||||
/** The accelerometer read resolution */
|
||||
typedef enum {
|
||||
MSA301_RESOLUTION_14 = 0, ///< 14-bit resolution
|
||||
MSA301_RESOLUTION_12 = 1, ///< 12-bit resolution
|
||||
MSA301_RESOLUTION_10 = 2, ///< 10-bit resolution
|
||||
MSA301_RESOLUTION_8 = 3, ///< 8-bit resolution
|
||||
/* The accelerometer read resolution */
|
||||
|
||||
typedef enum
|
||||
{
|
||||
MSA301_RESOLUTION_14 = 0, /* 14-bit resolution */
|
||||
MSA301_RESOLUTION_12 = 1, /* 12-bit resolution */
|
||||
MSA301_RESOLUTION_10 = 2, /* 10-bit resolution */
|
||||
MSA301_RESOLUTION_8 = 3, /* 8-bit resolution */
|
||||
} msa301_resolution_t;
|
||||
|
||||
#define MSA301_CTL_RESOLUTION_SHIFT (0x2)
|
||||
@ -99,12 +140,15 @@ struct msa301_sensor_data_s
|
||||
float z_acc; /* Z axis acceleration */
|
||||
};
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Public Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
int msa301_sensor_register(FAR const char *devpath,
|
||||
FAR struct i2c_master_s *i2c
|
||||
);
|
||||
|
Loading…
Reference in New Issue
Block a user