65
79
/**********************************************************/
82
/**********************************************************/
84
/* Optional defines: */
86
/**********************************************************/
89
/* Build a 1k bootloader, not 512 bytes. This turns on */
90
/* extra functionality. */
93
/* Set bootloader baud rate. */
95
/* LUDICROUS_SPEED: */
99
/* Use AVR305 soft-UART instead of hardware UART. */
101
/* LED_START_FLASHES: */
102
/* Number of LED flashes on bootup. */
104
/* LED_DATA_FLASH: */
105
/* Flash LED when transferring data. For boards without */
106
/* TX or RX LEDs, or for people who like blinky lights. */
108
/* SUPPORT_EEPROM: */
109
/* Support reading and writing from EEPROM. This is not */
110
/* used by Arduino, so off by default. */
113
/* Bootloader timeout period, in milliseconds. */
114
/* 500,1000,2000,4000,8000 supported. */
116
/**********************************************************/
118
/**********************************************************/
119
/* Version Numbers! */
121
/* Arduino Optiboot now includes this Version number in */
122
/* the source and object code. */
124
/* Version 3 was released as zip from the optiboot */
125
/* repository and was distributed with Arduino 0022. */
126
/* Version 4 starts with the arduino repository commit */
127
/* that brought the arduino repository up-to-date with */
128
/* the optiboot source tree changes since v3. */
130
/**********************************************************/
132
/**********************************************************/
135
/* 4.4 WestfW: add initialization of address to keep */
136
/* the compiler happy. Change SC'ed targets. */
137
/* Return the SW version via READ PARAM */
138
/* 4.3 WestfW: catch framing errors in getch(), so that */
139
/* AVRISP works without HW kludges. */
140
/* http://code.google.com/p/arduino/issues/detail?id=368n*/
141
/* 4.2 WestfW: reduce code size, fix timeouts, change */
142
/* verifySpace to use WDT instead of appstart */
143
/* 4.1 WestfW: put version number in binary. */
144
/**********************************************************/
146
#define OPTIBOOT_MAJVER 4
147
#define OPTIBOOT_MINVER 4
149
#define MAKESTR(a) #a
150
#define MAKEVER(a, b) MAKESTR(a*256+b)
152
asm(" .section .version\n"
153
"optiboot_version: .word " MAKEVER(OPTIBOOT_MAJVER, OPTIBOOT_MINVER) "\n"
154
" .section .text\n");
67
156
#include <inttypes.h>
68
157
#include <avr/io.h>
69
158
#include <avr/pgmspace.h>
72
//#define LED_DATA_FLASH
160
// <avr/boot.h> uses sts instructions, but this version uses out instructions
161
// This saves cycles and program memory.
165
// We don't use <avr/wdt.h> as those routines have interrupt overhead we don't need.
167
#include "pin_defs.h"
74
170
#ifndef LED_START_FLASHES
75
171
#define LED_START_FLASHES 0
78
/* Build-time variables */
79
/* BAUD_RATE Programming baud rate */
80
/* LED_NO_FLASHES Number of LED flashes on boot */
81
/* FLASH_TIME_MS Duration of each LED flash */
82
/* BOOT_TIMEOUT_MS Serial port wait time before exiting bootloader */
174
#ifdef LUDICROUS_SPEED
175
#define BAUD_RATE 230400L
84
/* set the UART baud rate */
178
/* set the UART baud rate defaults */
86
#define BAUD_RATE 19200
89
#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
90
/* Onboard LED is connected to pin PB5 in Arduino NG, Diecimila, and Duemilanove */
92
#define LED_PORT PORTB
96
/* Ports for soft UART */
98
#define UART_PORT PORTD
100
#define UART_DDR DDRD
101
#define UART_TX_BIT 1
102
#define UART_RX_BIT 0
106
#if defined(__AVR_ATtiny84__)
107
/* Onboard LED is connected to pin PB5 in Arduino NG, Diecimila, and Duemilanove */
109
#define LED_PORT PORTA
113
/* Ports for soft UART - left port only for now*/
115
#define UART_PORT PORTA
116
#define UART_PIN PINA
117
#define UART_DDR DDRA
118
#define UART_TX_BIT 2
119
#define UART_RX_BIT 3
123
/* STK500 constants list, from AVRDUDE */
125
#define STK_FAILED 0x11 // Not used
126
#define STK_UNKNOWN 0x12 // Not used
127
#define STK_NODEVICE 0x13 // Not used
128
#define STK_INSYNC 0x14 // ' '
129
#define STK_NOSYNC 0x15 // Not used
130
#define ADC_CHANNEL_ERROR 0x16 // Not used
131
#define ADC_MEASURE_OK 0x17 // Not used
132
#define PWM_CHANNEL_ERROR 0x18 // Not used
133
#define PWM_ADJUST_OK 0x19 // Not used
134
#define CRC_EOP 0x20 // 'SPACE'
135
#define STK_GET_SYNC 0x30 // '0'
136
#define STK_GET_SIGN_ON 0x31 // '1'
137
#define STK_SET_PARAMETER 0x40 // '@'
138
#define STK_GET_PARAMETER 0x41 // 'A'
139
#define STK_SET_DEVICE 0x42 // 'B'
140
#define STK_SET_DEVICE_EXT 0x45 // 'E'
141
#define STK_ENTER_PROGMODE 0x50 // 'P'
142
#define STK_LEAVE_PROGMODE 0x51 // 'Q'
143
#define STK_CHIP_ERASE 0x52 // 'R'
144
#define STK_CHECK_AUTOINC 0x53 // 'S'
145
#define STK_LOAD_ADDRESS 0x55 // 'U'
146
#define STK_UNIVERSAL 0x56 // 'V'
147
#define STK_PROG_FLASH 0x60 // '`'
148
#define STK_PROG_DATA 0x61 // 'a'
149
#define STK_PROG_FUSE 0x62 // 'b'
150
#define STK_PROG_LOCK 0x63 // 'c'
151
#define STK_PROG_PAGE 0x64 // 'd'
152
#define STK_PROG_FUSE_EXT 0x65 // 'e'
153
#define STK_READ_FLASH 0x70 // 'p'
154
#define STK_READ_DATA 0x71 // 'q'
155
#define STK_READ_FUSE 0x72 // 'r'
156
#define STK_READ_LOCK 0x73 // 's'
157
#define STK_READ_PAGE 0x74 // 't'
158
#define STK_READ_SIGN 0x75 // 'u'
159
#define STK_READ_OSCCAL 0x76 // 'v'
160
#define STK_READ_FUSE_EXT 0x77 // 'w'
161
#define STK_READ_OSCCAL_EXT 0x78 // 'x'
180
#if F_CPU >= 8000000L
181
#define BAUD_RATE 115200L // Highest rate Avrdude win32 will support
182
#elsif F_CPU >= 1000000L
183
#define BAUD_RATE 9600L // 19200 also supported, but with significant error
184
#elsif F_CPU >= 128000L
185
#define BAUD_RATE 4800L // Good for 128kHz internal RC
187
#define BAUD_RATE 1200L // Good even at 32768Hz
191
/* Switch in soft UART for hard baud rates */
192
#if (F_CPU/BAUD_RATE) > 280 // > 57600 for 16MHz
163
198
/* Watchdog settings */
164
199
#define WATCHDOG_OFF (0)
192
229
void appStart() __attribute__ ((naked));
231
#if defined(__AVR_ATmega168__)
232
#define RAMSTART (0x100)
233
#define NRWWSTART (0x3800)
234
#elif defined(__AVR_ATmega328P__)
235
#define RAMSTART (0x100)
236
#define NRWWSTART (0x7000)
237
#elif defined (__AVR_ATmega644P__)
238
#define RAMSTART (0x100)
239
#define NRWWSTART (0xE000)
240
#elif defined(__AVR_ATtiny84__)
241
#define RAMSTART (0x100)
242
#define NRWWSTART (0x0000)
243
#elif defined(__AVR_ATmega1280__)
244
#define RAMSTART (0x200)
245
#define NRWWSTART (0xE000)
246
#elif defined(__AVR_ATmega8__) || defined(__AVR_ATmega88__)
247
#define RAMSTART (0x100)
248
#define NRWWSTART (0x1800)
194
251
/* C zero initialises all global variables. However, that requires */
195
252
/* These definitions are NOT zero initialised, but that doesn't matter */
196
253
/* This allows us to drop the zero init code, saving us memory */
197
#define buff ((uint8_t*)(0x100))
198
#define address (*(uint16_t*)(0x200))
199
#define length (*(uint8_t*)(0x202))
254
#define buff ((uint8_t*)(RAMSTART))
200
255
#ifdef VIRTUAL_BOOT_PARTITION
201
#define rstVect (*(uint16_t*)(0x204))
202
#define wdtVect (*(uint16_t*)(0x206))
256
#define rstVect (*(uint16_t*)(RAMSTART+SPM_PAGESIZE*2+4))
257
#define wdtVect (*(uint16_t*)(RAMSTART+SPM_PAGESIZE*2+6))
204
260
/* main program starts here */
265
* Making these local and in registers prevents the need for initializing
266
* them, and also saves space because code no longer stores to memory.
267
* (initializing address keeps the compiler happy, but isn't really
268
* necessary, and uses 4 bytes of flash.)
270
register uint16_t address = 0;
271
register uint8_t length;
206
273
// After the zero init loop, this is the first code to run.
208
275
// This code makes the following assumptions:
213
280
// If not, uncomment the following instructions:
215
// SP=RAMEND; // This is done by hardware reset
216
282
asm volatile ("clr __zero_reg__");
283
#ifdef __AVR_ATmega8__
284
SP=RAMEND; // This is done by hardware reset
287
// Adaboot no-wait mod
290
if (!(ch & _BV(EXTRF))) appStart();
220
292
#if LED_START_FLASHES > 0
221
293
// Set up Timer 1 for timeout counter
222
294
TCCR1B = _BV(CS12) | _BV(CS10); // div 1024
224
296
#ifndef SOFT_UART
297
#ifdef __AVR_ATmega8__
298
UCSRA = _BV(U2X); //Double speed mode USART
299
UCSRB = _BV(RXEN) | _BV(TXEN); // enable Rx & Tx
300
UCSRC = _BV(URSEL) | _BV(UCSZ1) | _BV(UCSZ0); // config USART; 8N1
301
UBRRL = (uint8_t)( (F_CPU + BAUD_RATE * 4L) / (BAUD_RATE * 8L) - 1 );
225
303
UCSR0A = _BV(U2X0); //Double speed mode USART0
226
304
UCSR0B = _BV(RXEN0) | _BV(TXEN0);
227
305
UCSR0C = _BV(UCSZ00) | _BV(UCSZ01);
228
306
UBRR0L = (uint8_t)( (F_CPU + BAUD_RATE * 4L) / (BAUD_RATE * 8L) - 1 );
231
// Adaboot no-wait mod
234
if (!(ch & _BV(EXTRF))) appStart();
236
310
// Set up watchdog to trigger after 500ms
237
watchdogConfig(WATCHDOG_500MS);
311
watchdogConfig(WATCHDOG_1S);
239
313
/* Set LED pin as output */
240
314
LED_DDR |= _BV(LED);