]> www.fi.muni.cz Git - heater.git/commitdiff
Rudimentary firmware infrastructure.
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Thu, 30 Jan 2014 14:29:05 +0000 (15:29 +0100)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Thu, 30 Jan 2014 14:29:05 +0000 (15:29 +0100)
firmware/.gitignore [new file with mode: 0644]
firmware/Makefile [new file with mode: 0644]
firmware/main.c [new file with mode: 0644]
firmware/version.pl [new file with mode: 0755]

diff --git a/firmware/.gitignore b/firmware/.gitignore
new file mode 100644 (file)
index 0000000..7b29fe9
--- /dev/null
@@ -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 (file)
index 0000000..da02dbb
--- /dev/null
@@ -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 (file)
index 0000000..30f904b
--- /dev/null
@@ -0,0 +1,33 @@
+#include <avr/io.h>
+#include <avr/eeprom.h>
+#include <util/delay.h>
+
+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 (executable)
index 0000000..cb4ecc7
--- /dev/null
@@ -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 <<EOF;
+/* DO NOT EDIT - GENERATED BY $0 */
+
+#include <avr/eeprom.h>
+
+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";
+}
+