From 2755b4c6000c4de64d83921bdb109077f4b38597 Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Thu, 30 Jan 2014 15:29:05 +0100 Subject: [PATCH] Rudimentary firmware infrastructure. --- firmware/.gitignore | 5 ++++ firmware/Makefile | 68 +++++++++++++++++++++++++++++++++++++++++++++ firmware/main.c | 33 ++++++++++++++++++++++ firmware/version.pl | 32 +++++++++++++++++++++ 4 files changed, 138 insertions(+) create mode 100644 firmware/.gitignore create mode 100644 firmware/Makefile create mode 100644 firmware/main.c create mode 100755 firmware/version.pl diff --git a/firmware/.gitignore b/firmware/.gitignore new file mode 100644 index 0000000..7b29fe9 --- /dev/null +++ b/firmware/.gitignore @@ -0,0 +1,5 @@ +heater.eep +heater.elf +heater.hex +*.o +version.c diff --git a/firmware/Makefile b/firmware/Makefile new file mode 100644 index 0000000..da02dbb --- /dev/null +++ b/firmware/Makefile @@ -0,0 +1,68 @@ + +PROGRAM=heater +SRC=version.c main.c +OBJ=$(SRC:.c=.o) + + +MCU=attiny25 +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) -B 3 -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 lights.h Makefile + $(CC) -c $(CFLAGS) $< -o $@ + +%.s: %.c lights.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/firmware/main.c b/firmware/main.c new file mode 100644 index 0000000..30f904b --- /dev/null +++ b/firmware/main.c @@ -0,0 +1,33 @@ +#include +#include +#include + +static uint16_t adcval EEMEM; + +int main() +{ + DDRB |= _BV(PB2) | _BV(PB4); + TCCR1 = _BV(CS10); // clk/1 = 1 MHz + // TCCR1 = _BV(CS11) | _BV(CS13); // clk/512 = 2 kHz + GTCCR = _BV(COM1B1) | _BV(PWM1B); + OCR1C = 255; + OCR1B = 9; + + ADCSRA = _BV(ADEN) | _BV(ADPS1) | _BV(ADPS0); // clk/8 = 125 kHz + ADMUX = _BV(REFS1) | _BV(MUX1) | _BV(MUX0); // 1.1V ref., PB3 single-ended + DIDR0 = _BV(ADC3D); + ADCSRA |= _BV(ADSC); + while (!(ADCSRA & _BV(ADIF))) + ; + ADCSRA |= _BV(ADSC); + while (!(ADCSRA & _BV(ADIF))) + ; + eeprom_write_word(&adcval, ADCW); + + while(1) { + PORTB |= _BV(PB2); + _delay_ms(500); + PORTB &= ~_BV(PB2); + _delay_ms(1000); + } +} diff --git a/firmware/version.pl b/firmware/version.pl new file mode 100755 index 0000000..cb4ecc7 --- /dev/null +++ b/firmware/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