Skip to content

Commit 7ed3c4f

Browse files
ssekar15nashif
authored andcommitted
samples: drivers: pwm: Add a sample to test pwm capture
Add a sample to test pwm capture function. Signed-off-by: Saravanan Sekar <[email protected]>
1 parent 7cd96d6 commit 7ed3c4f

File tree

7 files changed

+160
-0
lines changed

7 files changed

+160
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
project(pwm_capture)
7+
8+
target_sources(app PRIVATE src/main.c)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
.. zephyr:code-sample:: capture
2+
:name: PWM Capture
3+
:relevant-api: pwm_interface
4+
5+
Capture a PWM signal.
6+
7+
Overview
8+
********
9+
This sample provides an example application using the :ref:`PWM API <pwm_api>` capture
10+
API to measure the period and pulse width of an external PWM signal.
11+
12+
Requirements
13+
************
14+
15+
This sample requires the support of a timer IP compatible with pwm capture block.
16+
17+
Building and Running
18+
********************
19+
20+
.. zephyr-app-commands::
21+
:zephyr-app: samples/drivers/pwm/capture
22+
:host-os: unix
23+
:board: lp_mspm0g3507
24+
:goals: run
25+
:compact:
26+
27+
Sample Output
28+
=============
29+
30+
.. code-block:: console
31+
32+
PWM capture lp_mspm0g3507/mspm0g350
33+
34+
timclk 80000000 Hz
35+
{period:1000 pulse width: 499} in TIMCLK cycle
36+
{period: 80000.000000 Hz duty: 49.900002}
37+
38+
<repeats endlessly>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (c) 2025 Linumiz GmbH
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/ {
8+
aliases {
9+
capture = &pwma1;
10+
};
11+
};
12+
13+
&pinctrl {
14+
tima1_ccp0_pb4 {
15+
input-enable;
16+
bias-pull-down;
17+
};
18+
};
19+
20+
&tima1 {
21+
status = "okay";
22+
23+
pwma1: pwma1 {
24+
pinctrl-0 = <&tima1_ccp0_pb4>;
25+
pinctrl-names = "default";
26+
ti,cc-index = <0>;
27+
ti,cc-mode = "PULSE_WIDTH";
28+
ti,period = <0xFFFF>;
29+
status = "okay";
30+
};
31+
};

samples/drivers/pwm/capture/prj.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CONFIG_PWM=y
2+
CONFIG_PWM_CAPTURE=y
3+
CONFIG_REQUIRES_FLOAT_PRINTF=y
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
sample:
2+
name: PWM capture
3+
description: input capture for PWM signal, time and duration application
4+
common:
5+
tags:
6+
- drivers
7+
- pwm
8+
harness: console
9+
harness_config:
10+
type: multi_line
11+
ordered: true
12+
regex:
13+
- "timclk ([0-9]*) Hz"
14+
- "{period: ([0-9]*) pulse width: ([0-9]*)} in TIMCLK cycle"
15+
- "{period: ([0-9]*) Hz duty: ([0-9]*)}"
16+
depends_on: pwm
17+
tests:
18+
sample.drivers.pwm.capture:
19+
platform_allow:
20+
- lp_mspm0g3507
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
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+
}

samples/drivers/pwm/index.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.. zephyr:code-sample-category:: pwm
2+
:name: PWM
3+
:show-listing:
4+
5+
These samples demonstrate how to use the :ref:`pwm <pwm_api>` driver API.

0 commit comments

Comments
 (0)