USBasp first stable release v0.1
[pub/USBaspLoader.git] / firmware / Makefile
1 # Name: Makefile
2 # Project: USBaspLoader
3 # Author: Christian Starkjohann
4 # Creation Date: 2007-12-10
5 # Tabsize: 4
6 # Copyright: (c) 2007 by OBJECTIVE DEVELOPMENT Software GmbH
7 # License: GNU GPL v2 (see License.txt)
8
9 ###############################################################################
10 # Configure the following variables according to your AVR.
11 # Program the device with
12 # make fuse # to set the clock generator, boot section size etc.
13 # make flash # to load the boot loader into flash
14 # make lock # to protect the boot loader from overwriting
15
16 F_CPU = 16000000
17 DEVICE = atmega8
18 # BOOTLOADER_ADDRESS is 1800 for 8k devices, 3800 for 16k and 7800 for 32k.
19 BOOTLOADER_ADDRESS = 1800
20 FUSEOPT = $(FUSEOPT_8)
21 LOCKOPT = -U lock:w:0x2f:m
22
23 PROGRAMMER = -c pony-stk200
24 # PROGRAMMER contains AVRDUDE options to address your programmer
25
26 FUSEOPT_8 = -U hfuse:w:0xc0:m -U lfuse:w:0x9f:m
27 FUSEOPT_88 = -U hfuse:w:0xd6:m -U lfuse:w:0xdf:m -U efuse:w:0x00:m
28 FUSEOPT_168 = -U hfuse:w:0xd6:m -U lfuse:w:0xdf:m -U efuse:w:0x00:m
29 FUSEOPT_328 = -U lfuse:w:0xf7:m -U hfuse:w:0xda:m -U efuse:w:0x03:m
30 # You may have to change the order of these -U commands.
31
32 #---------------------------------------------------------------------
33 # ATMega8
34 #---------------------------------------------------------------------
35 # Fuse high byte:
36 # 0xc0 = 1 1 0 0 0 0 0 0 <-- BOOTRST (boot reset vector at 0x1800)
37 # ^ ^ ^ ^ ^ ^ ^------ BOOTSZ0
38 # | | | | | +-------- BOOTSZ1
39 # | | | | + --------- EESAVE (preserve EEPROM over chip erase)
40 # | | | +-------------- CKOPT (full output swing)
41 # | | +---------------- SPIEN (allow serial programming)
42 # | +------------------ WDTON (WDT not always on)
43 # +-------------------- RSTDISBL (reset pin is enabled)
44 # Fuse low byte:
45 # 0x9f = 1 0 0 1 1 1 1 1
46 # ^ ^ \ / \--+--/
47 # | | | +------- CKSEL 3..0 (external >8M crystal)
48 # | | +--------------- SUT 1..0 (crystal osc, BOD enabled)
49 # | +------------------ BODEN (BrownOut Detector enabled)
50 # +-------------------- BODLEVEL (2.7V)
51 #---------------------------------------------------------------------
52 # ATMega88, ATMega168
53 #---------------------------------------------------------------------
54 # Fuse extended byte:
55 # 0x00 = 0 0 0 0 0 0 0 0 <-- BOOTRST (boot reset vector at 0x1800)
56 # \+/
57 # +------- BOOTSZ (00 = 2k bytes)
58 # Fuse high byte:
59 # 0xd6 = 1 1 0 1 0 1 1 0
60 # ^ ^ ^ ^ ^ \-+-/
61 # | | | | | +------ BODLEVEL 0..2 (110 = 1.8 V)
62 # | | | | + --------- EESAVE (preserve EEPROM over chip erase)
63 # | | | +-------------- WDTON (if 0: watchdog always on)
64 # | | +---------------- SPIEN (allow serial programming)
65 # | +------------------ DWEN (debug wire enable)
66 # +-------------------- RSTDISBL (reset pin is enabled)
67 # Fuse low byte:
68 # 0xdf = 1 1 0 1 1 1 1 1
69 # ^ ^ \ / \--+--/
70 # | | | +------- CKSEL 3..0 (external >8M crystal)
71 # | | +--------------- SUT 1..0 (crystal osc, BOD enabled)
72 # | +------------------ CKOUT (if 0: Clock output enabled)
73 # +-------------------- CKDIV8 (if 0: divide by 8)
74 #---------------------------------------------------------------------
75 # ATMega328P
76 #---------------------------------------------------------------------
77 # Fuse extended byte:
78 # 0x03 = - - - - - 0 1 1
79 # \-+-/
80 # +------ BODLEVEL 0..2 (011 = 4.3V)
81 # Fuse high byte:
82 # 0xda = 1 1 0 1 1 0 1 0 <-- BOOTRST (0 = jump to bootloader at start)
83 # ^ ^ ^ ^ ^ \+/
84 # | | | | | +------- BOOTSZ 0..1 (01 = 2KB starting at 0x7800)
85 # | | | | + --------- EESAVE (don't preserve EEPROM over chip erase)
86 # | | | +-------------- WDTON (1 = watchdog disabled at start)
87 # | | +---------------- SPIEN (0 = allow serial programming)
88 # | +------------------ DWEN (1 = debug wire disable)
89 # +-------------------- RSTDISBL (1 = reset pin is enabled)
90 # Fuse low byte:
91 # 0xf7 = 1 1 1 1 0 1 1 1
92 # ^ ^ \ / \--+--/
93 # | | | +------- CKSEL 3..0 (0111 = external full-swing crystal)
94 # | | +--------------- SUT 1..0 (11 = startup time 16K CK/14K + 65ms)
95 # | +------------------ CKOUT (1 = clock output disabled)
96 # +-------------------- CKDIV8 (1 = do not divide clock by 8)
97
98
99 ###############################################################################
100
101 # Tools:
102 AVRDUDE = @echo avrdude $(PROGRAMMER) -p $(DEVICE)
103 CC = avr-gcc
104
105 # Options:
106 DEFINES = #-DDEBUG_LEVEL=2
107 # Remove the -fno-* options when you use gcc 3, it does not understand them
108 CFLAGS = -Wall -Os -fno-move-loop-invariants -fno-tree-scev-cprop -fno-inline-small-functions -I. -mmcu=$(DEVICE) -DBOOTLOADER_ADDRESS=0x$(BOOTLOADER_ADDRESS) -DF_CPU=$(F_CPU) $(DEFINES)
109 LDFLAGS = -Wl,--relax,--gc-sections -Wl,--section-start=.text=$(BOOTLOADER_ADDRESS)
110
111 OBJECTS = usbdrv/usbdrvasm.o usbdrv/oddebug.o main.o
112
113 # symbolic targets:
114 all: main.hex
115
116 .c.o:
117 $(CC) $(CFLAGS) -c $< -o $@
118
119 .S.o:
120 $(CC) $(CFLAGS) -x assembler-with-cpp -c $< -o $@
121 # "-x assembler-with-cpp" should not be necessary since this is the default
122 # file type for the .S (with capital S) extension. However, upper case
123 # characters are not always preserved on Windows. To ensure WinAVR
124 # compatibility define the file type manually.
125
126 .c.s:
127 $(CC) $(CFLAGS) -S $< -o $@
128
129 flash: all
130 $(AVRDUDE) -U flash:w:main.hex:i
131
132 readflash:
133 $(AVRDUDE) -U flash:r:read.hex:i
134
135 fuse:
136 $(AVRDUDE) $(FUSEOPT)
137
138 lock:
139 $(AVRDUDE) $(LOCKOPT)
140
141 read_fuses:
142 $(UISP) --rd_fuses
143
144 deepclean: clean
145 rm -f *~
146
147 clean:
148 rm -f main.hex main.bin *.o usbdrv/*.o main.s usbdrv/oddebug.s usbdrv/usbdrv.s
149
150 # file targets:
151 main.bin: $(OBJECTS)
152 $(CC) $(CFLAGS) -o main.bin $(OBJECTS) $(LDFLAGS)
153
154 main.hex: main.bin
155 rm -f main.hex main.eep.hex
156 avr-objcopy -j .text -j .data -O ihex main.bin main.hex
157 avr-size main.bin
158
159 disasm: main.bin
160 avr-objdump -d main.bin
161
162 cpp:
163 $(CC) $(CFLAGS) -E main.c
164
165 # Special rules for generating hex files for various devices and clock speeds
166 ALLHEXFILES = hexfiles/mega8_12mhz.hex hexfiles/mega8_15mhz.hex hexfiles/mega8_16mhz.hex \
167 hexfiles/mega88_12mhz.hex hexfiles/mega88_15mhz.hex hexfiles/mega88_16mhz.hex hexfiles/mega88_20mhz.hex\
168 hexfiles/mega168_12mhz.hex hexfiles/mega168_15mhz.hex hexfiles/mega168_16mhz.hex hexfiles/mega168_20mhz.hex\
169 hexfiles/mega328p_12mhz.hex hexfiles/mega328p_15mhz.hex hexfiles/mega328p_16mhz.hex hexfiles/mega328p_20mhz.hex
170
171 allhexfiles: $(ALLHEXFILES)
172 $(MAKE) clean
173 avr-size hexfiles/*.hex
174
175 $(ALLHEXFILES):
176 @[ -d hexfiles ] || mkdir hexfiles
177 @device=`echo $@ | sed -e 's|.*/mega||g' -e 's|_.*||g'`; \
178 clock=`echo $@ | sed -e 's|.*_||g' -e 's|mhz.*||g'`; \
179 addr=`echo $$device | sed -e 's/\([0-9]\)8/\1/g' | awk '{printf("%x", ($$1 - 2) * 1024)}'`; \
180 echo "### Make with F_CPU=$${clock}000000 DEVICE=atmega$$device BOOTLOADER_ADDRESS=$$addr"; \
181 $(MAKE) clean; \
182 $(MAKE) main.hex F_CPU=$${clock}000000 DEVICE=atmega$$device BOOTLOADER_ADDRESS=$$addr DEFINES=-DUSE_AUTOCONFIG=1
183 mv main.hex $@