diff --git a/samples/sensor/sm351lt/CMakeLists.txt b/samples/sensor/sm351lt/CMakeLists.txt new file mode 100644 index 000000000000..6bdc739360cc --- /dev/null +++ b/samples/sensor/sm351lt/CMakeLists.txt @@ -0,0 +1,12 @@ +# +# Copyright (c) 2020, Laird Connectivity +# +# SPDX-License-Identifier: Apache-2.0 +# + +cmake_minimum_required(VERSION 3.13.1) +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(sm351lt) + +FILE(GLOB app_sources src/*.c) +target_sources(app PRIVATE ${app_sources}) diff --git a/samples/sensor/sm351lt/README.rst b/samples/sensor/sm351lt/README.rst new file mode 100644 index 000000000000..18513076058c --- /dev/null +++ b/samples/sensor/sm351lt/README.rst @@ -0,0 +1,51 @@ +.. _sm351lt: + +SM351LT: Magnetoresistive Sensor Example +######################################## + +Overview +******** + +This sample application periodically polls an SM351LT magnetoresistive sensor +and displays if a magnet near to the sensor, via the console. + +Requirements +************ + +This sample uses the Honeywell SM351LT sensor. + +References +********** + +For more information about the SM351LT magnetoresistive sensor see +https://sensing.honeywell.com/SM351LT-low-power + +Building and Running +******************** + +The SM351LT (or compatible) sensors are available on the following boards: + +* :ref:`bt510` + +Building on bt510 +================== + +:ref:`bt510` includes a Honeywell SM351LT magnetoresistive sensor. + +.. zephyr-app-commands:: + :zephyr-app: samples/sensor/sm351lt + :board: bt510 + :goals: build flash + :compact: + +Sample Output +============= + +.. code-block:: console + + Polling at 0.5 Hz + #1 @ 6 ms: 1 + #2 @ 2007 ms: 0 + #3 @ 4009 ms: 0 + #4 @ 6010 ms: 1 + #5 @ 8012 ms: 1 diff --git a/samples/sensor/sm351lt/prj.conf b/samples/sensor/sm351lt/prj.conf new file mode 100644 index 000000000000..e5fc568598ee --- /dev/null +++ b/samples/sensor/sm351lt/prj.conf @@ -0,0 +1,4 @@ +CONFIG_STDOUT_CONSOLE=y +CONFIG_GPIO=y +CONFIG_SENSOR=y +CONFIG_SM351LT=y diff --git a/samples/sensor/sm351lt/sample.yaml b/samples/sensor/sm351lt/sample.yaml new file mode 100644 index 000000000000..dab2270afa5e --- /dev/null +++ b/samples/sensor/sm351lt/sample.yaml @@ -0,0 +1,7 @@ +sample: + name: SM351LT Magnetoresistive Sensor Example +tests: + sample.sensor.sm351lt: + harness: sensor + tags: samples sensor + depends_on: gpio sm351lt diff --git a/samples/sensor/sm351lt/src/main.c b/samples/sensor/sm351lt/src/main.c new file mode 100644 index 000000000000..9e9dbbbe6f3e --- /dev/null +++ b/samples/sensor/sm351lt/src/main.c @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2020 Laird Connectivity + * Based on code by Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include +#include + +static void fetch_and_display(struct device *sensor) +{ + static unsigned int count; + struct sensor_value mag; + int rc = sensor_sample_fetch(sensor); + + ++count; + if (rc == 0) { + rc = sensor_channel_get(sensor, + SENSOR_CHAN_PROX, + &mag); + } + if (rc < 0) { + printf("ERROR: Update failed: %d\n", rc); + } else { + printf("#%u @ %u ms: %d\n", + count, k_uptime_get_32(), mag.val1); + } +} + +#ifdef CONFIG_SM351LT_TRIGGER +static void trigger_handler(struct device *dev, + struct sensor_trigger *trig) +{ + fetch_and_display(dev); +} +#endif + +void main(void) +{ + struct device *sensor = device_get_binding(DT_LABEL(DT_INST(0, honeywell_sm351lt))); + + if (sensor == NULL) { + printf("Could not get %s device\n", + DT_LABEL(DT_INST(0, honeywell_sm351lt))); + return; + } + +#if CONFIG_SM351LT_TRIGGER + { + struct sensor_trigger trig; + int rc; + + trig.type = SENSOR_TRIG_NEAR_FAR; + trig.chan = SENSOR_CHAN_PROX; + + struct sensor_value trigger_type = { + .val1 = GPIO_INT_EDGE_BOTH, + }; + + rc = sensor_attr_set(sensor, trig.chan, + SENSOR_ATTR_PRIV_START, + &trigger_type); + if (rc != 0) { + printf("Failed to set trigger type: %d\n", rc); + return; + } + + rc = sensor_trigger_set(sensor, &trig, trigger_handler); + if (rc != 0) { + printf("Failed to set trigger: %d\n", rc); + return; + } + + printf("Waiting for triggers\n"); + while (true) { + k_sleep(K_MSEC(2000)); + } + } +#else /* CONFIG_SM351LT_TRIGGER */ + printf("Polling at 0.5 Hz\n"); + while (true) { + fetch_and_display(sensor); + k_sleep(K_MSEC(2000)); + } +#endif /* CONFIG_SM351LT_TRIGGER */ +}