Skip to content

Commit

Permalink
samples: sensor: fdc2x1x: Add FDC2X1X sample application
Browse files Browse the repository at this point in the history
This sample application periodically reads raw data from the FDC2X1X
sensor in polling mode or optionally with data ready trigger.
It is able to read the 12-Bit and 28-Bit, as well as the 2-Channel
and 4-Channel versions (FDC2112, FDC2114, FDC2212, FDC2214).
The 4-channel versions are chosen through devicetree properties.
The default is FDC2112.

Signed-off-by: Igor Knippenberg <igor.knippenberg@gmail.com>
  • Loading branch information
skullbox305 authored and carlescufi committed Mar 17, 2021
1 parent 79a3d1b commit 066be0a
Show file tree
Hide file tree
Showing 6 changed files with 313 additions and 0 deletions.
11 changes: 11 additions & 0 deletions samples/sensor/fdc2x1x/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#
# Copyright (c) 2020 arithmetics.io
#
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.13.1)
find_package(Zephyr HINTS $ENV{ZEPHYR_BASE})
project(fdc2x1x)

FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})
103 changes: 103 additions & 0 deletions samples/sensor/fdc2x1x/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
.. _fdc2x1x:

FDC2X1X: Capacitance-to-Digital Converter
#########################################

Overview
********

This sample application periodically reads frequency and capacitance data from the
FDC2X1X sensor in polling mode or optionally with data ready trigger. It is able
to read the 12-Bit and 28-Bit, as well as the 2-Channel and 4-Channel versions
(FDC2112, FDC2114, FDC2212, FDC2214). The 4-channel versions are chosen through
devicetree properties. The default in this sample is the 2-channel version.

Capacitive sensing is a low-power, low-cost, high-resolution contactless sensing
technique that can be applied to a variety of applications ranging from proximity
detection and gesture recognition to remote liquid level sensing. The sensor in
a capacitive sensing system is any metal or conductor, allowing for low cost and
highly flexible system design.
The main challenge limiting sensitivity in capacitive sensing applications is
noise susceptibility of the sensors. With the FDC2x1x innovative EMI resistant
architecture, performance can be maintained even in presence of high-noise environments.


Wiring
*******

This sample uses the FDC2X1X sensor controlled using the I2C interface.
Connect supply **VDD** and **GND**. The supply voltage can be in
the 2.7V to 3.6V range.

Connect **SD** to a GPIO to control the Shutdown Mode.

Connect Interface: **SDA**, **SCL** and optionally connect **INTB** to a
interrupt capable GPIO.

For detailed description refer to the `FDC2X1X datasheet`_
at pages 4-5.


Building and Running
********************

This sample outputs sensor data to the console and can be read by any serial
console program. It should work with any platform featuring a I2C interface.
The platform in use requires a custom devicetree overlay.
In this example the :ref:`nrf9160dk_nrf9160` board is used. The devicetree
overlay of this board provides example settings for evaluation, which
you can use as a reference for other platforms.

.. zephyr-app-commands::
:zephyr-app: samples/sensor/fdc2x1x
:board: nrf9160dk_nrf9160
:goals: build flash
:compact:

Sample Output: 2-Channel, 28-Bit (FDC2212)
==========================================

.. code-block:: console
ch0: 5.318888 MHz ch1: 5.150293 MHz
ch0: 49.742308 pF ch1: 53.052260 pF
ch0: 5.318819 MHz ch1: 5.150307 MHz
ch0: 49.743612 pF ch1: 53.051964 pF
ch0: 5.318822 MHz ch1: 5.150200 MHz
ch0: 49.743548 pF ch1: 53.054176 pF
ch0: 5.318752 MHz ch1: 5.150265 MHz
ch0: 49.744860 pF ch1: 53.052828 pF
<repeats endlessly>
Sample Output: 4-Channel, 12-Bit (FDC2114)
==========================================

.. code-block:: console
ch0: 4.966171 MHz ch1: 4.946465 MHz ch2: 4.985879 MHz ch3: 4.907051 MHz
ch0: 57.059016 pF ch1: 57.514568 pF ch2: 56.608844 pF ch3: 58.442204 pF
ch0: 4.966171 MHz ch1: 4.946465 MHz ch2: 4.985879 MHz ch3: 4.907051 MHz
ch0: 57.059016 pF ch1: 57.514568 pF ch2: 56.608844 pF ch3: 58.442204 pF
ch0: 4.966171 MHz ch1: 4.946465 MHz ch2: 4.985879 MHz ch3: 4.907051 MHz
ch0: 57.059016 pF ch1: 57.514568 pF ch2: 56.608844 pF ch3: 58.442204 pF
ch0: 4.966171 MHz ch1: 4.946465 MHz ch2: 4.985879 MHz ch3: 4.907051 MHz
ch0: 57.059016 pF ch1: 57.514568 pF ch2: 56.608844 pF ch3: 58.442204 pF
<repeats endlessly>
References
**********

FDC2X1X Datasheet and Product Info:
https://www.ti.com/product/FDC2114

.. _FDC2X1X datasheet: https://www.ti.com/lit/gpn/fdc2114
38 changes: 38 additions & 0 deletions samples/sensor/fdc2x1x/boards/nrf9160dk_nrf9160.overlay
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2020 arithmetics.io
*
* SPDX-License-Identifier: Apache-2.0
*/

