File: pwmtestperf.c

package info (click to toggle)
libpwm 1.0~rc2-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 1,596 kB
  • sloc: sh: 4,443; ansic: 859; makefile: 58
file content (78 lines) | stat: -rw-r--r-- 1,466 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/* SPDX-License-Identifier: 0BSD */
/* SPDX-FileCopyrightText: 2023-2025 Uwe Kleine-König <u.kleine-koenig@baylibre.com> */

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <pwm.h>

int main(int argc, char *const argv[])
{
	struct pwm_chip *chip;
	struct pwm *pwm;
	struct pwm_waveform wf = {
		.period_length_ns = 50000,
		.duty_length_ns = 0,
		.duty_offset_ns = 0,
	};
	int ret;
	int opt;
	long long step = 1;

	unsigned int chipno = 0;
	unsigned int pwmno = 0;

	while ((opt = getopt(argc, argv, "c:p:P:S:")) != -1) {
		switch (opt) {
		case 'c':
			chipno = atoi(optarg);
			break;

		case 'p':
			pwmno = atoi(optarg);
			break;

		case 'P':
			wf.period_length_ns = atoll(optarg);
			break;

		case 'S':
			step = atoll(optarg);
			if (!step) {
				errno = EINVAL;
				perror("Invalid step value");
				return EXIT_FAILURE;
			}
			break;

		default:
			return EXIT_FAILURE;
		}
	}

	chip = pwm_chip_open_by_number(chipno);
	if (!chip) {
		perror("Failed to open pwmchip0");
		return EXIT_FAILURE;
	}

	pwm = pwm_chip_get_pwm(chip, pwmno);
	if (!pwm) {
		perror("Failed to get pwm0");
		return EXIT_FAILURE;
	}

	for (wf.duty_length_ns = (step > 0 ? 0 : wf.period_length_ns); wf.duty_length_ns <= wf.period_length_ns; wf.duty_length_ns += step) {
		ret = pwm_set_waveform(pwm, &wf);
		if (ret < 0) {
			perror("Failed to configure PWM");
			return EXIT_FAILURE;
		}
	}

	pwm_chip_close(chip);

	return EXIT_SUCCESS;
}