samples: bme280: provide generic arduino I2C and SPI support

Adjust the documentation and devicetree overlays so the sample can be
built for any board with an Arduino I2C and SPI pinout, defaulting I2C
and SPI to y to make it easier to switch between the two without
requiring a pristine build.

The user has to choose an appropriate overlay or have a sensor built
in to the board.

Use the newly introduced DEVICE_DT_GET_ANY() in main.c to ask for a
bosch,bme280 without worrying over the details or exposing
DT_DRV_COMPAT-based functionality that is really meant for drivers.

Remove the no-longer-needed board specific overlay for nRF52840 DK;
this is covered by the generic Arduino overlays now.

Fix the datasheet link while we're here.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
This commit is contained in:
Martí Bolívar
2020-11-05 10:29:59 -08:00
committed by Anas Nashif
parent 4a381fce6b
commit d090751c3d
8 changed files with 181 additions and 56 deletions
+5 -9
View File
@@ -10,17 +10,13 @@ config LOG_PRINTK
config SENSOR_LOG_LEVEL
default 4
# Workaround for not being able to have commas in macro arguments
DT_COMPAT_BOSCH_BME280 := bosch,bme280
# Enable SPI support by default if there are any BME280 sensors
# on a SPI bus.
# Enable SPI and I2C support by default so that the sample works with
# the device connected either way. These defaults can be overridden if
# needed.
config SPI
default $(dt_compat_on_bus,$(DT_COMPAT_BOSCH_BME280),spi)
default y
# Enable I2C support by default if there are any BME280 sensors
# on an I2C bus.
config I2C
default $(dt_compat_on_bus,$(DT_COMPAT_BOSCH_BME280),i2c)
default y
source "Kconfig.zephyr"
+110 -19
View File
@@ -6,38 +6,129 @@ BME280 Humidity and Pressure Sensor
Overview
********
This sample application periodically reads temperature, pressure and humidity data from
the first available device that implements SENSOR_CHAN_AMBIENT_TEMP, SENSOR_CHAN_PRESS,
and SENSOR_CHAN_HUMIDITY. This sample checks the sensor in polling mode (without
interrupt trigger).
This sample shows how to use the Zephyr :ref:`sensor_api` API driver for the
`Bosch BME280`_ environmental sensor.
.. _Bosch BME280:
https://www.bosch-sensortec.com/products/environmental-sensors/humidity-sensors-bme280/`
The sample periodically reads temperature, pressure and humidity data from the
first available BME280 device discovered in the system. The sample checks the
sensor in polling mode (without interrupt trigger).
Building and Running
********************
This sample application uses an BME280 sensor connected to a board via I2C.
Connect the sensor pins according to the connection diagram given in the `bme280 datasheet`_
at page 38.
The sample can be configured to support BME280 sensors connected via either I2C
or SPI. Configuration is done via :ref:`devicetree <dt-guide>`. The devicetree
must have an enabled node with ``compatible = "bosch,bme280";``. See below for
examples and common configurations.
If the sensor is not built into your board, start by wiring the sensor pins
according to the connection diagram given in the `BME280 datasheet`_ at
page 38.
.. _BME280 datasheet:
https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bme280-ds002.pdf
Boards with a built-in BME280
=============================
Your board may have a BME280 node configured in its devicetree by default. Make
sure this node has ``status = "okay";``, then build and run with:
.. zephyr-app-commands::
:zephyr-app: samples/sensor/bme280
:board: nrf52840dk_nrf52840
:goals: flash
:compact:
:goals: build flash
:board: adafruit_feather_m0_basic_proto
BME280 via Arduino SPI pins
===========================
If you wired the sensor to a SPI peripheral on an Arduino header, build and
flash with:
.. zephyr-app-commands::
:zephyr-app: samples/sensor/bme280
:goals: build flash
:gen-args: -DDTC_OVERLAY_FILE=arduino_spi.overlay
The devicetree overlay :zephyr_file:`samples/sensor/bme280/arduino_spi.overlay`
works on any board with a properly configured Arduino pin-compatible SPI
peripheral.
BME280 via Arduino I2C pins
===========================
If you wired the sensor to an I2C peripheral on an Arduino header, build and
flash with:
.. zephyr-app-commands::
:zephyr-app: samples/sensor/bme280
:goals: build flash
:gen-args: -DDTC_OVERLAY_FILE=arduino_i2c.overlay
The devicetree overlay :zephyr_file:`samples/sensor/bme280/arduino_i2c.overlay`
works on any board with a properly configured Arduino pin-compatible I2C
peripheral.
Board-specific overlays
=======================
You can create a board-specific devicetree overlay for any board in the
:file:`boards` directory. This sample must provide
The build system uses these overlays by default when targeting those boards, so
no ``DTC_OVERLAY_FILE`` setting is needed when building and running.
For example, to build for the :ref:`adafruit_feather_m0_basic_proto` using the
:zephyr_file:`samples/sensor/bme280/boards/adafruit_feather_m0_basic_proto.overlay`
overlay provided with this sample:
.. zephyr-app-commands::
:zephyr-app: samples/sensor/bme280
:goals: build flash
:board: adafruit_feather_m0_basic_proto
Sample Output
=============
To check output of this sample , any serial console program can be used.
This example uses ``picocom`` on the serial port ``/dev/ttyACM0``:
.. code-block:: console
The sample prints output to the serial console. BME280 device driver messages
are also logged. Refer to your board's documentation for information on
connecting to its serial console.
$ sudo picocom -D /dev/ttyUSB0
Here is example output for the default application settings, assuming that only
one BME280 sensor is connected to the standard Arduino I2C pins:
.. code-block:: console
.. code-block:: none
temp: 27.950000; press: 100.571027; humidity: 61.014648
temp: 27.940000; press: 100.570269; humidity: 61.012695
temp: 27.950000; press: 100.570695; humidity: 61.002929
[00:00:00.379,760] <dbg> BME280.bme280_init: initializing "BME280_SPI" on bus "SPI_3"
[00:00:00.379,821] <dbg> BME280.bme280_init: bad chip id 0xff
[00:00:00.379,821] <dbg> BME280.bme280_init: initializing "BME280_I2C" on bus "I2C_0"
[00:00:00.380,340] <dbg> BME280.bme280_init: ID OK
[00:00:00.385,559] <dbg> BME280.bme280_init: BME280_I2C OK
*** Booting Zephyr OS build zephyr-v2.4.0-2940-gbb732ada394f ***
Found device BME280_I2C, getting sensor data
temp: 20.260000; press: 99.789019; humidity: 46.458984
temp: 20.260000; press: 99.789480; humidity: 46.424804
temp: 20.250000; press: 99.789246; humidity: 46.423828
.. _bme280 datasheet: https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BME280-DS002.pdf
Here is example output for the default application settings, assuming that two
different BME280 sensors are connected to the standard Arduino I2C and SPI pins:
.. code-block:: none
[00:00:00.377,777] <dbg> BME280.bme280_init: initializing "BME280_SPI" on bus "SPI_3"
[00:00:00.377,838] <dbg> BME280.bme280_init: ID OK
[00:00:00.379,608] <dbg> BME280.bme280_init: BME280_SPI OK
[00:00:00.379,638] <dbg> BME280.bme280_init: initializing "BME280_I2C" on bus "I2C_0"
[00:00:00.380,126] <dbg> BME280.bme280_init: ID OK
[00:00:00.385,345] <dbg> BME280.bme280_init: BME280_I2C OK
*** Booting Zephyr OS build zephyr-v2.4.0-2940-gbb732ada394f ***
Found device BME280_I2C, getting sensor data
temp: 20.150000; press: 99.857675; humidity: 46.447265
temp: 20.150000; press: 99.859121; humidity: 46.458984
temp: 20.150000; press: 99.859234; humidity: 46.469726
That the driver logs include a line saying ``BME280_I2C OK`` in both cases, but
``BME280_SPI OK`` is missing when that device is not connected.
+20
View File
@@ -0,0 +1,20 @@
/*
* Copyright (c) 2020, Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
/*
* Example configuration of a BME280 device on an Arduino I2C bus.
*
* Device address 0x77 is assumed. Your device may have a different
* address; check your device documentation if unsure.
*/
&arduino_i2c {
status = "okay";
bme280@77 {
compatible = "bosch,bme280";
reg = <0x77>;
label = "BME280_I2C";
};
};
+18
View File
@@ -0,0 +1,18 @@
/*
* Copyright (c) 2020, Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
/* Example configuration of a BME280 device on an Arduino SPI bus. */
&arduino_spi {
status = "okay";
cs-gpios = <&arduino_header 16 GPIO_ACTIVE_LOW>;
bme280@0 {
compatible = "bosch,bme280";
reg = <0>;
label = "BME280_SPI";
spi-max-frequency = <1000000>; /* conservatively set to 1MHz */
};
};
@@ -5,7 +5,7 @@
*/
&feather_i2c {
bme280@76 {
bme280: bme280@76 {
/* 0x76 - SDO <-> GND */
/* 0x77 - SDO <-> VCC */
compatible = "bosch,bme280";
@@ -1,13 +0,0 @@
/*
* Copyright (c) 2020, Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
&i2c0 {
bme280@77 {
compatible = "bosch,bme280";
reg = <0x77>;
label = "BME280";
};
};
+2 -2
View File
@@ -4,7 +4,7 @@ tests:
sample.sensor.bme280:
harness: console
tags: sensors
platform_allow: nrf52840dk_nrf52840 adafruit_feather_m0_basic_proto
platform_allow: adafruit_feather_m0_basic_proto
harness_config:
type: one_line
regex:
@@ -14,7 +14,7 @@ tests:
harness: console
tags: sensors
depends_on: spi bme280
extra_args: "CONF_FILE=prj_spi.conf"
extra_args: "DTC_OVERLAY_FILE=arduino_spi.overlay"
harness_config:
type: one_line
regex:
+25 -12
View File
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2012-2014 Wind River Systems, Inc.
* Copyright (c) 2021 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -9,25 +10,37 @@
#include <devicetree.h>
#include <drivers/sensor.h>
#define BME280 DT_INST(0, bosch_bme280)
/*
* Get a device structure from a devicetree node with compatible
* "bosch,bme280". (If there are multiple, just pick one.)
*/
static const struct device *get_bme280_device(void)
{
const struct device *dev = DEVICE_DT_GET_ANY(bosch_bme280);
#if DT_NODE_HAS_STATUS(BME280, okay)
#define BME280_LABEL DT_LABEL(BME280)
#else
#error Your devicetree has no enabled nodes with compatible "bosch,bme280"
#define BME280_LABEL "<none>"
#endif
if (dev == NULL) {
/* No such node, or the node does not have status "okay". */
printk("\nError: no device found.\n");
return NULL;
}
if (!device_is_ready(dev)) {
printk("\nError: Device \"%s\" is not ready; "
"check the driver initialization logs for errors.\n",
dev->name);
return NULL;
}
printk("Found device \"%s\", getting sensor data\n", dev->name);
return dev;
}
void main(void)
{
const struct device *dev = device_get_binding(BME280_LABEL);
const struct device *dev = get_bme280_device();
if (dev == NULL) {
printk("No device \"%s\" found; did initialization fail?\n",
BME280_LABEL);
return;
} else {
printk("Found device \"%s\"\n", BME280_LABEL);
}
while (1) {