1 /*
2 * Copyright (c) 2023 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 /**
8 * @file
9 * @brief Sample application for timeaware GPIO.
10 * This example demonstrates the following
11 * a. How to generate pulses based on ART time on an output pin
12 * b. How to timestamp a pulse on an input pin
13 */
14
15 /* Local Includes */
16 #include <zephyr/sys/util.h>
17 #include <zephyr/drivers/misc/timeaware_gpio/timeaware_gpio.h>
18 #include <zephyr/kernel.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #define TGPIO_LABEL DT_NODELABEL(tgpio)
24 #define TGPIO_PIN_IN 0
25 #define TGPIO_PIN_OUT 1
26
main(void)27 int main(void)
28 {
29 const struct device *tgpio_dev;
30 uint64_t tm, ts, ec, ret;
31 uint32_t cycles;
32
33 /* Get the device handle for Timeaware-GPIO instance */
34 tgpio_dev = DEVICE_DT_GET(TGPIO_LABEL);
35 if (!device_is_ready(tgpio_dev)) {
36 printk("[TGPIO] Bind failed\n");
37 return -EINVAL;
38 }
39
40 printk("[TGPIO] Bind Success\n");
41
42 tgpio_port_get_time(tgpio_dev, &tm);
43 printk("[TGPIO] Time now: %016llx\n", tm);
44
45 tgpio_port_get_cycles_per_second(tgpio_dev, &cycles);
46 printk("[TGPIO] Running rate: %d\n", cycles);
47
48 /* Configure start time of first pulse, time has to be in future */
49 tm += cycles;
50 printk("[TGPIO] Periodic pulses start at: %016llx\n", tm);
51
52 ret = tgpio_pin_periodic_output(tgpio_dev, TGPIO_PIN_OUT,
53 tm, cycles, true);
54 if (ret) {
55 printk("[TGPIO] periodic output configuration failed\n");
56 return -EINVAL;
57 }
58
59 /* Configure external timestamp for input pulses */
60 ret = tgpio_pin_config_ext_timestamp(tgpio_dev, TGPIO_PIN_IN, 0);
61 if (ret) {
62 printk("[TGPIO] external timestamp configuration failed\n");
63 return -EINVAL;
64 }
65
66 while (1) {
67 /* Read timestamp and event counter values */
68 tgpio_pin_read_ts_ec(tgpio_dev, TGPIO_PIN_IN, &ts, &ec);
69 printk("[TGPIO] timestamp: %016llx, event count: %016llx\n", ts, ec);
70 k_sleep(K_MSEC(500));
71 }
72 return 0;
73 }
74