Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | /*
* Copyright (c) 2016 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file Sample app to utilize APA102C LED on Arduino 101 (x86).
*
* On x86 side of Arduino 101:
* 1. GPIO_16 is on IO8 (for data)
* 2. GPIO_19 is on IO4 (for clock)
*
* The gpio_dw driver is being used for bit-banging.
*
* The APA102/C requires 5V data and clock signals, so logic
* level shifter (preferred) or pull-up resistors are needed.
* Make sure the pins are 5V tolerant if using pull-up
* resistors.
*
* WARNING: the APA102C are very bright even at low settings.
* Protect your eyes and do not look directly into those LEDs.
*/
#include <zephyr.h>
#include <misc/printk.h>
#include <device.h>
#include <gpio.h>
/* in millisecond */
#define SLEEPTIME 250
#define GPIO_DATA_PIN 16
#define GPIO_CLK_PIN 19
#define GPIO_NAME "GPIO_"
#define GPIO_DRV_NAME CONFIG_GPIO_QMSI_0_NAME
#define APA102C_START_FRAME 0x00000000
#define APA102C_END_FRAME 0xFFFFFFFF
/* The LED is very bright. So to protect the eyes,
* brightness is set very low, and RGB values are
* set low too.
*/
uint32_t rgb[] = {
0xE1000010,
0xE1001000,
0xE1100000,
0xE1101010,
};
#define NUM_RGB 4
/* Number of LEDS linked together */
#define NUM_LEDS 1
void send_rgb(struct device *gpio_dev, uint32_t rgb)
{
int i;
for (i = 0; i < 32; i++) {
/* MSB goes in first */
gpio_pin_write(gpio_dev, GPIO_DATA_PIN, !!(rgb & 0x80000000));
/* Latch data into LED */
gpio_pin_write(gpio_dev, GPIO_CLK_PIN, 1);
gpio_pin_write(gpio_dev, GPIO_CLK_PIN, 0);
rgb <<= 1;
}
}
void main(void)
{
struct device *gpio_dev;
int ret;
int idx = 0;
int leds = 0;
gpio_dev = device_get_binding(GPIO_DRV_NAME);
if (!gpio_dev) {
printk("Cannot find %s!\n", GPIO_DRV_NAME);
return;
}
/* Setup GPIO output */
ret = gpio_pin_configure(gpio_dev, GPIO_DATA_PIN, (GPIO_DIR_OUT));
if (ret) {
printk("Error configuring " GPIO_NAME "%d!\n", GPIO_DATA_PIN);
}
ret = gpio_pin_configure(gpio_dev, GPIO_CLK_PIN, (GPIO_DIR_OUT));
if (ret) {
printk("Error configuring " GPIO_NAME "%d!\n", GPIO_CLK_PIN);
}
while (1) {
send_rgb(gpio_dev, APA102C_START_FRAME);
for (leds = 0; leds < NUM_LEDS; leds++) {
send_rgb(gpio_dev, rgb[(idx + leds) % NUM_RGB]);
}
/* If there are more LEDs linked together,
* then what NUM_LEDS is, the NUM_LEDS+1
* LED is going to be full bright.
*/
send_rgb(gpio_dev, APA102C_END_FRAME);
idx++;
if (idx >= NUM_RGB) {
idx = 0;
}
k_sleep(SLEEPTIME);
}
}
|