~ubuntu-branches/debian/experimental/arduino/experimental

« back to all changes in this revision

Viewing changes to hardware/arduino/bootloaders/optiboot/optiboot.c

  • Committer: Package Import Robot
  • Author(s): Scott Howard
  • Date: 2012-03-11 18:19:42 UTC
  • mfrom: (1.1.5) (5.1.14 sid)
  • Revision ID: package-import@ubuntu.com-20120311181942-be2clnbz1gcehixb
Tags: 1:1.0.1~rc1+dfsg-1
New upstream release, experimental.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/**********************************************************/
2
2
/* Optiboot bootloader for Arduino                        */
3
3
/*                                                        */
 
4
/* http://optiboot.googlecode.com                         */
 
5
/*                                                        */
 
6
/* Arduino-maintained version : See README.TXT            */
 
7
/* http://code.google.com/p/arduino/                      */
 
8
/*                                                        */
4
9
/* Heavily optimised bootloader that is faster and        */
5
10
/* smaller than the Arduino standard bootloader           */
6
11
/*                                                        */
10
15
/*   Higher baud rate speeds up programming               */
11
16
/*   Written almost entirely in C                         */
12
17
/*   Customisable timeout with accurate timeconstant      */
 
18
/*   Optional virtual UART. No hardware UART required.    */
 
19
/*   Optional virtual boot partition for devices without. */
13
20
/*                                                        */
14
21
/* What you lose:                                         */
15
22
/*   Implements a skeleton STK500 protocol which is       */
18
25
/*   High baud rate breaks compatibility with standard    */
19
26
/*     Arduino flash settings                             */
20
27
/*                                                        */
21
 
/* Currently supports:                                    */
22
 
/*   ATmega168 based devices (Diecimila etc)              */
 
28
/* Fully supported:                                       */
 
29
/*   ATmega168 based devices  (Diecimila etc)             */
23
30
/*   ATmega328P based devices (Duemilanove etc)           */
24
31
/*                                                        */
 
32
/* Alpha test                                             */
 
33
/*   ATmega1280 based devices (Arduino Mega)              */
 
34
/*                                                        */
 
35
/* Work in progress:                                      */
 
36
/*   ATmega644P based devices (Sanguino)                  */
 
37
/*   ATtiny84 based devices (Luminet)                     */
 
38
/*                                                        */
25
39
/* Does not support:                                      */
26
 
/*   ATmega1280 based devices (eg. Mega)                  */
 
40
/*   USB based devices (eg. Teensy)                       */
27
41
/*                                                        */
28
42
/* Assumptions:                                           */
29
43
/*   The code makes several assumptions that reduce the   */
64
78
/*                                                        */
65
79
/**********************************************************/
66
80
 
 
81
 
 
82
/**********************************************************/
 
83
/*                                                        */
 
84
/* Optional defines:                                      */
 
85
/*                                                        */
 
86
/**********************************************************/
 
87
/*                                                        */
 
88
/* BIG_BOOT:                                              */
 
89
/* Build a 1k bootloader, not 512 bytes. This turns on    */
 
90
/* extra functionality.                                   */
 
91
/*                                                        */
 
92
/* BAUD_RATE:                                             */
 
93
/* Set bootloader baud rate.                              */
 
94
/*                                                        */
 
95
/* LUDICROUS_SPEED:                                       */
 
96
/* 230400 baud :-)                                        */
 
97
/*                                                        */
 
98
/* SOFT_UART:                                             */
 
99
/* Use AVR305 soft-UART instead of hardware UART.         */
 
100
/*                                                        */
 
101
/* LED_START_FLASHES:                                     */
 
102
/* Number of LED flashes on bootup.                       */
 
103
/*                                                        */
 
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.   */
 
107
/*                                                        */
 
108
/* SUPPORT_EEPROM:                                        */
 
109
/* Support reading and writing from EEPROM. This is not   */
 
110
/* used by Arduino, so off by default.                    */
 
111
/*                                                        */
 
112
/* TIMEOUT_MS:                                            */
 
113
/* Bootloader timeout period, in milliseconds.            */
 
114
/* 500,1000,2000,4000,8000 supported.                     */
 
115
/*                                                        */
 
116
/**********************************************************/
 
117
 
 
118
/**********************************************************/
 
119
/* Version Numbers!                                       */
 
120
/*                                                        */
 
121
/* Arduino Optiboot now includes this Version number in   */
 
122
/* the source and object code.                            */
 
123
/*                                                        */
 
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.             */
 
129
/*                                                        */
 
130
/**********************************************************/
 
131
 
 
132
/**********************************************************/
 
133
/* Edit History:                                          */
 
134
/*                                                        */
 
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
/**********************************************************/
 
145
 
 
146
#define OPTIBOOT_MAJVER 4
 
147
#define OPTIBOOT_MINVER 4
 
148
 
 
149
#define MAKESTR(a) #a
 
150
#define MAKEVER(a, b) MAKESTR(a*256+b)
 
151
 
 
152
asm("  .section .version\n"
 
153
    "optiboot_version:  .word " MAKEVER(OPTIBOOT_MAJVER, OPTIBOOT_MINVER) "\n"
 
154
    "  .section .text\n");
 
155
 
67
156
#include <inttypes.h>
68
157
#include <avr/io.h>
69
158
#include <avr/pgmspace.h>
70
 
#include <avr/boot.h>
71
 
 
72
 
//#define LED_DATA_FLASH
 
159
 
 
160
// <avr/boot.h> uses sts instructions, but this version uses out instructions
 
161
// This saves cycles and program memory.
 
162
#include "boot.h"
 
163
 
 
164
 
 
165
// We don't use <avr/wdt.h> as those routines have interrupt overhead we don't need.
 
166
 
 
167
#include "pin_defs.h"
 
168
#include "stk500.h"
73
169
 
74
170
#ifndef LED_START_FLASHES
75
171
#define LED_START_FLASHES 0
76
172
#endif
77
173
 
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
 
176
#endif
83
177
 
84
 
/* set the UART baud rate */
 
178
/* set the UART baud rate defaults */
85
179
#ifndef BAUD_RATE
86
 
#define BAUD_RATE   19200
87
 
#endif
88
 
 
89
 
#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
90
 
/* Onboard LED is connected to pin PB5 in Arduino NG, Diecimila, and Duemilanove */ 
91
 
#define LED_DDR     DDRB
92
 
#define LED_PORT    PORTB
93
 
#define LED_PIN     PINB
94
 
#define LED         PINB5
95
 
 
96
 
/* Ports for soft UART */
97
 
#ifdef SOFT_UART
98
 
#define UART_PORT   PORTD
99
 
#define UART_PIN    PIND
100
 
#define UART_DDR    DDRD
101
 
#define UART_TX_BIT 1
102
 
#define UART_RX_BIT 0
103
 
#endif
104
 
#endif
105
 
 
106
 
#if defined(__AVR_ATtiny84__)
107
 
/* Onboard LED is connected to pin PB5 in Arduino NG, Diecimila, and Duemilanove */ 
108
 
#define LED_DDR     DDRA
109
 
#define LED_PORT    PORTA
110
 
#define LED_PIN     PINA
111
 
#define LED         PINA4
112
 
 
113
 
/* Ports for soft UART - left port only for now*/
114
 
#ifdef SOFT_UART
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
120
 
#endif
121
 
#endif
122
 
 
123
 
/* STK500 constants list, from AVRDUDE */
124
 
#define STK_OK              0x10
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
 
186
#else
 
187
#define BAUD_RATE 1200L     // Good even at 32768Hz
 
188
#endif
 
189
#endif
 
190
 
 
191
/* Switch in soft UART for hard baud rates */
 
192
#if (F_CPU/BAUD_RATE) > 280 // > 57600 for 16MHz
 
193
#ifndef SOFT_UART
 
194
#define SOFT_UART
 
195
#endif
 
196
#endif
162
197
 
163
198
/* Watchdog settings */
164
199
#define WATCHDOG_OFF    (0)
170
205
#define WATCHDOG_500MS  (_BV(WDP2) | _BV(WDP0) | _BV(WDE))
171
206
#define WATCHDOG_1S     (_BV(WDP2) | _BV(WDP1) | _BV(WDE))
172
207
#define WATCHDOG_2S     (_BV(WDP2) | _BV(WDP1) | _BV(WDP0) | _BV(WDE))
173
 
#define WATCHDOG_4S     (_BV(WDE3) | _BV(WDE))
174
 
