Topics

[RFC PATCH 6/6] samples: mcp9808: support two devices


Vlad Dogaru <vlad.dogaru@...>
 

Update the sample app to describe and support two MCP9808 devices. This
is meant to illustrate the new style device configuration.

Change-Id: I850c7ac307a6a3932bc02c7586e3d0fc03475152
Signed-off-by: Vlad Dogaru <vlad.dogaru(a)intel.com>
---
samples/sensor/mcp9808/Makefile | 3 +-
samples/sensor/mcp9808/prj.conf | 4 ++-
samples/sensor/mcp9808/src/Makefile | 2 +-
samples/sensor/mcp9808/src/devices.c | 30 ++++++++++++++++
samples/sensor/mcp9808/src/main.c | 66 +++++++++++++++++++++++-------------
5 files changed, 78 insertions(+), 27 deletions(-)
create mode 100644 samples/sensor/mcp9808/src/devices.c

diff --git a/samples/sensor/mcp9808/Makefile b/samples/sensor/mcp9808/Makefile
index 65b4d36..06a0183 100644
--- a/samples/sensor/mcp9808/Makefile
+++ b/samples/sensor/mcp9808/Makefile
@@ -1,5 +1,6 @@
KERNEL_TYPE = nano
-BOARD ?= arduino_101_sss
+BOARD ?= quark_d2000_crb
CONF_FILE = prj.conf
+CFLAGS += -I${ZEPHYR_BASE}

include ${ZEPHYR_BASE}/Makefile.inc
diff --git a/samples/sensor/mcp9808/prj.conf b/samples/sensor/mcp9808/prj.conf
index a352a2c..067bb35 100644
--- a/samples/sensor/mcp9808/prj.conf
+++ b/samples/sensor/mcp9808/prj.conf
@@ -10,5 +10,7 @@ CONFIG_NANO_TIMEOUTS=y

CONFIG_SENSOR=y
CONFIG_SENSOR_DEBUG=y
+CONFIG_SENSOR_DELAYED_WORK=y
+
CONFIG_MCP9808=y
-CONFIG_MCP9808_TRIGGER_GLOBAL=y
+CONFIG_MCP9808_TRIGGER=y
diff --git a/samples/sensor/mcp9808/src/Makefile b/samples/sensor/mcp9808/src/Makefile
index b666967..7fdc3fe 100644
--- a/samples/sensor/mcp9808/src/Makefile
+++ b/samples/sensor/mcp9808/src/Makefile
@@ -1 +1 @@
-obj-y += main.o
+obj-y += main.o devices.o
diff --git a/samples/sensor/mcp9808/src/devices.c b/samples/sensor/mcp9808/src/devices.c
new file mode 100644
index 0000000..a620576
--- /dev/null
+++ b/samples/sensor/mcp9808/src/devices.c
@@ -0,0 +1,30 @@
+#include <i2c.h>
+#include <gpio.h>
+#include <device.h>
+
+#include "drivers/sensor/sensor_mcp9808.h"
+
+#ifdef CONFIG_MCP9808_TRIGGER
+static __stack char mcp9808_fiber_stack[1024];
+#endif
+static struct mcp9808_data mcp9808_0_data;
+static struct mcp9808_config mcp9808_0_config = {
+ I2C_CLIENT("I2C0", 0x18),
+#ifdef CONFIG_MCP9808_TRIGGER
+ GPIO_PIN("GPIO_0", 8),
+ SENSOR_TRIG_OWN_FIBER(mcp9808_fiber_stack, 10),
+#endif
+};
+DEVICE_INIT(mcp9808_0, "MCP9808_0", mcp9808_init, &mcp9808_0_data,
+ &mcp9808_0_config, SECONDARY, 70);
+
+static struct mcp9808_data mcp9808_1_data;
+static struct mcp9808_config mcp9808_1_config = {
+ I2C_CLIENT("I2C0", 0x19),
+#ifdef CONFIG_MCP9808_TRIGGER
+ GPIO_PIN("GPIO_0", 9),
+ SENSOR_TRIG_GLOBAL_FIBER,
+#endif
+};
+DEVICE_INIT(mcp9808_1, "MCP9808_1", mcp9808_init, &mcp9808_1_data,
+ &mcp9808_1_config, SECONDARY, 70);
diff --git a/samples/sensor/mcp9808/src/main.c b/samples/sensor/mcp9808/src/main.c
index 8275689..acdc5bb 100644
--- a/samples/sensor/mcp9808/src/main.c
+++ b/samples/sensor/mcp9808/src/main.c
@@ -28,6 +28,8 @@
#define PRINT printk
#endif

