2015-07-20 21:18:27 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/sensors/bmp180.c
|
|
|
|
*
|
2021-03-13 17:55:07 +08: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
|
2015-07-20 21:18:27 +08:00
|
|
|
*
|
2021-03-13 17:55:07 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2016-02-01 22:57:22 +08:00
|
|
|
*
|
2021-03-13 17:55:07 +08: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.
|
2015-07-20 21:18:27 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-03-13 17:55:07 +08:00
|
|
|
/* Character driver for the Freescale BMP1801 Barometer Sensor */
|
|
|
|
|
2015-07-20 21:18:27 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-09-06 11:02:33 +08:00
|
|
|
#include "bmp180_base.h"
|
2015-07-20 21:18:27 +08:00
|
|
|
|
2017-08-24 23:45:46 +08:00
|
|
|
#if defined(CONFIG_I2C) && defined(CONFIG_SENSORS_BMP180)
|
2015-07-20 21:18:27 +08:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Type Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Character driver methods */
|
|
|
|
|
2015-10-11 00:41:00 +08:00
|
|
|
static ssize_t bmp180_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen);
|
2015-07-20 21:18:27 +08:00
|
|
|
static ssize_t bmp180_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static const struct file_operations g_bmp180fops =
|
|
|
|
{
|
2022-04-12 18:16:13 +08:00
|
|
|
NULL, /* open */
|
|
|
|
NULL, /* close */
|
2015-07-20 21:18:27 +08:00
|
|
|
bmp180_read, /* read */
|
|
|
|
bmp180_write, /* write */
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: bmp180_read
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t bmp180_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct bmp180_dev_s *priv = inode->i_private;
|
|
|
|
FAR uint32_t *press = (FAR uint32_t *) buffer;
|
|
|
|
|
|
|
|
if (!buffer)
|
|
|
|
{
|
2016-06-12 08:05:29 +08:00
|
|
|
snerr("ERROR: Buffer is null\n");
|
2015-07-20 21:18:27 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-10-11 00:41:00 +08:00
|
|
|
if (buflen != 4)
|
2015-07-20 21:18:27 +08:00
|
|
|
{
|
2020-12-03 07:36:40 +08:00
|
|
|
snerr("ERROR: You can't read something "
|
|
|
|
"other than 32 bits (4 bytes)\n");
|
2015-07-20 21:18:27 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the pressure compensated */
|
|
|
|
|
2023-09-06 11:02:33 +08:00
|
|
|
*press = (int32_t) bmp180_getpressure(priv, NULL);
|
2015-07-20 21:18:27 +08:00
|
|
|
|
|
|
|
/* Return size of uint32_t (4 bytes) */
|
|
|
|
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: bmp180_write
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t bmp180_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen)
|
|
|
|
{
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: bmp180_register
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Register the BMP180 character device as 'devpath'
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* devpath - The full path to the driver to register. E.g., "/dev/press0"
|
|
|
|
* i2c - An instance of the I2C interface to use to communicate with
|
|
|
|
* BMP180
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) on success; a negated errno value on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-01-30 22:36:47 +08:00
|
|
|
int bmp180_register(FAR const char *devpath, FAR struct i2c_master_s *i2c)
|
2015-07-20 21:18:27 +08:00
|
|
|
{
|
|
|
|
FAR struct bmp180_dev_s *priv;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Initialize the BMP180 device structure */
|
|
|
|
|
2023-09-06 11:02:33 +08:00
|
|
|
priv = (FAR struct bmp180_dev_s *)kmm_malloc(sizeof(struct bmp180_dev_s));
|
2015-07-20 21:18:27 +08:00
|
|
|
if (!priv)
|
|
|
|
{
|
2016-06-12 08:05:29 +08:00
|
|
|
snerr("ERROR: Failed to allocate instance\n");
|
2015-07-20 21:18:27 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->i2c = i2c;
|
|
|
|
priv->addr = BMP180_ADDR;
|
|
|
|
priv->freq = BMP180_FREQ;
|
|
|
|
|
|
|
|
/* Check Device ID */
|
|
|
|
|
|
|
|
ret = bmp180_checkid(priv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 08:05:29 +08:00
|
|
|
snerr("ERROR: Failed to register driver: %d\n", ret);
|
2015-07-20 21:18:27 +08:00
|
|
|
kmm_free(priv);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the coefficient value */
|
|
|
|
|
|
|
|
bmp180_updatecaldata(priv);
|
|
|
|
|
|
|
|
/* Register the character driver */
|
|
|
|
|
|
|
|
ret = register_driver(devpath, &g_bmp180fops, 0666, priv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 08:05:29 +08:00
|
|
|
snerr("ERROR: Failed to register driver: %d\n", ret);
|
2015-07-20 21:18:27 +08:00
|
|
|
kmm_free(priv);
|
|
|
|
}
|
|
|
|
|
2016-06-12 01:59:51 +08:00
|
|
|
sninfo("BMP180 driver loaded successfully!\n");
|
2015-07-20 21:18:27 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-08-24 23:45:46 +08:00
|
|
|
#endif /* CONFIG_I2C && CONFIG_SENSORS_BMP180 */
|