#define WATCHDOG_8S     (_BV(WDE3) | _BV(WDE0) | _BV(WDE))
 
208
#ifndef __AVR_ATmega8__
 
209
#define WATCHDOG_4S     (_BV(WDP3) | _BV(WDE))
 
210
#define WATCHDOG_8S     (_BV(WDP3) | _BV(WDP0) | _BV(WDE))
 
211
#endif
175
212
 
176
213
/* Function Prototypes */
177
214
/* The main function is in init9, which removes the interrupt vector table */
191
228
#endif
192
229
void appStart() __attribute__ ((naked));
193
230
 
 
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)
 
249
#endif
 
250
 
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))
203
258
#endif
 
259
 
204
260
/* main program starts here */
205
261
int main(void) {
 
262
  uint8_t ch;
 
263
 
 
264
  /*
 
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.)
 
269
   */
 
270
  register uint16_t address = 0;
 
271
  register uint8_t  length;
 
272
 
206
273
  // After the zero init loop, this is the first code to run.
207
274
  //
208
275
  // This code makes the following assumptions:
212
279
  //
213
280
  // If not, uncomment the following instructions:
214
281
  // cli();
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
 
285
#endif
217
286
 
218
 
  uint8_t ch;
 
287
  // Adaboot no-wait mod
 
288
  ch = MCUSR;
 
289
  MCUSR = 0;
 
290
  if (!(ch & _BV(EXTRF))) appStart();
219
291
 
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
223
295
#endif
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 );
 
302
#else
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 );
229
307
#endif
230
 
 
231
 
  // Adaboot no-wait mod
232
 
  ch = MCUSR;
233
 
  MCUSR = 0;
234
 
  if (!(ch & _BV(EXTRF))) appStart();
 
308
#endif
235
309
 
236
310
  // Set up watchdog to trigger after 500ms
237
 
  watchdogConfig(WATCHDOG_500MS);
 
311
  watchdogConfig(WATCHDOG_1S);
238
312
 
239
313
  /* Set LED pin as output */
240
314
  LED_DDR |= _BV(LED);
255
329
    ch = getch();
256
330
 
257
331
    if(ch == STK_GET_PARAMETER) {
258
 
      // GET PARAMETER returns a generic 0x03 reply - enough to keep Avrdude happy
259
 
      getNch(1);
260
 
      putch(0x03);
 
332
      unsigned char which = getch();
 
333
      verifySpace();
 
334
      if (which == 0x82) {
 
335
        /*
 
336
         * Send optiboot version as "minor SW version"
 
337
         */
 
338
        putch(OPTIBOOT_MINVER);
 
339
      } else if (which == 0x81) {
 
340
          putch(OPTIBOOT_MAJVER);
 
341
      } else {
 
342
        /*
 
343
         * GET PARAMETER returns a generic 0x03 reply for
 
344
         * other parameters - enough to keep Avrdude happy
 
345
         */
 
346
        putch(0x03);
 
347
      }
261
348
    }
262
349
    else if(ch == STK_SET_DEVICE) {
263
350
      // SET DEVICE is ignored
269
356
    }
270
357
    else if(ch == STK_LOAD_ADDRESS) {
271
358
      // LOAD ADDRESS
272
 
      address = getch();
273
 
      address = (address & 0xff) | (getch() << 8);
274
 
      address += address; // Convert from word address to byte address
 
359
      uint16_t newAddress;
 
360
      newAddress = getch();
 
361
      newAddress = (newAddress & 0xff) | (getch() << 8);
 
362
#ifdef RAMPZ
 
363
      // Transfer top bit to RAMPZ
 
364
      RAMPZ = (newAddress & 0x8000) ? 1 : 0;
 
365
#endif
 
366
      newAddress += newAddress; // Convert from word address to byte address
 
367
      address = newAddress;
275
368
      verifySpace();
276
369
    }
277
370
    else if(ch == STK_UNIVERSAL) {
279
372
      getNch(4);
280
373
      putch(0x00);
281
374
    }
282
 
    /* Write memory, length is big endian and is in bytes  */
 
375
    /* Write memory, length is big endian and is in bytes */
