1 /*
2 * Copyright (c) 2025 Linumiz
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <stdio.h>
8 #include <zephyr/kernel.h>
9 #include "zephyr/drivers/pwm.h"
10
main(void)11 int main(void)
12 {
13 int ret;
14 uint64_t tim_clk_cycles;
15 const struct device *const dev = DEVICE_DT_GET(DT_ALIAS(capture));
16
17 printk("PWM capture %s\n", CONFIG_BOARD_TARGET);
18 if (!device_is_ready(dev)) {
19 printk("device is not ready\n");
20 return 0;
21 }
22
23 pwm_get_cycles_per_sec(dev, 0, &tim_clk_cycles);
24 printk("timclk %llu Hz\n", tim_clk_cycles);
25
26 while (1) {
27 uint32_t period = 0;
28 uint32_t width = 0;
29
30 k_msleep(250);
31 ret = pwm_capture_cycles(dev, 0,
32 (PWM_CAPTURE_TYPE_BOTH | PWM_CAPTURE_MODE_SINGLE),
33 &period, &width, Z_TIMEOUT_MS(500));
34
35 if (ret == -EBUSY) {
36 pwm_disable_capture(dev, 0);
37 continue;
38 }
39
40 if (ret) {
41 printk("capture cycle err %d\n", ret);
42 continue;
43 }
44
45 printk("{period:%u pulse width: %u} in TIMCLK cycle\n",
46 period,
47 width);
48
49 printk("{period: %f Hz duty: %f}\n",
50 (float)tim_clk_cycles / (float)period,
51 (float)(width * 100) / (float)period);
52 }
53
54 return 0;
55 }
56