Now that device_api attribute is unmodified at runtime, as well as all the other attributes, it is possible to switch all device driver instance to be constant. A coccinelle rule is used for this: @r_const_dev_1 disable optional_qualifier @ @@ -struct device * +const struct device * @r_const_dev_2 disable optional_qualifier @ @@ -struct device * const +const struct device * Fixes #27399 Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
48 lines
1.1 KiB
C
48 lines
1.1 KiB
C
/*
|
|
* Copyright (c) 2012-2014 Wind River Systems, Inc.
|
|
*
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
#include <zephyr.h>
|
|
#include <device.h>
|
|
#include <devicetree.h>
|
|
#include <drivers/sensor.h>
|
|
|
|
#define BME280 DT_INST(0, 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
|
|
|
|
void main(void)
|
|
{
|
|
const struct device *dev = device_get_binding(BME280_LABEL);
|
|
|
|
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) {
|
|
struct sensor_value temp, press, humidity;
|
|
|
|
sensor_sample_fetch(dev);
|
|
sensor_channel_get(dev, SENSOR_CHAN_AMBIENT_TEMP, &temp);
|
|
sensor_channel_get(dev, SENSOR_CHAN_PRESS, &press);
|
|
sensor_channel_get(dev, SENSOR_CHAN_HUMIDITY, &humidity);
|
|
|
|
printk("temp: %d.%06d; press: %d.%06d; humidity: %d.%06d\n",
|
|
temp.val1, temp.val2, press.val1, press.val2,
|
|
humidity.val1, humidity.val2);
|
|
|
|
k_sleep(K_MSEC(1000));
|
|
}
|
|
}
|