| 1 |
52 |
zero_gravi |
// #################################################################################################
|
| 2 |
|
|
// # << NEORV32 - Smart LED (NeoPixel/WS2812) Hardware Interface (NEOLED) Demo Program >> #
|
| 3 |
|
|
// # ********************************************************************************************* #
|
| 4 |
|
|
// # BSD 3-Clause License #
|
| 5 |
|
|
// # #
|
| 6 |
|
|
// # Copyright (c) 2021, Stephan Nolting. All rights reserved. #
|
| 7 |
|
|
// # #
|
| 8 |
|
|
// # Redistribution and use in source and binary forms, with or without modification, are #
|
| 9 |
|
|
// # permitted provided that the following conditions are met: #
|
| 10 |
|
|
// # #
|
| 11 |
|
|
// # 1. Redistributions of source code must retain the above copyright notice, this list of #
|
| 12 |
|
|
// # conditions and the following disclaimer. #
|
| 13 |
|
|
// # #
|
| 14 |
|
|
// # 2. Redistributions in binary form must reproduce the above copyright notice, this list of #
|
| 15 |
|
|
// # conditions and the following disclaimer in the documentation and/or other materials #
|
| 16 |
|
|
// # provided with the distribution. #
|
| 17 |
|
|
// # #
|
| 18 |
|
|
// # 3. Neither the name of the copyright holder nor the names of its contributors may be used to #
|
| 19 |
|
|
// # endorse or promote products derived from this software without specific prior written #
|
| 20 |
|
|
// # permission. #
|
| 21 |
|
|
// # #
|
| 22 |
|
|
// # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS #
|
| 23 |
|
|
// # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF #
|
| 24 |
|
|
// # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE #
|
| 25 |
|
|
// # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, #
|
| 26 |
|
|
// # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
|
| 27 |
|
|
// # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED #
|
| 28 |
|
|
// # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING #
|
| 29 |
|
|
// # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED #
|
| 30 |
|
|
// # OF THE POSSIBILITY OF SUCH DAMAGE. #
|
| 31 |
|
|
// # ********************************************************************************************* #
|
| 32 |
|
|
// # The NEORV32 Processor - https://github.com/stnolting/neorv32 (c) Stephan Nolting #
|
| 33 |
|
|
// #################################################################################################
|
| 34 |
|
|
|
| 35 |
|
|
|
| 36 |
|
|
/**********************************************************************//**
|
| 37 |
|
|
* @file demo_neopixel/main.c
|
| 38 |
|
|
* @author Stephan Nolting
|
| 39 |
|
|
* @brief NeoPixel (WS2812) interface demo using the processor's smart LED interface (NEOLED).
|
| 40 |
|
|
**************************************************************************/
|
| 41 |
|
|
|
| 42 |
|
|
#include <neorv32.h>
|
| 43 |
|
|
|
| 44 |
|
|
|
| 45 |
|
|
/**********************************************************************//**
|
| 46 |
|
|
* @name User configuration
|
| 47 |
|
|
**************************************************************************/
|
| 48 |
|
|
/**@{*/
|
| 49 |
|
|
/** UART BAUD rate */
|
| 50 |
|
|
#define BAUD_RATE 19200
|
| 51 |
|
|
/** Number of RGB LEDs in stripe A (24-bit data) */
|
| 52 |
|
|
#define NUM_LEDS_24BIT (12)
|
| 53 |
|
|
/** Number of RGBW LEDs in stripe B (32-bit data) */
|
| 54 |
|
|
#define NUM_LEDS_32BIT (8)
|
| 55 |
|
|
/**@}*/
|
| 56 |
|
|
|
| 57 |
|
|
|
| 58 |
|
|
/**********************************************************************//**
|
| 59 |
|
|
* Main function
|
| 60 |
|
|
* This demo uses two NeoPixel stripes: Stripe A is a 12-LED RGB ring (arranged as ring - NOT CONNECTED as ring), stripe B is a 8-LED RGBW stripe
|
| 61 |
|
|
*
|
| 62 |
|
|
* @note This program requires the NEOLED controller to be synthesized (UART0 is optional).
|
| 63 |
|
|
* @note NeoPixel stripe connection: NEORV32.neoled_o -> Stripe A ("NUM_LEDS_24BIT" RGB-LEDs) -> Stripe B ("NUM_LEDS_32BIT" RGBW LEDs)
|
| 64 |
|
|
*
|
| 65 |
|
|
* @return Irrelevant.
|
| 66 |
|
|
**************************************************************************/
|
| 67 |
|
|
int main() {
|
| 68 |
|
|
|
| 69 |
|
|
// capture all exceptions and give debug info via UART0
|
| 70 |
|
|
// this is not required, but keeps us safe
|
| 71 |
|
|
neorv32_rte_setup();
|
| 72 |
|
|
|
| 73 |
|
|
|
| 74 |
|
|
// init UART0 at default baud rate, no parity bits, no hw flow control
|
| 75 |
|
|
neorv32_uart_setup(BAUD_RATE, PARITY_NONE, FLOW_CONTROL_NONE);
|
| 76 |
|
|
neorv32_uart0_printf("<<< NEORV32 NeoPixel (WS2812) hardware interface (NEOLED) demo >>>\n");
|
| 77 |
|
|
neorv32_uart0_printf("(c) 'NeoPixel' is a trademark of Adafruit Industries.\n");
|
| 78 |
|
|
|
| 79 |
|
|
|
| 80 |
|
|
// check if NEOLED unit is implemented at all, abort if not
|
| 81 |
|
|
if (neorv32_neoled_available() == 0) {
|
| 82 |
|
|
neorv32_uart_printf("Error! No NEOLED unit synthesized!\n");
|
| 83 |
|
|
return 0;
|
| 84 |
|
|
}
|
| 85 |
|
|
|
| 86 |
|
|
|
| 87 |
|
|
// clearify setup
|
| 88 |
|
|
neorv32_uart0_printf("\nThis demo uses the following LED setup:\n");
|
| 89 |
|
|
neorv32_uart0_printf("NEORV32.neoled_o -> %u RGB-LEDs (24-bit) -> %u RGBW-LEDs (32-bit)\n\n", (uint32_t)NUM_LEDS_24BIT, (uint32_t)NUM_LEDS_32BIT);
|
| 90 |
|
|
|
| 91 |
|
|
|
| 92 |
|
|
// use the "neorv32_neoled_setup_ws2812()" setup function here instead the raw "neorv32_neoled_setup_raw()"
|
| 93 |
|
|
// neorv32_neoled_setup_ws2812() will configure all timing parameters according to the WS2812 specs. for the current processor clock speed
|
| 94 |
|
|
neorv32_neoled_setup_ws2812(0); // use bscon = 0 (busy_flag clears / IRQ fires if at least one buffer entry is free)
|
| 95 |
|
|
|
| 96 |
|
|
|
| 97 |
|
|
// check NEOLED configuration
|
| 98 |
|
|
neorv32_uart0_printf("Checking NEOLED configuration:\n", neorv32_neoled_get_buffer_size());
|
| 99 |
|
|
neorv32_uart0_printf(" Hardware buffer size: %u entries\n", neorv32_neoled_get_buffer_size());
|
| 100 |
|
|
neorv32_uart0_printf(" Control register: 0x%x\n\n", NEOLED_CT);
|
| 101 |
|
|
|
| 102 |
|
|
|
| 103 |
|
|
// clear all LEDs
|
| 104 |
|
|
neorv32_uart0_printf("Clearing all LEDs...\n");
|
| 105 |
|
|
int i;
|
| 106 |
|
|
for (i=0; i<(NUM_LEDS_24BIT+NUM_LEDS_32BIT); i++) { // just send a lot of zeros
|
| 107 |
|
|
neorv32_neoled_send_polling(1, 0); // mode = 1 = 32-bit, -> send 32 zero bits in each iteration
|
| 108 |
|
|
}
|
| 109 |
|
|
neorv32_cpu_delay_ms(1000);
|
| 110 |
|
|
|
| 111 |
|
|
|
| 112 |
|
|
// a simple (but fancy!) animation example
|
| 113 |
|
|
neorv32_uart0_printf("Starting animation...\n");
|
| 114 |
|
|
int stripe_pos_rgb = 0, flash_position = 0, flash_direction = -1;
|
| 115 |
|
|
int stripe_pos_rgbw = 0, circle_position = 0;
|
| 116 |
|
|
uint32_t circle_color = 0x00000004;
|
| 117 |
|
|
|
| 118 |
|
|
while (1) {
|
| 119 |
|
|
|
| 120 |
|
|
// RGB LEDs: turning circle, changes color after each completed cycle
|
| 121 |
|
|
for (stripe_pos_rgb=0; stripe_pos_rgb<NUM_LEDS_24BIT; stripe_pos_rgb++) {
|
| 122 |
|
|
if (stripe_pos_rgb == circle_position) {
|
| 123 |
|
|
neorv32_neoled_send_polling(0, circle_color);
|
| 124 |
|
|
}
|
| 125 |
|
|
else {
|
| 126 |
|
|
neorv32_neoled_send_polling(0, 0); // LED off
|
| 127 |
|
|
}
|
| 128 |
|
|
}
|
| 129 |
|
|
if (circle_position == (NUM_LEDS_24BIT-1)) {
|
| 130 |
|
|
circle_position = 0;
|
| 131 |
|
|
circle_color = (circle_color << 8) | ((circle_color >> 16) & 0xff);
|
| 132 |
|
|
}
|
| 133 |
|
|
else {
|
| 134 |
|
|
circle_position++;
|
| 135 |
|
|
}
|
| 136 |
|
|
|
| 137 |
|
|
|
| 138 |
|
|
// RGBW LEDs: knight rider!
|
| 139 |
|
|
if ((flash_position == (NUM_LEDS_32BIT-1)) || (flash_position == 0)) {
|
| 140 |
|
|
flash_direction = -flash_direction;
|
| 141 |
|
|
}
|
| 142 |
|
|
for (stripe_pos_rgbw=0; stripe_pos_rgbw<NUM_LEDS_32BIT; stripe_pos_rgbw++) {
|
| 143 |
|
|
if (stripe_pos_rgbw == flash_position) {
|
| 144 |
|
|
neorv32_neoled_send_polling(1, 0x00000004); // white dot using the dedicated white LED chip
|
| 145 |
|
|
}
|
| 146 |
|
|
else {
|
| 147 |
|
|
neorv32_neoled_send_polling(1, 0); // LED off
|
| 148 |
|
|
}
|
| 149 |
|
|
}
|
| 150 |
|
|
flash_position += flash_direction;
|
| 151 |
|
|
|
| 152 |
|
|
|
| 153 |
|
|
// delay between frames; also used to "send" ws2812.reset command
|
| 154 |
|
|
neorv32_cpu_delay_ms(100);
|
| 155 |
|
|
}
|
| 156 |
|
|
|
| 157 |
|
|
return 0;
|
| 158 |
|
|
}
|