summaryrefslogtreecommitdiffstats
path: root/firmware/bootloaderconfig.h
blob: a5b1f7d9a4d62280e54be423171a5a73b06f65a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/* Name: bootloaderconfig.h
 * Project: USBaspLoader
 * Author: Christian Starkjohann
 * Creation Date: 2007-12-08
 * Tabsize: 4
 * Copyright: (c) 2007 by OBJECTIVE DEVELOPMENT Software GmbH
 * Portions Copyright: (c) 2012 Louis Beaudoin
 * License: GNU GPL v2 (see License.txt)
 * This Revision: $Id: bootloaderconfig.h 729 2009-03-20 09:03:58Z cs $
 */

#ifndef __bootloaderconfig_h_included__
#define __bootloaderconfig_h_included__

// uncomment this to enable the 'jumper from d5 to gnd to enable programming' mode
//#define BUILD_JUMPER_MODE 1

#ifndef BOOTLOADER_ADDRESS
#define BOOTLOADER_ADDRESS 0
#endif

/*
General Description:
This file (together with some settings in Makefile) configures the boot loader
according to the hardware.

This file contains (besides the hardware configuration normally found in
usbconfig.h) two functions or macros: bootLoaderInit() and
bootLoaderCondition(). Whether you implement them as macros or as static
inline functions is up to you, decide based on code size and convenience.

bootLoaderInit() is called as one of the first actions after reset. It should
be a minimum initialization of the hardware so that the boot loader condition
can be read. This will usually consist of activating a pull-up resistor for an
external jumper which selects boot loader mode.

bootLoaderCondition() is called immediately after initialization and in each
main loop iteration. If it returns TRUE, the boot loader will be active. If it
returns FALSE, the boot loader jumps to address 0 (the loaded application)
immediately.

For compatibility with Thomas Fischl's avrusbboot, we also support the macro
names BOOTLOADER_INIT and BOOTLOADER_CONDITION for this functionality. If
these macros are defined, the boot loader uses them.
*/

#define TINY85_HARDWARE_CONFIG_1  1
#define TINY85_HARDWARE_CONFIG_2  2

/* ---------------------------- Hardware Config ---------------------------- */
#define HARDWARE_CONFIG     TINY85_HARDWARE_CONFIG_2

#define USB_CFG_IOPORTNAME      B
/* This is the port where the USB bus is connected. When you configure it to
 * "B", the registers PORTB, PINB and DDRB will be used.
 */

#ifndef __AVR_ATtiny85__
# define USB_CFG_DMINUS_BIT      0
/* This is the bit number in USB_CFG_IOPORT where the USB D- line is connected.
 * This may be any bit in the port.
 */
# define USB_CFG_DPLUS_BIT       2
/* This is the bit number in USB_CFG_IOPORT where the USB D+ line is connected.
 * This may be any bit in the port. Please note that D+ must also be connected
 * to interrupt pin INT0!
 */
#endif
 
#if (defined __AVR_ATtiny85__) && (HARDWARE_CONFIG == TINY85_HARDWARE_CONFIG_1)
# define USB_CFG_DMINUS_BIT      0
/* This is the bit number in USB_CFG_IOPORT where the USB D- line is connected.
 * This may be any bit in the port.
 */
# define USB_CFG_DPLUS_BIT       2
/* This is the bit number in USB_CFG_IOPORT where the USB D+ line is connected.
 * This may be any bit in the port, but must be configured as a pin change interrupt.
 */
 #endif
 
#if (defined __AVR_ATtiny85__) && (HARDWARE_CONFIG == TINY85_HARDWARE_CONFIG_2)
# define USB_CFG_DMINUS_BIT      3
/* This is the bit number in USB_CFG_IOPORT where the USB D- line is connected.
 * This may be any bit in the port.
 */
# define USB_CFG_DPLUS_BIT       4
/* This is the bit number in USB_CFG_IOPORT where the USB D+ line is connected.
 * This may be any bit in the port, but must be configured as a pin change interrupt.
 */
 #endif

#define USB_CFG_CLOCK_KHZ       (F_CPU/1000)
/* Clock rate of the AVR in MHz. Legal values are 12000, 16000 or 16500.
 * The 16.5 MHz version of the code requires no crystal, it tolerates +/- 1%
 * deviation from the nominal frequency. All other rates require a precision
 * of 2000 ppm and thus a crystal!
 * Default if not specified: 12 MHz
 */

/* ----------------------- Optional Hardware Config ------------------------ */

/* #define USB_CFG_PULLUP_IOPORTNAME   D */
/* If you connect the 1.5k pullup resistor from D- to a port pin instead of
 * V+, you can connect and disconnect the device from firmware by calling
 * the macros usbDeviceConnect() and usbDeviceDisconnect() (see usbdrv.h).
 * This constant defines the port on which the pullup resistor is connected.
 */
/* #define USB_CFG_PULLUP_BIT          4 */
/* This constant defines the bit number in USB_CFG_PULLUP_IOPORT (defined
 * above) where the 1.5k pullup resistor is connected. See description
 * above for details.
 */

/* ------------------------------------------------------------------------- */
/* ---------------------- feature / code size options ---------------------- */
/* ------------------------------------------------------------------------- */

#define BOOTLOADER_CAN_EXIT         1
/* If this macro is defined to 1, the boot loader will exit shortly after the
 * programmer closes the connection to the device. Costs ~36 bytes.
 * Required for TINY85MODE
 */
 
/* ----------------------- Optional MCU Description ------------------------ */

/* tiny85 Architecture Specifics */
#ifndef __AVR_ATtiny85__
#  error "uBoot is only designed for attiny85"
#endif
#define TINY85MODE

/* ------------- Set up interrupt configuration (CPU specific) -------------- */

// setup interrupt for Pin Change for D+
#define USB_INTR_CFG            PCMSK
#define USB_INTR_CFG_SET        (1 << USB_CFG_DPLUS_BIT)
#define USB_INTR_CFG_CLR        0
#define USB_INTR_ENABLE         GIMSK
#define USB_INTR_ENABLE_BIT     PCIE
#define USB_INTR_PENDING        GIFR
#define USB_INTR_PENDING_BIT    PCIF
#define USB_INTR_VECTOR         PCINT0_vect

// Microcontroller vectortable entries in the flash
#define RESET_VECTOR_OFFSET         0
#define USBPLUS_VECTOR_OFFSET       2

// number of bytes before the boot loader vectors to store the tiny application vector table
#define TINYVECTOR_RESET_OFFSET     4
#define TINYVECTOR_USBPLUS_OFFSET   2
#define TINYVECTOR_OSCCAL_OFFSET    6


/* Example configuration: Port D bit 3 is connected to a jumper which ties
 * this pin to GND if the boot loader is requested. Initialization allows
 * several clock cycles for the input voltage to stabilize before
 * bootLoaderCondition() samples the value.
 * We use a function for bootLoaderInit() for convenience and a macro for
 * bootLoaderCondition() for efficiency.
 */

#define JUMPER_BIT  0   /* jumper is connected to this bit in port B, active low */


#ifdef BUILD_JUMPER_MODE
  #define START_JUMPER_PIN 5
  #define digitalRead(pin) (PINB & _BV(pin))
  #define bootLoaderStartCondition() (!digitalRead(START_JUMPER_PIN))
  #define bootLoaderCondition() 1
  
  #ifndef __ASSEMBLER__   /* assembler cannot parse function definitions */
    static inline void  bootLoaderInit(void) {
      // DeuxVis pin-5 pullup
      PORTB |= _BV(START_JUMPER_PIN); // has pullup enabled
      _delay_ms(10);
    }
    static inline void  bootLoaderExit(void) {
      // DeuxVis pin-5 pullup
      PORTB = 0;
    }
  #endif /* __ASSEMBLER__ */
    
