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:
parent
22a10c3b6c
commit
9824608935
6
samples/drivers/pwm/rgb_led/Makefile
Normal file
6
samples/drivers/pwm/rgb_led/Makefile
Normal file
|
@ -0,0 +1,6 @@
|
|||
BOARD ?= arduino_101
|
||||
|
||||
KERNEL_TYPE ?= nano
|
||||
CONF_FILE = prj.conf
|
||||
|
||||
include ${ZEPHYR_BASE}/Makefile.inc
|
21
samples/drivers/pwm/rgb_led/README
Normal file
21
samples/drivers/pwm/rgb_led/README
Normal 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.
|
6
samples/drivers/pwm/rgb_led/prj.conf
Normal file
6
samples/drivers/pwm/rgb_led/prj.conf
Normal 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
|
1
samples/drivers/pwm/rgb_led/src/Makefile
Normal file
1
samples/drivers/pwm/rgb_led/src/Makefile
Normal file
|
@ -0,0 +1 @@
|
|||
obj-y = main.o
|
83
samples/drivers/pwm/rgb_led/src/main.c
Normal file
83
samples/drivers/pwm/rgb_led/src/main.c
Normal 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
6
samples/drivers/pwm/rgb_led/testcase.ini
Normal file
6
samples/drivers/pwm/rgb_led/testcase.ini
Normal file
|
@ -0,0 +1,6 @@
|
|||
[test]
|
||||
build_only = true
|
||||
tags = apps
|
||||
|
||||
arch_whitelist = x86
|
||||
platform_whitelist = arduino_101
|
Loading…
Reference in a new issue