aboutsummaryrefslogtreecommitdiff
path: root/firmware/Makefile
blob: 83fa50b9f10d096c940edb77f41ff5fd1b8b0bd7 (plain)
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
# required packages
# sudo aptitude install avrdude gcc-avr avr-libc
# the program
TARGET=led-controller
# to compile
CC=avr-gcc
OBJDUMP=avr-objdump
OBJCOPY=avr-objcopy
CFLAGS=-g -Wall -Werror -O3 -std=c99
# the target
DEVICE=atmega328p
F_CPU=18432000UL
# the flasher
PROGRAMMER=usbtiny
# to flash
FLASHER=avrdude -p $(DEVICE) -c $(PROGRAMMER)
# source files to compile
SRC = main.c uart.c ir_nec.c settings.c
# header files.
HEADER = $(SRC:.c=.h)
# object files.
OBJ = $(SRC:.c=.o)
# listing files.
LST = $(SRC:.c=.lst)

all: program $(TARGET).lst verify burn-fuse flash

verify:
	$(FLASHER)
	if [ $$? -eq 0 ]; then \
		echo "flasher configured correctly"; \
	else \
		echo "flasher not configured correctly in Makefile"; \
		exit 1; \
	fi

reset:
	$(FLASHER)

read-fuse:
	$(FLASHER) -U lfuse:r:lfuse.raw:r -U hfuse:r:hfuse.raw:r -U efuse:r:efuse.raw:r

# Fuse low byte:
# 0xff = 1 1 1 1   1 1 1 1
#        ^ ^ \+/   \--+--/
#        | |  |       +------- CKSEL 3..0 (clock selection -> full swing crytal oscillator)
#        | |  +--------------- SUT 1..0 (select start-up time -> slowly rising power)
#        | +------------------ CKOUT (clock output on CKOUT pin -> unprogrammed)
#        +-------------------- CKDIV8 (divide clock by 8 -> unprogrammed)
#
# Fuse high byte:
# 0xdf = 1 1 0 1   1 1 1 1
#        ^ ^ ^ ^   ^ \+/ ^
#        | | | |   |  |  +---- BOOTRST (Select Reset Vector -> jump to application at start)
#        | | | |   |  +------- BOOTSZ 1..0 (Select Boot Size -> 256 words starting at 0x3F00)
#        | | | |   +---------- EESAVE (preserve EEPROM on Chip Erase -> unprogrammed)
#        | | | +-------------- WDTON (watchdog timer always on -> unprogrammed)
#        | | +---------------- SPIEN (enable serial programming -> programmed)
#        | +------------------ DWEN (debug wire enable -> unprogrammed)
#        +-------------------- RSTDISBL (disable external reset -> unprogrammed)
#
# Fuse extended byte:
# 0x04 = - - - -   - 1 0 0
#                    \-+-/
#                      +------ BODLEVEL 2..0 (Brown-out Detector trigger level -> 4.3V)
burn-fuse:
	$(FLASHER) -U lfuse:w:0xff:m -U hfuse:w:0xdf:m -U efuse:w:0x04:m

read-flash:
	$(FLASHER) -U flash:r:flash_dump.hex:i

flash:
	$(FLASHER) -U flash:w:$(TARGET).hex:i

program: $(TARGET).hex

# compile
%.o: %.c %.h
	$(CC) $(CFLAGS) -I. -mmcu=$(DEVICE) -DF_CPU=$(F_CPU) -c -o $@ $<

# link
%.elf: $(OBJ)
	$(CC) $(CFLAGS) -I. -mmcu=$(DEVICE) -DF_CPU=$(F_CPU) -Wl,-Map=$(TARGET).map,--cref -o $@ $(OBJ)

# extended listing
%.lst: %.elf
	$(OBJDUMP) -h -S $< > $@

# output file
%.hex: %.elf
	$(OBJCOPY) -j .text -j .data -O ihex $< $@

clean:
	rm -f $(TARGET) $(TARGET).hex $(TARGET).elf $(LST) $(TARGET).lst $(OBJ)