Skip to content

Commit

Permalink
samples: drivers: Add comparator samples
Browse files Browse the repository at this point in the history
Adds comparator samples which:
- set and monitors trigger using callback
- set and periodically checks if trigger is pending

Signed-off-by: Bjarki Arge Andreasen <[email protected]>
  • Loading branch information
bjarki-andreasen committed Sep 1, 2024
1 parent 7b02fbc commit fcd0e89
Show file tree
Hide file tree
Showing 14 changed files with 312 additions and 0 deletions.
8 changes: 8 additions & 0 deletions samples/drivers/comparator/callback/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) 2024 Nordic Semiconductor
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(sample)

target_sources(app PRIVATE src/main.c)
28 changes: 28 additions & 0 deletions samples/drivers/comparator/callback/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
.. zephyr:code-sample:: comparator_callback
:name: Comparator callback
:relevant-api: comparator_interface

Monitor the output of a comparator

Overview
********

This sample demonstrates how to monitor the output from a comparator
using the comparator device driver API.

Requirements
************

This sample requires a board with a comparator device present and enabled
in the devicetree. The comparator shall be pointed to by the devicetree alias
``comparator``.

Sample Output
*************

.. code-block:: console
comparator output is high
comparator output is low
comparator output is high
comparator output is low
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2024 Nordic Semiconductor
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <nxp/kinetis/MKE17Z256VLL7-pinctrl.h>

/{
aliases {
comparator = &cmp0;
};
};

&pinctrl {
cmp0_default: cmp0_default {
group0 {
pinmux = <ACMP0_IN0_PTA0>,
<ACMP0_IN1_PTA1>;

/* required but not used */
drive-strength = "high";
};
};
};

&cmp0 {
pinctrl-0 = <&cmp0_default>;
pinctrl-names = "default";
status = "okay";

positive-mux-input = "IN0";
positive-port-input = "MUX";
negative-mux-input = "IN1";
negative-port-input = "MUX";
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2024 Nordic Semiconductor
*
* SPDX-License-Identifier: Apache-2.0
*/

/{
aliases {
comparator = &comp;
};
};

&comp {
positive-input = "INPUT_0"; /* P0.04 */
external-reference = "EXT_REF_1"; /* P0.05 */
main-mode = "DIFF";
speed-mode = "NORMAL";
hysteresis = "50MV";
isource = "OFF";
status = "okay";
};
4 changes: 4 additions & 0 deletions samples/drivers/comparator/callback/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright (c) 2024 Nordic Semiconductor ASA
# SPDX-License-Identifier: Apache-2.0

CONFIG_COMPARATOR=y
11 changes: 11 additions & 0 deletions samples/drivers/comparator/callback/sample.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Copyright (c) 2024 Nordic Semiconductor ASA
# SPDX-License-Identifier: Apache-2.0

sample:
name: Comparator callback sample
tests:
sample.drivers.comparator.callback:
tags:
- drivers
- comparator
filter: dt_alias_exists("comparator")
57 changes: 57 additions & 0 deletions samples/drivers/comparator/callback/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2024 Nordic Semiconductor
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/drivers/comparator.h>
#include <zephyr/kernel.h>
#include <stdio.h>

const struct device *sample_comparator = DEVICE_DT_GET(DT_ALIAS(comparator));

static void sample_get_output_handler(struct k_work *work)
{
int ret;

ARG_UNUSED(work);

ret = comparator_get_output(sample_comparator);
if (ret == 1) {
printf("comparator output is %s\n", "high");
} else if (ret == 0) {
printf("comparator output is %s\n", "low");
} else {
printf("failed to get comparator output (ret = %i)\n", ret);
}
}

K_WORK_DEFINE(sample_get_output_work, sample_get_output_handler);

static void sample_callback(const struct device *dev, void *user_data)
{
ARG_UNUSED(dev);
ARG_UNUSED(user_data);

k_work_submit(&sample_get_output_work);
}

int main(void)
{
int ret;

ret = comparator_set_trigger(sample_comparator, COMPARATOR_TRIGGER_BOTH_EDGES);
if (ret < 0) {
printf("failed to set comparator trigger (ret = %i)\n", ret);
}

ret = comparator_set_trigger_callback(sample_comparator,
sample_callback,
NULL);
if (ret < 0) {
printf("failed to set comparator callback (ret = %i)\n", ret);
}

k_work_submit(&sample_get_output_work);
return 0;
}
8 changes: 8 additions & 0 deletions samples/drivers/comparator/pending/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) 2024 Nordic Semiconductor
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(sample)

