ATmegaBOOT.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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_ATmega644P__)
  96. #define SIG2 0x96
  97. #define SIG3 0x0A
  98. #elif defined(__AVR_ATmega644__)
  99. #define SIG2 0x96
  100. #define SIG3 0x09
  101. #elif defined(__AVR_ATmega324P__)
  102. #define SIG2 0x95
  103. #define SIG3 0x08
  104. #endif
  105. #define PAGE_SIZE 0x080U //128 words
  106. #define PAGE_SIZE_BYTES 0x100U //256 bytes
  107. /* function prototypes */
  108. void putch(char);
  109. char getch(void);
  110. void getNch(uint8_t);
  111. void byte_response(uint8_t);
  112. void nothing_response(void);
  113. char gethex(void);
  114. void puthex(char);
  115. void flash_led(uint8_t);
  116. /* some variables */
  117. union address_union
  118. {
  119. uint16_t word;
  120. uint8_t byte[2];
  121. } address;
  122. union length_union
  123. {
  124. uint16_t word;
  125. uint8_t byte[2];
  126. } length;
  127. struct flags_struct
  128. {
  129. unsigned eeprom : 1;
  130. unsigned rampz : 1;
  131. } flags;
  132. uint8_t buff[256];
  133. uint8_t error_count = 0;
  134. uint8_t sreg;
  135. void (*app_start)(void) = 0x0000;
  136. /* main program starts here */
  137. int main(void)
  138. {
  139. uint8_t ch,ch2;
  140. uint16_t w;
  141. uint16_t i;
  142. asm volatile("nop\n\t");
  143. #ifdef ADABOOT // BBR/LF 10/8/2007 & 9/13/2008
  144. ch = MCUSR;
  145. MCUSR = 0;
  146. WDTCSR |= _BV(WDCE) | _BV(WDE);
  147. WDTCSR = 0;
  148. // Check if the WDT was used to reset, in which case we dont bootload and skip straight to the code. woot.
  149. if (! (ch & _BV(EXTRF))) // if its a not an external reset...
  150. app_start(); // skip bootloader
  151. #endif
  152. //initialize our serial port.
  153. UBRR0L = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
  154. UBRR0H = (F_CPU/(BAUD_RATE*16L)-1) >> 8;
  155. UCSR0B = (1<<RXEN0) | (1<<TXEN0);
  156. UCSR0C = (1<<UCSZ00) | (1<<UCSZ01);
  157. /* Enable internal pull-up resistor on pin D0 (RX), in order
  158. to supress line noise that prevents the bootloader from
  159. timing out (DAM: 20070509) */
  160. DDRD &= ~_BV(PIND0);
  161. PORTD |= _BV(PIND0);
  162. /* set LED pin as output */
  163. LED_DDR |= _BV(LED);
  164. /* flash onboard LED to signal entering of bootloader */
  165. /* ADABOOT will do two series of flashes. first 4 - signifying ADABOOT */
  166. /* then a pause and another flash series signifying ADABOOT sub-version */
  167. flash_led(NUM_LED_FLASHES);
  168. #ifdef ADABOOT
  169. flash_led(ADABOOT_VER); // BBR 9/13/2008
  170. #endif
  171. /* forever loop */
  172. for (;;)
  173. {
  174. /* get character from UART */
  175. ch = getch();
  176. /* A bunch of if...else if... gives smaller code than switch...case ! */
  177. /* Hello is anyone home ? */
  178. if(ch=='0')
  179. nothing_response();
  180. /* Request programmer ID */
  181. /* Not using PROGMEM string due to boot block in m128 being beyond 64kB boundry */
  182. /* Would need to selectively manipulate RAMPZ, and it's only 9 characters anyway so who cares. */
  183. else if(ch=='1')
  184. {
  185. if (getch() == ' ')
  186. {
  187. putch(0x14);
  188. putch('A');
  189. putch('V');
  190. putch('R');
  191. putch(' ');
  192. putch('I');
  193. putch('S');
  194. putch('P');
  195. putch(0x10);
  196. }
  197. else
  198. {
  199. if (++error_count == MAX_ERROR_COUNT)
  200. app_start();
  201. }
  202. }
  203. /* AVR ISP/STK500 board commands DON'T CARE so default nothing_response */
  204. else if(ch=='@')
  205. {
  206. ch2 = getch();
  207. if (ch2 > 0x85)
  208. getch();
  209. nothing_response();
  210. }
  211. /* AVR ISP/STK500 board requests */
  212. else if(ch=='A')
  213. {
  214. ch2 = getch();
  215. if(ch2 == 0x80)
  216. byte_response(HW_VER); // Hardware version
  217. else if(ch2==0x81)
  218. byte_response(SW_MAJOR); // Software major version
  219. else if(ch2==0x82)
  220. byte_response(SW_MINOR); // Software minor version
  221. else if(ch2==0x98)
  222. byte_response(0x03); // Unknown but seems to be required by avr studio 3.56
  223. else
  224. byte_response(0x00); // Covers various unnecessary responses we don't care about
  225. }
  226. /* Device Parameters DON'T CARE, DEVICE IS FIXED */
  227. else if(ch=='B')
  228. {
  229. getNch(20);
  230. nothing_response();
  231. }
  232. /* Parallel programming stuff DON'T CARE */
  233. else if(ch=='E')
  234. {
  235. getNch(5);
  236. nothing_response();
  237. }
  238. /* Enter programming mode */
  239. else if(ch=='P')
  240. {
  241. nothing_response();
  242. }
  243. /* Leave programming mode */
  244. else if(ch=='Q')
  245. {
  246. nothing_response();
  247. #ifdef ADABOOT
  248. // autoreset via watchdog (sneaky!) BBR/LF 9/13/2008
  249. WDTCSR = _BV(WDE);
  250. while (1); // 16 ms
  251. #endif
  252. }
  253. /* Erase device, don't care as we will erase one page at a time anyway. */
  254. else if(ch=='R')
  255. {
  256. nothing_response();
  257. }
  258. /* Set address, little endian. EEPROM in bytes, FLASH in words */
  259. /* Perhaps extra address bytes may be added in future to support > 128kB FLASH. */
  260. /* This might explain why little endian was used here, big endian used everywhere else. */
  261. else if(ch=='U')
  262. {
  263. address.byte[0] = getch();
  264. address.byte[1] = getch();
  265. nothing_response();
  266. }
  267. /* Universal SPI programming command, disabled. Would be used for fuses and lock bits. */
  268. else if(ch=='V')
  269. {
  270. getNch(4);
  271. byte_response(0x00);
  272. }
  273. /* Write memory, length is big endian and is in bytes */
  274. else if(ch=='d')
  275. {
  276. length.byte[1] = getch();
  277. length.byte[0] = getch();
  278. flags.eeprom = 0;
  279. if (getch() == 'E')
  280. flags.eeprom = 1;
  281. for (i=0; i<PAGE_SIZE; i++)
  282. buff[i] = 0;
  283. for (w = 0; w < length.word; w++)
  284. {
  285. // Store data in buffer, can't keep up with serial data stream whilst programming pages
  286. buff[w] = getch();
  287. }
  288. if (getch() == ' ')
  289. {
  290. if (flags.eeprom)
  291. {
  292. //Write to EEPROM one byte at a time
  293. for(w=0;w<length.word;w++)
  294. {
  295. while(EECR & (1<<EEPE));
  296. EEAR = (uint16_t)(void *)address.word;
  297. EEDR = buff[w];
  298. EECR |= (1<<EEMPE);
  299. EECR |= (1<<EEPE);
  300. address.word++;
  301. }
  302. }
  303. else
  304. {
  305. //address * 2 -> byte location
  306. address.word = address.word << 1;
  307. //Even up an odd number of bytes
  308. if ((length.byte[0] & 0x01))
  309. length.word++;
  310. // HACKME: EEPE used to be EEWE
  311. //Wait for previous EEPROM writes to complete
  312. //while(bit_is_set(EECR,EEPE));
  313. while(EECR & (1<<EEPE));
  314. asm volatile(
  315. "clr r17 \n\t" //page_word_count
  316. "lds r30,address \n\t" //Address of FLASH location (in bytes)
  317. "lds r31,address+1 \n\t"
  318. "ldi r28,lo8(buff) \n\t" //Start of buffer array in RAM
  319. "ldi r29,hi8(buff) \n\t"
  320. "lds r24,length \n\t" //Length of data to be written (in bytes)
  321. "lds r25,length+1 \n\t"
  322. "length_loop: \n\t" //Main loop, repeat for number of words in block
  323. "cpi r17,0x00 \n\t" //If page_word_count=0 then erase page
  324. "brne no_page_erase \n\t"
  325. "wait_spm1: \n\t"
  326. "lds r16,%0 \n\t" //Wait for previous spm to complete
  327. "andi r16,1 \n\t"
  328. "cpi r16,1 \n\t"
  329. "breq wait_spm1 \n\t"
  330. "ldi r16,0x03 \n\t" //Erase page pointed to by Z
  331. "sts %0,r16 \n\t"
  332. "spm \n\t"
  333. "wait_spm2: \n\t"
  334. "lds r16,%0 \n\t" //Wait for previous spm to complete
  335. "andi r16,1 \n\t"
  336. "cpi r16,1 \n\t"
  337. "breq wait_spm2 \n\t"
  338. "ldi r16,0x11 \n\t" //Re-enable RWW section
  339. "sts %0,r16 \n\t"
  340. "spm \n\t"
  341. "no_page_erase: \n\t"
  342. "ld r0,Y+ \n\t" //Write 2 bytes into page buffer
  343. "ld r1,Y+ \n\t"
  344. "wait_spm3: \n\t"
  345. "lds r16,%0 \n\t" //Wait for previous spm to complete
  346. "andi r16,1 \n\t"
  347. "cpi r16,1 \n\t"
  348. "breq wait_spm3 \n\t"
  349. "ldi r16,0x01 \n\t" //Load r0,r1 into FLASH page buffer
  350. "sts %0,r16 \n\t"
  351. "spm \n\t"
  352. "inc r17 \n\t" //page_word_count++
  353. "cpi r17,%1 \n\t"
  354. "brlo same_page \n\t" //Still same page in FLASH
  355. "write_page: \n\t"
  356. "clr r17 \n\t" //New page, write current one first
  357. "wait_spm4: \n\t"
  358. "lds r16,%0 \n\t" //Wait for previous spm to complete
  359. "andi r16,1 \n\t"
  360. "cpi r16,1 \n\t"
  361. "breq wait_spm4 \n\t"
  362. "ldi r16,0x05 \n\t" //Write page pointed to by Z
  363. "sts %0,r16 \n\t"
  364. "spm \n\t"
  365. "wait_spm5: \n\t"
  366. "lds r16,%0 \n\t" //Wait for previous spm to complete
  367. "andi r16,1 \n\t"
  368. "cpi r16,1 \n\t"
  369. "breq wait_spm5 \n\t"
  370. "ldi r16,0x11 \n\t" //Re-enable RWW section
  371. "sts %0,r16 \n\t"
  372. "spm \n\t"
  373. "same_page: \n\t"
  374. "adiw r30,2 \n\t" //Next word in FLASH
  375. "sbiw r24,2 \n\t" //length-2
  376. "breq final_write \n\t" //Finished
  377. "rjmp length_loop \n\t"
  378. "final_write: \n\t"
  379. "cpi r17,0 \n\t"
  380. "breq block_done \n\t"
  381. "adiw r24,2 \n\t" //length+2, fool above check on length after short page write
  382. "rjmp write_page \n\t"
  383. "block_done: \n\t"
  384. "clr __zero_reg__ \n\t" //restore zero register
  385. : "=m" (SPMCSR) : "M" (PAGE_SIZE) : "r0","r16","r17","r24","r25","r28","r29","r30","r31"
  386. );
  387. }
  388. putch(0x14);
  389. putch(0x10);
  390. }
  391. else
  392. {
  393. if (++error_count == MAX_ERROR_COUNT)
  394. app_start();
  395. }
  396. }
  397. /* Read memory block mode, length is big endian. */
  398. else if(ch=='t')
  399. {
  400. length.byte[1] = getch();
  401. length.byte[0] = getch();
  402. if (getch() == 'E')
  403. flags.eeprom = 1;
  404. else
  405. {
  406. flags.eeprom = 0;
  407. address.word = address.word << 1; // address * 2 -> byte location
  408. }
  409. // Command terminator
  410. if (getch() == ' ')
  411. {
  412. putch(0x14);
  413. for (w=0; w<length.word; w++)
  414. {
  415. // Can handle odd and even lengths okay
  416. if (flags.eeprom)
  417. {
  418. // Byte access EEPROM read
  419. while(EECR & (1<<EEPE));
  420. EEAR = (uint16_t)(void *)address.word;
  421. EECR |= (1<<EERE);
  422. putch(EEDR);
  423. address.word++;
  424. }
  425. else
  426. {
  427. if (!flags.rampz)
  428. putch(pgm_read_byte_near(address.word));
  429. address.word++;
  430. }
  431. }
  432. putch(0x10);
  433. }
  434. }
  435. /* Get device signature bytes */
  436. else if(ch=='u')
  437. {
  438. if (getch() == ' ')
  439. {
  440. putch(0x14);
  441. putch(SIG1);
  442. putch(SIG2);
  443. putch(SIG3);
  444. putch(0x10);
  445. }
  446. else
  447. {
  448. if (++error_count == MAX_ERROR_COUNT)
  449. app_start();
  450. }
  451. }
  452. /* Read oscillator calibration byte */
  453. else if(ch=='v')
  454. byte_response(0x00);
  455. else if (++error_count == MAX_ERROR_COUNT)
  456. app_start();
  457. }
  458. /* end of forever loop */
  459. }
  460. char gethex(void)
  461. {
  462. char ah,al;
  463. ah = getch();
  464. putch(ah);
  465. al = getch();
  466. putch(al);
  467. if(ah >= 'a')
  468. ah = ah - 'a' + 0x0a;
  469. else if(ah >= '0')
  470. ah -= '0';
  471. if(al >= 'a')
  472. al = al - 'a' + 0x0a;
  473. else if(al >= '0')
  474. al -= '0';
  475. return (ah << 4) + al;
  476. }
  477. void puthex(char ch)
  478. {
  479. char ah,al;
  480. ah = (ch & 0xf0) >> 4;
  481. if(ah >= 0x0a)
  482. ah = ah - 0x0a + 'a';
  483. else
  484. ah += '0';
  485. al = (ch & 0x0f);
  486. if(al >= 0x0a)
  487. al = al - 0x0a + 'a';
  488. else
  489. al += '0';
  490. putch(ah);
  491. putch(al);
  492. }
  493. void putch(char ch)
  494. {
  495. while (!(UCSR0A & _BV(UDRE0)));
  496. UDR0 = ch;
  497. }
  498. char getch(void)
  499. {
  500. uint32_t count = 0;
  501. #ifdef ADABOOT
  502. LED_PORT &= ~_BV(LED); // toggle LED to show activity - BBR/LF 10/3/2007 & 9/13/2008
  503. #endif
  504. while(!(UCSR0A & _BV(RXC0)))
  505. {
  506. /* 20060803 DojoCorp:: Addon coming from the previous Bootloader*/
  507. /* HACKME:: here is a good place to count times*/
  508. count++;
  509. if (count > MAX_TIME_COUNT)
  510. app_start();
  511. }
  512. #ifdef ADABOOT
  513. LED_PORT |= _BV(LED); // toggle LED to show activity - BBR/LF 10/3/2007 & 9/13/2008
  514. #endif
  515. return UDR0;
  516. }
  517. void getNch(uint8_t count)
  518. {
  519. uint8_t i;
  520. for(i=0;i<count;i++)
  521. {
  522. while(!(UCSR0A & _BV(RXC0)));
  523. UDR0;
  524. }
  525. }
  526. void byte_response(uint8_t val)
  527. {
  528. if (getch() == ' ')
  529. {
  530. putch(0x14);
  531. putch(val);
  532. putch(0x10);
  533. }
  534. else
  535. {
  536. if (++error_count == MAX_ERROR_COUNT)
  537. app_start();
  538. }
  539. }
  540. void nothing_response(void)
  541. {
  542. if (getch() == ' ')
  543. {
  544. putch(0x14);
  545. putch(0x10);
  546. }
  547. else
  548. {
  549. if (++error_count == MAX_ERROR_COUNT)
  550. app_start();
  551. }
  552. }
  553. #ifdef ADABOOT
  554. void flash_led(uint8_t count)
  555. {
  556. /* flash onboard LED count times to signal entering of bootloader */
  557. /* l needs to be volatile or the delay loops below might get */
  558. /* optimized away if compiling with optimizations (DAM). */
  559. volatile uint32_t l;
  560. if (count == 0) {
  561. count = ADABOOT;
  562. }
  563. int8_t i;
  564. for (i = 0; i < count; ++i) {
  565. LED_PORT |= _BV(LED); // LED on
  566. for(l = 0; l < (F_CPU / 1000); ++l); // delay NGvalue was 1000 for both loops - BBR
  567. LED_PORT &= ~_BV(LED); // LED off
  568. for(l = 0; l < (F_CPU / 250); ++l); // delay asymmteric for ADA BOOT BBR
  569. }
  570. for(l = 0; l < (F_CPU / 100); ++l); // pause ADA BOOT BBR
  571. }
  572. #else
  573. void flash_led(uint8_t count)
  574. {
  575. /* flash onboard LED three times to signal entering of bootloader */
  576. /* l needs to be volatile or the delay loops below might get
  577. optimized away if compiling with optimizations (DAM). */
  578. volatile uint32_t l;
  579. if (count == 0) {
  580. count = 3;
  581. }
  582. int8_t i;
  583. for (i = 0; i < count; ++i) {
  584. LED_PORT |= _BV(LED);
  585. for(l = 0; l < (F_CPU / 1000); ++l);
  586. LED_PORT &= ~_BV(LED);
  587. for(l = 0; l < (F_CPU / 1000); ++l);
  588. }
  589. }
  590. #endif
  591. /* end of file ATmegaBOOT.c */