Skip to content

Commit 1e37184

Browse files
khoa-nguyen-18kartben
authored andcommitted
samples: boards: renesas: Add comparator sample for Renesas
Add comparator sample for Renesas boards Signed-off-by: Khoa Nguyen <khoa.nguyen.xh@renesas.com>
1 parent b331ed7 commit 1e37184

File tree

9 files changed

+267
-0
lines changed

9 files changed

+267
-0
lines changed

MAINTAINERS.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4072,6 +4072,7 @@ Renesas RA Platforms:
40724072
- dts/arm/renesas/ra/
40734073
- dts/bindings/*/*renesas,ra*
40744074
- soc/renesas/ra/
4075+
- samples/boards/renesas/
40754076
labels:
40764077
- "platform: Renesas RA"
40774078
description: >-
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Copyright (c) 2025 Renesas Electronics Corporation
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
cmake_minimum_required(VERSION 3.20.0)
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
project(renesas_comparator)
7+
8+
FILE(GLOB app_sources src/*.c)
9+
target_sources(app PRIVATE ${app_sources})
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright (c) 2025 Renesas Electronics Corporation
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
mainmenu "Comparator sample additional options"
5+
6+
source "Kconfig.zephyr"
7+
8+
ZEPHYR_USER := zephyr,user
9+
10+
config DAC_REFERENCE_SOURCE
11+
bool
12+
select DAC
13+
default y if $(dt_node_has_prop,/$(ZEPHYR_USER),vref-dac)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
.. zephyr:code-sample:: renesas_comparator
2+
:name: Renesas comparator
3+
4+
Monitor the output of comparator.
5+
6+
Overview
7+
********
8+
9+
A sample that help to monitor the output of comparator.
10+
11+
Hardware Setup
12+
**************
13+
14+
* The IVREF is connected to the internal output of DA0 (DAC module).
15+
By default, DA0 is set to ~1.75V (half of the full-scale voltage).
16+
17+
* For IVCOMP, the user can connect the IVCMP3 input to either:
18+
19+
* Case 1: IVCMP3 with SW0 (button on board, on ek_ra8m1 is P600).
20+
* Case 2: IVCMP3 with external voltage for comparison.
21+
22+
Building and Running
23+
********************
24+
25+
This application can be built and executed on RA boards as follows:
26+
27+
.. zephyr-app-commands::
28+
:zephyr-app: samples/boards/renesas/comparator
29+
:board: ek_ra8m1
30+
:goals: build flash
31+
:compact:
32+
33+
The comparator's output controls LED0:
34+
35+
* If the comparator output is HIGH, LED0 turns on.
36+
* If the comparator output is LOW, LED0 turns off.
37+
38+
Example case:
39+
40+
* Case 1: IVCMP3 connected to SW0 (Pull up)
41+
42+
* SW0 not pressed --> Comparator output HIGH --> LED0 on
43+
* SW0 pressed --> Comparator output LOW --> LED0 off
44+
45+
* Case 2: IVCMP3 connected to another reference voltage
46+
47+
* IVCMP3 HIGH --> Comparator output HIGH --> LED0 on
48+
* IVCMP3 LOW --> Comparator output LOW --> LED0 off
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (c) 2025 Renesas Electronics Corporation
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include <zephyr/dt-bindings/gpio/gpio.h>
7+
8+
/ {
9+
aliases {
10+
sample-comp = &acmphs0;
11+
};
12+
13+
zephyr,user {
14+
vref-dac = <&dac0>;
15+
};
16+
};
17+
18+
&acmphs_global {
19+
status = "okay";
20+
21+
acmphs0 {
22+
/*
23+
* Since ivref3 is internally routed from to DA0 (DAC),
24+
* there is no need to add pinsel of ivref3.
25+
*/
26+
pinctrl-0 = <&acmphs0_ivcmp3>;
27+
pinctrl-names = "default";
28+
interrupts = <90 12>;
29+
interrupt-names = "hs";
30+
reference-input-source = "ivref3";
31+
compare-input-source = "ivcmp3";
32+
status = "okay";
33+
};
34+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (c) 2025 Renesas Electronics Corporation
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
/ {
7+
aliases {
8+
sample-comp = &acmphs0;
9+
};
10+
11+
zephyr,user {
12+
vref-dac = <&dac0>;
13+
};
14+
};
15+
16+
&acmphs_global {
17+
status = "okay";
18+
19+
acmphs0 {
20+
/*
21+
* Since ivref3 is internally routed from to DA0 (DAC),
22+
* there is no need to add pinsel of ivref3.
23+
*/
24+
pinctrl-0 = <&acmphs0_ivcmp3>;
25+
pinctrl-names = "default";
26+
interrupts = <90 12>;
27+
interrupt-names = "hs";
28+
reference-input-source = "ivref3";
29+
compare-input-source = "ivcmp3";
30+
status = "okay";
31+
};
32+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Copyright (c) 2025 Renesas Electronics Corporation
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
CONFIG_GPIO=y
5+
CONFIG_COMPARATOR=y
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
sample:
2+
name: Comparator
3+
tests:
4+
sample.boards.renesas.comparator:
5+
build_only: true
6+
platform_allow:
7+
- ek_ra8m1
8+
- ek_ra8d1
9+
tags: comparator
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright (c) 2025 Renesas Electronics Corporation
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
#include <zephyr/device.h>
9+
#include <zephyr/drivers/comparator.h>
10+
#include <zephyr/drivers/gpio.h>
11+
#include <zephyr/kernel.h>
12+
#include <zephyr/sys/util.h>
13+
#include <zephyr/sys/printk.h>
14+
#ifdef CONFIG_DAC_REFERENCE_SOURCE
15+
#include <zephyr/drivers/dac.h>
16+
#endif
17+
18+
/*
19+
* Get button configuration from the devicetree sw0 alias. This is mandatory.
20+
*/
21+
#define SW0_NODE DT_ALIAS(sw0)
22+
#if !DT_NODE_HAS_STATUS_OKAY(SW0_NODE)
23+
#error "Unsupported board: sw0 devicetree alias is not defined"
24+
#endif
25+
26+
static const struct gpio_dt_spec button = GPIO_DT_SPEC_GET_OR(SW0_NODE, gpios, {0});
27+
static struct gpio_dt_spec led = GPIO_DT_SPEC_GET_OR(DT_ALIAS(led0), gpios, {0});
28+
static const struct device *comp_dev = DEVICE_DT_GET(DT_ALIAS(sample_comp));
29+
30+
#ifdef CONFIG_DAC_REFERENCE_SOURCE
31+
static const struct dac_channel_cfg dac_ch_cfg = {
32+
.channel_id = 0,
33+
.resolution = 12,
34+
.buffered = true,
35+
};
36+
37+
static const struct device *init_dac(void)
38+
{
39+
int ret;
40+
const struct device *const dac_dev = DEVICE_DT_GET(DT_PROP(DT_PATH(zephyr_user), vref_dac));
41+
42+
ret = device_is_ready(dac_dev);
43+
if (ret != true) {
44+
printk("DAC device is not ready\n");
45+
return NULL;
46+
}
47+
48+
ret = dac_channel_setup(dac_dev, &dac_ch_cfg);
49+
if (ret != 0) {
50+
printk("Setting up of the first channel failed with code %d\n", ret);
51+
return NULL;
52+
}
53+
54+
return dac_dev;
55+
}
56+
#endif
57+
58+
static void comp_callback(const struct device *dev, void *user_data)
59+
{
60+
int val = comparator_get_output(comp_dev);
61+
62+
gpio_pin_set_dt(&led, val);
63+
printk("Comparator output value %d\n", val);
64+
}
65+
66+
int main(void)
67+
{
68+
int ret;
69+
#ifdef CONFIG_DAC_REFERENCE_SOURCE
70+
const struct device *dac_dev = init_dac();
71+
72+
if (dac_dev == NULL) {
73+
printk("Init_dac failed\n");
74+
return 0;
75+
}
76+
/* write a value of half the full scale resolution */
77+
ret = dac_write_value(dac_dev, dac_ch_cfg.channel_id, (1U << dac_ch_cfg.resolution) / 2);
78+
if (ret != 0) {
79+
printk("dac_write_value() failed with code %d", ret);
80+
return 0;
81+
}
82+
#endif
83+
84+
if (!gpio_is_ready_dt(&button)) {
85+
printk("Error: button device %s is not ready\n", button.port->name);
86+
return 0;
87+
}
88+
89+
ret = gpio_pin_configure_dt(&button, GPIO_INPUT);
90+
if (ret != 0) {
91+
printk("Error %d: failed to configure %s pin %d\n", ret, button.port->name,
92+
button.pin);
93+
return 0;
94+
}
95+
96+
if (led.port && !gpio_is_ready_dt(&led)) {
97+
printk("Error %d: LED device %s is not ready; ignoring it\n", ret, led.port->name);
98+
led.port = NULL;
99+
}
100+
101+
if (led.port) {
102+
ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT);
103+
if (ret != 0) {
104+
printk("Error %d: failed to configure LED device %s pin %d\n", ret,
105+
led.port->name, led.pin);
106+
led.port = NULL;
107+
}
108+
}
109+
110+
comparator_set_trigger(comp_dev, COMPARATOR_TRIGGER_BOTH_EDGES);
111+
comparator_set_trigger_callback(comp_dev, comp_callback, NULL);
112+
113+
printk("Press the button\n");
114+
115+
return 0;
116+
}

0 commit comments

Comments
 (0)