target_sources(app PRIVATE src/main.c)
32 changes: 32 additions & 0 deletions samples/drivers/comparator/pending/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
.. zephyr:code-sample:: comparator_pending
:name: Comparator pending
:relevant-api: comparator_interface

Monitor the output of a comparator

Overview
********

This sample demonstrates how to enable the comparator trigger and
periodically check and clear the trigger pending flag using the
comparator device driver API.

Requirements
************

This sample requires a board with a comparator device present and enabled
in the devicetree. The comparator shall be pointed to by the devicetree alias
``comparator``.

Sample Output
*************

.. code-block:: console
comparator trigger is not pending
comparator trigger is not pending
comparator trigger is not pending
comparator trigger is not pending
comparator trigger is pending
comparator trigger is not pending
comparator trigger is not pending
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2024 Nordic Semiconductor
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <nxp/kinetis/MKE17Z256VLL7-pinctrl.h>

/{
aliases {
comparator = &cmp0;
};
};

&pinctrl {
cmp0_default: cmp0_default {
group0 {
pinmux = <ACMP0_IN0_PTA0>,
<ACMP0_IN1_PTA1>;

/* required but not used */
drive-strength = "high";
};
};
};

&cmp0 {
pinctrl-0 = <&cmp0_default>;
pinctrl-names = "default";
status = "okay";

positive-mux-input = "IN0";
positive-port-input = "MUX";
negative-mux-input = "IN1";
negative-port-input = "MUX";
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2024 Nordic Semiconductor
*
* SPDX-License-Identifier: Apache-2.0
*/

/{
aliases {
comparator = &comp;
};
};

&comp {
positive-input = "INPUT_0"; /* P0.04 */
external-reference = "EXT_REF_1"; /* P0.05 */
main-mode = "DIFF";
speed-mode = "NORMAL";
hysteresis = "50MV";
isource = "OFF";
status = "okay";
};
4 changes: 4 additions & 0 deletions samples/drivers/comparator/pending/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright (c) 2024 Nordic Semiconductor ASA
# SPDX-License-Identifier: Apache-2.0

CONFIG_COMPARATOR=y
11 changes: 11 additions & 0 deletions samples/drivers/comparator/pending/sample.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Copyright (c) 2024 Nordic Semiconductor ASA
# SPDX-License-Identifier: Apache-2.0

sample:
name: Comparator pending sample
tests:
sample.drivers.comparator.pending:
tags:
- drivers
- comparator
filter: dt_alias_exists("comparator")
35 changes: 35 additions & 0 deletions samples/drivers/comparator/pending/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (c) 2024 Nordic Semiconductor
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/drivers/comparator.h>
#include <zephyr/kernel.h>
#include <stdio.h>

const struct device *sample_comparator = DEVICE_DT_GET(DT_ALIAS(comparator));

int main(void)
{
int ret;

ret = comparator_set_trigger(sample_comparator, COMPARATOR_TRIGGER_BOTH_EDGES);
if (ret < 0) {
printf("failed to set comparator trigger (ret = %i)\n", ret);
}

while (1) {
k_msleep(1000);
ret = comparator_trigger_is_pending(sample_comparator);
if (ret == 1) {
printf("comparator trigger is %s\n", "pending");
} else if (ret == 0) {
printf("comparator trigger is %s\n", "not pending");
} else {
printf("failed to check if trigger is pending (ret = %i)\n", ret);
}
}

return 0;
}

0 comments on commit fcd0e89

Please sign in to comment.