+#define DEBUG
+
#ifdef CONFIG_MCP9808_TRIGGER
static void trigger_handler(struct device *dev, struct sensor_trigger *trig)
{
@@ -36,49 +38,65 @@ static void trigger_handler(struct device *dev, struct sensor_trigger *trig)
sensor_sample_fetch(dev);
sensor_channel_get(dev, SENSOR_CHAN_TEMP, &temp);

- PRINT("trigger fired, temp %d.%06d\n", temp.val1, temp.val2);
+ PRINT("trigger fired on %s, temp %d.%06d\n", dev->config->name,
+ temp.val1, temp.val2);
}
-#endif

-void main(void)
+static void trigger_setup(struct device *dev, int thresh_temp)
{
- struct device *dev = device_get_binding("MCP9808");
-
- if (dev == NULL) {
- printk("device not found. aborting test.\n");
- return;
- }
-
-#ifdef DEBUG
- PRINT("dev %p\n", dev);
- PRINT("dev %p name %s\n", dev, dev->config->name);
-#endif
-
-#ifdef CONFIG_MCP9808_TRIGGER
struct sensor_value val;
struct sensor_trigger trig;
+ int ret;

val.type = SENSOR_TYPE_INT;
- val.val1 = 26;
+ val.val1 = thresh_temp;

- sensor_attr_set(dev, SENSOR_CHAN_TEMP,
- SENSOR_ATTR_UPPER_THRESH, &val);
+ ret = sensor_attr_set(dev, SENSOR_CHAN_TEMP,
+ SENSOR_ATTR_UPPER_THRESH, &val);
+ if (ret < 0) {
+ PRINT("failed to set threshold attribute on %s\n",
+ dev->config->name);
+ return;
+ }

trig.type = SENSOR_TRIG_THRESHOLD;
trig.chan = SENSOR_CHAN_TEMP;

sensor_trigger_set(dev, &trig, trigger_handler);
+}
+#endif
+
+void main(void)
+{
+ struct device *dev0 = device_get_binding("MCP9808_0");
+ if (dev0 == NULL) {
+ printk("device 0 not found. aborting test.\n");
+ return;
+ }
+
+ struct device *dev1 = device_get_binding("MCP9808_1");
+ if (dev1 == NULL) {
+ printk("device 1 not found. aborting test.\n");
+ return;
+ }
+
+#ifdef CONFIG_MCP9808_TRIGGER
+ trigger_setup(dev0, 26);
+ trigger_setup(dev1, 27);
#endif

while (1) {
- struct sensor_value temp;
+ struct sensor_value temp0, temp1;
+
+ sensor_sample_fetch(dev0);
+ sensor_channel_get(dev0, SENSOR_CHAN_TEMP, &temp0);

- sensor_sample_fetch(dev);
- sensor_channel_get(dev, SENSOR_CHAN_TEMP, &temp);
+ sensor_sample_fetch(dev1);
+ sensor_channel_get(dev1, SENSOR_CHAN_TEMP, &temp1);

- PRINT("temp: %d.%06d\n", temp.val1, temp.val2);
+ PRINT("temp: %d.%06d %d.%06d\n",
+ temp0.val1, temp0.val2, temp1.val1, temp1.val2);

task_sleep(sys_clock_ticks_per_sec);
}
}
-
--
1.9.1