-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
samples: drivers: Add comparator samples
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
1 parent
b526b3a
commit 3216dc7
Showing
14 changed files
with
312 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
36 changes: 36 additions & 0 deletions
36
samples/drivers/comparator/callback/boards/frdm_ke15z_mke15z7.overlay
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
}; |
21 changes: 21 additions & 0 deletions
21
samples/drivers/comparator/callback/boards/nrf5340dk_nrf5340_cpuapp.overlay
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
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"; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
36 changes: 36 additions & 0 deletions
36
samples/drivers/comparator/pending/boards/frdm_ke15z_mke15z7.overlay
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
}; |
21 changes: 21 additions & 0 deletions
21
samples/drivers/comparator/pending/boards/nrf5340dk_nrf5340_cpuapp.overlay
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
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"; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |