005d9bbe76950b4ca70f2d22bd64eebb395f99a2
[pub/USBaspLoader.git] / firmware / bootloaderconfig.h
1 /* Name: bootloaderconfig.h
2 * Project: USBaspLoader
3 * Author: Christian Starkjohann
4 * Author: Stephan Baerwolf
5 * Creation Date: 2007-12-08
6 * Modification Date: 2012-11-10
7 * Tabsize: 4
8 * Copyright: (c) 2007 by OBJECTIVE DEVELOPMENT Software GmbH
9 * License: GNU GPL v2 (see License.txt)
10 * This Revision: $Id: bootloaderconfig.h 729 2009-03-20 09:03:58Z cs $
11 */
12
13 #ifndef __bootloaderconfig_h_included__
14 #define __bootloaderconfig_h_included__
15 #include <avr/io.h>
16
17 /*
18 General Description:
19 This file (together with some settings in Makefile) configures the boot loader
20 according to the hardware.
21
22 This file contains (besides the hardware configuration normally found in
23 usbconfig.h) two functions or macros: bootLoaderInit() and
24 bootLoaderCondition(). Whether you implement them as macros or as static
25 inline functions is up to you, decide based on code size and convenience.
26
27 bootLoaderInit() is called as one of the first actions after reset. It should
28 be a minimum initialization of the hardware so that the boot loader condition
29 can be read. This will usually consist of activating a pull-up resistor for an
30 external jumper which selects boot loader mode.
31
32 bootLoaderCondition() is called immediately after initialization and in each
33 main loop iteration. If it returns TRUE, the boot loader will be active. If it
34 returns FALSE, the boot loader jumps to address 0 (the loaded application)
35 immediately.
36
37 For compatibility with Thomas Fischl's avrusbboot, we also support the macro
38 names BOOTLOADER_INIT and BOOTLOADER_CONDITION for this functionality. If
39 these macros are defined, the boot loader usees them.
40 */
41
42 /* ---------------------------- Macro Magic ---------------------------- */
43 #define PIN_CONCAT(a,b) a ## b
44 #define PIN_CONCAT3(a,b,c) a ## b ## c
45
46 #define PIN_PORT(a) PIN_CONCAT(PORT, a)
47 #define PIN_PIN(a) PIN_CONCAT(PIN, a)
48 #define PIN_DDR(a) PIN_CONCAT(DDR, a)
49
50 #define PIN(a, b) PIN_CONCAT3(P, a, b)
51
52 /* ---------------------------- Hardware Config ---------------------------- */
53
54 #ifndef USB_CFG_IOPORTNAME
55 #define USB_CFG_IOPORTNAME D
56 #endif
57 /* This is the port where the USB bus is connected. When you configure it to
58 * "B", the registers PORTB, PINB and DDRB will be used.
59 */
60 #ifndef USB_CFG_DMINUS_BIT
61 #define USB_CFG_DMINUS_BIT 6 /* old value was 4 */
62 #endif
63 /* This is the bit number in USB_CFG_IOPORT where the USB D- line is connected.
64 * This may be any bit in the port.
65 */
66 #ifndef USB_CFG_DPLUS_BIT
67 #define USB_CFG_DPLUS_BIT 2
68 #endif
69 /* This is the bit number in USB_CFG_IOPORT where the USB D+ line is connected.
70 * This may be any bit in the port. Please note that D+ must also be connected
71 * to interrupt pin INT0!
72 */
73 #ifndef JUMPER_PORT
74 #define JUMPER_PORT USB_CFG_IOPORTNAME
75 #endif
76 /*
77 * jumper is connected to this port
78 */
79 #ifndef JUMPER_BIT
80 #define JUMPER_BIT 7 /* old value was 0 */
81 #endif
82 /*
83 * jumper is connected to this bit in port "JUMPER_PORT", active low
84 */
85
86 #define USB_CFG_CLOCK_KHZ (F_CPU/1000)
87 /* Clock rate of the AVR in MHz. Legal values are 12000, 16000 or 16500.
88 * The 16.5 MHz version of the code requires no crystal, it tolerates +/- 1%
89 * deviation from the nominal frequency. All other rates require a precision
90 * of 2000 ppm and thus a crystal!
91 * Default if not specified: 12 MHz
92 */
93
94 /* ----------------------- Optional Hardware Config ------------------------ */
95
96 /* #define USB_CFG_PULLUP_IOPORTNAME D */
97 /* If you connect the 1.5k pullup resistor from D- to a port pin instead of
98 * V+, you can connect and disconnect the device from firmware by calling
99 * the macros usbDeviceConnect() and usbDeviceDisconnect() (see usbdrv.h).
100 * This constant defines the port on which the pullup resistor is connected.
101 */
102 /* #define USB_CFG_PULLUP_BIT 4 */
103 /* This constant defines the bit number in USB_CFG_PULLUP_IOPORT (defined
104 * above) where the 1.5k pullup resistor is connected. See description
105 * above for details.
106 */
107
108 /* ------------------------------------------------------------------------- */
109 /* ---------------------- feature / code size options ---------------------- */
110 /* ------------------------------------------------------------------------- */
111
112 #ifndef CONFIG_NO__HAVE_READ_LOCK_FUSE
113 #define HAVE_READ_LOCK_FUSE 1
114 #else
115 #define HAVE_READ_LOCK_FUSE 0
116 #endif
117 /*
118 * enable the loaders capability to load its lfuse, hfuse and lockbits
119 * ...However, programming of these is prohibited...
120 */
121
122 #ifndef CONFIG_NO__HAVE_BLB11_SOFTW_LOCKBIT
123 #define HAVE_BLB11_SOFTW_LOCKBIT 1
124 #else
125 #define HAVE_BLB11_SOFTW_LOCKBIT 0
126 #endif
127 /*
128 * The IC itself do not need to prgra BLB11, but the bootloader will avaoid
129 * to erase itself from the bootregion
130 */
131
132 #ifndef CONFIG_NO__HAVE_SPMINTEREFACE
133 #define HAVE_SPMINTEREFACE 1
134 #else
135 #define HAVE_SPMINTEREFACE 0
136 #endif
137 /*
138 * Since code within normal section of application memory (rww-section) is
139 * not able to call spm for programming flash-pages, this option (when
140 * enabled) will insert a small subroutine into the bootloader-section
141 * to enable applications to circumvent this limitation and make them
142 * able to program the flash in a similar way as the bootloader does, too.
143 * For further details see "spminterface.h", which implements this
144 * feature.
145 */
146
147 #define HAVE_SPMINTEREFACE_NORETMAGIC 1
148 /*
149 * If sth. went wrong within "bootloader__do_spm" and this macro is ACTIVATED,
150 * then "bootloader__do_spm" will not return the call and loop infinity instead.
151 *
152 * This feature prevents old updaters to do sth. undefined on wrong magic.
153 */
154
155 /* all boards should use a magic to make it safe to confuse updatefiles :-) */
156 #define HAVE_SPMINTEREFACE_MAGICVALUE 0
157 /* If this feature is enabled (value != 0), the configured 32bit value is
158 * used as a magic value within spminterface. "bootloader__do_spm" will check
159 * additional four (4) registers for this value and only proceed, if they contain
160 * the right value. With this feature you can identify your board and avoid
161 * updating the wrong bootloader to the wrong board!
162 *
163 * Not all values are possible - "SPMINTEREFACE_MAGICVALUE" must be very sparse!
164 * To avoid collisions, magic-values will be organized centrally by Stephan
165 * Following values are definitly blocked or reserved and must not be used:
166 * 0x00000000, 0x12345678,
167 * 0x00a500a5, 0x00a5a500, 0xa50000a5, 0xa500a500,
168 * 0x005a005a, 0x005a5a00, 0x5a00005a, 0x5a005a00,
169 * 0x5aa55aa5, 0x5aa5a55a, 0xa55a5aa5, 0xa55aa55a,
170 * 0x5a5a5a5a, 0xa5a5a5a5,
171 * 0xffa5ffa5, 0xffa5a5ff, 0xa5ffffa5, 0xa5ffa5ff,
172 * 0xff5aff5a, 0xff5a5aff, 0x5affff5a, 0x5aff5aff,
173 * 0x00ff00ff, 0x00ffff00, 0xff0000ff, 0xff00ff00,
174 * 0xffffffff
175 *
176 * To request your own magic, please send at least following information
177 * about yourself and your board together within an informal request to:
178 * stephan@matrixstorm.com / matrixstorm@gmx.de / stephan.baerwolf@tu-ilmenau.de
179 * - your name
180 * - your e-mail
181 * - your project (maybe an url?)
182 * - your type of MCU used
183 * --> your used "BOOTLOADER_ADDRESS" (since same magics can be reused for different "BOOTLOADER_ADDRESS")
184 *
185 * There may be no garanty for it, but Stephan will then send you an
186 * response with a "SPMINTEREFACE_MAGICVALUE" just for your board/project...
187 * WITH REQUESTING A MAGIC YOU AGREE TO PUBLISHED YOUR DATA SEND WITHIN THE REQUEST
188 */
189
190 #ifndef CONFIG_NO__EEPROM_PAGED_ACCESS
191 # define HAVE_EEPROM_PAGED_ACCESS 1
192 #else
193 # define HAVE_EEPROM_PAGED_ACCESS 0
194 #endif
195 /* If HAVE_EEPROM_PAGED_ACCESS is defined to 1, page mode access to EEPROM is
196 * compiled in. Whether page mode or byte mode access is used by AVRDUDE
197 * depends on the target device. Page mode is only used if the device supports
198 * it, e.g. for the ATMega88, 168 etc. You can save quite a bit of memory by
199 * disabling page mode EEPROM access. Costs ~ 138 bytes.
200 */
201
202 #ifndef CONFIG_NO__EEPROM_BYTE_ACCESS
203 # define HAVE_EEPROM_BYTE_ACCESS 1
204 #else
205 # define HAVE_EEPROM_BYTE_ACCESS 0
206 #endif
207 /* If HAVE_EEPROM_BYTE_ACCESS is defined to 1, byte mode access to EEPROM is
208 * compiled in. Byte mode is only used if the device (as identified by its
209 * signature) does not support page mode for EEPROM. It is required for
210 * accessing the EEPROM on the ATMega8. Costs ~54 bytes.
211 */
212
213 #ifndef CONFIG_NO__BOOTLOADER_CAN_EXIT
214 # define BOOTLOADER_CAN_EXIT 1
215 #else
216 # define BOOTLOADER_CAN_EXIT 0
217 #endif
218 /* If this macro is defined to 1, the boot loader will exit shortly after the
219 * programmer closes the connection to the device. Costs extra bytes.
220 */
221
222 #ifndef CONFIG_NO__CHIP_ERASE
223 # define HAVE_CHIP_ERASE 1
224 #else
225 # define HAVE_CHIP_ERASE 0
226 #endif
227 /* If this macro is defined to 1, the boot loader implements the Chip Erase
228 * ISP command. Otherwise pages are erased on demand before they are written.
229 */
230 #ifndef CONFIG_NO__ONDEMAND_PAGEERASE
231 # define HAVE_ONDEMAND_PAGEERASE 1
232 #else
233 # define HAVE_ONDEMAND_PAGEERASE 0
234 #endif
235 /* Even if "HAVE_CHIP_ERASE" is avtivated - enabling the "HAVE_ONDEMAND_PAGEERASE"-
236 * feature the bootloader will erase pages on demand short before writing new data
237 * to it.
238 * If pages are not erase before reprogram (for example because user call avrdude -D)
239 * then data may become inconsistent since writing only allow to unset bits in the flash.
240 * This feature may prevent this...
241 */
242
243 #ifndef CONFIG_NO__NEED_WATCHDOG
244 # define NEED_WATCHDOG 1
245 #else
246 # define NEED_WATCHDOG 0
247 #endif
248 /* ATTANTION: This macro MUST BE 1, if the MCU has reset enabled watchdog (WDTON is 0).
249 * If this macro is defined to 1, the bootloader implements an additional "wdt_disable()"
250 * after its contional entry point.
251 * If the used MCU is fused not to enable watchdog after reset (WDTON is 1 - safty level 1)
252 * then "NEED_WATCHDOG" may be deactivated in order to save some memory.
253 */
254
255 #ifndef CONFIG_NO__PRECISESLEEP
256 # define HAVE_UNPRECISEWAIT 0
257 #else
258 # define HAVE_UNPRECISEWAIT 1
259 #endif
260 /* This macro enables hand-optimized assembler code
261 * instead to use _sleep_ms for delaying USB enumeration.
262 * Because normally these timings do not need to be exact,
263 * the optimized assembler code does not need to be precise.
264 * Therefore it is very small, which saves some PROGMEM bytes!
265 */
266
267 #ifndef CONFIG_NO__FLASH_BYTE_READACCESS
268 # define HAVE_FLASH_BYTE_READACCESS 1
269 #else
270 # define HAVE_FLASH_BYTE_READACCESS 0
271 #endif
272 /* If HAVE_FLASH_BYTE_READACCESS is defined to 1, byte mode access to FLASH is
273 * compiled in. Byte mode sometimes might be used by some programming softwares
274 * (avrdude in terminal mode). Without this feature the device would return "0"
275 * instead the right content of the flash memory.
276 */
277
278 //#define SIGNATURE_BYTES 0x1e, 0x93, 0x07, 0 /* ATMega8 */
279 /* This macro defines the signature bytes returned by the emulated USBasp to
280 * the programmer software. They should match the actual device at least in
281 * memory size and features. If you don't define this, values for ATMega8,
282 * ATMega88, ATMega168 and ATMega328 are guessed correctly.
283 */
284
285
286 /* ------------------------------------------------------------------------- */
287
288 /* Example configuration: Port D bit 3 is connected to a jumper which ties
289 * this pin to GND if the boot loader is requested. Initialization allows
290 * several clock cycles for the input voltage to stabilize before
291 * bootLoaderCondition() samples the value.
292 * We use a function for bootLoaderInit() for convenience and a macro for
293 * bootLoaderCondition() for efficiency.
294 */
295
296 #ifndef __ASSEMBLER__ /* assembler cannot parse function definitions */
297
298 #ifndef MCUCSR /* compatibility between ATMega8 and ATMega88 */
299 # define MCUCSR MCUSR
300 #endif
301
302 static inline void bootLoaderInit(void)
303 {
304 PIN_DDR(JUMPER_PORT) = 0;
305 PIN_PORT(JUMPER_PORT) = (1<< PIN(JUMPER_PORT, JUMPER_BIT)); /* activate pull-up */
306
307 // deactivated by Stephan - reset after each avrdude op is annoing!
308 // if(!(MCUCSR & (1 << EXTRF))) /* If this was not an external reset, ignore */
309 // leaveBootloader();
310
311 MCUCSR = 0; /* clear all reset flags for next time */
312 }
313
314 static inline void bootLoaderExit(void)
315 {
316 PIN_PORT(JUMPER_PORT) = 0; /* undo bootLoaderInit() changes */
317 }
318
319 #define bootLoaderCondition() ((PIN_PIN(JUMPER_PORT) & (1 << PIN(JUMPER_PORT, JUMPER_BIT))) == 0)
320
321 #endif /* __ASSEMBLER__ */
322
323 /* ------------------------------------------------------------------------- */
324
325 #endif /* __bootloader_h_included__ */