&i2c2 {
clock-frequency = <I2C_BITRATE_FAST>;
fdc2x1x@2A {
compatible = "ti,fdc2x1x";
reg = <0x2A>;
label = "FDC2X1X";
sd-gpios = <&gpio0 16 GPIO_ACTIVE_HIGH>;
intb-gpios = <&gpio0 20 GPIO_ACTIVE_LOW>;

autoscan;
deglitch = <5>;
fref = <43360>;

channel_0 {
rcount = <7499>;
settlecount = <48>;
fref-divider = <1>;
idrive = <10>;
fin-sel = <2>;
inductance = < 18 >;
};

channel_1 {
rcount = <7499>;
settlecount = <48>;
fref-divider = <1>;
idrive = <10>;
fin-sel = <2>;
inductance = <18>;
};
};
};
9 changes: 9 additions & 0 deletions samples/sensor/fdc2x1x/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CONFIG_LOG=y
CONFIG_I2C=y
CONFIG_SENSOR=y
CONFIG_PM_DEVICE=n
CONFIG_NEWLIB_LIBC=y
CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y

CONFIG_FDC2X1X=y
CONFIG_FDC2X1X_TRIGGER_GLOBAL_THREAD=n
8 changes: 8 additions & 0 deletions samples/sensor/fdc2x1x/sample.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
sample:
name: fdc2x1x sample
description: FDC2X1X sample application
tests:
sample.sensor.fdc2x1x:
harness: sensor
tags: sensors
platform_allow: nrf9160dk_nrf9160
144 changes: 144 additions & 0 deletions samples/sensor/fdc2x1x/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* Copyright (c) 2020 arithmetics.io
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr.h>
#include <device.h>
#include <sys/util.h>
#include <drivers/sensor.h>
#include "drivers/sensor/fdc2x1x.h"
#include <stdio.h>

#define CH_BUF_INIT(m) {},

K_SEM_DEFINE(sem, 0, 1);

#ifdef CONFIG_FDC2X1X_TRIGGER
static void trigger_handler(const struct device *dev,
struct sensor_trigger *trigger)
{
switch (trigger->type) {
case SENSOR_TRIG_DATA_READY:
if (sensor_sample_fetch(dev)) {
printk("Sample fetch error\n");
return;
}
k_sem_give(&sem);
break;
default:
printk("Unknown trigger\n");
}
}
#endif

#ifdef CONFIG_PM_DEVICE
static void pm_cb(const struct device *dev,
int status,
void *context,
void *arg)
{
ARG_UNUSED(dev);
ARG_UNUSED(arg);

switch (*(uint32_t *)context) {
case DEVICE_PM_ACTIVE_STATE:
printk("Enter ACTIVE_STATE ");
break;
case DEVICE_PM_LOW_POWER_STATE:
printk("Enter LOW_POWER_STATE ");
break;
case DEVICE_PM_OFF_STATE:
printk("Enter OFF_STATE ");
break;
}

if (status) {
printk("Fail\n");
} else {
printk("Success\n");
}
}
#endif

void main(void)
{
struct sensor_value ch_buf[] = {
DT_FOREACH_CHILD(DT_INST(0, ti_fdc2x1x), CH_BUF_INIT)
};

uint8_t num_ch = ARRAY_SIZE(ch_buf);
enum sensor_channel base;
int i;

const struct device *dev =
device_get_binding(DT_LABEL(DT_INST(0, ti_fdc2x1x)));

if (dev == NULL) {
printk("Could not get %s device\n",
DT_LABEL(DT_INST(0, ti_fdc2x1x)));
return;
}

#ifdef CONFIG_FDC2X1X_TRIGGER
struct sensor_trigger trig = {
.type = SENSOR_TRIG_DATA_READY,
.chan = SENSOR_CHAN_ALL,
};

if (sensor_trigger_set(dev, &trig, trigger_handler)) {
printk("Could not set trigger\n");
return;
}
#endif

#ifdef CONFIG_PM_DEVICE
/* Testing the power modes */
uint32_t p_state;

p_state = DEVICE_PM_LOW_POWER_STATE;
device_set_power_state(dev, p_state, pm_cb, NULL);

p_state = DEVICE_PM_OFF_STATE;
device_set_power_state(dev, p_state, pm_cb, NULL);

p_state = DEVICE_PM_ACTIVE_STATE;
device_set_power_state(dev, p_state, pm_cb, NULL);
#endif

while (1) {
#ifdef CONFIG_FDC2X1X_TRIGGER
k_sem_take(&sem, K_FOREVER);
#else
if (sensor_sample_fetch(dev) < 0) {
printk("Sample fetch failed\n");
return;
}
#endif
base = SENSOR_CHAN_FDC2X1X_FREQ_CH0;
for (i = 0; i < num_ch; i++) {
sensor_channel_get(dev, base++, &ch_buf[i]);
printf("ch%d: %f MHz ", i, sensor_value_to_double(&ch_buf[i]));
}
printf("\n");

base = SENSOR_CHAN_FDC2X1X_CAPACITANCE_CH0;
for (i = 0; i < num_ch; i++) {
sensor_channel_get(dev, base++, &ch_buf[i]);
printf("ch%d: %f pF ", i, sensor_value_to_double(&ch_buf[i]));
}
printf("\n\n");


#ifdef CONFIG_PM_DEVICE
p_state = DEVICE_PM_OFF_STATE;
device_set_power_state(dev, p_state, pm_cb, NULL);
k_sleep(K_MSEC(2000));
p_state = DEVICE_PM_ACTIVE_STATE;
device_set_power_state(dev, p_state, pm_cb, NULL);
#elif CONFIG_FDC2X1X_TRIGGER_NONE
k_sleep(K_MSEC(100));
#endif
}
}

0 comments on commit 066be0a

Please sign in to comment.