sensor: bme280: Converting to using device tree

Convert the BME280 driver to use device tree and new DT_<COMPAT>
defines.

Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
This commit is contained in:
Kumar Gala 2019-01-09 10:38:25 -06:00 committed by Maureen Helm
commit be2b6f870d
10 changed files with 76 additions and 80 deletions

View file

@ -15,60 +15,6 @@ menuconfig BME280
if BME280
config BME280_DEV_NAME
string "BME280 device name"
default "BME280"
choice
prompt "BME280 device select"
default BME280_DEV_TYPE_I2C
help
Select interface to communicate with BME280 sensor. This sensor can communicate
with both SPI and I2C. I2C is the default, select SPI if you sensor is connected
via the SPI interface.
config BME280_DEV_TYPE_SPI
depends on SPI
bool "SPI"
config BME280_DEV_TYPE_I2C
depends on I2C
bool "I2C"
endchoice
config BME280_I2C_ADDR
hex "BME280 I2C slave address"
default 0x76
depends on BME280_DEV_TYPE_I2C
help
Specify the I2C slave address for the BME280.
0x76: Ground
0x77: VCC
config BME280_I2C_MASTER_DEV_NAME
string "I2C master where BME280 is connected"
depends on BME280_DEV_TYPE_I2C
default "I2C_0"
help
Specify the device name of the I2C master device to which BME280 is
connected.
config BME280_SPI_DEV_NAME
string "SPI device where BME280 is connected"
depends on BME280_DEV_TYPE_SPI
default "SPI_0"
help
Specify the device name of the SPI device to which BME280 is
connected.
config BME280_SPI_DEV_SLAVE
int "SPI Slave Select where BME280 is connected"
depends on BME280_DEV_TYPE_SPI
default 3
help
Specify the Slave Select pin of the SPI device to which BME280 CS is
connected.
menu "Attributes"
choice

View file

