/* * Copyright (c) 2016 Intel Corporation * Copyright (c) 2020 Nordic Semiconductor ASA * * SPDX-License-Identifier: Apache-2.0 */ /** * @file Sample app to demonstrate PWM-based LED fade */ #include #include #include #include #define PWM_LED0_NODE DT_ALIAS(pwm_led0) #if DT_NODE_HAS_STATUS(PWM_LED0_NODE, okay) #define PWM_CTLR DT_PWMS_CTLR(PWM_LED0_NODE) #define PWM_CHANNEL DT_PWMS_CHANNEL(PWM_LED0_NODE) #define PWM_FLAGS DT_PWMS_FLAGS(PWM_LED0_NODE) #else #error "Unsupported board: pwm-led0 devicetree alias is not defined" #define PWM_CTLR DT_INVALID_NODE #define PWM_CHANNEL 0 #define PWM_FLAGS 0 #endif /* * This period should be fast enough to be above the flicker fusion * threshold. The steps should also be small enough, and happen * quickly enough, to make the output fade change appear continuous. */ #define PERIOD_USEC 20000U #define NUM_STEPS 50U #define STEP_USEC (PERIOD_USEC / NUM_STEPS) #define SLEEP_MSEC 25U void main(void) { const struct device *pwm; uint32_t pulse_width = 0U; uint8_t dir = 1U; int ret; printk("PWM-based LED fade\n"); pwm = DEVICE_DT_GET(PWM_CTLR); if (!device_is_ready(pwm)) { printk("Error: PWM device %s is not ready\n", pwm->name); return; } while (1) { ret = pwm_pin_set_usec(pwm, PWM_CHANNEL, PERIOD_USEC, pulse_width, PWM_FLAGS); if (ret) { printk("Error %d: failed to set pulse width\n", ret); return; } if (dir) { pulse_width += STEP_USEC; if (pulse_width >= PERIOD_USEC) { pulse_width = PERIOD_USEC - STEP_USEC; dir = 0U; } } else { if (pulse_width >= STEP_USEC) { pulse_width -= STEP_USEC; } else { pulse_width = STEP_USEC; dir = 1U; } } k_sleep(K_MSEC(SLEEP_MSEC)); } }