zephyr/samples/basic/servo_motor/src/main.c
Henrik Brix Andersen db611e6781 drivers: pwm: add support for inverted PWM signals
Add support for requesting an inverted PWM pulse (active-low) when
setting up the period and pulse width of a PWM pin. This is useful
when driving external, active-low circuitry (e.g. an LED) with a PWM
signal.

All in-tree PWM drivers is updated to match the new API signature, but
no driver support for inverted PWM signals is added yet.

All in-tree PWM consumers are updated to pass a flags value of 0
(0 meaning default, which is normal PWM polarity).

Fixes #21384.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2020-01-07 18:13:18 +01:00

72 lines
1.3 KiB
C

/*
* 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 <sys/printk.h>
#include <device.h>
#include <drivers/pwm.h>
#ifndef DT_ALIAS_PWM_0_LABEL
#error "Choose supported board or add new board for the application"
#endif
/*
* Unlike pulse width, period is not a critical parameter for
* motor control. 20ms is commonly used.
*/
#define PERIOD (USEC_PER_SEC / 50U)
/* all in micro second */
#define STEPSIZE 100
#define MINPULSEWIDTH 700
#define MAXPULSEWIDTH 2300
void main(void)
{
struct device *pwm_dev;
u32_t pulse_width = MINPULSEWIDTH;
u8_t dir = 0U;
printk("PWM demo app-servo control\n");
pwm_dev = device_get_binding(DT_ALIAS_PWM_0_LABEL);
if (!pwm_dev) {
printk("Cannot find PWM device!\n");
return;
}
while (1) {
if (pwm_pin_set_usec(pwm_dev, 0, PERIOD, pulse_width, 0)) {
printk("pwm pin set fails\n");
return;
}
if (dir) {
if (pulse_width <= MINPULSEWIDTH) {
dir = 0U;
pulse_width = MINPULSEWIDTH;
} else {
pulse_width -= STEPSIZE;
}
} else {
pulse_width += STEPSIZE;
if (pulse_width >= MAXPULSEWIDTH) {
dir = 1U;
pulse_width = MAXPULSEWIDTH;
}
}
k_sleep(MSEC_PER_SEC);
}
}