Skip to content

Commit b71daf0

Browse files
committed
samples: boards: renesas: Add LVD sample for Renesas
Add a simple sample using Comparator API to monitor the voltage on Renesas boards Signed-off-by: Quy Tran <[email protected]>
1 parent e1ff275 commit b71daf0

File tree

6 files changed

+161
-0
lines changed

6 files changed

+161
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Copyright (c) 2025 Renesas Electronics Corporation
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
cmake_minimum_required(VERSION 3.20.0)
5+
6+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
7+
project(lvd)
8+
9+
FILE(GLOB app_sources src/*.c)
10+
target_sources(app PRIVATE src/main.c)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
.. zephyr:code-sample:: renesas_lvd
2+
:name: Renesas Low-voltage Detection Sample using Comparator
3+
4+
Demonstrates monitoring and reacting to voltage levels.
5+
6+
Overview
7+
********
8+
9+
This sample application shows how to use Comparator to monitor voltage levels
10+
with Renesas Low-voltage Detection (LVD).
11+
12+
Hardware Setup
13+
**************
14+
15+
- Ensure that the monitored target pin is supplied with power.
16+
17+
Building and Running
18+
********************
19+
20+
To build and flash the sample on a supported Renesas RX board:
21+
22+
.. zephyr-app-commands::
23+
:zephyr-app: samples/boards/renesas/lvd
24+
:board: rsk_rx130@512kb
25+
:goals: build flash
26+
:compact:
27+
28+
The comparator configures trigger is rising edge. When the voltage on the monitored pin
29+
crosses the threshold (Vref) - defined in the board's device tree, an interrupt is generated
30+
and turns the LED on.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (c) 2025 Renesas Electronics Corporation
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
&pinctrl {
7+
lvd1_default: lvd1_default {
8+
group1 {
9+
/* CMPA2 */
10+
psels = <RX_PSEL(RX_PSEL_LVD, 14, 4)>;
11+
renesas,analog-enable;
12+
input-enable;
13+
};
14+
};
15+
};
16+
17+
&lvd1 {
18+
pinctrl-0 = <&lvd1_default>;
19+
pinctrl-names = "default";
20+
lvd-action = "maskable-interrupt";
21+
voltage-level = <384>;
22+
vdet-target = "cmpa";
23+
lvd-trigger = "rising";
24+
lvd-stabilization = <0>;
25+
status = "okay";
26+
};
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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
sample:
2+
name: Low Voltage Detector (LVD)
3+
tests:
4+
sample.boards.renesas.lvd:
5+
platform_allow:
6+
- rsk_rx130@512kb
7+
tags: lvd
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
15+
#if DT_HAS_COMPAT_STATUS_OKAY(renesas_rx_lvd)
16+
#define LVD_DEV DT_INST(0, renesas_rx_lvd)
17+
#else
18+
#error "Please set the correct device"
19+
#endif
20+
21+
static struct gpio_dt_spec led = GPIO_DT_SPEC_GET_OR(DT_ALIAS(led0), gpios, {0});
22+
static const struct device *lvd_dev = DEVICE_DT_GET(LVD_DEV);
23+
24+
#define VREF_MV (DT_PROP(LVD_DEV, voltage_level))
25+
26+
static void lvd_callback(const struct device *dev, void *user_data)
27+
{
28+
int ret;
29+
30+
printk("[WARNING] Voltage instability detected! Check power supply.\n");
31+
gpio_pin_set_dt(&led, 1);
32+
33+
ret = comparator_get_output(lvd_dev);
34+
if (ret < 0) {
35+
printk("Error: failed to get comparator output\n");
36+
return;
37+
}
38+
printk("Comparator output is %s Vref (%.2fV)\n", ret ? "ABOVE" : "BELOW",
39+
((double)VREF_MV / 100));
40+
}
41+
42+
int main(void)
43+
{
44+
int ret;
45+
46+
if (!device_is_ready(lvd_dev)) {
47+
printk("Comparator device not ready\n");
48+
return 0;
49+
}
50+
51+
ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE);
52+
if (ret != 0) {
53+
printk("Error: failed to configure LED\n");
54+
return -EINVAL;
55+
}
56+
57+
gpio_pin_set_dt(&led, 0);
58+
59+
ret = comparator_get_output(lvd_dev);
60+
if (ret < 0) {
61+
printk("Error: failed to get comparator output\n");
62+
return -EINVAL;
63+
}
64+
65+
printk("Comparator output is %s Vref (%.2fV)\n", ret ? "ABOVE" : "BELOW",
66+
((double)VREF_MV / 100));
67+
68+
ret = comparator_set_trigger(lvd_dev, COMPARATOR_TRIGGER_RISING_EDGE);
69+
if (ret < 0) {
70+
printk("Error: failed to set comparator trigger\n");
71+
return -EINVAL;
72+
}
73+
74+
ret = comparator_set_trigger_callback(lvd_dev, lvd_callback, NULL);
75+
if (ret < 0) {
76+
printk("Error: failed to set comparator callback\n");
77+
return -EINVAL;
78+
}
79+
80+
while (1) {
81+
k_sleep(K_MSEC(100));
82+
}
83+
}

0 commit comments

Comments
 (0)