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