secret
Last active

WS2811 Test

  • Download Gist
Makefile
Makefile
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
#
# Copyright (C) 2009 Chris McClelland
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
 
TARGET = firmware
MCU = at90usb162
F_CPU = 16000000
 
SRCS = $(shell ls *.c)
OBJS = $(SRCS:%.c=$(OBJDIR)/%.o)
LIBS =
CC = avr-gcc
OBJDIR = .build
DEPDIR = .deps
CFLAGS = \
-O3 -mmcu=$(MCU) -funsigned-char -funsigned-bitfields -ffunction-sections \
-fpack-struct -fshort-enums -finline-limit=20 -Wall -Wstrict-prototypes \
-Wundef -std=gnu99 -Wall -pedantic
LDFLAGS = -mmcu=$(MCU) -Wl,--relax -Wl,--gc-sections -lm
 
all: $(TARGET).hex
 
$(TARGET).hex: $(OBJS)
$(CC) $(LDFLAGS) -Wl,-Map=$(OBJDIR)/$(TARGET).map,--cref -o $(OBJDIR)/$(TARGET).elf $(OBJS) $(LIBS)
avr-objcopy -O ihex -R .eeprom $(OBJDIR)/$(TARGET).elf $(TARGET).hex
 
$(OBJDIR)/%.o : %.c
$(CC) -c $(CFLAGS) -MMD -MP -MF $(DEPDIR)/$(@F).d -Wa,-adhlns=$(OBJDIR)/$<.lst $< -o $@ -DF_CPU=$(F_CPU)
 
clean: FORCE
rm -rf $(OBJDIR) $(TARGET).hex $(DEPDIR)
 
flip: $(TARGET).hex
batchisp -hardware usb -device $(MCU) -operation erase f blankcheck loadbuffer $(TARGET).hex program verify start reset 0
 
dfu: $(TARGET).hex
-dfu-programmer $(MCU) erase
-dfu-programmer $(MCU) flash $(TARGET).hex
-dfu-programmer $(MCU) reset
-dfu-programmer $(MCU) reset
 
