From 616f20d134e94462b5920a2f93be54cdb9fe74e0 Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Fri, 5 Jul 2013 19:35:16 +0200 Subject: [PATCH] RGB LED string - a new project Files copied from the step-up project, and slightly modified. --- projects/rgb-led-string/Makefile | 68 ++++++++++++++++++++++ projects/rgb-led-string/README | 19 +++++++ projects/rgb-led-string/logging.c | 81 +++++++++++++++++++++++++++ projects/rgb-led-string/main.c | 20 +++++++ projects/rgb-led-string/rgbstring.h | 87 +++++++++++++++++++++++++++++ projects/rgb-led-string/version.pl | 32 +++++++++++ 6 files changed, 307 insertions(+) create mode 100644 projects/rgb-led-string/Makefile create mode 100644 projects/rgb-led-string/README create mode 100644 projects/rgb-led-string/logging.c create mode 100644 projects/rgb-led-string/main.c create mode 100644 projects/rgb-led-string/rgbstring.h create mode 100755 projects/rgb-led-string/version.pl diff --git a/projects/rgb-led-string/Makefile b/projects/rgb-led-string/Makefile new file mode 100644 index 0000000..2fa9344 --- /dev/null +++ b/projects/rgb-led-string/Makefile @@ -0,0 +1,68 @@ + +PROGRAM=rgbstring +SRC=version.c main.c logging.c +OBJ=$(SRC:.c=.o) + + +MCU=attiny45 +AVRDUDE_MCU=$(MCU) +AVRDUDE_PROGRAMMER=usbasp + +CFLAGS=-Wall -Os -mmcu=$(MCU) -DUSE_LOGGING=1 -DF_CPU=1000000UL -std=gnu99 +LDFLAGS= +AVRDUDE_FLAGS= -p$(AVRDUDE_MCU) -c $(AVRDUDE_PROGRAMMER) + +FORMAT=ihex + +CC=avr-gcc +OBJCOPY=avr-objcopy +OBJDUMP=avr-objdump +AVRDUDE=avrdude + +all: $(PROGRAM).hex $(PROGRAM).eep + +program: $(PROGRAM).hex $(PROGRAM).eep + $(AVRDUDE) $(AVRDUDE_FLAGS) -U flash:w:$(PROGRAM).hex:i -U eeprom:w:$(PROGRAM).eep:i + +program_flash: $(PROGRAM).hex + $(AVRDUDE) $(AVRDUDE_FLAGS) -U flash:w:$(PROGRAM).hex:i + +program_eeprom: $(PROGRAM).eep + $(AVRDUDE) $(AVRDUDE_FLAGS) eeprom:w:$(PROGRAM).eep:i + +dump_eeprom: + $(AVRDUDE) $(AVRDUDE_FLAGS) -U eeprom:r:eeprom.raw:r + od -tx1 eeprom.raw + +objdump: $(PROGRAM).elf + $(OBJDUMP) --disassemble $< + +.PRECIOUS : $(OBJ) $(PROGRAM).elf + +%.hex: %.elf + $(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@ + +%.eep: %.elf + $(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \ + --change-section-lma .eeprom=0 -O $(FORMAT) $< $@ + +%.elf: $(OBJ) + $(CC) $(CFLAGS) $(OBJ) -o $@ $(LDFLAGS) + +%.o: %.c rgbstring.h Makefile + $(CC) -c $(CFLAGS) $< -o $@ + +%.s: %.c rgbstring.h Makefile + $(CC) -S -c $(CFLAGS) $< -o $@ + +%.o: %.S + $(CC) -c $(CFLAGS) $< -o $@ + +clean: + rm -f $(PROGRAM).hex $(PROGRAM).eep $(PROGRAM).elf *.o *.s eeprom.raw + +version.c: + ./version.pl > version.c + +.PHONY: all clean dump_eeprom program program_flash program_eeprom objdump version.c + diff --git a/projects/rgb-led-string/README b/projects/rgb-led-string/README new file mode 100644 index 0000000..c4e4d11 --- /dev/null +++ b/projects/rgb-led-string/README @@ -0,0 +1,19 @@ + +Controller for the string of RGB LEDs + +CPU: ATtiny45 + +Bill of materials: + +C3 10uF ceramic +U1 ATtiny45-20SU + +Pin-out: + +PB0 header: + 2: CLK +PB2 header: + 2: DATA + + + diff --git a/projects/rgb-led-string/logging.c b/projects/rgb-led-string/logging.c new file mode 100644 index 0000000..d11275b --- /dev/null +++ b/projects/rgb-led-string/logging.c @@ -0,0 +1,81 @@ +#ifdef USE_LOGGING + +#include +#include + +#include "rgbstring.h" + +#define LOG_BUFFER 64 +static unsigned char log_buffer_ee[LOG_BUFFER] EEMEM; +static unsigned char log_buffer_count; +static unsigned char log_buffer[LOG_BUFFER]; +static unsigned char log_state EEMEM; +/* Upper 4 bits are reset count, lower 4 bits are reset reason from MCUSR */ +static unsigned char reboot_count EEMEM = 0; +static unsigned char can_write_eeprom = 0; +static uint16_t flushed_end; + +void log_set_state(unsigned char val) +{ + if (can_write_eeprom) + eeprom_write_byte(&log_state, val); +} + +void init_log() +{ + unsigned char r_count; + + r_count = eeprom_read_byte(&reboot_count); + r_count >>= 4; + + if (r_count < 5) { + r_count++; + eeprom_write_byte(&reboot_count, + (r_count << 4) | (MCUSR & 0xF)); + MCUSR = 0; + can_write_eeprom = 1; + } else { + //eeprom_write_byte(&log_state, 0xFF); + can_write_eeprom = 0; + } + + log_set_state(1); + log_buffer_count = 0; + flushed_end = 0; +} + +void log_byte(unsigned char byte) { + if (log_buffer_count >= LOG_BUFFER) + return; + + // eeprom_write_word(&log_buffer[log_buffer_count], word); + log_buffer[log_buffer_count++] = byte; + + if (log_buffer_count == LOG_BUFFER) + log_flush(); +} + +void log_word(uint16_t word) { + log_byte(word & 0xFF); + log_byte(word >> 8); +} + +void log_flush() { + unsigned char i; + + if (!can_write_eeprom) + return; + + for (i=flushed_end; i < log_buffer_count; i++) { + eeprom_write_byte(&log_buffer_ee[i], + log_buffer[i]); + } + + flushed_end = i; + + if (flushed_end == LOG_BUFFER) + log_set_state(0x42); +} + +#endif + diff --git a/projects/rgb-led-string/main.c b/projects/rgb-led-string/main.c new file mode 100644 index 0000000..4c82f48 --- /dev/null +++ b/projects/rgb-led-string/main.c @@ -0,0 +1,20 @@ +#include +#include +#include + +#include "rgbstring.h" + +int main(void) +{ + init_log(); + + log_set_state(3); + + DDRB |= _BV(PB2); + while (1) { + PORTB |= _BV( PB2 ); + _delay_ms(200); + PORTB &=~ _BV( PB2 ); + _delay_ms(200); + } +} diff --git a/projects/rgb-led-string/rgbstring.h b/projects/rgb-led-string/rgbstring.h new file mode 100644 index 0000000..565deef --- /dev/null +++ b/projects/rgb-led-string/rgbstring.h @@ -0,0 +1,87 @@ +#ifndef LIGHTS_H__ +#define LIGHTS_H__ 1 + +#define N_PWMLED_MODES 3 + +/* logging.c */ +#ifdef USE_LOGGING +void init_log(); +void log_set_state(unsigned char val); +void log_flush(); +void log_byte(unsigned char byte); +void log_word(uint16_t word); +#else +void inline init_log() { } +void inline log_set_state(unsigned char val) { } +void inline log_flush() { } +void inline log_byte(unsigned char byte) { } +void inline log_word(uint16_t word) { } +#endif + +/* adc.c */ +#define PWMLED_ADC_SHIFT 1 /* 1<<1 measurements per single callback */ +extern volatile unsigned char need_battery_adc; +extern volatile unsigned char need_pwmled_adc; +extern volatile unsigned char adc_enabled; +void init_adc(); +void susp_adc(); +void start_next_adc(); + +/* pwm.c */ +#define PWM_MAX 0xFF +extern volatile unsigned char pwm_enabled; +void init_pwm(); +void susp_pwm(); +void pwm_off(); +void pwm_set(uint8_t stride); + +/* pwmled.c */ +void init_pwmled(); +void pwmled_adc(uint16_t adcval); +void pwmled_set_target(unsigned char mode); +void pwmled_on_off(unsigned char on); + +/* pattern.c */ +void init_pattern(); +void patterns_next_tick(); +void led_set_pattern(unsigned char led, unsigned char bits_len, + unsigned char bits_start, unsigned char *data); +void led_set_number_pattern(unsigned char led, + unsigned char num, unsigned char inv); +void pattern_reload(); + +/* buttons.c */ +void init_buttons(); +void susp_buttons(); +void timer_check_buttons(); +unsigned char buttons_wait_for_release(); +void status_led_on_off(unsigned char on); + +/* battery.c */ +void battery_adc(); +void init_battery(); +unsigned char battery_gauge(); + +/* control.c */ +void init_control(); +void long_press_start(); +void long_press(); +void short_press(); +void brake_on(); +void brake_off(); +void pwmled_pattern_select(unsigned char led); +void status_led_pattern_select(unsigned char led); +#define ERR_BATTERY 1 +#define ERR_PWMLED 2 +void set_error(unsigned char err); + +/* wdt.c */ +extern volatile uint16_t jiffies; +void init_wdt(); +void susp_wdt(); + +/* main.c */ +void power_down(); + +#endif /* !LIGHTS_H__ */ + diff --git a/projects/rgb-led-string/version.pl b/projects/rgb-led-string/version.pl new file mode 100755 index 0000000..cb4ecc7 --- /dev/null +++ b/projects/rgb-led-string/version.pl @@ -0,0 +1,32 @@ +#!/usr/bin/perl -w + +use strict; +use POSIX qw(strftime); + +my $git = `git rev-parse --short HEAD`; +chomp $git; + +my $now = strftime('%Y%m%d', localtime(time)); + +print < + +unsigned char version[] EEMEM = { +EOF + +print hex2c($git, "git revision"); +print hex2c($now, "date"); + +print "};\n\n/* EOF - this file has not been truncated */\n\n"; + +sub hex2c { + my ($data, $comment) = @_; + + my $data1 = $data; + $data1 .= '0' if (length($data1) & 1 == 1); + $data1 =~ s/(..)/0x$1, /g; + return "\t$data1 /* $comment $data */\n"; +} + -- 2.43.5