samples: sensor: adt7420: Add ADT7420 sample application

This simple application periodically prints the ambient temperature.
Optional support for threshold triggers is provided.

Signed-off-by: Michael Hennerich <michael.hennerich@analog.com>
This commit is contained in:
Michael Hennerich 2018-07-12 09:17:23 +02:00 committed by Maureen Helm
commit 92c1c2a24b
6 changed files with 180 additions and 0 deletions

View file

@ -0,0 +1,5 @@
include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
project(NONE)
FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})

View file

@ -0,0 +1,55 @@
.. _adt7420:
ADT7420: High accuracy digital I2C temperature sensor
#####################################################
Description
***********
This sample application periodically (1Hz) measures the ambient temperature
in degrees Celsius. The result is written to the console.
Optionally, it also shows how to use the upper and lower threshold triggers.
References
**********
- ADT7420: http://www.analog.com/adt7420
Wiring
*******
This sample uses the ADT7420 sensor controlled using the I2C interface.
Connect Supply: **VDD**, **GND** and Interface: **SDA**, **SCL**
and optionally connect the **INT** to a interrupt capable GPIO.
The supply voltage can be in the 2.7V to 5.5V range.
Depending on the baseboard used, the **SDA** and **SCL** lines require Pull-Up
resistors.
Building and Running
********************
This project outputs sensor data to the console. It requires an ADT7420
sensor. It should work with any platform featuring a I2C peripheral interface.
It does not work on QEMU.
In this example below the :ref:`nrf52_pca10040` board is used.
.. zephyr-app-commands::
:zephyr-app: samples/sensors/adt7420
:board: nrf52_pca10040
:goals: build flash
Sample Output
=============
.. code-block:: console
device is 0x20002b74, name is ADT7420
temperature 24.984375 C
temperature 24.968750 C
temperature 24.968750 C
temperature 24.968750 C
temperature 24.953125 C
<repeats endlessly>

View file

@ -0,0 +1,9 @@
#if defined(CONFIG_HAS_DTS_I2C_DEVICE)
#ifndef CONFIG_ADT7420_NAME
#define CONFIG_ADT7420_NAME ""
#define CONFIG_ADT7420_I2C_ADDR 0
#define CONFIG_ADT7420_I2C_MASTER_DEV_NAME ""
#endif
#endif /* CONFIG_HAS_DTS_I2C_DEVICE */

View file

@ -0,0 +1,4 @@
CONFIG_STDOUT_CONSOLE=y
CONFIG_I2C=y
CONFIG_SENSOR=y
CONFIG_ADT7420=y

View file

@ -0,0 +1,7 @@
sample:
name: ADT7420 Sensor Sample
tests:
test:
harness: sensor
tags: samples
depends_on: i2c

View file

@ -0,0 +1,100 @@
/*
* Copyright (c) 2018 Analog Devices Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr.h>
#include <device.h>
#include <sensor.h>
#include <stdio.h>
#include <misc/__assert.h>
K_SEM_DEFINE(sem, 0, 1);
static void trigger_handler(struct device *dev, struct sensor_trigger *trigger)
{
k_sem_give(&sem);
}
static int sensor_set_attribute(struct device *dev, enum sensor_channel chan,
enum sensor_attribute attr, int value)
{
struct sensor_value sensor_val;
int ret;
sensor_val.val1 = value / 1000000;
sensor_val.val2 = value % 1000000;
ret = sensor_attr_set(dev, chan, attr, &sensor_val);
if (ret) {
printf("sensor_attr_set failed ret %d\n", ret);
}
return ret;
}
static void process(struct device *dev)
{
struct sensor_value temp_val;
int ret;
/* Set upddate rate to 240 mHz */
sensor_set_attribute(dev, SENSOR_CHAN_AMBIENT_TEMP,
SENSOR_ATTR_SAMPLING_FREQUENCY, 240 * 1000);
if (IS_ENABLED(CONFIG_ADT7420_TRIGGER)) {
struct sensor_trigger trig = {
.type = SENSOR_TRIG_THRESHOLD,
.chan = SENSOR_CHAN_AMBIENT_TEMP,
};
/* Set lower and upper threshold to 10 and 30 °C */
sensor_set_attribute(dev, SENSOR_CHAN_AMBIENT_TEMP,
SENSOR_ATTR_UPPER_THRESH, 30 * 1000000);
sensor_set_attribute(dev, SENSOR_CHAN_AMBIENT_TEMP,
SENSOR_ATTR_LOWER_THRESH, 10 * 1000000);
if (sensor_trigger_set(dev, &trig, trigger_handler)) {
printf("Could not set trigger\n");
return;
}
}
while (1) {
if (IS_ENABLED(CONFIG_ADT7420_TRIGGER)) {
printf("Waiting for a threshold event\n");
k_sem_take(&sem, K_FOREVER);
}
ret = sensor_sample_fetch(dev);
if (ret) {
printf("sensor_sample_fetch failed ret %d\n", ret);
return;
}
ret = sensor_channel_get(dev, SENSOR_CHAN_AMBIENT_TEMP,
&temp_val);
if (ret) {
printf("sensor_channel_get failed ret %d\n", ret);
return;
}
printf("temperature %.6f C\n",
sensor_value_to_double(&temp_val));
if (!IS_ENABLED(CONFIG_ADT7420_TRIGGER)) {
k_sleep(1000);
}
}
}
void main(void)
{
struct device *dev = device_get_binding(CONFIG_ADT7420_NAME);
__ASSERT(dev != NULL, "Failed to get device binding");
printf("device is %p, name is %s\n", dev, dev->config->name);
process(dev);
}