fix nxstyle

This commit is contained in:
daviepeng 2021-08-27 11:03:22 +08:00 committed by Xiang Xiao
parent c7cda607f4
commit 60b6199120
2 changed files with 134 additions and 75 deletions

View File

@ -1,43 +1,33 @@
/**************************************************************************** /****************************************************************************
* drivers/sensors/msa301.c * 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 * Licensed to the Apache Software Foundation (ASF) under one or more
* notice, this list of conditions and the following disclaimer. * contributor license agreements. See the NOTICE file distributed with
* 2. Redistributions in binary form must reproduce the above copyright * this work for additional information regarding copyright ownership. The
* notice, this list of conditions and the following disclaimer in * ASF licenses this file to you under the Apache License, Version 2.0 (the
* the documentation and/or other materials provided with the * "License"); you may not use this file except in compliance with the
* distribution. * License. You may obtain a copy of the License at
* 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.
* *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * http://www.apache.org/licenses/LICENSE-2.0
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * Unless required by applicable law or agreed to in writing, software
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * License for the specific language governing permissions and limitations
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * under the License.
* 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.
* *
****************************************************************************/ ****************************************************************************/
#include <nuttx/config.h> /****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <errno.h> #include <errno.h>
#include <assert.h>
#include <debug.h> #include <debug.h>
#include <assert.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <nuttx/kmalloc.h> #include <nuttx/kmalloc.h>
#include <nuttx/signal.h> #include <nuttx/signal.h>
#include <nuttx/fs/fs.h> #include <nuttx/fs/fs.h>
@ -46,6 +36,10 @@
#if defined(CONFIG_I2C) && defined(CONFIG_SENSORS_MSA301) #if defined(CONFIG_I2C) && defined(CONFIG_SENSORS_MSA301)
/****************************************************************************
* Private Types
****************************************************************************/
struct msa301_dev_s struct msa301_dev_s
{ {
FAR struct i2c_master_s * i2c; /* I2C interface */ FAR struct i2c_master_s * i2c; /* I2C interface */
@ -114,7 +108,8 @@ static int msa301_register(FAR const char * devpath,
* Private Data * Private Data
****************************************************************************/ ****************************************************************************/
static const struct file_operations g_fops = { static const struct file_operations g_fops =
{
msa301_open, msa301_open,
msa301_close, msa301_close,
msa301_read, msa301_read,
@ -128,7 +123,8 @@ static const struct file_operations g_fops = {
# endif # 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_config,
msa301_sensor_start, msa301_sensor_start,
msa301_sensor_stop, msa301_sensor_stop,
@ -176,6 +172,7 @@ static int msa301_readreg(FAR struct msa301_dev_s *priv,
return OK; return OK;
} }
/**************************************************************************** /****************************************************************************
* Name: msa301_readreg8 * Name: msa301_readreg8
* *
@ -268,7 +265,8 @@ static int msa301_writereg8(FAR struct msa301_dev_s *priv,
return OK; 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; uint8_t ctl;
@ -299,7 +297,8 @@ static int msa301_set_rate(FAR struct msa301_dev_s *priv, msa301_rate_t rate)
return OK; 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; uint8_t ctl;
@ -311,7 +310,8 @@ static int msa301_set_powermode(FAR struct msa301_dev_s *priv, msa301_powermode_
return OK; 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; 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) static int msa301_sensor_config(FAR struct msa301_dev_s *priv)
{ {
/* Sanity check */ /* Sanity check */
DEBUGASSERT(priv != NULL); DEBUGASSERT(priv != NULL);
msa301_set_resolution(priv, MSA301_RESOLUTION_14); 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) static int msa301_sensor_start(FAR struct msa301_dev_s *priv)
{ {
/* Sanity check */ /* Sanity check */
DEBUGASSERT(priv != NULL); DEBUGASSERT(priv != NULL);
// Power normal /* Power normal */
msa301_set_powermode(priv, MSA301_NORMALMODE); msa301_set_powermode(priv, MSA301_NORMALMODE);
/* Enable the accelerometer */ /* Enable the accelerometer */
msa301_set_axis(priv, 1); msa301_set_axis(priv, 1);
up_mdelay(5); 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) static int msa301_sensor_stop(FAR struct msa301_dev_s *priv)
{ {
/* Sanity check */ /* Sanity check */
DEBUGASSERT(priv != NULL); DEBUGASSERT(priv != NULL);
/* Disable the accelerometer */ /* Disable the accelerometer */
msa301_set_axis(priv, 0); msa301_set_axis(priv, 0);
// Power suspend /* Power suspend */
msa301_set_powermode(priv, MSA301_SUSPENDMODE); msa301_set_powermode(priv, MSA301_SUSPENDMODE);
sninfo("Stoping...."); 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) static int msa301_sensor_read(FAR struct msa301_dev_s *priv)
{ {
uint8_t xyz_value[6];
float scale = 1;
DEBUGASSERT(priv != NULL); DEBUGASSERT(priv != NULL);
uint8_t xyz_value[6] = { 0 };
if (msa301_readreg(priv, MSA301_REG_OUT_X_L, xyz_value, 6) < 0) if (msa301_readreg(priv, MSA301_REG_OUT_X_L, xyz_value, 6) < 0)
{ {
return -EIO; 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]; priv->sensor_data.z_data = xyz_value[5] << 8 | xyz_value[4];
/* 14 bit resolution */ /* 14 bit resolution */
priv->sensor_data.x_data >>= 2; priv->sensor_data.x_data >>= 2;
priv->sensor_data.y_data >>= 2; priv->sensor_data.y_data >>= 2;
priv->sensor_data.z_data >>= 2; priv->sensor_data.z_data >>= 2;
float scale = 1;
if (priv->range == MSA301_RANGE_2_G) if (priv->range == MSA301_RANGE_2_G)
{ {
scale = 4096; scale = 4096;
@ -531,6 +540,7 @@ static ssize_t msa301_read(FAR struct file *filep,
{ {
FAR struct inode * inode; FAR struct inode * inode;
FAR struct msa301_dev_s *priv; FAR struct msa301_dev_s *priv;
int datalen = sizeof(struct msa301_sensor_data_s);
/* Sanity check */ /* Sanity check */
@ -553,7 +563,6 @@ static ssize_t msa301_read(FAR struct file *filep,
return -EIO; return -EIO;
} }
int datalen = sizeof(struct msa301_sensor_data_s);
memcpy(buffer, &priv->sensor_data, datalen); memcpy(buffer, &priv->sensor_data, datalen);
return datalen; return datalen;
@ -618,6 +627,7 @@ static int msa301_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
break; break;
/* Unrecognized commands */ /* Unrecognized commands */
default: default:
snerr("ERROR: Unrecognized cmd: %d arg: %lu\n", cmd, arg); snerr("ERROR: Unrecognized cmd: %d arg: %lu\n", cmd, arg);
ret = -ENOTTY; ret = -ENOTTY;
@ -652,7 +662,8 @@ static int msa301_register(FAR const char * devpath,
FAR const struct msa301_ops_s *ops) FAR const struct msa301_ops_s *ops)
{ {
FAR struct msa301_dev_s *priv; FAR struct msa301_dev_s *priv;
int ret; int ret;
uint8_t id = 0;
/* Sanity check */ /* Sanity check */
@ -673,7 +684,7 @@ static int msa301_register(FAR const char * devpath,
priv->ops = ops; priv->ops = ops;
/* ID check */ /* ID check */
uint8_t id = 0;
msa301_readreg8(priv, MSA301_REG_PARTID, &id); msa301_readreg8(priv, MSA301_REG_PARTID, &id);
if (id != 0x13) if (id != 0x13)
{ {
@ -683,6 +694,7 @@ static int msa301_register(FAR const char * devpath,
} }
/* Configure the device */ /* Configure the device */
ret = priv->ops->config(priv); ret = priv->ops->config(priv);
if (ret < 0) if (ret < 0)
{ {
@ -694,6 +706,7 @@ static int msa301_register(FAR const char * devpath,
nxsem_init(&priv->exclsem, 0, 1); nxsem_init(&priv->exclsem, 0, 1);
/* Register the character driver */ /* Register the character driver */
ret = register_driver(devpath, &g_fops, 0666, priv); ret = register_driver(devpath, &g_fops, 0666, priv);
if (ret < 0) if (ret < 0)
{ {
@ -730,6 +743,8 @@ static int msa301_register(FAR const char * devpath,
int msa301_sensor_register(FAR const char * devpath, int msa301_sensor_register(FAR const char * devpath,
FAR struct i2c_master_s *i2c) 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 */ #endif /* CONFIG_I2C && CONFIG_SENSORS_MSA301 */

View File

@ -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/config.h>
#include <nuttx/sensors/ioctl.h> #include <nuttx/sensors/ioctl.h>
#if defined(CONFIG_I2C) && defined(CONFIG_SENSORS_MSA301) /****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define MSA301_ACCEL_ADDR0 0x26 #define MSA301_ACCEL_ADDR0 0x26
@ -33,30 +62,38 @@
#define MSA301_REG_TAPDUR 0x2A #define MSA301_REG_TAPDUR 0x2A
#define MSA301_REG_TAPTH 0x2B #define MSA301_REG_TAPTH 0x2B
/** The accelerometer ranges */ /****************************************************************************
typedef enum { * Public Types
MSA301_RANGE_2_G = 0x0, ///< +/- 2g ****************************************************************************/
MSA301_RANGE_4_G = 0x1, ///< +/- 4g
MSA301_RANGE_8_G = 0x2, ///< +/- 8g /* The accelerometer ranges */
MSA301_RANGE_16_G = 0x3, ///< +/- 16g
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; } msa301_range_t;
#define MSA301_CTL_RANGE_SHIFT (0x0) #define MSA301_CTL_RANGE_SHIFT (0x0)
#define MSA301_CTL_RANGE_MASK (0x3<<0) #define MSA301_CTL_RANGE_MASK (0x3<<0)
/** The accelerometer data rate */ /* The accelerometer data rate */
typedef enum {
MSA301_RATE_1_HZ = 0, ///< 1 Hz typedef enum
MSA301_RATE_1_95_HZ = 1, ///< 1.95 Hz {
MSA301_RATE_3_9_HZ = 2, ///< 3.9 Hz MSA301_RATE_1_HZ = 0, /* 1 Hz */
MSA301_RATE_7_81_HZ = 3, ///< 7.81 Hz MSA301_RATE_1_95_HZ = 1, /* 1.95 Hz */
MSA301_RATE_15_63_HZ = 4, ///< 15.63 Hz MSA301_RATE_3_9_HZ = 2, /* 3.9 Hz */
MSA301_RATE_31_25_HZ = 5, ///< 31.25 Hz MSA301_RATE_7_81_HZ = 3, /* 7.81 Hz */
MSA301_RATE_62_5_HZ = 6, ///< 62.5 Hz MSA301_RATE_15_63_HZ = 4, /* 15.63 Hz */
MSA301_RATE_125_HZ = 7, ///< 125 Hz MSA301_RATE_31_25_HZ = 5, /* 31.25 Hz */
MSA301_RATE_250_HZ = 8, ///< 250 Hz MSA301_RATE_62_5_HZ = 6, /* 62.5 Hz */
MSA301_RATE_500_HZ = 9, ///< 500 Hz MSA301_RATE_125_HZ = 7, /* 125 Hz */
MSA301_RATE_1000_HZ = 10, ///< 1000 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; } msa301_rate_t;
#define MSA301_CTL_RATE_SHIFT (0x0) #define MSA301_CTL_RATE_SHIFT (0x0)
@ -67,22 +104,26 @@ typedef enum {
#define MSA301_CTL_AXIS_SHIFT (0x05) #define MSA301_CTL_AXIS_SHIFT (0x05)
#define MSA301_CTL_AXIS_MASK (0x7<<5) #define MSA301_CTL_AXIS_MASK (0x7<<5)
/** The accelerometer power mode */ /* The accelerometer power mode */
typedef enum {
MSA301_NORMALMODE = 0x00, ///< Normal (high speed) mode typedef enum
MSA301_LOWPOWERMODE = 0x01, ///< Low power (slow speed) mode {
MSA301_SUSPENDMODE = 0x10, ///< Suspend (sleep) mode MSA301_NORMALMODE = 0x00, /* Normal (high speed) mode */
MSA301_LOWPOWERMODE = 0x01, /* Low power (slow speed) mode */
MSA301_SUSPENDMODE = 0x10, /* Suspend (sleep) mode */
} msa301_powermode_t; } msa301_powermode_t;
#define MSA301_CTL_POWERMODE_SHIFT (0x6) #define MSA301_CTL_POWERMODE_SHIFT (0x6)
#define MSA301_CTL_POWERMODE_MASK (0x3<<6) #define MSA301_CTL_POWERMODE_MASK (0x3<<6)
/** The accelerometer read resolution */ /* The accelerometer read resolution */
typedef enum {
MSA301_RESOLUTION_14 = 0, ///< 14-bit resolution typedef enum
MSA301_RESOLUTION_12 = 1, ///< 12-bit resolution {
MSA301_RESOLUTION_10 = 2, ///< 10-bit resolution MSA301_RESOLUTION_14 = 0, /* 14-bit resolution */
MSA301_RESOLUTION_8 = 3, ///< 8-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; } msa301_resolution_t;
#define MSA301_CTL_RESOLUTION_SHIFT (0x2) #define MSA301_CTL_RESOLUTION_SHIFT (0x2)
@ -99,12 +140,15 @@ struct msa301_sensor_data_s
float z_acc; /* Z axis acceleration */ float z_acc; /* Z axis acceleration */
}; };
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C"
{ {
#endif #endif
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
int msa301_sensor_register(FAR const char *devpath, int msa301_sensor_register(FAR const char *devpath,
FAR struct i2c_master_s *i2c FAR struct i2c_master_s *i2c
); );