From 7ed3c4fc46a05b449f44cdfc7571c5a8cd3a45cb Mon Sep 17 00:00:00 2001 From: Saravanan Sekar Date: Wed, 25 Jun 2025 09:18:19 +0530 Subject: [PATCH] samples: drivers: pwm: Add a sample to test pwm capture Add a sample to test pwm capture function. Signed-off-by: Saravanan Sekar --- samples/drivers/pwm/capture/CMakeLists.txt | 8 +++ samples/drivers/pwm/capture/README.rst | 38 +++++++++++++ .../pwm/capture/boards/lp_mspm0g3507.overlay | 31 +++++++++++ samples/drivers/pwm/capture/prj.conf | 3 + samples/drivers/pwm/capture/sample.yaml | 20 +++++++ samples/drivers/pwm/capture/src/main.c | 55 +++++++++++++++++++ samples/drivers/pwm/index.rst | 5 ++ 7 files changed, 160 insertions(+) create mode 100644 samples/drivers/pwm/capture/CMakeLists.txt create mode 100644 samples/drivers/pwm/capture/README.rst create mode 100644 samples/drivers/pwm/capture/boards/lp_mspm0g3507.overlay create mode 100644 samples/drivers/pwm/capture/prj.conf create mode 100644 samples/drivers/pwm/capture/sample.yaml create mode 100644 samples/drivers/pwm/capture/src/main.c create mode 100644 samples/drivers/pwm/index.rst diff --git a/samples/drivers/pwm/capture/CMakeLists.txt b/samples/drivers/pwm/capture/CMakeLists.txt new file mode 100644 index 00000000000..d2a4ba1361f --- /dev/null +++ b/samples/drivers/pwm/capture/CMakeLists.txt @@ -0,0 +1,8 @@ +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(pwm_capture) + +target_sources(app PRIVATE src/main.c) diff --git a/samples/drivers/pwm/capture/README.rst b/samples/drivers/pwm/capture/README.rst new file mode 100644 index 00000000000..8f4938d294e --- /dev/null +++ b/samples/drivers/pwm/capture/README.rst @@ -0,0 +1,38 @@ +.. zephyr:code-sample:: capture + :name: PWM Capture + :relevant-api: pwm_interface + + Capture a PWM signal. + +Overview +******** +This sample provides an example application using the :ref:`PWM API ` capture +API to measure the period and pulse width of an external PWM signal. + +Requirements +************ + +This sample requires the support of a timer IP compatible with pwm capture block. + +Building and Running +******************** + + .. zephyr-app-commands:: + :zephyr-app: samples/drivers/pwm/capture + :host-os: unix + :board: lp_mspm0g3507 + :goals: run + :compact: + +Sample Output +============= + + .. code-block:: console + + PWM capture lp_mspm0g3507/mspm0g350 + + timclk 80000000 Hz + {period:1000 pulse width: 499} in TIMCLK cycle + {period: 80000.000000 Hz duty: 49.900002} + + diff --git a/samples/drivers/pwm/capture/boards/lp_mspm0g3507.overlay b/samples/drivers/pwm/capture/boards/lp_mspm0g3507.overlay new file mode 100644 index 00000000000..0e1422ab2d6 --- /dev/null +++ b/samples/drivers/pwm/capture/boards/lp_mspm0g3507.overlay @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2025 Linumiz GmbH + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/ { + aliases { + capture = &pwma1; + }; +}; + +&pinctrl { + tima1_ccp0_pb4 { + input-enable; + bias-pull-down; + }; +}; + +&tima1 { + status = "okay"; + + pwma1: pwma1 { + pinctrl-0 = <&tima1_ccp0_pb4>; + pinctrl-names = "default"; + ti,cc-index = <0>; + ti,cc-mode = "PULSE_WIDTH"; + ti,period = <0xFFFF>; + status = "okay"; + }; +}; diff --git a/samples/drivers/pwm/capture/prj.conf b/samples/drivers/pwm/capture/prj.conf new file mode 100644 index 00000000000..df27a9df192 --- /dev/null +++ b/samples/drivers/pwm/capture/prj.conf @@ -0,0 +1,3 @@ +CONFIG_PWM=y +CONFIG_PWM_CAPTURE=y +CONFIG_REQUIRES_FLOAT_PRINTF=y diff --git a/samples/drivers/pwm/capture/sample.yaml b/samples/drivers/pwm/capture/sample.yaml new file mode 100644 index 00000000000..b9e3823ae6f --- /dev/null +++ b/samples/drivers/pwm/capture/sample.yaml @@ -0,0 +1,20 @@ +sample: + name: PWM capture + description: input capture for PWM signal, time and duration application +common: + tags: + - drivers + - pwm + harness: console + harness_config: + type: multi_line + ordered: true + regex: + - "timclk ([0-9]*) Hz" + - "{period: ([0-9]*) pulse width: ([0-9]*)} in TIMCLK cycle" + - "{period: ([0-9]*) Hz duty: ([0-9]*)}" + depends_on: pwm +tests: + sample.drivers.pwm.capture: + platform_allow: + - lp_mspm0g3507 diff --git a/samples/drivers/pwm/capture/src/main.c b/samples/drivers/pwm/capture/src/main.c new file mode 100644 index 00000000000..1028b310b48 --- /dev/null +++ b/samples/drivers/pwm/capture/src/main.c @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2025 Linumiz + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include "zephyr/drivers/pwm.h" + +int main(void) +{ + int ret; + uint64_t tim_clk_cycles; + const struct device *const dev = DEVICE_DT_GET(DT_ALIAS(capture)); + + printk("PWM capture %s\n", CONFIG_BOARD_TARGET); + if (!device_is_ready(dev)) { + printk("device is not ready\n"); + return 0; + } + + pwm_get_cycles_per_sec(dev, 0, &tim_clk_cycles); + printk("timclk %llu Hz\n", tim_clk_cycles); + + while (1) { + uint32_t period = 0; + uint32_t width = 0; + + k_msleep(250); + ret = pwm_capture_cycles(dev, 0, + (PWM_CAPTURE_TYPE_BOTH | PWM_CAPTURE_MODE_SINGLE), + &period, &width, Z_TIMEOUT_MS(500)); + + if (ret == -EBUSY) { + pwm_disable_capture(dev, 0); + continue; + } + + if (ret) { + printk("capture cycle err %d\n", ret); + continue; + } + + printk("{period:%u pulse width: %u} in TIMCLK cycle\n", + period, + width); + + printk("{period: %f Hz duty: %f}\n", + (float)tim_clk_cycles / (float)period, + (float)(width * 100) / (float)period); + } + + return 0; +} diff --git a/samples/drivers/pwm/index.rst b/samples/drivers/pwm/index.rst new file mode 100644 index 00000000000..c9f219c72c9 --- /dev/null +++ b/samples/drivers/pwm/index.rst @@ -0,0 +1,5 @@ +.. zephyr:code-sample-category:: pwm + :name: PWM + :show-listing: + + These samples demonstrate how to use the :ref:`pwm ` driver API.