| /* |
| * Copyright (c) 2016 Intel Corporation |
| * |
| * SPDX-License-Identifier: Apache-2.0 |
| */ |
| |
| /** |
| * @file Sample app to demonstrate PWM. |
| * |
| * This app uses PWM[0]. |
| */ |
| |
| #include <zephyr.h> |
| #include <misc/printk.h> |
| #include <device.h> |
| #include <pwm.h> |
| |
| #if defined(CONFIG_SOC_STM32F401XE) || defined(CONFIG_SOC_STM32L476XX) |
| #define PWM_DRIVER "PWM_2" |
| #define PWM_CHANNEL 1 |
| #elif CONFIG_SOC_STM32F103XB |
| #define PWM_DRIVER "PWM_1" |
| #define PWM_CHANNEL 1 |
| #else |
| #define PWM_DRIVER "PWM_0" |
| #define PWM_CHANNEL 0 |
| #endif |
| |
| /* |
| * 50 is flicker fusion threshold. Modulated light will be perceived |
| * as steady by our eyes when blinking rate is at least 50. |
| */ |
| #define PERIOD (USEC_PER_SEC / 50) |
| |
| /* in micro second */ |
| #define FADESTEP 2000 |
| |
| void main(void) |
| { |
| struct device *pwm_dev; |
| uint32_t pulse_width = 0; |
| uint8_t dir = 0; |
| |
| printk("PWM demo app-fade LED\n"); |
| |
| pwm_dev = device_get_binding(PWM_DRIVER); |
| if (!pwm_dev) { |
| printk("Cannot find %s!\n", PWM_DRIVER); |
| return; |
| } |
| |
| while (1) { |
| if (pwm_pin_set_usec(pwm_dev, PWM_CHANNEL, |
| PERIOD, pulse_width)) { |
| printk("pwm pin set fails\n"); |
| return; |
| } |
| |
| if (dir) { |
| if (pulse_width < FADESTEP) { |
| dir = 0; |
| pulse_width = 0; |
| } else { |
| pulse_width -= FADESTEP; |
| } |
| } else { |
| pulse_width += FADESTEP; |
| |
| if (pulse_width >= PERIOD) { |
| dir = 1; |
| pulse_width = PERIOD; |
| } |
| } |
| |
| k_sleep(MSEC_PER_SEC); |
| } |
| } |