ATmegaBOOT.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. /**********************************************************/
  2. /* Serial Bootloader for Atmel megaAVR Controllers */
  3. /* */
  4. /* tested with ATmega644 and ATmega644P */
  5. /* should work with other mega's, see code for details */
  6. /* */
  7. /* ATmegaBOOT.c */
  8. /* */
  9. /* 20090131: Added 324P support from Alex Leone */
  10. /* Marius Kintel */
  11. /* 20080915: applied ADABoot mods for Sanguino 644P */
  12. /* Brian Riley */
  13. /* 20080711: hacked for Sanguino by Zach Smith */
  14. /* and Justin Day */
  15. /* 20070626: hacked for Arduino Diecimila (which auto- */
  16. /* resets when a USB connection is made to it) */
  17. /* by D. Mellis */
  18. /* 20060802: hacked for Arduino by D. Cuartielles */
  19. /* based on a previous hack by D. Mellis */
  20. /* and D. Cuartielles */
  21. /* */
  22. /* Monitor and debug functions were added to the original */
  23. /* code by Dr. Erik Lins, chip45.com. (See below) */
  24. /* */
  25. /* Thanks to Karl Pitrich for fixing a bootloader pin */
  26. /* problem and more informative LED blinking! */
  27. /* */
  28. /* For the latest version see: */
  29. /* http://www.chip45.com/ */
  30. /* */
  31. /* ------------------------------------------------------ */
  32. /* */
  33. /* based on stk500boot.c */
  34. /* Copyright (c) 2003, Jason P. Kyle */
  35. /* All rights reserved. */
  36. /* see avr1.org for original file and information */
  37. /* */
  38. /* This program is free software; you can redistribute it */
  39. /* and/or modify it under the terms of the GNU General */
  40. /* Public License as published by the Free Software */
  41. /* Foundation; either version 2 of the License, or */
  42. /* (at your option) any later version. */
  43. /* */
  44. /* This program is distributed in the hope that it will */
  45. /* be useful, but WITHOUT ANY WARRANTY; without even the */
  46. /* implied warranty of MERCHANTABILITY or FITNESS FOR A */
  47. /* PARTICULAR PURPOSE. See the GNU General Public */
  48. /* License for more details. */
  49. /* */
  50. /* You should have received a copy of the GNU General */
  51. /* Public License along with this program; if not, write */
  52. /* to the Free Software Foundation, Inc., */
  53. /* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  54. /* */
  55. /* Licence can be viewed at */
  56. /* http://www.fsf.org/licenses/gpl.txt */
  57. /* */
  58. /* Target = Atmel AVR m128,m64,m32,m16,m8,m162,m163,m169, */
  59. /* m8515,m8535. ATmega161 has a very small boot block so */
  60. /* isn't supported. */
  61. /* */
  62. /* Tested with m168 */
  63. /**********************************************************/
  64. /* $Id$ */
  65. /* some includes */
  66. #include <inttypes.h>
  67. #include <avr/io.h>
  68. #include <avr/pgmspace.h>
  69. #include <avr/interrupt.h>
  70. #include <avr/wdt.h>
  71. #include <avr/boot.h>
  72. #ifdef ADABOOT
  73. #define NUM_LED_FLASHES 3
  74. #define ADABOOT_VER 1
  75. #endif
  76. /* 20070707: hacked by David A. Mellis - after this many errors give up and launch application */
  77. #define MAX_ERROR_COUNT 5
  78. /* set the UART baud rate */
  79. /* 20080711: hack by Zach Hoeken */
  80. #define BAUD_RATE 38400
  81. /* SW_MAJOR and MINOR needs to be updated from time to time to avoid warning message from AVR Studio */
  82. /* never allow AVR Studio to do an update !!!! */
  83. #define HW_VER 0x02
  84. #define SW_MAJOR 0x01
  85. #define SW_MINOR 0x10
  86. /* onboard LED is used to indicate, that the bootloader was entered (3x flashing) */
  87. /* if monitor functions are included, LED goes on after monitor was entered */
  88. #define LED_DDR DDRB
  89. #define LED_PORT PORTB
  90. #define LED_PIN PINB
  91. #define LED PINB0
  92. /* define various device id's */
  93. /* manufacturer byte is always the same */
  94. #define SIG1 0x1E // Yep, Atmel is the only manufacturer of AVR micros. Single source :(
  95. #if defined(__AVR_ATmega1284P__)
  96. #define SIG2 0x97
  97. #define SIG3 0x05
  98. #elif defined(__AVR_ATmega644P__)
  99. #define SIG2 0x96
  100. #define SIG3 0x0A
  101. #elif defined(__AVR_ATmega644__)
  102. #define SIG2 0x96
  103. #define SIG3 0x09
  104. #elif defined(__AVR_ATmega324P__)
  105. #define SIG2 0x95
  106. #define SIG3 0x08
  107. #endif
  108. #define PAGE_SIZE 0x080U //128 words
  109. #define PAGE_SIZE_BYTES 0x100U //256 bytes
  110. /* function prototypes */
  111. void putch(char);
  112. char getch(void);
  113. void getNch(uint8_t);
  114. void byte_response(uint8_t);
  115. void nothing_response(void);
  116. char gethex(void);
  117. void puthex(char);
  118. void flash_led(uint8_t);
  119. /* some variables */
  120. union address_union
  121. {
  122. uint16_t word;
  123. uint8_t byte[2];
  124. } address;
  125. union length_union
  126. {
  127. uint16_t word;
  128. uint8_t byte[2];
  129. } length;
  130. struct flags_struct
  131. {
  132. unsigned eeprom : 1;
  133. unsigned rampz : 1;
  134. } flags;
  135. uint8_t buff[256];
  136. uint8_t error_count = 0;
  137. uint8_t sreg;
  138. void (*app_start)(void) = 0x0000;
  139. /* main program starts here */
  140. int main(void)
  141. {
  142. uint8_t ch,ch2;
  143. uint16_t w;
  144. uint16_t i;
  145. asm volatile("nop\n\t");
  146. #ifdef ADABOOT // BBR/LF 10/8/2007 & 9/13/2008
  147. ch = MCUSR;
  148. MCUSR = 0;
  149. WDTCSR |= _BV(WDCE) | _BV(WDE);
  150. WDTCSR = 0;
  151. // Check if the WDT was used to reset, in which case we dont bootload and skip straight to the code. woot.
  152. if (! (ch & _BV(EXTRF))) // if it's a not an external reset...
  153. app_start(); // skip bootloader
  154. #endif
  155. //initialize our serial port.
  156. UBRR0L = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
  157. UBRR0H = (F_CPU/(BAUD_RATE*16L)-1) >> 8;
  158. UCSR0B = (1<<RXEN0) | (1<<TXEN0);
  159. UCSR0C = (1<<UCSZ00) | (1<<UCSZ01);
  160. /* Enable internal pull-up resistor on pin D0 (RX), in order
  161. to supress line noise that prevents the bootloader from
  162. timing out (DAM: 20070509) */
  163. DDRD &= ~_BV(PIND0);
  164. PORTD |= _BV(PIND0);
  165. /* set LED pin as output */
  166. LED_DDR |= _BV(LED);
  167. /* flash onboard LED to signal entering of bootloader */
  168. /* ADABOOT will do two series of flashes. first 4 - signifying ADABOOT */
  169. /* then a pause and another flash series signifying ADABOOT sub-version */
  170. flash_led(NUM_LED_FLASHES);
  171. #ifdef ADABOOT
  172. flash_led(ADABOOT_VER); // BBR 9/13/2008
  173. #endif
  174. /* forever loop */
  175. for (;;)
  176. {
  177. /* get character from UART */
  178. ch = getch();
  179. /* A bunch of if...else if... gives smaller code than switch...case ! */
  180. /* Hello is anyone home ? */
  181. if(ch=='0')
  182. nothing_response();
  183. /* Request programmer ID */
  184. /* Not using PROGMEM string due to boot block in m128 being beyond 64kB boundry */
  185. /* Would need to selectively manipulate RAMPZ, and it's only 9 characters anyway so who cares. */
  186. else if(ch=='1')
  187. {
  188. if (getch() == ' ')
  189. {
  190. putch(0x14);
  191. putch('A');
  192. putch('V');
  193. putch('R');
  194. putch(' ');
  195. putch('I');
  196. putch('S');
  197. putch('P');
  198. putch(0x10);
  199. }
  200. else
  201. {
  202. if (++error_count == MAX_ERROR_COUNT)
  203. app_start();
  204. }
  205. }
  206. /* AVR ISP/STK500 board commands DON'T CARE so default nothing_response */
  207. else if(ch=='@')
  208. {
  209. ch2 = getch();
  210. if (ch2 > 0x85)
  211. getch();
  212. nothing_response();
  213. }
  214. /* AVR ISP/STK500 board requests */
  215. else if(ch=='A')
  216. {
  217. ch2 = getch();
  218. if(ch2 == 0x80)
  219. byte_response(HW_VER); // Hardware version
  220. else if(ch2==0x81)
  221. byte_response(SW_MAJOR); // Software major version
  222. else if(ch2==0x82)
  223. byte_response(SW_MINOR); // Software minor version
  224. else if(ch2==0x98)
  225. byte_response(0x03); // Unknown but seems to be required by avr studio 3.56
  226. else
  227. byte_response(0x00); // Covers various unnecessary responses we don't care about
  228. }
  229. /* Device Parameters DON'T CARE, DEVICE IS FIXED */
  230. else if(ch=='B')
  231. {
  232. getNch(20);
  233. nothing_response();
  234. }
  235. /* Parallel programming stuff DON'T CARE */
  236. else if(ch=='E')
  237. {
  238. getNch(5);
  239. nothing_response();
  240. }
  241. /* Enter programming mode */
  242. else if(ch=='P')
  243. {
  244. nothing_response();
  245. }
  246. /* Leave programming mode */
  247. else if(ch=='Q')
  248. {
  249. nothing_response();
  250. #ifdef ADABOOT
  251. // autoreset via watchdog (sneaky!) BBR/LF 9/13/2008
  252. WDTCSR = _BV(WDE);
  253. while (1); // 16 ms
  254. #endif
  255. }
  256. /* Erase device, don't care as we will erase one page at a time anyway. */
  257. else if(ch=='R')
  258. {
  259. nothing_response();
  260. }
  261. /* Set address, little endian. EEPROM in bytes, FLASH in words */
  262. /* Perhaps extra address bytes may be added in future to support > 128kB FLASH. */
  263. /* This might explain why little endian was used here, big endian used everywhere else. */
  264. else if(ch=='U')
  265. {
  266. address.byte[0] = getch();
  267. address.byte[1] = getch();
  268. nothing_response();
  269. }
  270. /* Universal SPI programming command, disabled. Would be used for fuses and lock bits. */
  271. else if(ch=='V')
  272. {
  273. getNch(4);
  274. byte_response(0x00);
  275. }
  276. /* Write memory, length is big endian and is in bytes */
  277. else if(ch=='d')
  278. {
  279. length.byte[1] = getch();
  280. length.byte[0] = getch();
  281. flags.eeprom = 0;
  282. if (getch() == 'E')
  283. flags.eeprom = 1;
  284. for (i=0; i<PAGE_SIZE; i++)
  285. buff[i] = 0;
  286. for (w = 0; w < length.word; w++)
  287. {
  288. // Store data in buffer, can't keep up with serial data stream whilst programming pages
  289. buff[w] = getch();
  290. }
  291. if (getch() == ' ')
  292. {
  293. if (flags.eeprom)
  294. {
  295. //Write to EEPROM one byte at a time
  296. for(w=0;w<length.word;w++)
  297. {
  298. while(EECR & (1<<EEPE));
  299. EEAR = (uint16_t)(void *)address.word;
  300. EEDR = buff[w];
  301. EECR |= (1<<EEMPE);
  302. EECR |= (1<<EEPE);
  303. address.word++;
  304. }
  305. }
  306. else
  307. {
  308. //address * 2 -> byte location
  309. address.word = address.word << 1;
  310. //Even up an odd number of bytes
  311. if ((length.byte[0] & 0x01))
  312. length.word++;
  313. // HACKME: EEPE used to be EEWE
  314. //Wait for previous EEPROM writes to complete
  315. //while(bit_is_set(EECR,EEPE));
  316. while(EECR & (1<<EEPE));
  317. asm volatile(
  318. "clr r17 \n\t" //page_word_count
  319. "lds r30,address \n\t" //Address of FLASH location (in bytes)
  320. "lds r31,address+1 \n\t"
  321. "ldi r28,lo8(buff) \n\t" //Start of buffer array in RAM
  322. "ldi r29,hi8(buff) \n\t"
  323. "lds r24,length \n\t" //Length of data to be written (in bytes)
  324. "lds r25,length+1 \n\t"
  325. "length_loop: \n\t" //Main loop, repeat for number of words in block
  326. "cpi r17,0x00 \n\t" //If page_word_count=0 then erase page
  327. "brne no_page_erase \n\t"
  328. "wait_spm1: \n\t"
  329. "lds r16,%0 \n\t" //Wait for previous spm to complete
  330. "andi r16,1 \n\t"
  331. "cpi r16,1 \n\t"
  332. "breq wait_spm1 \n\t"
  333. "ldi r16,0x03 \n\t" //Erase page pointed to by Z
  334. "sts %0,r16 \n\t"
  335. "spm \n\t"
  336. "wait_spm2: \n\t"
  337. "lds r16,%0 \n\t" //Wait for previous spm to complete
  338. "andi r16,1 \n\t"
  339. "cpi r16,1 \n\t"
  340. "breq wait_spm2 \n\t"
  341. "ldi r16,0x11 \n\t" //Re-enable RWW section
  342. "sts %0,r16 \n\t"
  343. "spm \n\t"
  344. "no_page_erase: \n\t"
  345. "ld r0,Y+ \n\t" //Write 2 bytes into page buffer
  346. "ld r1,Y+ \n\t"
  347. "wait_spm3: \n\t"
  348. "lds r16,%0 \n\t" //Wait for previous spm to complete
  349. "andi r16,1 \n\t"
  350. "cpi r16,1 \n\t"
  351. "breq wait_spm3 \n\t"
  352. "ldi r16,0x01 \n\t" //Load r0,r1 into FLASH page buffer
  353. "sts %0,r16 \n\t"
  354. "spm \n\t"
  355. "inc r17 \n\t" //page_word_count++
  356. "cpi r17,%1 \n\t"
  357. "brlo same_page \n\t" //Still same page in FLASH
  358. "write_page: \n\t"
  359. "clr r17 \n\t" //New page, write current one first
  360. "wait_spm4: \n\t"
  361. "lds r16,%0 \n\t" //Wait for previous spm to complete
  362. "andi r16,1 \n\t"
  363. "cpi r16,1 \n\t"
  364. "breq wait_spm4 \n\t"
  365. "ldi r16,0x05 \n\t" //Write page pointed to by Z
  366. "sts %0,r16 \n\t"
  367. "spm \n\t"
  368. "wait_spm5: \n\t"
  369. "lds r16,%0 \n\t" //Wait for previous spm to complete
  370. "andi r16,1 \n\t"
  371. "cpi r16,1 \n\t"
  372. "breq wait_spm5 \n\t"
  373. "ldi r16,0x11 \n\t" //Re-enable RWW section
  374. "sts %0,r16 \n\t"
  375. "spm \n\t"
  376. "same_page: \n\t"
  377. "adiw r30,2 \n\t" //Next word in FLASH
  378. "sbiw r24,2 \n\t" //length-2
  379. "breq final_write \n\t" //Finished
  380. "rjmp length_loop \n\t"
  381. "final_write: \n\t"
  382. "cpi r17,0 \n\t"
  383. "breq block_done \n\t"
  384. "adiw r24,2 \n\t" //length+2, fool above check on length after short page write
  385. "rjmp write_page \n\t"
  386. "block_done: \n\t"
  387. "clr __zero_reg__ \n\t" //restore zero register
  388. : "=m" (SPMCSR) : "M" (PAGE_SIZE) : "r0","r16","r17","r24","r25","r28","r29","r30","r31"
  389. );
  390. }
  391. putch(0x14);
  392. putch(0x10);
  393. }
  394. else
  395. {
  396. if (++error_count == MAX_ERROR_COUNT)
  397. app_start();
  398. }
  399. }
  400. /* Read memory block mode, length is big endian. */
  401. else if(ch=='t')
  402. {
  403. length.byte[1] = getch();
  404. length.byte[0] = getch();
  405. if (getch() == 'E')
  406. flags.eeprom = 1;
  407. else
  408. {
  409. flags.eeprom = 0;
  410. address.word = address.word << 1; // address * 2 -> byte location
  411. }
  412. // Command terminator
  413. if (getch() == ' ')
  414. {
  415. putch(0x14);
  416. for (w=0; w<length.word; w++)
  417. {
  418. // Can handle odd and even lengths okay
  419. if (flags.eeprom)
  420. {
  421. // Byte access EEPROM read
  422. while(EECR & (1<<EEPE));
  423. EEAR = (uint16_t)(void *)address.word;
  424. EECR |= (1<<EERE);
  425. putch(EEDR);
  426. address.word++;
  427. }
  428. else
  429. {
  430. if (!flags.rampz)
  431. putch(pgm_read_byte_near(address.word));
  432. address.word++;
  433. }
  434. }
  435. putch(0x10);
  436. }
  437. }
  438. /* Get device signature bytes */
  439. else if(ch=='u')
  440. {
  441. if (getch() == ' ')
  442. {
  443. putch(0x14);
  444. putch(SIG1);
  445. putch(SIG2);
  446. putch(SIG3);
  447. putch(0x10);
  448. }
  449. else
  450. {
  451. if (++error_count == MAX_ERROR_COUNT)
  452. app_start();
  453. }
  454. }
  455. /* Read oscillator calibration byte */
  456. else if(ch=='v')
  457. byte_response(0x00);
  458. else if (++error_count == MAX_ERROR_COUNT)
  459. app_start();
  460. }
  461. /* end of forever loop */
  462. }
  463. char gethex(void)
  464. {
  465. char ah,al;
  466. ah = getch();
  467. putch(ah);
  468. al = getch();
  469. putch(al);
  470. if(ah >= 'a')
  471. ah = ah - 'a' + 0x0a;
  472. else if(ah >= '0')
  473. ah -= '0';
  474. if(al >= 'a')
  475. al = al - 'a' + 0x0a;
  476. else if(al >= '0')
  477. al -= '0';
  478. return (ah << 4) + al;
  479. }
  480. void puthex(char ch)
  481. {
  482. char ah,al;
  483. ah = (ch & 0xf0) >> 4;
  484. if(ah >= 0x0a)
  485. ah = ah - 0x0a + 'a';
  486. else
  487. ah += '0';
  488. al = (ch & 0x0f);
  489. if(al >= 0x0a)
  490. al = al - 0x0a + 'a';
  491. else
  492. al += '0';
  493. putch(ah);
  494. putch(al);
  495. }
  496. void putch(char ch)
  497. {
  498. while (!(UCSR0A & _BV(UDRE0)));
  499. UDR0 = ch;
  500. }
  501. char getch(void)
  502. {
  503. uint32_t count = 0;
  504. #ifdef ADABOOT
  505. LED_PORT &= ~_BV(LED); // toggle LED to show activity - BBR/LF 10/3/2007 & 9/13/2008
  506. #endif
  507. while(!(UCSR0A & _BV(RXC0)))
  508. {
  509. /* 20060803 DojoCorp:: Addon coming from the previous Bootloader*/
  510. /* HACKME:: here is a good place to count times*/
  511. count++;
  512. if (count > MAX_TIME_COUNT)
  513. app_start();
  514. }
  515. #ifdef ADABOOT
  516. LED_PORT |= _BV(LED); // toggle LED to show activity - BBR/LF 10/3/2007 & 9/13/2008
  517. #endif
  518. return UDR0;
  519. }
  520. void getNch(uint8_t count)
  521. {
  522. uint8_t i;
  523. for(i=0;i<count;i++)
  524. {
  525. while(!(UCSR0A & _BV(RXC0)));
  526. UDR0;
  527. }
  528. }
  529. void byte_response(uint8_t val)
  530. {
  531. if (getch() == ' ')
  532. {
  533. putch(0x14);
  534. putch(val);
  535. putch(0x10);
  536. }
  537. else
  538. {
  539. if (++error_count == MAX_ERROR_COUNT)
  540. app_start();
  541. }
  542. }
  543. void nothing_response(void)
  544. {
  545. if (getch() == ' ')
  546. {
  547. putch(0x14);
  548. putch(0x10);
  549. }
  550. else
  551. {
  552. if (++error_count == MAX_ERROR_COUNT)
  553. app_start();
  554. }
  555. }
  556. #ifdef ADABOOT
  557. void flash_led(uint8_t count)
  558. {
  559. /* flash onboard LED count times to signal entering of bootloader */
  560. /* l needs to be volatile or the delay loops below might get */
  561. /* optimized away if compiling with optimizations (DAM). */
  562. volatile uint32_t l;
  563. if (count == 0) {
  564. count = ADABOOT;
  565. }
  566. int8_t i;
  567. for (i = 0; i < count; ++i) {
  568. LED_PORT |= _BV(LED); // LED on
  569. for(l = 0; l < (F_CPU / 1000); ++l); // delay NGvalue was 1000 for both loops - BBR
  570. LED_PORT &= ~_BV(LED); // LED off
  571. for(l = 0; l < (F_CPU / 250); ++l); // delay asymmteric for ADA BOOT BBR
  572. }
  573. for(l = 0; l < (F_CPU / 100); ++l); // pause ADA BOOT BBR
  574. }
  575. #else
  576. void flash_led(uint8_t count)
  577. {
  578. /* flash onboard LED three times to signal entering of bootloader */
  579. /* l needs to be volatile or the delay loops below might get
  580. optimized away if compiling with optimizations (DAM). */
  581. volatile uint32_t l;
  582. if (count == 0) {
  583. count = 3;
  584. }
  585. int8_t i;
  586. for (i = 0; i < count; ++i) {
  587. LED_PORT |= _BV(LED);
  588. for(l = 0; l < (F_CPU / 1000); ++l);
  589. LED_PORT &= ~_BV(LED);
  590. for(l = 0; l < (F_CPU / 1000); ++l);
  591. }
  592. }
  593. #endif
  594. /* end of file ATmegaBOOT.c */