283
376
    else if(ch == STK_PROG_PAGE) {
284
377
      // PROGRAM PAGE - we support flash programming only, not EEPROM
285
378
      uint8_t *bufPtr;
286
379
      uint16_t addrPtr;
287
380
 
288
 
      getLen();
 
381
      getch();                  /* getlen() */
 
382
      length = getch();
 
383
      getch();
289
384
 
290
 
      // Immediately start page erase - this will 4.5ms
291
 
      boot_page_erase((uint16_t)(void*)address);
 
385
      // If we are in RWW section, immediately start page erase
 
386
      if (address < NRWWSTART) __boot_page_erase_short((uint16_t)(void*)address);
292
387
 
293
388
      // While that is going on, read in page contents
294
389
      bufPtr = buff;
295
390
      do *bufPtr++ = getch();
296
391
      while (--length);
297
392
 
 
393
      // If we are in NRWW section, page erase has to be delayed until now.
 
394
      // Todo: Take RAMPZ into account
 
395
      if (address >= NRWWSTART) __boot_page_erase_short((uint16_t)(void*)address);
 
396
 
298
397
      // Read command terminator, start reply
299
398
      verifySpace();
300
 
      
 
399
 
301
400
      // If only a partial page is to be programmed, the erase might not be complete.
302
401
      // So check that here
303
402
      boot_spm_busy_wait();
310
409
        // Move RESET vector to WDT vector
311
410
        uint16_t vect = buff[0] | (buff[1]<<8);
312
411
        rstVect = vect;
313
 
        wdtVect = buff[10] | (buff[11]<<8);
 
412
        wdtVect = buff[8] | (buff[9]<<8);
314
413
        vect -= 4; // Instruction is a relative jump (rjmp), so recalculate.
315
 
        buff[10] = vect & 0xff;
316
 
        buff[11] = vect >> 8;
 
414
        buff[8] = vect & 0xff;
 
415
        buff[9] = vect >> 8;
317
416
 
318
417
        // Add jump to bootloader at RESET vector
319
418
        buff[0] = 0x7f;
329
428
        uint16_t a;
330
429
        a = *bufPtr++;
331
430
        a |= (*bufPtr++) << 8;
332
 
        boot_page_fill((uint16_t)(void*)addrPtr,a);
 
431
        __boot_page_fill_short((uint16_t)(void*)addrPtr,a);
333
432
        addrPtr += 2;
334
433
      } while (--ch);
335
 
      
 
434
 
336
435
      // Write from programming buffer
337
 
      boot_page_write((uint16_t)(void*)address);
 
436
      __boot_page_write_short((uint16_t)(void*)address);
338
437
      boot_spm_busy_wait();
339
438
 
340
439
#if defined(RWWSRE)
346
445
    /* Read memory block mode, length is big endian.  */
347
446
    else if(ch == STK_READ_PAGE) {
348
447
      // READ PAGE - we only read flash
349
 
      getLen();
 
448
      getch();                  /* getlen() */
 
449
      length = getch();
 
450
      getch();
 
451
 
350
452
      verifySpace();
351
453
#ifdef VIRTUAL_BOOT_PARTITION
352
454
      do {
353
455
        // Undo vector patch in bottom page so verify passes
354
456
        if (address == 0)       ch=rstVect & 0xff;
355
457
        else if (address == 1)  ch=rstVect >> 8;
356
 
        else if (address == 10)  ch=wdtVect & 0xff;
357
 
        else if (address == 11) ch=wdtVect >> 8;
 
458
        else if (address == 8)  ch=wdtVect & 0xff;
 
459
        else if (address == 9) ch=wdtVect >> 8;
358
460
        else ch = pgm_read_byte_near(address);
359
461
        address++;
360
462
        putch(ch);
361
463
      } while (--length);
362
464
#else
 
465
#ifdef __AVR_ATmega1280__
 
466
//      do putch(pgm_read_byte_near(address++));
 
467
//      while (--length);
 
468
      do {
 
469
        uint8_t result;
 
470
        __asm__ ("elpm %0,Z\n":"=r"(result):"z"(address));
 
471
        putch(result);
 
472
        address++;
 
473
      }
 
474
      while (--length);
 
475
#else
363
476
      do putch(pgm_read_byte_near(address++));
364
477
      while (--length);
365
478
#endif
 
479
#endif
366
480
    }
367
481
 
368
482
    /* Get device signature bytes  */
419
533
uint8_t getch(void) {
420
534
  uint8_t ch;
421
535
 
422
 
  watchdogReset();
423
 
 
424
536
#ifdef LED_DATA_FLASH
 
537
#ifdef __AVR_ATmega8__
 
538
  LED_PORT ^= _BV(LED);
 
539
#else
425
540
  LED_PIN |= _BV(LED);
426
541
#endif
 
542
#endif
427
543
 
428
544
#ifdef SOFT_UART
429
545
  __asm__ __volatile__ (
434
550
    "   rcall uartDelay\n"              // Wait 1 bit period
435
551
    "   clc\n"
436
552
    "   sbic  %[uartPin],%[uartBit]\n"
437
 
    "   sec\n"                          
 
553
    "   sec\n"
438
554
    "   dec   %[bitCnt]\n"
439
555
    "   breq  3f\n"
440
556
    "   ror   %[ch]\n"
450
566
      "r25"
451
567
);
452
568
#else
453
 
  while(!(UCSR0A & _BV(RXC0)));
 
569
  while(!(UCSR0A & _BV(RXC0)))
 
570
    ;
 
571
  if (!(UCSR0A & _BV(FE0))) {
 
572
      /*
 
573
       * A Framing Error indicates (probably) that something is talking
 
574
       * to us at the wrong bit rate.  Assume that this is because it
 
575
       * expects to be talking to the application, and DON'T reset the
 
576
       * watchdog.  This should cause the bootloader to abort and run
 
577
       * the application "soon", if it keeps happening.  (Note that we
 
578
       * don't care that an invalid char is returned...)
 
579
       */
 
580
    watchdogReset();
 
581
  }
 
582
  
454
583
  ch = UDR0;
455
584
#endif
456
585
 
457
586
#ifdef LED_DATA_FLASH
 
587
#ifdef __AVR_ATmega8__
 
588
  LED_PORT ^= _BV(LED);
 
589
#else
458
590
  LED_PIN |= _BV(LED);
459
591
#endif
 
592
#endif
460
593
 
461
594
  return ch;
462
595
}
463
596
 
464
597
#ifdef SOFT_UART
465
 
//#define UART_B_VALUE (((F_CPU/BAUD_RATE)-23)/6)
 
598
// AVR350 equation: #define UART_B_VALUE (((F_CPU/BAUD_RATE)-23)/6)
 
599
// Adding 3 to numerator simulates nearest rounding for more accurate baud rates
466
600
#define UART_B_VALUE (((F_CPU/BAUD_RATE)-20)/6)
467
601
#if UART_B_VALUE > 255
468
602
#error Baud rate too slow for soft UART
485
619
}
486
620
 
487
621
void verifySpace() {
488
 
  if (getch() != CRC_EOP) appStart();
 
622
  if (getch() != CRC_EOP) {
 
623
    watchdogConfig(WATCHDOG_16MS);    // shorten WD timeout
 
624
    while (1)                         // and busy-loop so that WD causes
 
625
      ;                               //  a reset and app start.
 
626
  }
489
627
  putch(STK_INSYNC);
490
628
}
491
629
 
495
633
    TCNT1 = -(F_CPU/(1024*16));
496
634
    TIFR1 = _BV(TOV1);
497
635
    while(!(TIFR1 & _BV(TOV1)));
 
636
#ifdef __AVR_ATmega8__
 
637
    LED_PORT ^= _BV(LED);
 
638
#else
498
639
    LED_PIN |= _BV(LED);
 
640
#endif
499
641
    watchdogReset();
500
642
  } while (--count);
501
643
}
502
644
#endif
503
645
 
504
 
uint8_t getLen() {
505
 
  getch();
506
 
  length = getch();
507
 
  return getch();
508
 
}
509
 
 
510
646
// Watchdog functions. These are only safe with interrupts turned off.
511
647
void watchdogReset() {
512
648
  __asm__ __volatile__ (
524
660
  __asm__ __volatile__ (
525
661
#ifdef VIRTUAL_BOOT_PARTITION
526
662
    // Jump to WDT vector
527
 
    "ldi r30,5\n"
 
663
    "ldi r30,4\n"
528
664
    "clr r31\n"
529
665
#else
530
666
    // Jump to RST vector