#else
  #define bootLoaderInit()
  #define bootLoaderExit()
  #define bootLoaderCondition()   (idlePolls < (AUTO_EXIT_MS * 10UL))
  #if LOW_POWER_MODE
    // only starts bootloader if USB D- is pulled high on startup - by putting your pullup in to an external connector
    // you can avoid ever entering an out of spec clock speed or waiting on bootloader when that pullup isn't there
    #define bootLoaderStartCondition() \
      (PINB & (_BV(USB_CFG_DMINUS_BIT) | _BV(USB_CFG_DMINUS_BIT))) == _BV(USB_CFG_DMINUS_BIT)
  #else
    #define bootLoaderStartCondition() 1
  #endif
#endif

/* ------------------------------------------------------------------------- */


/*
 * Define bootloader timeout value. 
 * 
 *  These will only be used if is bootLoaderCondition() evaluates idlePolls below!
 * 
 *  AUTO_EXIT_NO_USB_MS        The bootloader will exit after this delay if no USB is connected.
 *                             Set to 0 to disable
 *                             Adds ~6 bytes.
 *                             (This will wait for an USB SE0 reset from the host)
 *  AUTO_EXIT_MS               The bootloader will exit after this delay if no USB communication
 *                             from the host tool was received.
 *                             Set to 0 to disable
 *  
 *  All values are approx. in milliseconds
 */

#define AUTO_EXIT_NO_USB_MS    0
#define AUTO_EXIT_MS           5000

 /*
 *	Defines the setting of the RC-oscillator calibration after quitting the bootloader. (OSCCAL)
 * 
 *  OSCCAL_RESTORE            Set this to '1' to revert to factory calibration, which is 16.0 MHZ +/-10%
 *                            Adds ~14 bytes.
 *
 *  OSCCAL_16.5MHz            Set this to '1' to use the same calibration as during program upload.
 *                            This value is 16.5Mhz +/-1% as calibrated from the USB timing. Please note
 *                            that only true if the ambient temperature does not change.
 *                            This is the default behaviour of the Digispark.
 *                            Adds ~38 bytes.
 *
 *  If both options are selected, OSCCAL_RESTORE takes precedence.
 *
 *  If no option is selected, OSCCAL will be left untouched and stay at either 16.0Mhz or 16.5Mhz depending
 *  on whether the bootloader was activated. This will take the least memory. You can use this if your program
 *  comes with its own OSCCAL calibration or an external clock source is used.
 */
 
 #define OSCCAL_RESTORE 0
 #define OSCCAL_16_5MHz 1
 
/*  
 *  Defines handling of an indicator LED while the bootloader is active.  
 * 
 *  LED_PRESENT               Set this this to '1' to active all LED related code. If this is 0, all other
 *                            defines are ignored.
 *                            Adds 18 bytes depending on implementation.								
 *
 *  LED_DDR,LED_PORT,LED_PIN  Where is your LED connected?
 *
 *  LED_INIT                  Called once after bootloader entry
 *  LED_EXIT                  Called once during bootloader exit
 *  LED_MACRO                 Called in the main loop with the idle counter as parameter.
 *                            Use to define pattern.
 */ 

#define	LED_PRESENT	    0

#define	LED_DDR			DDRB
#define LED_PORT		PORTB
#define	LED_PIN			PB1

#if LED_PRESENT
  #define LED_INIT(x)		LED_PORT &=~_BV(LED_PIN);
  #define LED_EXIT(x)		LED_DDR  &=~_BV(LED_PIN);
  #define LED_MACRO(x)	if ( x & 0xd ) {LED_DDR&=~_BV(LED_PIN);} else {LED_DDR|=_BV(LED_PIN);}
#else
  #define LED_INIT(x)		
  #define LED_EXIT(x)		
  #define LED_MACRO(x)	
#endif
/* ------------------------------------------------------------------------- */

#endif /* __bootloader_h_included__ */