1 /* 2 * Copyright (c) 2016 Intel Corporation 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 7 /** 8 * @file Sample app to demonstrate PWM-based RGB LED control 9 */ 10 11 #include <zephyr/kernel.h> 12 #include <zephyr/sys/printk.h> 13 #include <zephyr/device.h> 14 #include <zephyr/drivers/pwm.h> 15 16 static const struct pwm_dt_spec red_pwm_led = 17 PWM_DT_SPEC_GET(DT_ALIAS(red_pwm_led)); 18 static const struct pwm_dt_spec green_pwm_led = 19 PWM_DT_SPEC_GET(DT_ALIAS(green_pwm_led)); 20 static const struct pwm_dt_spec blue_pwm_led = 21 PWM_DT_SPEC_GET(DT_ALIAS(blue_pwm_led)); 22 23 #define STEP_SIZE PWM_USEC(2000) 24 main(void)25int main(void) 26 { 27 uint32_t pulse_red, pulse_green, pulse_blue; /* pulse widths */ 28 int ret; 29 30 printk("PWM-based RGB LED control\n"); 31 32 if (!pwm_is_ready_dt(&red_pwm_led) || 33 !pwm_is_ready_dt(&green_pwm_led) || 34 !pwm_is_ready_dt(&blue_pwm_led)) { 35 printk("Error: one or more PWM devices not ready\n"); 36 return 0; 37 } 38 39 while (1) { 40 for (pulse_red = 0U; pulse_red <= red_pwm_led.period; 41 pulse_red += STEP_SIZE) { 42 ret = pwm_set_pulse_dt(&red_pwm_led, pulse_red); 43 if (ret != 0) { 44 printk("Error %d: red write failed\n", ret); 45 return 0; 46 } 47 48 for (pulse_green = 0U; 49 pulse_green <= green_pwm_led.period; 50 pulse_green += STEP_SIZE) { 51 ret = pwm_set_pulse_dt(&green_pwm_led, 52 pulse_green); 53 if (ret != 0) { 54 printk("Error %d: green write failed\n", 55 ret); 56 return 0; 57 } 58 59 for (pulse_blue = 0U; 60 pulse_blue <= blue_pwm_led.period; 61 pulse_blue += STEP_SIZE) { 62 ret = pwm_set_pulse_dt(&blue_pwm_led, 63 pulse_blue); 64 if (ret != 0) { 65 printk("Error %d: " 66 "blue write failed\n", 67 ret); 68 return 0; 69 } 70 k_sleep(K_SECONDS(1)); 71 } 72 } 73 } 74 } 75 return 0; 76 } 77