|
| 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/drivers/gpio.h> |
| 12 | + |
| 13 | +#if defined(CONFIG_LIN_BUS_BAUDRATE_2400) |
| 14 | +#define LIN_BUS_BAUDRATE 2400 |
| 15 | +#elif defined(CONFIG_LIN_BUS_BAUDRATE_9600) |
| 16 | +#define LIN_BUS_BAUDRATE 9600 |
| 17 | +#elif defined(CONFIG_LIN_BUS_BAUDRATE_10400) |
| 18 | +#define LIN_BUS_BAUDRATE 10400 |
| 19 | +#elif defined(CONFIG_LIN_BUS_BAUDRATE_19200) |
| 20 | +#define LIN_BUS_BAUDRATE 19200 |
| 21 | +#else |
| 22 | +#error "Invalid baudrate setting" |
| 23 | +#endif |
| 24 | + |
| 25 | +#if defined(CONFIG_LIN_BUS_CLASSIC_CHECKSUM) |
| 26 | +#define LIN_CHECKSUM_TYPE LIN_CHECKSUM_CLASSIC |
| 27 | +#elif defined(CONFIG_LIN_BUS_ENHANCED_CHECKSUM) |
| 28 | +#define LIN_CHECKSUM_TYPE LIN_CHECKSUM_ENHANCED |
| 29 | +#else |
| 30 | +#error "Invalid checksum type setting" |
| 31 | +#endif |
| 32 | + |
| 33 | +/* Test configuration */ |
| 34 | +#define COUNTER_UPDATE_FRAME_ID CONFIG_COUNTER_UPDATE_ID |
| 35 | +#define COUNTER_UPDATE_INTERVAL_MS CONFIG_COUNTER_UPDATE_PERIOD_MS |
| 36 | + |
| 37 | +/* LED blink duration */ |
| 38 | +#define LED_BLINK_DURATION_MS 100 |
| 39 | + |
| 40 | +/* Recommended slave node framing configuration as mentioned in the LIN 2.1 specification */ |
| 41 | +#define LIN_BUS_BREAK_LEN 11 |
| 42 | +#define LIN_BUS_BREAK_DELIMITER_LEN 1 |
| 43 | + |
| 44 | +static const struct device *lin_dut = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(dut)); |
| 45 | +static struct gpio_dt_spec led = GPIO_DT_SPEC_GET_OR(DT_ALIAS(led0), gpios, {0}); |
| 46 | + |
| 47 | +/* LIN DUT configuration */ |
| 48 | +static const struct lin_config dut_config = { |
| 49 | + .mode = LIN_MODE_RESPONDER, |
| 50 | + .baudrate = LIN_BUS_BAUDRATE, |
| 51 | + .break_len = LIN_BUS_BREAK_LEN, |
| 52 | + .break_delimiter_len = LIN_BUS_BREAK_DELIMITER_LEN, |
| 53 | + .flags = 0, |
| 54 | +}; |
| 55 | + |
| 56 | +static uint32_t counter; |
| 57 | + |
| 58 | +static void led_blink_work_handler(struct k_work *work) |
| 59 | +{ |
| 60 | + gpio_pin_set_dt(&led, 0); |
| 61 | +} |
| 62 | + |
| 63 | +K_WORK_DELAYABLE_DEFINE(led_blink_work, led_blink_work_handler); |
| 64 | + |
| 65 | +void lin_dut_event_handler(const struct device *dev, const struct lin_event *event, void *user_data) |
| 66 | +{ |
| 67 | + int ret; |
| 68 | + |
| 69 | + if (event->type == LIN_EVT_RX_HEADER) { |
| 70 | + struct lin_msg msg = { |
| 71 | + .data_len = sizeof(uint32_t), |
| 72 | + .checksum_type = LIN_CHECKSUM_TYPE, |
| 73 | + .id = lin_get_frame_id(event->header.pid), |
| 74 | + .data = &counter, |
| 75 | + .flags = 0, |
| 76 | + }; |
| 77 | + |
| 78 | + if (msg.id == COUNTER_UPDATE_FRAME_ID) { |
| 79 | + /* Send response with the current counter value */ |
| 80 | + printk("Counter Update Frame has been transmitted\n"); |
| 81 | + ret = lin_response(dev, &msg, K_FOREVER); |
| 82 | + if (ret < 0) { |
| 83 | + printk("Error: Failed to send Counter Update response\n"); |
| 84 | + return; |
| 85 | + } |
| 86 | + } else { |
| 87 | + /* Do nothing for other IDs */ |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + if (led.port != NULL) { |
| 92 | + /* Blink LED to indicate activity */ |
| 93 | + gpio_pin_set_dt(&led, 1); |
| 94 | + |
| 95 | + /* Schedule work to toggle LED off after duration */ |
| 96 | + k_work_schedule(&led_blink_work, K_MSEC(LED_BLINK_DURATION_MS)); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +int main(void) |
| 102 | +{ |
| 103 | + int ret; |
| 104 | + |
| 105 | + if (!device_is_ready(lin_dut)) { |
| 106 | + printk("LIN DUT device not found\n"); |
| 107 | + return 0; |
| 108 | + } |
| 109 | + |
| 110 | + if (led.port != NULL) { |
| 111 | + if (!device_is_ready(led.port)) { |
| 112 | + printk("LED device not found\n"); |
| 113 | + return 0; |
| 114 | + } |
| 115 | + |
| 116 | + ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT_INACTIVE); |
| 117 | + if (ret < 0) { |
| 118 | + printk("Failed to configure LED GPIO pin\n"); |
| 119 | + return 0; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + ret = lin_configure(lin_dut, &dut_config); |
| 124 | + if (ret < 0) { |
| 125 | + printk("Error configuring LIN DUT [%d]", ret); |
| 126 | + return 0; |
| 127 | + } |
| 128 | + |
| 129 | + ret = lin_set_callback(lin_dut, lin_dut_event_handler, NULL); |
| 130 | + if (ret < 0) { |
| 131 | + printk("Error setting LIN DUT event handler [%d]", ret); |
| 132 | + return 0; |
| 133 | + } |
| 134 | + |
| 135 | + ret = lin_start(lin_dut); |
| 136 | + if (ret) { |
| 137 | + printk("LIN start failed: %d\n", ret); |
| 138 | + return 0; |
| 139 | + } |
| 140 | + |
| 141 | + printk("LIN DUT started in RESPONDER mode\n"); |
| 142 | + printk("Counter ID: 0x%02X\n", COUNTER_UPDATE_FRAME_ID); |
| 143 | + |
| 144 | + counter = 0; |
| 145 | + |
| 146 | + while (true) { |
| 147 | + printk("Counter: %u\n", counter++); |
| 148 | + k_msleep(COUNTER_UPDATE_INTERVAL_MS); |
| 149 | + } |
| 150 | + |
| 151 | + return 0; |
| 152 | +} |
0 commit comments