Debian Codegraph @

bootloaders/optiboot/optiboot.c

raw symbols SCIP
1 /**********************************************************/
2 /* Optiboot bootloader for Arduino */
3 /* */
4 /* http://optiboot.googlecode.com */
5 /* */
6 /* Arduino-maintained version : See README.TXT */
7 /* http://code.google.com/p/arduino/ */
8 /* */
9 /* Heavily optimised bootloader that is faster and */
10 /* smaller than the Arduino standard bootloader */
11 /* */
12 /* Enhancements: */
13 /* Fits in 512 bytes, saving 1.5K of code space */
14 /* Background page erasing speeds up programming */
15 /* Higher baud rate speeds up programming */
16 /* Written almost entirely in C */
17 /* Customisable timeout with accurate timeconstant */
18 /* Optional virtual UART. No hardware UART required. */
19 /* Optional virtual boot partition for devices without. */
20 /* */
21 /* What you lose: */
22 /* Implements a skeleton STK500 protocol which is */
23 /* missing several features including EEPROM */
24 /* programming and non-page-aligned writes */
25 /* High baud rate breaks compatibility with standard */
26 /* Arduino flash settings */
27 /* */
28 /* Fully supported: */
29 /* ATmega168 based devices (Diecimila etc) */
30 /* ATmega328P based devices (Duemilanove etc) */
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 /* */
39 /* Does not support: */
40 /* USB based devices (eg. Teensy) */
41 /* */
42 /* Assumptions: */
43 /* The code makes several assumptions that reduce the */
44 /* code size. They are all true after a hardware reset, */
45 /* but may not be true if the bootloader is called by */
46 /* other means or on other hardware. */
47 /* No interrupts can occur */
48 /* UART and Timer 1 are set to their reset state */
49 /* SP points to RAMEND */
50 /* */
51 /* Code builds on code, libraries and optimisations from: */
52 /* stk500boot.c by Jason P. Kyle */
53 /* Arduino bootloader http://arduino.cc */
54 /* Spiff's 1K bootloader http://spiffie.org/know/arduino_1k_bootloader/bootloader.shtml */
55 /* avr-libc project http://nongnu.org/avr-libc */
56 /* Adaboot http://www.ladyada.net/library/arduino/bootloader.html */
57 /* AVR305 Atmel Application Note */
58 /* */
59 /* This program is free software; you can redistribute it */
60 /* and/or modify it under the terms of the GNU General */
61 /* Public License as published by the Free Software */
62 /* Foundation; either version 2 of the License, or */
63 /* (at your option) any later version. */
64 /* */
65 /* This program is distributed in the hope that it will */
66 /* be useful, but WITHOUT ANY WARRANTY; without even the */
67 /* implied warranty of MERCHANTABILITY or FITNESS FOR A */
68 /* PARTICULAR PURPOSE. See the GNU General Public */
69 /* License for more details. */
70 /* */
71 /* You should have received a copy of the GNU General */
72 /* Public License along with this program; if not, write */
73 /* to the Free Software Foundation, Inc., */
74 /* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
75 /* */
76 /* Licence can be viewed at */
77 /* http://www.fsf.org/licenses/gpl.txt */
78 /* */
79 /**********************************************************/
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 /* Jan 2012: */
136 /* 4.5 WestfW: fix NRWW value for m1284. */
137 /* 4.4 WestfW: use attribute OS_main instead of naked for */
138 /* main(). This allows optimizations that we */
139 /* count on, which are prohibited in naked */
140 /* functions due to PR42240. (keeps us less */
141 /* than 512 bytes when compiler is gcc4.5 */
142 /* (code from 4.3.2 remains the same.) */
143 /* 4.4 WestfW and Maniacbug: Add m1284 support. This */
144 /* does not change the 328 binary, so the */
145 /* version number didn't change either. (?) */
146 /* June 2011: */
147 /* 4.4 WestfW: remove automatic soft_uart detect (didn't */
148 /* know what it was doing or why.) Added a */
149 /* check of the calculated BRG value instead. */
150 /* Version stays 4.4; existing binaries are */
151 /* not changed. */
152 /* 4.4 WestfW: add initialization of address to keep */
153 /* the compiler happy. Change SC'ed targets. */
154 /* Return the SW version via READ PARAM */
155 /* 4.3 WestfW: catch framing errors in getch(), so that */
156 /* AVRISP works without HW kludges. */
157 /* http://code.google.com/p/arduino/issues/detail?id=368n*/
158 /* 4.2 WestfW: reduce code size, fix timeouts, change */
159 /* verifySpace to use WDT instead of appstart */
160 /* 4.1 WestfW: put version number in binary. */
161 /**********************************************************/
162
163 #define OPTIBOOT_MAJVER 4
164 #define OPTIBOOT_MINVER 5
165
166 #define MAKESTR(a) #a
167 #define MAKEVER(a, b) MAKESTR(a*256+b)
168
169 asm(" .section .version\n"
170 "optiboot_version: .word " MAKEVER(OPTIBOOT_MAJVER, OPTIBOOT_MINVER) "\n"
171 " .section .text\n");
172
173 #include <inttypes.h>
174 #include <avr/io.h>
175 #include <avr/pgmspace.h>
176
177 // <avr/boot.h> uses sts instructions, but this version uses out instructions
178 // This saves cycles and program memory.
179 #include "boot.h"
180
181
182 // We don't use <avr/wdt.h> as those routines have interrupt overhead we don't need.
183
184 #include "pin_defs.h"
185 #include "stk500.h"
186
187 #ifndef LED_START_FLASHES
188 #define LED_START_FLASHES 0
189 #endif
190
191 #ifdef LUDICROUS_SPEED
192 #define BAUD_RATE 230400L
193 #endif
194
195 /* set the UART baud rate defaults */
196 #ifndef BAUD_RATE
197 #if F_CPU >= 8000000L
198 #define BAUD_RATE 115200L // Highest rate Avrdude win32 will support
199 #elsif F_CPU >= 1000000L
200 #define BAUD_RATE 9600L // 19200 also supported, but with significant error
201 #elsif F_CPU >= 128000L
202 #define BAUD_RATE 4800L // Good for 128kHz internal RC
203 #else
204 #define BAUD_RATE 1200L // Good even at 32768Hz
205 #endif
206 #endif
207
208 #if 0
209 /* Switch in soft UART for hard baud rates */
210 /*
211 * I don't understand what this was supposed to accomplish, where the
212 * constant "280" came from, or why automatically (and perhaps unexpectedly)
213 * switching to a soft uart is a good thing, so I'm undoing this in favor
214 * of a range check using the same calc used to config the BRG...
215 */
216 #if (F_CPU/BAUD_RATE) > 280 // > 57600 for 16MHz
217 #ifndef SOFT_UART
218 #define SOFT_UART
219 #endif
220 #endif
221 #else // 0
222 #if (F_CPU + BAUD_RATE * 4L) / (BAUD_RATE * 8L) - 1 > 250
223 #error Unachievable baud rate (too slow) BAUD_RATE
224 #endif // baud rate slow check
225 #if (F_CPU + BAUD_RATE * 4L) / (BAUD_RATE * 8L) - 1 < 3
226 #error Unachievable baud rate (too fast) BAUD_RATE
227 #endif // baud rate fastn check
228 #endif
229
230 /* Watchdog settings */
231 #define WATCHDOG_OFF (0)
232 #define WATCHDOG_16MS (_BV(WDE))
233 #define WATCHDOG_32MS (_BV(WDP0) | _BV(WDE))
234 #define WATCHDOG_64MS (_BV(WDP1) | _BV(WDE))
235 #define WATCHDOG_125MS (_BV(WDP1) | _BV(WDP0) | _BV(WDE))
236 #define WATCHDOG_250MS (_BV(WDP2) | _BV(WDE))
237 #define WATCHDOG_500MS (_BV(WDP2) | _BV(WDP0) | _BV(WDE))
238 #define WATCHDOG_1S (_BV(WDP2) | _BV(WDP1) | _BV(WDE))
239 #define WATCHDOG_2S (_BV(WDP2) | _BV(WDP1) | _BV(WDP0) | _BV(WDE))
240 #ifndef __AVR_ATmega8__
241 #define WATCHDOG_4S (_BV(WDP3) | _BV(WDE))
242 #define WATCHDOG_8S (_BV(WDP3) | _BV(WDP0) | _BV(WDE))
243 #endif
244
245 /* Function Prototypes */
246 /* The main function is in init9, which removes the interrupt vector table */
247 /* we don't need. It is also 'naked', which means the compiler does not */
248 /* generate any entry or exit code itself. */
249 int main(void) __attribute__ ((OS_main)) __attribute__ ((section (".init9")));
250 void putch(char);
251 uint8_t getch(void);
252 static inline void getNch(uint8_t); /* "static inline" is a compiler hint to reduce code size */
253 void verifySpace();
254 static inline void flash_led(uint8_t);
255 uint8_t getLen();
256 static inline void watchdogReset();
257 void watchdogConfig(uint8_t x);
258 #ifdef SOFT_UART
259 void uartDelay() __attribute__ ((naked));
260 #endif
261 void appStart() __attribute__ ((naked));
262
263 /*
264 * NRWW memory
265 * Addresses below NRWW (Non-Read-While-Write) can be programmed while
266 * continuing to run code from flash, slightly speeding up programming
267 * time. Beware that Atmel data sheets specify this as a WORD address,
268 * while optiboot will be comparing against a 16-bit byte address. This
269 * means that on a part with 128kB of memory, the upper part of the lower
270 * 64k will get NRWW processing as well, even though it doesn't need it.
271 * That's OK. In fact, you can disable the overlapping processing for
272 * a part entirely by setting NRWWSTART to zero. This reduces code
273 * space a bit, at the expense of being slightly slower, overall.
274 *
275 * RAMSTART should be self-explanatory. It's bigger on parts with a
276 * lot of peripheral registers.
277 */
278 #if defined(__AVR_ATmega168__)
279 #define RAMSTART (0x100)
280 #define NRWWSTART (0x3800)
281 #elif defined(__AVR_ATmega328P__)
282 #define RAMSTART (0x100)
283 #define NRWWSTART (0x7000)
284 #elif defined (__AVR_ATmega644P__)
285 #define RAMSTART (0x100)
286 #define NRWWSTART (0xE000)
287 #elif defined (__AVR_ATmega1284P__)
288 #define RAMSTART (0x100)
289 #define NRWWSTART (0xE000)
290 #elif defined(__AVR_ATtiny84__)
291 #define RAMSTART (0x100)
292 #define NRWWSTART (0x0000)
293 #elif defined(__AVR_ATmega1280__)
294 #define RAMSTART (0x200)
295 #define NRWWSTART (0xE000)
296 #elif defined(__AVR_ATmega8__) || defined(__AVR_ATmega88__)
297 #define RAMSTART (0x100)
298 #define NRWWSTART (0x1800)
299 #endif
300
301 /* C zero initialises all global variables. However, that requires */
302 /* These definitions are NOT zero initialised, but that doesn't matter */
303 /* This allows us to drop the zero init code, saving us memory */
304 #define buff ((uint8_t*)(RAMSTART))
305 #ifdef VIRTUAL_BOOT_PARTITION
306 #define rstVect (*(uint16_t*)(RAMSTART+SPM_PAGESIZE*2+4))
307 #define wdtVect (*(uint16_t*)(RAMSTART+SPM_PAGESIZE*2+6))
308 #endif
309
310 /* main program starts here */
311 int main(void) {
312 uint8_t ch;
313
314 /*
315 * Making these local and in registers prevents the need for initializing
316 * them, and also saves space because code no longer stores to memory.
317 * (initializing address keeps the compiler happy, but isn't really
318 * necessary, and uses 4 bytes of flash.)
319 */
320 register uint16_t address = 0;
321 register uint8_t length;
322
323 // After the zero init loop, this is the first code to run.
324 //
325 // This code makes the following assumptions:
326 // No interrupts will execute
327 // SP points to RAMEND
328 // r1 contains zero
329 //
330 // If not, uncomment the following instructions:
331 // cli();
332 asm volatile ("clr __zero_reg__");
333 #ifdef __AVR_ATmega8__
334 SP=RAMEND; // This is done by hardware reset
335 #endif
336
337 // Adaboot no-wait mod
338 ch = MCUSR;
339 MCUSR = 0;
340 if (!(ch & _BV(EXTRF))) appStart();
341
342 #if LED_START_FLASHES > 0
343 // Set up Timer 1 for timeout counter
344 TCCR1B = _BV(CS12) | _BV(CS10); // div 1024
345 #endif
346 #ifndef SOFT_UART
347 #ifdef __AVR_ATmega8__
348 UCSRA = _BV(U2X); //Double speed mode USART
349 UCSRB = _BV(RXEN) | _BV(TXEN); // enable Rx & Tx
350 UCSRC = _BV(URSEL) | _BV(UCSZ1) | _BV(UCSZ0); // config USART; 8N1
351 UBRRL = (uint8_t)( (F_CPU + BAUD_RATE * 4L) / (BAUD_RATE * 8L) - 1 );
352 #else
353 UCSR0A = _BV(U2X0); //Double speed mode USART0
354 UCSR0B = _BV(RXEN0) | _BV(TXEN0);
355 UCSR0C = _BV(UCSZ00) | _BV(UCSZ01);
356 UBRR0L = (uint8_t)( (F_CPU + BAUD_RATE * 4L) / (BAUD_RATE * 8L) - 1 );
357 #endif
358 #endif
359
360 // Set up watchdog to trigger after 500ms
361 watchdogConfig(WATCHDOG_1S);
362
363 /* Set LED pin as output */
364 LED_DDR |= _BV(LED);
365
366 #ifdef SOFT_UART
367 /* Set TX pin as output */
368 UART_DDR |= _BV(UART_TX_BIT);
369 #endif
370
371 #if LED_START_FLASHES > 0
372 /* Flash onboard LED to signal entering of bootloader */
373 flash_led(LED_START_FLASHES * 2);
374 #endif
375
376 /* Forever loop */
377 for (;;) {
378 /* get character from UART */
379 ch = getch();
380
381 if(ch == STK_GET_PARAMETER) {
382 unsigned char which = getch();
383 verifySpace();
384 if (which == 0x82) {
385 /*
386 * Send optiboot version as "minor SW version"
387 */
388 putch(OPTIBOOT_MINVER);
389 } else if (which == 0x81) {
390 putch(OPTIBOOT_MAJVER);
391 } else {
392 /*
393 * GET PARAMETER returns a generic 0x03 reply for
394 * other parameters - enough to keep Avrdude happy
395 */
396 putch(0x03);
397 }
398 }
399 else if(ch == STK_SET_DEVICE) {
400 // SET DEVICE is ignored
401 getNch(20);
402 }
403 else if(ch == STK_SET_DEVICE_EXT) {
404 // SET DEVICE EXT is ignored
405 getNch(5);
406 }
407 else if(ch == STK_LOAD_ADDRESS) {
408 // LOAD ADDRESS
409 uint16_t newAddress;
410 newAddress = getch();
411 newAddress = (newAddress & 0xff) | (getch() << 8);
412 #ifdef RAMPZ
413 // Transfer top bit to RAMPZ
414 RAMPZ = (newAddress & 0x8000) ? 1 : 0;
415 #endif
416 newAddress += newAddress; // Convert from word address to byte address
417 address = newAddress;
418 verifySpace();
419 }
420 else if(ch == STK_UNIVERSAL) {
421 // UNIVERSAL command is ignored
422 getNch(4);
423 putch(0x00);
424 }
425 /* Write memory, length is big endian and is in bytes */
426 else if(ch == STK_PROG_PAGE) {
427 // PROGRAM PAGE - we support flash programming only, not EEPROM
428 uint8_t *bufPtr;
429 uint16_t addrPtr;
430
431 getch(); /* getlen() */
432 length = getch();
433 getch();
434
435 // If we are in RWW section, immediately start page erase
436 if (address < NRWWSTART) __boot_page_erase_short((uint16_t)(void*)address);
437
438 // While that is going on, read in page contents
439 bufPtr = buff;
440 do *bufPtr++ = getch();
441 while (--length);
442
443 // If we are in NRWW section, page erase has to be delayed until now.
444 // Todo: Take RAMPZ into account
445 if (address >= NRWWSTART) __boot_page_erase_short((uint16_t)(void*)address);
446
447 // Read command terminator, start reply
448 verifySpace();
449
450 // If only a partial page is to be programmed, the erase might not be complete.
451 // So check that here
452 boot_spm_busy_wait();
453
454 #ifdef VIRTUAL_BOOT_PARTITION
455 if ((uint16_t)(void*)address == 0) {
456 // This is the reset vector page. We need to live-patch the code so the
457 // bootloader runs.
458 //
459 // Move RESET vector to WDT vector
460 uint16_t vect = buff[0] | (buff[1]<<8);
461 rstVect = vect;
462 wdtVect = buff[8] | (buff[9]<<8);
463 vect -= 4; // Instruction is a relative jump (rjmp), so recalculate.
464 buff[8] = vect & 0xff;
465 buff[9] = vect >> 8;
466
467 // Add jump to bootloader at RESET vector
468 buff[0] = 0x7f;
469 buff[1] = 0xce; // rjmp 0x1d00 instruction
470 }
471 #endif
472
473 // Copy buffer into programming buffer
474 bufPtr = buff;
475 addrPtr = (uint16_t)(void*)address;
476 ch = SPM_PAGESIZE / 2;
477 do {
478 uint16_t a;
479 a = *bufPtr++;
480 a |= (*bufPtr++) << 8;
481 __boot_page_fill_short((uint16_t)(void*)addrPtr,a);
482 addrPtr += 2;
483 } while (--ch);
484
485 // Write from programming buffer
486 __boot_page_write_short((uint16_t)(void*)address);
487 boot_spm_busy_wait();
488
489 #if defined(RWWSRE)
490 // Reenable read access to flash
491 boot_rww_enable();
492 #endif
493
494 }
495 /* Read memory block mode, length is big endian. */
496 else if(ch == STK_READ_PAGE) {
497 // READ PAGE - we only read flash
498 getch(); /* getlen() */
499 length = getch();
500 getch();
501
502 verifySpace();
503 #ifdef VIRTUAL_BOOT_PARTITION
504 do {
505 // Undo vector patch in bottom page so verify passes
506 if (address == 0) ch=rstVect & 0xff;
507 else if (address == 1) ch=rstVect >> 8;
508 else if (address == 8) ch=wdtVect & 0xff;
509 else if (address == 9) ch=wdtVect >> 8;
510 else ch = pgm_read_byte_near(address);
511 address++;
512 putch(ch);
513 } while (--length);
514 #else
515 #ifdef RAMPZ
516 // Since RAMPZ should already be set, we need to use EPLM directly.
517 // do putch(pgm_read_byte_near(address++));
518 // while (--length);
519 do {
520 uint8_t result;
521 __asm__ ("elpm %0,Z\n":"=r"(result):"z"(address));
522 putch(result);
523 address++;
524 }
525 while (--length);
526 #else
527 do putch(pgm_read_byte_near(address++));
528 while (--length);
529 #endif
530 #endif
531 }
532
533 /* Get device signature bytes */
534 else if(ch == STK_READ_SIGN) {
535 // READ SIGN - return what Avrdude wants to hear
536 verifySpace();
537 putch(SIGNATURE_0);
538 putch(SIGNATURE_1);
539 putch(SIGNATURE_2);
540 }
541 else if (ch == STK_LEAVE_PROGMODE) { /* 'Q' */
542 // Adaboot no-wait mod
543 watchdogConfig(WATCHDOG_16MS);
544 verifySpace();
545 }
546 else {
547 // This covers the response to commands like STK_ENTER_PROGMODE
548 verifySpace();
549 }
550 putch(STK_OK);
551 }
552 }
553
554 void putch(char ch) {
555 #ifndef SOFT_UART
556 while (!(UCSR0A & _BV(UDRE0)));
557 UDR0 = ch;
558 #else
559 __asm__ __volatile__ (
560 " com %[ch]\n" // ones complement, carry set
561 " sec\n"
562 "1: brcc 2f\n"
563 " cbi %[uartPort],%[uartBit]\n"
564 " rjmp 3f\n"
565 "2: sbi %[uartPort],%[uartBit]\n"
566 " nop\n"
567 "3: rcall uartDelay\n"
568 " rcall uartDelay\n"
569 " lsr %[ch]\n"
570 " dec %[bitcnt]\n"
571 " brne 1b\n"
572 :
573 :
574 [bitcnt] "d" (10),
575 [ch] "r" (ch),
576 [uartPort] "I" (_SFR_IO_ADDR(UART_PORT)),
577 [uartBit] "I" (UART_TX_BIT)
578 :
579 "r25"
580 );
581 #endif
582 }
583
584 uint8_t getch(void) {
585 uint8_t ch;
586
587 #ifdef LED_DATA_FLASH
588 #ifdef __AVR_ATmega8__
589 LED_PORT ^= _BV(LED);
590 #else
591 LED_PIN |= _BV(LED);
592 #endif
593 #endif
594
595 #ifdef SOFT_UART
596 __asm__ __volatile__ (
597 "1: sbic %[uartPin],%[uartBit]\n" // Wait for start edge
598 " rjmp 1b\n"
599 " rcall uartDelay\n" // Get to middle of start bit
600 "2: rcall uartDelay\n" // Wait 1 bit period
601 " rcall uartDelay\n" // Wait 1 bit period
602 " clc\n"
603 " sbic %[uartPin],%[uartBit]\n"
604 " sec\n"
605 " dec %[bitCnt]\n"
606 " breq 3f\n"
607 " ror %[ch]\n"
608 " rjmp 2b\n"
609 "3:\n"
610 :
611 [ch] "=r" (ch)
612 :
613 [bitCnt] "d" (9),
614 [uartPin] "I" (_SFR_IO_ADDR(UART_PIN)),
615 [uartBit] "I" (UART_RX_BIT)
616 :
617 "r25"
618 );
619 #else
620 while(!(UCSR0A & _BV(RXC0)))
621 ;
622 if (!(UCSR0A & _BV(FE0))) {
623 /*
624 * A Framing Error indicates (probably) that something is talking
625 * to us at the wrong bit rate. Assume that this is because it
626 * expects to be talking to the application, and DON'T reset the
627 * watchdog. This should cause the bootloader to abort and run
628 * the application "soon", if it keeps happening. (Note that we
629 * don't care that an invalid char is returned...)
630 */
631 watchdogReset();
632 }
633
634 ch = UDR0;
635 #endif
636
637 #ifdef LED_DATA_FLASH
638 #ifdef __AVR_ATmega8__
639 LED_PORT ^= _BV(LED);
640 #else
641 LED_PIN |= _BV(LED);
642 #endif
643 #endif
644
645 return ch;
646 }
647
648 #ifdef SOFT_UART
649 // AVR305 equation: #define UART_B_VALUE (((F_CPU/BAUD_RATE)-23)/6)
650 // Adding 3 to numerator simulates nearest rounding for more accurate baud rates
651 #define UART_B_VALUE (((F_CPU/BAUD_RATE)-20)/6)
652 #if UART_B_VALUE > 255
653 #error Baud rate too slow for soft UART
654 #endif
655
656 void uartDelay() {
657 __asm__ __volatile__ (
658 "ldi r25,%[count]\n"
659 "1:dec r25\n"
660 "brne 1b\n"
661 "ret\n"
662 ::[count] "M" (UART_B_VALUE)
663 );
664 }
665 #endif
666
667 void getNch(uint8_t count) {
668 do getch(); while (--count);
669 verifySpace();
670 }
671
672 void verifySpace() {
673 if (getch() != CRC_EOP) {
674 watchdogConfig(WATCHDOG_16MS); // shorten WD timeout
675 while (1) // and busy-loop so that WD causes
676 ; // a reset and app start.
677 }
678 putch(STK_INSYNC);
679 }
680
681 #if LED_START_FLASHES > 0
682 void flash_led(uint8_t count) {
683 do {
684 TCNT1 = -(F_CPU/(1024*16));
685 TIFR1 = _BV(TOV1);
686 while(!(TIFR1 & _BV(TOV1)));
687 #ifdef __AVR_ATmega8__
688 LED_PORT ^= _BV(LED);
689 #else
690 LED_PIN |= _BV(LED);
691 #endif
692 watchdogReset();
693 } while (--count);
694 }
695 #endif
696
697 // Watchdog functions. These are only safe with interrupts turned off.
698 void watchdogReset() {
699 __asm__ __volatile__ (
700 "wdr\n"
701 );
702 }
703
704 void watchdogConfig(uint8_t x) {
705 WDTCSR = _BV(WDCE) | _BV(WDE);
706 WDTCSR = x;
707 }
708
709 void appStart() {
710 watchdogConfig(WATCHDOG_OFF);
711 __asm__ __volatile__ (
712 #ifdef VIRTUAL_BOOT_PARTITION
713 // Jump to WDT vector
714 "ldi r30,4\n"
715 "clr r31\n"
716 #else
717 // Jump to RST vector
718 "clr r30\n"
719 "clr r31\n"
720 #endif
721 "ijmp\n"
722 );
723 }