|
| 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/sys/printk.h> |
| 9 | +#include <zephyr/device.h> |
| 10 | +#include <zephyr/drivers/lin.h> |
| 11 | +#include <zephyr/sys/byteorder.h> |
| 12 | + |
| 13 | +#include "ncv7430.h" |
| 14 | + |
| 15 | +#define LIN_BUS_BAUDRATE 19200U |
| 16 | +#define LIN_BUS_BREAK_LEN 13U |
| 17 | +#define LIN_BUS_BREAK_DELIMITER_LEN 1U |
| 18 | + |
| 19 | +#define LED_CHANGE_INTERVAL_MS 1000 |
| 20 | + |
| 21 | +/* RGB LED test sequence (RGB888 format, separate channels) */ |
| 22 | +static const struct rgb_led_color rgb_led_test_colors[8] = { |
| 23 | + {255, 0, 0}, {0, 255, 0}, {0, 0, 255}, {255, 255, 0}, |
| 24 | + {0, 255, 255}, {255, 0, 255}, {255, 255, 255}, {0, 0, 0}, |
| 25 | +}; |
| 26 | + |
| 27 | +/* RGB LED color names in string */ |
| 28 | +static const unsigned char rgb_test_color_name[][8] = { |
| 29 | + "Red", "Green", "Blue", "Yellow", "Cyan", "Magenta", "White", "Black", |
| 30 | +}; |
| 31 | + |
| 32 | +static const struct lin_config dut_config = { |
| 33 | + .mode = LIN_MODE_COMMANDER, |
| 34 | + .baudrate = LIN_BUS_BAUDRATE, |
| 35 | + .break_len = LIN_BUS_BREAK_LEN, |
| 36 | + .break_delimiter_len = LIN_BUS_BREAK_DELIMITER_LEN, |
| 37 | + .flags = 0, |
| 38 | +}; |
| 39 | + |
| 40 | +static unsigned int current_led_idx; |
| 41 | + |
| 42 | +int main(void) |
| 43 | +{ |
| 44 | + const struct device *dev = DEVICE_DT_GET(DT_NODELABEL(dut)); |
| 45 | + int ret; |
| 46 | + |
| 47 | + ret = device_is_ready(dev); |
| 48 | + if (!ret) { |
| 49 | + printk("NCV7430 device not found\n"); |
| 50 | + return 0; |
| 51 | + } |
| 52 | + |
| 53 | + ret = lin_configure(dev, &dut_config); |
| 54 | + if (ret) { |
| 55 | + printk("LIN configure failed: %d\n", ret); |
| 56 | + return 0; |
| 57 | + } |
| 58 | + |
| 59 | + ret = lin_start(dev); |
| 60 | + if (ret) { |
| 61 | + printk("LIN start failed: %d\n", ret); |
| 62 | + return 0; |
| 63 | + } |
| 64 | + |
| 65 | + ret = ncv7430_init(dev, NCV7430_NODE_ADDRESS); |
| 66 | + if (ret) { |
| 67 | + printk("NCV7430 initialization failed: %d\n", ret); |
| 68 | + return 0; |
| 69 | + } |
| 70 | + |
| 71 | + printk("NCV7430 sample started\n"); |
| 72 | + current_led_idx = 0; |
| 73 | + |
| 74 | + while (true) { |
| 75 | + struct rgb_led_color color = rgb_led_test_colors[current_led_idx]; |
| 76 | + struct rgb_led_color read_color; |
| 77 | + uint32_t color_code; |
| 78 | + |
| 79 | + ret = ncv7430_set_led_color(dev, NCV7430_NODE_ADDRESS, &color); |
| 80 | + if (ret != 0) { |
| 81 | + printk("Failed to set LED color: %d\n", ret); |
| 82 | + return 0; |
| 83 | + } |
| 84 | + |
| 85 | + ret = ncv7430_get_led_color(dev, NCV7430_NODE_ADDRESS, &read_color); |
| 86 | + if (ret != 0) { |
| 87 | + printk("Failed to get LED color: %d\n", ret); |
| 88 | + return 0; |
| 89 | + } |
| 90 | + |
| 91 | + color_code = sys_get_be24((uint8_t *)&read_color); |
| 92 | + printk("Set LED color: %s - read back actual color code: 0x%06X\n", |
| 93 | + rgb_test_color_name[current_led_idx], color_code); |
| 94 | + |
| 95 | + if ((read_color.red != color.red) || (read_color.green != color.green) || |
| 96 | + (read_color.blue != color.blue)) { |
| 97 | + printk("Warning: Mismatch in set and read back color " |
| 98 | + "values!\n"); |
| 99 | + } |
| 100 | + |
| 101 | + current_led_idx++; |
| 102 | + if (current_led_idx == ARRAY_SIZE(rgb_led_test_colors)) { |
| 103 | + current_led_idx = 0; |
| 104 | + } |
| 105 | + |
| 106 | + k_msleep(LED_CHANGE_INTERVAL_MS); |
| 107 | + } |
| 108 | + |
| 109 | + return 0; |
| 110 | +} |
0 commit comments