-include $(shell mkdir -p $(OBJDIR) $(DEPDIR) 2>/dev/null) $(wildcard $(DEPDIR)/*)
 
FORCE:
delay_x.h
C
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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
/*
Copyright (c) 2005, Hans-Juergen Heinrichs
All rights reserved.
 
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
 
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
 
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
 
* Neither the name of the copyright holders nor the names of
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
 
 
/*
* delay_x.h
*
* Accurate delays ranging from a single CPU cycle up to
* more than 500 second (e.g. with 8MHz device):
*
* The idea for the functions below was heavily inspired by the
* file <avr/delay.h> which is part of the excellent WinAVR
* distribution. Therefore, thanks to Marek Michalkiewicz and
* Joerg Wunsch.
*
* The idea is to have the GCC preprocessor handle all calculations
* necessary for determining the exact implementation of a delay
* algorithm. The implementation itself is then inlined into the
* user code.
* In this way it is possible to always get the code size optimized
* delay implementation.
*
* !!======================================================!!
* !! Requires compile time constants for the delay !!
* !! Requires compiler optimization !!
* !!======================================================!!
*
*/
 
#ifndef _AVR_DELAY_X_H_
#define _AVR_DELAY_X_H_ 1
 
#include <inttypes.h>
 
#ifndef F_CPU
# warning "Macro F_CPU must be defined"
#endif
 
 
/*
*
* _ d e l a y _ n s (double __ns)
* _ d e l a y _ u s (double __us)
* _ d e l a y _ m s (double __ms)
* _ d e l a y _ s (double __s)
*
* Perform a very exact delay with a resolution as accurate as a
* single CPU clock (the macro F_CPU is supposed to be defined to a
* constant defining the CPU clock frequency in Hertz).
*
*/
#define _delay_ns(__ns) _delay_cycles( (double)(F_CPU)*((double)__ns)/1.0e9 + 0.5 )
#define _delay_us(__us) _delay_cycles( (double)(F_CPU)*((double)__us)/1.0e6 + 0.5 )
#define _delay_ms(__ms) _delay_cycles( (double)(F_CPU)*((double)__ms)/1.0e3 + 0.5 )
#define _delay_s( __s) _delay_cycles( (double)(F_CPU)*((double)__s )/1.0e0 + 0.5 )
 
/* ==========================================================================*/
 
/*
* Forward declaration for all functions with attribute
* 'always_inline' enforces GCC to inline the code (even
* if it would be better not to do so from optimization
* perspective).
* Without this attribute GCC is free to implement
* inline code or not (using the keyword 'inline'
* alone is not sufficient).
*
*/
static __inline__ void _NOP1( void) __attribute__((always_inline));
static __inline__ void _NOP2( void) __attribute__((always_inline));
static __inline__ void _NOP3( void) __attribute__((always_inline));
static __inline__ void _NOP4( void) __attribute__((always_inline));
static __inline__ void _NOP5( void) __attribute__((always_inline));
static __inline__ void _NOP6( void) __attribute__((always_inline));
static __inline__ void _NOP7( void) __attribute__((always_inline));
static __inline__ void _NOP8( void) __attribute__((always_inline));
static __inline__ void _NOP9( void) __attribute__((always_inline));
static __inline__ void _NOP10(void) __attribute__((always_inline));
static __inline__ void _NOP11(void) __attribute__((always_inline));
static __inline__ void _NOP12(void) __attribute__((always_inline));
 
static __inline__ void _delay_loop_3( uint32_t) __attribute__((always_inline));
static __inline__ void _delay_loop_1_x( uint8_t) __attribute__((always_inline));
static __inline__ void _delay_loop_2_x(uint16_t) __attribute__((always_inline));
static __inline__ void _delay_loop_3_x(uint32_t) __attribute__((always_inline));
 
static __inline__ void _delay_cycles(const double) __attribute__((always_inline));
 
 
/*
* _ N O P x ( void )
*
* Code size optimized NOPs - not using any registers
*
* These NOPs will be used for very short delays where
* it is more code efficient than executing loops.
*
*/
static __inline__ void _NOP1 (void) { __asm__ volatile ( "nop " "\n\t" ); }
static __inline__ void _NOP2 (void) { __asm__ volatile ( "rjmp 1f" "\n\t" "1:" "\n\t" ); }
static __inline__ void _NOP3 (void) { __asm__ volatile ( "lpm " "\n\t" ); }
static __inline__ void _NOP4 (void) { _NOP3(); _NOP1(); }
static __inline__ void _NOP5 (void) { _NOP3(); _NOP2(); }
static __inline__ void _NOP6 (void) { _NOP3(); _NOP3(); }
static __inline__ void _NOP7 (void) { _NOP3(); _NOP3(); _NOP1(); }
static __inline__ void _NOP8 (void) { _NOP3(); _NOP3(); _NOP2(); }
static __inline__ void _NOP9 (void) { _NOP3(); _NOP3(); _NOP3(); }
static __inline__ void _NOP10(void) { _NOP3(); _NOP3(); _NOP3(); _NOP1(); }
static __inline__ void _NOP11(void) { _NOP3(); _NOP3(); _NOP3(); _NOP2(); }
static __inline__ void _NOP12(void) { _NOP3(); _NOP3(); _NOP3(); _NOP3(); }
 
 
 
/*
* _ d e l a y _ l o o p _ 3( uint32_t __count )
*
* This delay loop is not used in the code below: It is
* a supplement to the _delay_loop_1() and _delay_loop_2()
* within standard WinAVR <arv/delay.h> giving a wider
* (32 bit) delay range.
*
*/
static __inline__ void
_delay_loop_3( uint32_t __count )
{
__asm__ volatile (
"1: sbiw %A0,1" "\n\t"
"sbc %C0,__zero_reg__" "\n\t"
"sbc %D0,__zero_reg__" "\n\t"
"brne 1b"
: "=w" (__count)
: "0" (__count)
);
}
 
 
/*
* _ d e l a y _ l o o p _ 1 _ x( uint8_t __n )
* _ d e l a y _ l o o p _ 2 _ x( uint16_t __n )
* _ d e l a y _ l o o p _ 3 _ x( uint32_t __n )
*
* These delay loops always have exactly 4(8) cycles per loop.
* They use a 8/16/32 bit register counter respectively.
*
*/
static __inline__ void /* exactly 4 cycles/loop, max 2**8 loops */
_delay_loop_1_x( uint8_t __n )
{ /* cycles per loop */
__asm__ volatile ( /* __n..one zero */
"1: dec %0" "\n\t" /* 1 1 */
" breq 2f" "\n\t" /* 1 2 */
"2: brne 1b" "\n\t" /* 2 1 */
: "=r" (__n) /* ----- ----- */
: "0" (__n) /* 4 4 */
);
}
 
static __inline__ void /* exactly 4 cycles/loop, max 2**16 loops */
_delay_loop_2_x( uint16_t __n )
{ /* cycles per loop */
__asm__ volatile ( /* __n..one zero */
"1: sbiw %0,1" "\n\t" /* 2 2 */
" brne 1b " "\n\t" /* 2 1 */
" nop " "\n\t" /* 1 */
: "=w" (__n) /* ----- ----- */
: "0" (__n) /* 4 4 */
);
}
 
static __inline__ void /* exactly 8 cycles/loop, max 2**32 loops */
_delay_loop_3_x( uint32_t __n )
{ /* cycles per loop */
__asm__ volatile ( /* __n..one zero */
"1: sbiw %A0,1 " "\n\t" /* 2 2 */
" sbc %C0,__zero_reg__" "\n\t" /* 1 1 */
" sbc %D0,__zero_reg__" "\n\t" /* 1 1 */
" nop " "\n\t" /* 1 1 */
" breq 2f " "\n\t" /* 1 2 */
"2: brne 1b " "\n\t" /* 2 1 */
: "=w" (__n) /* ----- ----- */
: "0" (__n) /* 8 8 */
);
}
 
 
/*
*
* _ d e l a y _ c y c l e s (double __ticks_d)
*
* Perform an accurate delay of a given number of processor cycles.
*
* All the floating point arithmetic will be handled by the
* GCC Preprocessor and no floating point code will be generated.
* Allthough the parameter __ticks_d is of type 'double' this
* function can be called with any constant integer value, too.
* GCC will handle the casting appropriately.
*
* With an 8 MHz clock e.g., delays ranging from 125 nanoseconds
* up to (2**32-1) * 125ns ~= 536,87 seconds are feasible.
*
*/
static __inline__ void
_delay_cycles(const double __ticks_d)
{
uint32_t __ticks = (uint32_t)(__ticks_d);
uint32_t __padding;
uint32_t __loops;
 
/*
* Special optimization for very
* small delays - not using any register.
*/
if( __ticks <= 12 ) { /* this can be done with 4 opcodes */
__padding = __ticks;
 
/* create a single byte counter */
} else if( __ticks <= 0x400 ) {
__ticks -= 1; /* caller needs 1 cycle to init counter */
__loops = __ticks / 4;
__padding = __ticks % 4;
if( __loops != 0 )
_delay_loop_1_x( (uint8_t)__loops );
 
/* create a two byte counter */
} else if( __ticks <= 0x40001 ) {
__ticks -= 2; /* caller needs 2 cycles to init counter */
__loops = __ticks / 4;
__padding = __ticks % 4;
if( __loops != 0 )
_delay_loop_2_x( (uint16_t)__loops );
 
/* create a four byte counter */
} else {
__ticks -= 4; /* caller needs 4 cycles to init counter */
__loops = __ticks / 8;
__padding = __ticks % 8;
if( __loops != 0 )
_delay_loop_3_x( (uint32_t)__loops );
}
 
if( __padding == 1 ) _NOP1();
if( __padding == 2 ) _NOP2();
if( __padding == 3 ) _NOP3();
if( __padding == 4 ) _NOP4();
if( __padding == 5 ) _NOP5();
if( __padding == 6 ) _NOP6();
if( __padding == 7 ) _NOP7();
if( __padding == 8 ) _NOP8();
if( __padding == 9 ) _NOP9();
if( __padding == 10 ) _NOP10();
if( __padding == 11 ) _NOP11();
if( __padding == 12 ) _NOP12();
}
 
 
#endif /* _AVR_DELAY_X_H_ */
main.c
C
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
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/power.h>
#include "delay_x.h"
 
int main(int argc, const char *argv[]) {
 
// Init system clock, disable WDT...
//
clock_prescale_set(clock_div_1);
MCUSR &= ~(1 << WDRF);
wdt_disable();
// Configure LEDs...
DDRB = (1 << 1);
 
PORTB = 0;
_delay_us(100);
PORTB = (1 << 1);
// Blinky...
for (;;){
for (int i=0; i < 8; i++) {
_delay_ns(600);
PORTB = (0 << 1);
_delay_ns(650);
PORTB = (1 << 1);
}
for (int i=0; i < 8; i++) {
_delay_ns(300);
PORTB = (0 << 1);
_delay_ns(950);
PORTB = (1 << 1);
}
for (int i=0; i < 8; i++) {
_delay_ns(300);
PORTB = (0 << 1);
_delay_ns(950);
PORTB = (1 << 1);
}
PORTB = (0 << 1);
_delay_us(100);
PORTB = (1 << 1);
_delay_ms(1000);
for (int i=0; i < 8; i++) {
_delay_ns(300);
PORTB = (0 << 1);
_delay_ns(950);
PORTB = (1 << 1);
}
for (int i=0; i < 8; i++) {
_delay_ns(600);
PORTB = (0 << 1);
_delay_ns(650);
PORTB = (1 << 1);
}
for (int i=0; i < 8; i++) {
_delay_ns(300);
PORTB = (0 << 1);
_delay_ns(950);
PORTB = (1 << 1);
}
PORTB = (0 << 1);
_delay_us(100);
PORTB = (1 << 1);
_delay_ms(1000);
for (int i=0; i < 8; i++) {
_delay_ns(300);
PORTB = (0 << 1);
_delay_ns(950);
PORTB = (1 << 1);
}
for (int i=0; i < 8; i++) {
_delay_ns(300);
PORTB = (0 << 1);
_delay_ns(950);
PORTB = (1 << 1);
}
for (int i=0; i < 8; i++) {
_delay_ns(600);
PORTB = (0 << 1);
_delay_ns(650);
PORTB = (1 << 1);
}
PORTB = (0 << 1);
_delay_us(100);
PORTB = (1 << 1);
_delay_ms(1000);
 
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.