samples: pwm: use new API and add rgb led app

Add a new pwm app (rgb led) using the new API.

Following the recent pwm RFC, all existing APIs will
be deprecated. Two new APIs will be added. The changes
for the new APIs and the required driver function have
been posted.

To demonstrate the usage of the new PWM APIs, this app
to drive a rgb led is added. It will use the new API
pwm_pin_set_usec().

This app will be located in a new sub directory.

Jira: ZEP-745 ZEP-1029

Origin: Original

Change-Id: If8e5d8ab22dcf8b7cf3665e38949715c61ca9c03
Signed-off-by: Baohong Liu <baohong.liu@intel.com>
This commit is contained in:
Baohong Liu 2016-10-18 15:35:20 -07:00 committed by Anas Nashif
parent 22a10c3b6c
commit 9824608935
6 changed files with 123 additions and 0 deletions

View file

@ -0,0 +1,6 @@
BOARD ?= arduino_101
KERNEL_TYPE ?= nano
CONF_FILE = prj.conf
include ${ZEPHYR_BASE}/Makefile.inc

View file

@ -0,0 +1,21 @@
This is a sample app which drives a RGB LED using PWM.
There are three LEDs of single color in a RGB LED. The
three LEDs will be driven by a pwm port each. The pulse
width for each pwm port will change from zero to period.
So, the brightness for each LED will change from dark to
max brightness in 11 steps. The three "for" loops (one
for each LED) will generate 1331 combinations and so,
1331 different colors. The whole process will repeat for
ever.
Arduino 101
===========
You will need to connect the LED pins to PWM0, PWM1 and PWM2
on arduino 101 via the shield. Depending on what kind of RGB
LED you are using, please connect the common cathode to the
ground or the common anode to Vcc. You need current limiting
resistor for each of the single color LEDs.
The sample app requires three PWM ports. So, it can not work
on Quark D2000 platform.

View file

@ -0,0 +1,6 @@
CONFIG_STDOUT_CONSOLE=y
CONFIG_PRINTK=y
CONFIG_PWM=y
CONFIG_NANO_TIMERS=y
CONFIG_NANO_TIMEOUTS=y
CONFIG_PWM_QMSI_NUM_PORTS=3

View file

@ -0,0 +1 @@
obj-y = main.o

View file

@ -0,0 +1,83 @@
/*
* Copyright (c) 2016 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file Sample app to demonstrate PWM.
*
* This app uses PWM[0].
*/
#include <zephyr.h>
#include <misc/printk.h>
#include <device.h>
#include <pwm.h>
/*
* 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 STEPSIZE 2000
static int write_pin(struct device *pwm_dev, uint32_t pwm_pin,
uint32_t pulse_width)
{
return pwm_pin_set_usec(pwm_dev, pwm_pin, PERIOD, pulse_width);
}
void main(void)
{
struct device *pwm_dev;
uint32_t pulse_width0, pulse_width1, pulse_width2;
printk("PWM demo app-RGB LED\n");
pwm_dev = device_get_binding("PWM_0");
if (!pwm_dev) {
printk("Cannot find PWM_0!\n");
return;
}
while (1) {
for (pulse_width0 = 0; pulse_width0 <= PERIOD;
pulse_width0 += STEPSIZE) {
if (write_pin(pwm_dev, 0, pulse_width0) != 0) {
printk("pin 0 write fails!\n");
return;
}
for (pulse_width1 = 0; pulse_width1 <= PERIOD;
pulse_width1 += STEPSIZE) {
if (write_pin(pwm_dev, 1, pulse_width1) != 0) {
printk("pin 1 write fails!\n");
return;
}
for (pulse_width2 = 0; pulse_width2 <= PERIOD;
pulse_width2 += STEPSIZE) {
if (write_pin(pwm_dev, 2, pulse_width2)
!= 0) {
printk("pin 2 write fails!\n");
return;
}
task_sleep(SECONDS(1));
}
}
}
}
}

View file

@ -0,0 +1,6 @@
[test]
build_only = true
tags = apps
arch_whitelist = x86
platform_whitelist = arduino_101