@ -14,9 +14,9 @@
#include <misc/byteorder.h>
#include <misc/__assert.h>
#ifdef CONFIG_BME280_DEV_TYPE_I2C
#ifdef DT_BOSCH_BME280_BUS_I2C
#include <i2c.h>
#elif defined CONFIG_BME280_DEV_TYPE_SPI
#elif defined DT_BOSCH_BME280_BUS_SPI
#include <spi.h>
#endif
#include <logging/log.h>
@ -30,10 +30,10 @@ static int bm280_reg_read(struct bme280_data *data,
u8_t start, u8_t *buf, int size)
{
#ifdef CONFIG_BME280_DEV_TYPE_I2C
#ifdef DT_BOSCH_BME280_BUS_I2C
return i2c_burst_read(data->i2c_master, data->i2c_slave_addr,
start, buf, size);
#elif defined CONFIG_BME280_DEV_TYPE_SPI
#elif defined DT_BOSCH_BME280_BUS_SPI
u8_t addr;
const struct spi_buf tx_buf = {
.buf = &addr,
@ -73,10 +73,10 @@ static int bm280_reg_read(struct bme280_data *data,
static int bm280_reg_write(struct bme280_data *data, u8_t reg, u8_t val)
{
#ifdef CONFIG_BME280_DEV_TYPE_I2C
#ifdef DT_BOSCH_BME280_BUS_I2C
return i2c_reg_write_byte(data->i2c_master, data->i2c_slave_addr,
reg, val);
#elif defined CONFIG_BME280_DEV_TYPE_SPI
#elif defined DT_BOSCH_BME280_BUS_SPI
u8_t cmd[2] = { reg & 0x7F, val };
const struct spi_buf tx_buf = {
.buf = cmd,
@ -333,20 +333,20 @@ static int bme280_chip_init(struct device *dev)
return 0;
}
#ifdef CONFIG_BME280_DEV_TYPE_SPI
#ifdef DT_BOSCH_BME280_BUS_SPI
static inline int bme280_spi_init(struct bme280_data *data)
{
data->spi = device_get_binding(CONFIG_BME280_SPI_DEV_NAME);
data->spi = device_get_binding(DT_BOSCH_BME280_0_BUS_NAME);
if (!data->spi) {
LOG_DBG("spi device not found: %s",
CONFIG_BME280_SPI_DEV_NAME);
DT_BOSCH_BME280_0_BUS_NAME);
return -EINVAL;
}
data->spi_cfg.operation = SPI_WORD_SET(8) | SPI_TRANSFER_MSB |
SPI_MODE_CPOL | SPI_MODE_CPHA;
data->spi_cfg.frequency = 8000000;
data->spi_cfg.slave = CONFIG_BME280_SPI_DEV_SLAVE;
data->spi_cfg.frequency = DT_BOSCH_BME280_0_SPI_MAX_FREQUENCY;
data->spi_cfg.slave = DT_BOSCH_BME280_0_BASE_ADDRESS;
return 0;
}
@ -356,19 +356,19 @@ int bme280_init(struct device *dev)
{
struct bme280_data *data = dev->driver_data;
#ifdef CONFIG_BME280_DEV_TYPE_I2C
data->i2c_master = device_get_binding(CONFIG_BME280_I2C_MASTER_DEV_NAME);
#ifdef DT_BOSCH_BME280_BUS_I2C
data->i2c_master = device_get_binding(DT_BOSCH_BME280_0_BUS_NAME);
if (!data->i2c_master) {
LOG_DBG("i2c master not found: %s",
CONFIG_BME280_I2C_MASTER_DEV_NAME);
DT_BOSCH_BME280_0_BUS_NAME);
return -EINVAL;
}
data->i2c_slave_addr = BME280_I2C_ADDR;
#elif defined CONFIG_BME280_DEV_TYPE_SPI
data->i2c_slave_addr = DT_BOSCH_BME280_0_BASE_ADDRESS;
#elif defined DT_BOSCH_BME280_BUS_SPI
if (bme280_spi_init(data) < 0) {
LOG_DBG("spi master not found: %s",
CONFIG_BME280_SPI_DEV_NAME);
DT_BOSCH_BME280_0_BUS_NAME);
return -EINVAL;
}
#endif
@ -382,6 +382,6 @@ int bme280_init(struct device *dev)
static struct bme280_data bme280_data;
DEVICE_AND_API_INIT(bme280, CONFIG_BME280_DEV_NAME, bme280_init, &bme280_data,
DEVICE_AND_API_INIT(bme280, DT_BOSCH_BME280_0_LABEL, bme280_init, &bme280_data,
NULL, POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY,
&bme280_api_funcs);

View file

@ -100,13 +100,11 @@
BME280_FILTER | \
BME280_SPI_3W_DISABLE)
#define BME280_I2C_ADDR CONFIG_BME280_I2C_ADDR
struct bme280_data {
#ifdef CONFIG_BME280_DEV_TYPE_I2C
#ifdef DT_BOSCH_BME280_BUS_I2C
struct device *i2c_master;
u16_t i2c_slave_addr;
#elif defined CONFIG_BME280_DEV_TYPE_SPI
#elif defined DT_BOSCH_BME280_BUS_SPI
struct device *spi;
struct spi_config spi_cfg;
#else

View file

@ -0,0 +1,20 @@
#
# Copyright (c) 2019, Linaro Limited
#
# SPDX-License-Identifier: Apache-2.0
#
---
title: BME280 Integrated environmental sensor
version: 0.1
description: >
This is a representation of the BME280 Integrated environmental sensor
inherits:
!include i2c-device.yaml
properties:
compatible:
constraint: "bosch,bme280"
...

View file

@ -0,0 +1,20 @@
#
# Copyright (c) 2019, Linaro Limited
#
# SPDX-License-Identifier: Apache-2.0
#
---
title: BME280 Integrated environmental sensor
version: 0.1
description: >
This is a representation of the BME280 Integrated environmental sensor
inherits:
!include spi-device.yaml
properties:
compatible:
constraint: "bosch,bme280"
...

View file

@ -13,4 +13,11 @@
label = "HDC1008";
drdy-gpios = <&gpio0 3 0>;
};
bmp280@76 {
compatible = "bosch,bme280";
reg = <0x76>;
label = "BMP280";
};
};

View file

@ -15,5 +15,4 @@ CONFIG_GROVE_LCD_RGB=y
# change these configs if you want to use different sensors
CONFIG_BME280=y
CONFIG_BME280_DEV_NAME="BMP280"
CONFIG_HDC1008=y

View file

@ -1,5 +1,4 @@
CONFIG_STDOUT_CONSOLE=y
CONFIG_SPI=y
CONFIG_BME280_DEV_TYPE_SPI=y
CONFIG_SENSOR=y
CONFIG_BME280=y

View file

@ -4,7 +4,7 @@ tests:
test:
harness: console
tags: sensors
depends_on: i2c
depends_on: i2c bme280
harness_config:
type: one_line
regex:
@ -13,7 +13,7 @@ tests:
test_spi:
harness: console
tags: sensors
depends_on: spi
depends_on: spi bme280
extra_args: "CONF_FILE=prj_spi.conf"
harness_config:
type: one_line

View file

@ -30,6 +30,13 @@
#define DT_APDS9960_GPIO_PIN_NUM 0
#endif
#ifndef DT_BOSCH_BME280_0_LABEL
#define DT_BOSCH_BME280_0_LABEL ""
#define DT_BOSCH_BME280_0_BASE_ADDRESS 0
#define DT_BOSCH_BME280_0_BUS_NAME ""
#define DT_BOSCH_BME280_BUS_I2C 1
#endif
#ifndef DT_CCS811_NAME
#define DT_CCS811_NAME ""
#define DT_CCS811_I2C_MASTER_DEV_NAME ""