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