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