My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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