1 /*
2  * Copyright (c) 2016 Intel Corporation
3  * Copyright (c) 2020 Nordic Semiconductor ASA
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 /**
9  * @file Sample app to demonstrate PWM.
10  */
11 
12 #include <zephyr/kernel.h>
13 #include <zephyr/sys/printk.h>
14 #include <zephyr/device.h>
15 #include <zephyr/drivers/pwm.h>
16 
17 static const struct pwm_dt_spec pwm_led0 = PWM_DT_SPEC_GET(DT_ALIAS(pwm_led0));
18 
19 #define MIN_PERIOD PWM_SEC(1U) / 128U
20 #define MAX_PERIOD PWM_SEC(1U)
21 
main(void)22 int main(void)
23 {
24 	uint32_t max_period;
25 	uint32_t period;
26 	uint8_t dir = 0U;
27 	int ret;
28 
29 	printk("PWM-based blinky\n");
30 
31 	if (!pwm_is_ready_dt(&pwm_led0)) {
32 		printk("Error: PWM device %s is not ready\n",
33 		       pwm_led0.dev->name);
34 		return 0;
35 	}
36 
37 	/*
38 	 * In case the default MAX_PERIOD value cannot be set for
39 	 * some PWM hardware, decrease its value until it can.
40 	 *
41 	 * Keep its value at least MIN_PERIOD * 4 to make sure
42 	 * the sample changes frequency at least once.
43 	 */
44 	printk("Calibrating for channel %d...\n", pwm_led0.channel);
45 	max_period = MAX_PERIOD;
46 	while (pwm_set_dt(&pwm_led0, max_period, max_period / 2U)) {
47 		max_period /= 2U;
48 		if (max_period < (4U * MIN_PERIOD)) {
49 			printk("Error: PWM device "
50 			       "does not support a period at least %lu\n",
51 			       4U * MIN_PERIOD);
52 			return 0;
53 		}
54 	}
55 
56 	printk("Done calibrating; maximum/minimum periods %u/%lu nsec\n",
57 	       max_period, MIN_PERIOD);
58 
59 	period = max_period;
60 	while (1) {
61 		ret = pwm_set_dt(&pwm_led0, period, period / 2U);
62 		if (ret) {
63 			printk("Error %d: failed to set pulse width\n", ret);
64 			return 0;
65 		}
66 		printk("Using period %d\n", period);
67 
68 		period = dir ? (period * 2U) : (period / 2U);
69 		if (period > max_period) {
70 			period = max_period / 2U;
71 			dir = 0U;
72 		} else if (period < MIN_PERIOD) {
73 			period = MIN_PERIOD * 2U;
74 			dir = 1U;
75 		}
76 
77 		k_sleep(K_SECONDS(4U));
78 	}
79 	return 0;
80 }
81