|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Renesas Electronics Corporation |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <zephyr/sys/util.h> |
| 8 | +#include <zephyr/drivers/misc/renesas_lvd/renesas_lvd.h> |
| 9 | +#include <zephyr/drivers/gpio.h> |
| 10 | +#include <zephyr/devicetree.h> |
| 11 | +#include <zephyr/kernel.h> |
| 12 | +#include <stdio.h> |
| 13 | +#include <stdlib.h> |
| 14 | +#include <string.h> |
| 15 | +#include <stdbool.h> |
| 16 | + |
| 17 | +/* LVD node & config from Device Tree */ |
| 18 | +#if DT_HAS_COMPAT_STATUS_OKAY(renesas_rx_lvd) |
| 19 | +#define LVD_NODE DT_INST(0, renesas_rx_lvd) |
| 20 | +#endif |
| 21 | + |
| 22 | +#define LVD_ACTION DT_ENUM_IDX(LVD_NODE, lvd_action) |
| 23 | +#define VOLTAGE_THRESHOLD DT_PROP(LVD_NODE, voltage_level) |
| 24 | + |
| 25 | +/* GPIO LEDs */ |
| 26 | +#define LED_BELOW_NODE DT_ALIAS(led0) |
| 27 | +#define LED_ABOVE_NODE DT_ALIAS(led1) |
| 28 | + |
| 29 | +static const struct gpio_dt_spec led_below = GPIO_DT_SPEC_GET(LED_BELOW_NODE, gpios); |
| 30 | +static const struct gpio_dt_spec led_above = GPIO_DT_SPEC_GET(LED_ABOVE_NODE, gpios); |
| 31 | + |
| 32 | +/* Semaphore for LVD interrupt */ |
| 33 | +static struct k_sem lvd_sem; |
| 34 | + |
| 35 | +#if LVD_ACTION == 2 |
| 36 | +#define LVD_INT 1 |
| 37 | +#else |
| 38 | +#define LVD_INT 0 |
| 39 | +#endif |
| 40 | + |
| 41 | +#if LVD_INT |
| 42 | +void user_callback(const struct device *dev, void *user_data) |
| 43 | +{ |
| 44 | + ARG_UNUSED(user_data); |
| 45 | + ARG_UNUSED(dev); |
| 46 | + printk("[LVD] Interrupt: Voltage level crossing detected!\n"); |
| 47 | + printk("[WARNING] Voltage instability detected! Check power supply.\n"); |
| 48 | + k_sem_give(&lvd_sem); |
| 49 | +} |
| 50 | +#endif |
| 51 | + |
| 52 | +/* Handle voltage status: get, print, update LEDs */ |
| 53 | +static void handle_voltage_status(const struct device *lvd_dev, const char *tag) |
| 54 | +{ |
| 55 | + int ret; |
| 56 | + renesas_lvd_status_t status; |
| 57 | + |
| 58 | + ret = renesas_lvd_get_status(lvd_dev, &status); |
| 59 | + if (ret != 0) { |
| 60 | + printk("[%s] Error: Failed to get LVD status.\n", tag); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + switch (status.position) { |
| 65 | + case RENESAS_LVD_POSITION_ABOVE: |
| 66 | + printk("[%s] Voltage is ABOVE threshold (%.2f V)\n", tag, |
| 67 | + ((double)VOLTAGE_THRESHOLD / 100)); |
| 68 | + gpio_pin_set_dt(&led_above, 1); |
| 69 | + gpio_pin_set_dt(&led_below, 0); |
| 70 | + break; |
| 71 | + case RENESAS_LVD_POSITION_BELOW: |
| 72 | + printk("[%s] Voltage is BELOW threshold (%.2f V)\n", tag, |
| 73 | + ((double)VOLTAGE_THRESHOLD / 100)); |
| 74 | + gpio_pin_set_dt(&led_below, 1); |
| 75 | + gpio_pin_set_dt(&led_above, 0); |
| 76 | + break; |
| 77 | + default: |
| 78 | + printk("[%s] Unknown voltage position\n", tag); |
| 79 | + break; |
| 80 | + } |
| 81 | + |
| 82 | + ret = renesas_lvd_clear_status(lvd_dev); |
| 83 | + if (ret != 0) { |
| 84 | + printk("[%s] Warning: Failed to clear LVD status\n", tag); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +int main(void) |
| 89 | +{ |
| 90 | + int ret; |
| 91 | + const struct device *const lvd_dev = DEVICE_DT_GET(LVD_NODE); |
| 92 | + |
| 93 | + k_sem_init(&lvd_sem, 0, 1); |
| 94 | + |
| 95 | + if (!device_is_ready(lvd_dev)) { |
| 96 | + printk("[LVD] Error: LVD device not ready.\n"); |
| 97 | + return -EINVAL; |
| 98 | + } |
| 99 | + |
| 100 | +#if LVD_INT |
| 101 | + ret = renesas_lvd_callback_set(lvd_dev, user_callback, NULL); |
| 102 | + if (ret != 0) { |
| 103 | + printk("[LVD] Error: Failed to register callback.\n"); |
| 104 | + return -EINVAL; |
| 105 | + } |
| 106 | +#endif |
| 107 | + |
| 108 | + if (!gpio_is_ready_dt(&led_below) || !gpio_is_ready_dt(&led_above)) { |
| 109 | + printk("[GPIO] Error: LED GPIOs not ready.\n"); |
| 110 | + return -ENODEV; |
| 111 | + } |
| 112 | + |
| 113 | + ret = gpio_pin_configure_dt(&led_below, GPIO_OUTPUT_ACTIVE); |
| 114 | + if (ret < 0) { |
| 115 | + return ret; |
| 116 | + } |
| 117 | + |
| 118 | + ret = gpio_pin_configure_dt(&led_above, GPIO_OUTPUT_ACTIVE); |
| 119 | + if (ret < 0) { |
| 120 | + return ret; |
| 121 | + } |
| 122 | + |
| 123 | + printk("===========================================================\n"); |
| 124 | + printk("LVD Voltage Monitoring Sample Started\n"); |
| 125 | + printk("Threshold Voltage Level : %.2f V\n", ((double)VOLTAGE_THRESHOLD / 100)); |
| 126 | + printk("===========================================================\n"); |
| 127 | + |
| 128 | + handle_voltage_status(lvd_dev, "LVD Init"); |
| 129 | + |
| 130 | + while (1) { |
| 131 | + k_sem_take(&lvd_sem, K_FOREVER); |
| 132 | + handle_voltage_status(lvd_dev, "LVD"); |
| 133 | + } |
| 134 | + |
| 135 | + return 0; |
| 136 | +} |
0 commit comments