samples: drivers: pwm: Add a sample to test pwm capture
Add a sample to test pwm capture function. Signed-off-by: Saravanan Sekar <saravanan@linumiz.com>
This commit is contained in:
parent
7cd96d693e
commit
7ed3c4fc46
8
samples/drivers/pwm/capture/CMakeLists.txt
Normal file
8
samples/drivers/pwm/capture/CMakeLists.txt
Normal file
@ -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)
|
||||
38
samples/drivers/pwm/capture/README.rst
Normal file
38
samples/drivers/pwm/capture/README.rst
Normal file
@ -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 <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}
|
||||
|
||||
<repeats endlessly>
|
||||
31
samples/drivers/pwm/capture/boards/lp_mspm0g3507.overlay
Normal file
31
samples/drivers/pwm/capture/boards/lp_mspm0g3507.overlay
Normal file
@ -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";
|
||||
};
|
||||
};
|
||||
3
samples/drivers/pwm/capture/prj.conf
Normal file
3
samples/drivers/pwm/capture/prj.conf
Normal file
@ -0,0 +1,3 @@
|
||||
CONFIG_PWM=y
|
||||
CONFIG_PWM_CAPTURE=y
|
||||
CONFIG_REQUIRES_FLOAT_PRINTF=y
|
||||
20
samples/drivers/pwm/capture/sample.yaml
Normal file
20
samples/drivers/pwm/capture/sample.yaml
Normal file
@ -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
|
||||
55
samples/drivers/pwm/capture/src/main.c
Normal file
55
samples/drivers/pwm/capture/src/main.c
Normal file
@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Linumiz
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <zephyr/kernel.h>
|
||||
#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;
|
||||
}
|
||||
5
samples/drivers/pwm/index.rst
Normal file
5
samples/drivers/pwm/index.rst
Normal file
@ -0,0 +1,5 @@
|
||||
.. zephyr:code-sample-category:: pwm
|
||||
:name: PWM
|
||||
:show-listing:
|
||||
|
||||
These samples demonstrate how to use the :ref:`pwm <pwm_api>` driver API.
|
||||
Loading…
Reference in New Issue
Block a user