ATmegaBOOT.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  1. /**********************************************************/
  2. /* Serial Bootloader for Atmel megaAVR Controllers */
  3. /* */
  4. /* tested with ATmega8, ATmega128 and ATmega168 */
  5. /* should work with other mega's, see code for details */
  6. /* */
  7. /* ATmegaBOOT.c */
  8. /* */
  9. /* 20070626: hacked for Arduino Diecimila (which auto- */
  10. /* resets when a USB connection is made to it) */
  11. /* by D. Mellis */
  12. /* 20060802: hacked for Arduino by D. Cuartielles */
  13. /* based on a previous hack by D. Mellis */
  14. /* and D. Cuartielles */
  15. /* */
  16. /* Monitor and debug functions were added to the original */
  17. /* code by Dr. Erik Lins, chip45.com. (See below) */
  18. /* */
  19. /* Thanks to Karl Pitrich for fixing a bootloader pin */
  20. /* problem and more informative LED blinking! */
  21. /* */
  22. /* For the latest version see: */
  23. /* http://www.chip45.com/ */
  24. /* */
  25. /* ------------------------------------------------------ */
  26. /* */
  27. /* based on stk500boot.c */
  28. /* Copyright (c) 2003, Jason P. Kyle */
  29. /* All rights reserved. */
  30. /* see avr1.org for original file and information */
  31. /* */
  32. /* This program is free software; you can redistribute it */
  33. /* and/or modify it under the terms of the GNU General */
  34. /* Public License as published by the Free Software */
  35. /* Foundation; either version 2 of the License, or */
  36. /* (at your option) any later version. */
  37. /* */
  38. /* This program is distributed in the hope that it will */
  39. /* be useful, but WITHOUT ANY WARRANTY; without even the */
  40. /* implied warranty of MERCHANTABILITY or FITNESS FOR A */
  41. /* PARTICULAR PURPOSE. See the GNU General Public */
  42. /* License for more details. */
  43. /* */
  44. /* You should have received a copy of the GNU General */
  45. /* Public License along with this program; if not, write */
  46. /* to the Free Software Foundation, Inc., */
  47. /* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  48. /* */
  49. /* Licence can be viewed at */
  50. /* http://www.fsf.org/licenses/gpl.txt */
  51. /* */
  52. /* Target = Atmel AVR m128,m64,m32,m16,m8,m162,m163,m169, */
  53. /* m8515,m8535. ATmega161 has a very small boot block so */
  54. /* isn't supported. */
  55. /* */
  56. /* Tested with m168 */
  57. /**********************************************************/
  58. /* some includes */
  59. #include <inttypes.h>
  60. #include <avr/io.h>
  61. #include <avr/pgmspace.h>
  62. #include <avr/interrupt.h>
  63. #include <avr/wdt.h>
  64. /* the current avr-libc eeprom functions do not support the ATmega168 */
  65. /* own eeprom write/read functions are used instead */
  66. #ifndef __AVR_ATmega168__
  67. #include <avr/eeprom.h>
  68. #endif
  69. /* Use the F_CPU defined in Makefile */
  70. /* 20060803: hacked by DojoCorp */
  71. /* 20070626: hacked by David A. Mellis to decrease waiting time for auto-reset */
  72. /* set the waiting time for the bootloader */
  73. /* get this from the Makefile instead */
  74. /* #define MAX_TIME_COUNT (F_CPU>>4) */
  75. /* 20070707: hacked by David A. Mellis - after this many errors give up and launch application */
  76. #define MAX_ERROR_COUNT 5
  77. /* set the UART baud rate */
  78. /* 20060803: hacked by DojoCorp */
  79. //#define BAUD_RATE 115200
  80. #define BAUD_RATE 19200
  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. /* Adjust to suit whatever pin your hardware uses to enter the bootloader */
  87. /* ATmega128 has two UARTS so two pins are used to enter bootloader and select UART */
  88. /* BL0... means UART0, BL1... means UART1 */
  89. #ifdef __AVR_ATmega128__
  90. #define BL_DDR DDRF
  91. #define BL_PORT PORTF
  92. #define BL_PIN PINF
  93. #define BL0 PINF7
  94. #define BL1 PINF6
  95. #else
  96. /* other ATmegas have only one UART, so only one pin is defined to enter bootloader */
  97. #define BL_DDR DDRD
  98. #define BL_PORT PORTD
  99. #define BL_PIN PIND
  100. #define BL PIND6
  101. #endif
  102. /* onboard LED is used to indicate, that the bootloader was entered (3x flashing) */
  103. /* if monitor functions are included, LED goes on after monitor was entered */
  104. #ifdef __AVR_ATmega128__
  105. /* Onboard LED is connected to pin PB7 (e.g. Crumb128, PROBOmega128, Savvy128) */
  106. #define LED_DDR DDRB
  107. #define LED_PORT PORTB
  108. #define LED_PIN PINB
  109. #define LED PINB7
  110. #else
  111. /* Onboard LED is connected to pin PB2 (e.g. Crumb8, Crumb168) */
  112. #define LED_DDR DDRB
  113. #define LED_PORT PORTB
  114. #define LED_PIN PINB
  115. /* 20060803: hacked by DojoCorp, LED pin is B5 in Arduino */
  116. /* #define LED PINB2 */
  117. #define LED PINB5
  118. #endif
  119. /* monitor functions will only be compiled when using ATmega128, due to bootblock size constraints */
  120. #ifdef __AVR_ATmega128__
  121. #define MONITOR
  122. #endif
  123. /* define various device id's */
  124. /* manufacturer byte is always the same */
  125. #define SIG1 0x1E // Yep, Atmel is the only manufacturer of AVR micros. Single source :(
  126. #if defined __AVR_ATmega128__
  127. #define SIG2 0x97
  128. #define SIG3 0x02
  129. #define PAGE_SIZE 0x80U //128 words
  130. #elif defined __AVR_ATmega64__
  131. #define SIG2 0x96
  132. #define SIG3 0x02
  133. #define PAGE_SIZE 0x80U //128 words
  134. #elif defined __AVR_ATmega32__
  135. #define SIG2 0x95
  136. #define SIG3 0x02
  137. #define PAGE_SIZE 0x40U //64 words
  138. #elif defined __AVR_ATmega16__
  139. #define SIG2 0x94
  140. #define SIG3 0x03
  141. #define PAGE_SIZE 0x40U //64 words
  142. #elif defined __AVR_ATmega8__
  143. #define SIG2 0x93
  144. #define SIG3 0x07
  145. #define PAGE_SIZE 0x20U //32 words
  146. #elif defined __AVR_ATmega88__
  147. #define SIG2 0x93
  148. #define SIG3 0x0a
  149. #define PAGE_SIZE 0x20U //32 words
  150. #elif defined __AVR_ATmega168__
  151. #define SIG2 0x94
  152. #define SIG3 0x06
  153. #define PAGE_SIZE 0x40U //64 words
  154. #elif defined __AVR_ATmega162__
  155. #define SIG2 0x94
  156. #define SIG3 0x04
  157. #define PAGE_SIZE 0x40U //64 words
  158. #elif defined __AVR_ATmega163__
  159. #define SIG2 0x94
  160. #define SIG3 0x02
  161. #define PAGE_SIZE 0x40U //64 words
  162. #elif defined __AVR_ATmega169__
  163. #define SIG2 0x94
  164. #define SIG3 0x05
  165. #define PAGE_SIZE 0x40U //64 words
  166. #elif defined __AVR_ATmega8515__
  167. #define SIG2 0x93
  168. #define SIG3 0x06
  169. #define PAGE_SIZE 0x20U //32 words
  170. #elif defined __AVR_ATmega8535__
  171. #define SIG2 0x93
  172. #define SIG3 0x08
  173. #define PAGE_SIZE 0x20U //32 words
  174. #endif
  175. /* function prototypes */
  176. void putch(char);
  177. char getch(void);
  178. void getNch(uint8_t);
  179. void byte_response(uint8_t);
  180. void nothing_response(void);
  181. char gethex(void);
  182. void puthex(char);
  183. void flash_led(uint8_t);
  184. /* some variables */
  185. union address_union {
  186. uint16_t word;
  187. uint8_t byte[2];
  188. } address;
  189. union length_union {
  190. uint16_t word;
  191. uint8_t byte[2];
  192. } length;
  193. struct flags_struct {
  194. unsigned eeprom : 1;
  195. unsigned rampz : 1;
  196. } flags;
  197. uint8_t buff[256];
  198. uint8_t address_high;
  199. uint8_t pagesz=0x80;
  200. uint8_t i;
  201. uint8_t bootuart = 0;
  202. uint8_t error_count = 0;
  203. void (*app_start)(void) = 0x0000;
  204. /* main program starts here */
  205. int main(void)
  206. {
  207. uint8_t ch,ch2;
  208. uint16_t w;
  209. asm volatile("nop\n\t");
  210. /* set pin direction for bootloader pin and enable pullup */
  211. /* for ATmega128, two pins need to be initialized */
  212. #ifdef __AVR_ATmega128__
  213. BL_DDR &= ~_BV(BL0);
  214. BL_DDR &= ~_BV(BL1);
  215. BL_PORT |= _BV(BL0);
  216. BL_PORT |= _BV(BL1);
  217. #else
  218. /* We run the bootloader regardless of the state of this pin. Thus, don't
  219. put it in a different state than the other pins. --DAM, 070709
  220. BL_DDR &= ~_BV(BL);
  221. BL_PORT |= _BV(BL);
  222. */
  223. #endif
  224. #ifdef __AVR_ATmega128__
  225. /* check which UART should be used for booting */
  226. if(bit_is_clear(BL_PIN, BL0)) {
  227. bootuart = 1;
  228. }
  229. else if(bit_is_clear(BL_PIN, BL1)) {
  230. bootuart = 2;
  231. }
  232. #endif
  233. /* check if flash is programmed already, if not start bootloader anyway */
  234. if(pgm_read_byte_near(0x0000) != 0xFF) {
  235. #ifdef __AVR_ATmega128__
  236. /* no UART was selected, start application */
  237. if(!bootuart) {
  238. app_start();
  239. }
  240. #else
  241. /* check if bootloader pin is set low */
  242. /* we don't start this part neither for the m8, nor m168 */
  243. //if(bit_is_set(BL_PIN, BL)) {
  244. // app_start();
  245. // }
  246. #endif
  247. }
  248. #ifdef __AVR_ATmega128__
  249. /* no bootuart was selected, default to uart 0 */
  250. if(!bootuart) {
  251. bootuart = 1;
  252. }
  253. #endif
  254. /* initialize UART(s) depending on CPU defined */
  255. #ifdef __AVR_ATmega128__
  256. if(bootuart == 1) {
  257. UBRR0L = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
  258. UBRR0H = (F_CPU/(BAUD_RATE*16L)-1) >> 8;
  259. UCSR0A = 0x00;
  260. UCSR0C = 0x06;
  261. UCSR0B = _BV(TXEN0)|_BV(RXEN0);
  262. }
  263. if(bootuart == 2) {
  264. UBRR1L = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
  265. UBRR1H = (F_CPU/(BAUD_RATE*16L)-1) >> 8;
  266. UCSR1A = 0x00;
  267. UCSR1C = 0x06;
  268. UCSR1B = _BV(TXEN1)|_BV(RXEN1);
  269. }
  270. #elif defined __AVR_ATmega163__
  271. UBRR = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
  272. UBRRHI = (F_CPU/(BAUD_RATE*16L)-1) >> 8;
  273. UCSRA = 0x00;
  274. UCSRB = _BV(TXEN)|_BV(RXEN);
  275. #elif defined __AVR_ATmega168__
  276. UBRR0L = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
  277. UBRR0H = (F_CPU/(BAUD_RATE*16L)-1) >> 8;
  278. UCSR0B = (1<<RXEN0) | (1<<TXEN0);
  279. UCSR0C = (1<<UCSZ00) | (1<<UCSZ01);
  280. /* Enable internal pull-up resistor on pin D0 (RX), in order
  281. to supress line noise that prevents the bootloader from
  282. timing out (DAM: 20070509) */
  283. DDRD &= ~_BV(PIND0);
  284. PORTD |= _BV(PIND0);
  285. #elif defined __AVR_ATmega8__
  286. /* m8 */
  287. UBRRH = (((F_CPU/BAUD_RATE)/16)-1)>>8; // set baud rate
  288. UBRRL = (((F_CPU/BAUD_RATE)/16)-1);
  289. UCSRB = (1<<RXEN)|(1<<TXEN); // enable Rx & Tx
  290. UCSRC = (1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0); // config USART; 8N1
  291. #else
  292. /* m16,m32,m169,m8515,m8535 */
  293. UBRRL = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
  294. UBRRH = (F_CPU/(BAUD_RATE*16L)-1) >> 8;
  295. UCSRA = 0x00;
  296. UCSRC = 0x06;
  297. UCSRB = _BV(TXEN)|_BV(RXEN);
  298. #endif
  299. /* set LED pin as output */
  300. LED_DDR |= _BV(LED);
  301. /* flash onboard LED to signal entering of bootloader */
  302. #ifdef __AVR_ATmega128__
  303. // 4x for UART0, 5x for UART1
  304. flash_led(NUM_LED_FLASHES + bootuart);
  305. #else
  306. flash_led(NUM_LED_FLASHES);
  307. #endif
  308. /* 20050803: by DojoCorp, this is one of the parts provoking the
  309. system to stop listening, cancelled from the original */
  310. //putch('\0');
  311. /* forever loop */
  312. for (;;) {
  313. /* get character from UART */
  314. ch = getch();
  315. /* A bunch of if...else if... gives smaller code than switch...case ! */
  316. /* Hello is anyone home ? */
  317. if(ch=='0') {
  318. nothing_response();
  319. }
  320. /* Request programmer ID */
  321. /* Not using PROGMEM string due to boot block in m128 being beyond 64kB boundry */
  322. /* Would need to selectively manipulate RAMPZ, and it's only 9 characters anyway so who cares. */
  323. else if(ch=='1') {
  324. if (getch() == ' ') {
  325. putch(0x14);
  326. putch('A');
  327. putch('V');
  328. putch('R');
  329. putch(' ');
  330. putch('I');
  331. putch('S');
  332. putch('P');
  333. putch(0x10);
  334. } else {
  335. if (++error_count == MAX_ERROR_COUNT)
  336. app_start();
  337. }
  338. }
  339. /* AVR ISP/STK500 board commands DON'T CARE so default nothing_response */
  340. else if(ch=='@') {
  341. ch2 = getch();
  342. if (ch2>0x85) getch();
  343. nothing_response();
  344. }
  345. /* AVR ISP/STK500 board requests */
  346. else if(ch=='A') {
  347. ch2 = getch();
  348. if(ch2==0x80) byte_response(HW_VER); // Hardware version
  349. else if(ch2==0x81) byte_response(SW_MAJOR); // Software major version
  350. else if(ch2==0x82) byte_response(SW_MINOR); // Software minor version
  351. else if(ch2==0x98) byte_response(0x03); // Unknown but seems to be required by avr studio 3.56
  352. else byte_response(0x00); // Covers various unnecessary responses we don't care about
  353. }
  354. /* Device Parameters DON'T CARE, DEVICE IS FIXED */
  355. else if(ch=='B') {
  356. getNch(20);
  357. nothing_response();
  358. }
  359. /* Parallel programming stuff DON'T CARE */
  360. else if(ch=='E') {
  361. getNch(5);
  362. nothing_response();
  363. }
  364. /* Enter programming mode */
  365. else if(ch=='P') {
  366. nothing_response();
  367. }
  368. /* Leave programming mode */
  369. else if(ch=='Q') {
  370. nothing_response();
  371. }
  372. /* Erase device, don't care as we will erase one page at a time anyway. */
  373. else if(ch=='R') {
  374. nothing_response();
  375. }
  376. /* Set address, little endian. EEPROM in bytes, FLASH in words */
  377. /* Perhaps extra address bytes may be added in future to support > 128kB FLASH. */
  378. /* This might explain why little endian was used here, big endian used everywhere else. */
  379. else if(ch=='U') {
  380. address.byte[0] = getch();
  381. address.byte[1] = getch();
  382. nothing_response();
  383. }
  384. /* Universal SPI programming command, disabled. Would be used for fuses and lock bits. */
  385. else if(ch=='V') {
  386. getNch(4);
  387. byte_response(0x00);
  388. }
  389. /* Write memory, length is big endian and is in bytes */
  390. else if(ch=='d') {
  391. length.byte[1] = getch();
  392. length.byte[0] = getch();
  393. flags.eeprom = 0;
  394. if (getch() == 'E') flags.eeprom = 1;
  395. for (w=0;w<length.word;w++) {
  396. buff[w] = getch(); // Store data in buffer, can't keep up with serial data stream whilst programming pages
  397. }
  398. if (getch() == ' ') {
  399. if (flags.eeprom) { //Write to EEPROM one byte at a time
  400. for(w=0;w<length.word;w++) {
  401. #ifdef __AVR_ATmega168__
  402. while(EECR & (1<<EEPE));
  403. EEAR = (uint16_t)(void *)address.word;
  404. EEDR = buff[w];
  405. EECR |= (1<<EEMPE);
  406. EECR |= (1<<EEPE);
  407. #else
  408. eeprom_write_byte((void *)address.word,buff[w]);
  409. #endif
  410. address.word++;
  411. }
  412. }
  413. else { //Write to FLASH one page at a time
  414. if (address.byte[1]>127) address_high = 0x01; //Only possible with m128, m256 will need 3rd address byte. FIXME
  415. else address_high = 0x00;
  416. #ifdef __AVR_ATmega128__
  417. RAMPZ = address_high;
  418. #endif
  419. address.word = address.word << 1; //address * 2 -> byte location
  420. /* if ((length.byte[0] & 0x01) == 0x01) length.word++; //Even up an odd number of bytes */
  421. if ((length.byte[0] & 0x01)) length.word++; //Even up an odd number of bytes
  422. cli(); //Disable interrupts, just to be sure
  423. // HACKME: EEPE used to be EEWE
  424. while(bit_is_set(EECR,EEPE)); //Wait for previous EEPROM writes to complete
  425. asm volatile(
  426. "clr r17 \n\t" //page_word_count
  427. "lds r30,address \n\t" //Address of FLASH location (in bytes)
  428. "lds r31,address+1 \n\t"
  429. "ldi r28,lo8(buff) \n\t" //Start of buffer array in RAM
  430. "ldi r29,hi8(buff) \n\t"
  431. "lds r24,length \n\t" //Length of data to be written (in bytes)
  432. "lds r25,length+1 \n\t"
  433. "length_loop: \n\t" //Main loop, repeat for number of words in block
  434. "cpi r17,0x00 \n\t" //If page_word_count=0 then erase page
  435. "brne no_page_erase \n\t"
  436. "wait_spm1: \n\t"
  437. "lds r16,%0 \n\t" //Wait for previous spm to complete
  438. "andi r16,1 \n\t"
  439. "cpi r16,1 \n\t"
  440. "breq wait_spm1 \n\t"
  441. "ldi r16,0x03 \n\t" //Erase page pointed to by Z
  442. "sts %0,r16 \n\t"
  443. "spm \n\t"
  444. #ifdef __AVR_ATmega163__
  445. ".word 0xFFFF \n\t"
  446. "nop \n\t"
  447. #endif
  448. "wait_spm2: \n\t"
  449. "lds r16,%0 \n\t" //Wait for previous spm to complete
  450. "andi r16,1 \n\t"
  451. "cpi r16,1 \n\t"
  452. "breq wait_spm2 \n\t"
  453. "ldi r16,0x11 \n\t" //Re-enable RWW section
  454. "sts %0,r16 \n\t"
  455. "spm \n\t"
  456. #ifdef __AVR_ATmega163__
  457. ".word 0xFFFF \n\t"
  458. "nop \n\t"
  459. #endif
  460. "no_page_erase: \n\t"
  461. "ld r0,Y+ \n\t" //Write 2 bytes into page buffer
  462. "ld r1,Y+ \n\t"
  463. "wait_spm3: \n\t"
  464. "lds r16,%0 \n\t" //Wait for previous spm to complete
  465. "andi r16,1 \n\t"
  466. "cpi r16,1 \n\t"
  467. "breq wait_spm3 \n\t"
  468. "ldi r16,0x01 \n\t" //Load r0,r1 into FLASH page buffer
  469. "sts %0,r16 \n\t"
  470. "spm \n\t"
  471. "inc r17 \n\t" //page_word_count++
  472. "cpi r17,%1 \n\t"
  473. "brlo same_page \n\t" //Still same page in FLASH
  474. "write_page: \n\t"
  475. "clr r17 \n\t" //New page, write current one first
  476. "wait_spm4: \n\t"
  477. "lds r16,%0 \n\t" //Wait for previous spm to complete
  478. "andi r16,1 \n\t"
  479. "cpi r16,1 \n\t"
  480. "breq wait_spm4 \n\t"
  481. #ifdef __AVR_ATmega163__
  482. "andi r30,0x80 \n\t" // m163 requires Z6:Z1 to be zero during page write
  483. #endif
  484. "ldi r16,0x05 \n\t" //Write page pointed to by Z
  485. "sts %0,r16 \n\t"
  486. "spm \n\t"
  487. #ifdef __AVR_ATmega163__
  488. ".word 0xFFFF \n\t"
  489. "nop \n\t"
  490. "ori r30,0x7E \n\t" // recover Z6:Z1 state after page write (had to be zero during write)
  491. #endif
  492. "wait_spm5: \n\t"
  493. "lds r16,%0 \n\t" //Wait for previous spm to complete
  494. "andi r16,1 \n\t"
  495. "cpi r16,1 \n\t"
  496. "breq wait_spm5 \n\t"
  497. "ldi r16,0x11 \n\t" //Re-enable RWW section
  498. "sts %0,r16 \n\t"
  499. "spm \n\t"
  500. #ifdef __AVR_ATmega163__
  501. ".word 0xFFFF \n\t"
  502. "nop \n\t"
  503. #endif
  504. "same_page: \n\t"
  505. "adiw r30,2 \n\t" //Next word in FLASH
  506. "sbiw r24,2 \n\t" //length-2
  507. "breq final_write \n\t" //Finished
  508. "rjmp length_loop \n\t"
  509. "final_write: \n\t"
  510. "cpi r17,0 \n\t"
  511. "breq block_done \n\t"
  512. "adiw r24,2 \n\t" //length+2, fool above check on length after short page write
  513. "rjmp write_page \n\t"
  514. "block_done: \n\t"
  515. "clr __zero_reg__ \n\t" //restore zero register
  516. #if defined __AVR_ATmega168__
  517. : "=m" (SPMCSR) : "M" (PAGE_SIZE) : "r0","r16","r17","r24","r25","r28","r29","r30","r31"
  518. #else
  519. : "=m" (SPMCR) : "M" (PAGE_SIZE) : "r0","r16","r17","r24","r25","r28","r29","r30","r31"
  520. #endif
  521. );
  522. /* Should really add a wait for RWW section to be enabled, don't actually need it since we never */
  523. /* exit the bootloader without a power cycle anyhow */
  524. }
  525. putch(0x14);
  526. putch(0x10);
  527. } else {
  528. if (++error_count == MAX_ERROR_COUNT)
  529. app_start();
  530. }
  531. }
  532. /* Read memory block mode, length is big endian. */
  533. else if(ch=='t') {
  534. length.byte[1] = getch();
  535. length.byte[0] = getch();
  536. #if defined __AVR_ATmega128__
  537. if (address.word>0x7FFF) flags.rampz = 1; // No go with m256, FIXME
  538. else flags.rampz = 0;
  539. #endif
  540. if (getch() == 'E') flags.eeprom = 1;
  541. else {
  542. flags.eeprom = 0;
  543. address.word = address.word << 1; // address * 2 -> byte location
  544. }
  545. if (getch() == ' ') { // Command terminator
  546. putch(0x14);
  547. for (w=0;w < length.word;w++) { // Can handle odd and even lengths okay
  548. if (flags.eeprom) { // Byte access EEPROM read
  549. #ifdef __AVR_ATmega168__
  550. while(EECR & (1<<EEPE));
  551. EEAR = (uint16_t)(void *)address.word;
  552. EECR |= (1<<EERE);
  553. putch(EEDR);
  554. #else
  555. putch(eeprom_read_byte((void *)address.word));
  556. #endif
  557. address.word++;
  558. }
  559. else {
  560. if (!flags.rampz) putch(pgm_read_byte_near(address.word));
  561. #if defined __AVR_ATmega128__
  562. else putch(pgm_read_byte_far(address.word + 0x10000));
  563. // Hmmmm, yuck FIXME when m256 arrvies
  564. #endif
  565. address.word++;
  566. }
  567. }
  568. putch(0x10);
  569. }
  570. }
  571. /* Get device signature bytes */
  572. else if(ch=='u') {
  573. if (getch() == ' ') {
  574. putch(0x14);
  575. putch(SIG1);
  576. putch(SIG2);
  577. putch(SIG3);
  578. putch(0x10);
  579. } else {
  580. if (++error_count == MAX_ERROR_COUNT)
  581. app_start();
  582. }
  583. }
  584. /* Read oscillator calibration byte */
  585. else if(ch=='v') {
  586. byte_response(0x00);
  587. }
  588. #ifdef MONITOR
  589. /* here come the extended monitor commands by Erik Lins */
  590. /* check for three times exclamation mark pressed */
  591. else if(ch=='!') {
  592. ch = getch();
  593. if(ch=='!') {
  594. ch = getch();
  595. if(ch=='!') {
  596. #ifdef __AVR_ATmega128__
  597. uint16_t extaddr;
  598. #endif
  599. uint8_t addrl, addrh;
  600. #ifdef CRUMB128
  601. PGM_P welcome = {"ATmegaBOOT / Crumb128 - (C) J.P.Kyle, E.Lins - 050815\n\r"};
  602. #elif defined PROBOMEGA128
  603. PGM_P welcome = {"ATmegaBOOT / PROBOmega128 - (C) J.P.Kyle, E.Lins - 050815\n\r"};
  604. #elif defined SAVVY128
  605. PGM_P welcome = {"ATmegaBOOT / Savvy128 - (C) J.P.Kyle, E.Lins - 050815\n\r"};
  606. #endif
  607. /* turn on LED */
  608. LED_DDR |= _BV(LED);
  609. LED_PORT &= ~_BV(LED);
  610. /* print a welcome message and command overview */
  611. for(i=0; welcome[i] != '\0'; ++i) {
  612. putch(welcome[i]);
  613. }
  614. /* test for valid commands */
  615. for(;;) {
  616. putch('\n');
  617. putch('\r');
  618. putch(':');
  619. putch(' ');
  620. ch = getch();
  621. putch(ch);
  622. /* toggle LED */
  623. if(ch == 't') {
  624. if(bit_is_set(LED_PIN,LED)) {
  625. LED_PORT &= ~_BV(LED);
  626. putch('1');
  627. } else {
  628. LED_PORT |= _BV(LED);
  629. putch('0');
  630. }
  631. }
  632. /* read byte from address */
  633. else if(ch == 'r') {
  634. ch = getch(); putch(ch);
  635. addrh = gethex();
  636. addrl = gethex();
  637. putch('=');
  638. ch = *(uint8_t *)((addrh << 8) + addrl);
  639. puthex(ch);
  640. }
  641. /* write a byte to address */
  642. else if(ch == 'w') {
  643. ch = getch(); putch(ch);
  644. addrh = gethex();
  645. addrl = gethex();
  646. ch = getch(); putch(ch);
  647. ch = gethex();
  648. *(uint8_t *)((addrh << 8) + addrl) = ch;
  649. }
  650. /* read from uart and echo back */
  651. else if(ch == 'u') {
  652. for(;;) {
  653. putch(getch());
  654. }
  655. }
  656. #ifdef __AVR_ATmega128__
  657. /* external bus loop */
  658. else if(ch == 'b') {
  659. putch('b');
  660. putch('u');
  661. putch('s');
  662. MCUCR = 0x80;
  663. XMCRA = 0;
  664. XMCRB = 0;
  665. extaddr = 0x1100;
  666. for(;;) {
  667. ch = *(volatile uint8_t *)extaddr;
  668. if(++extaddr == 0) {
  669. extaddr = 0x1100;
  670. }
  671. }
  672. }
  673. #endif
  674. else if(ch == 'j') {
  675. app_start();
  676. }
  677. }
  678. /* end of monitor functions */
  679. }
  680. }
  681. }
  682. /* end of monitor */
  683. #endif
  684. else if (++error_count == MAX_ERROR_COUNT) {
  685. app_start();
  686. }
  687. }
  688. /* end of forever loop */
  689. }
  690. char gethex(void) {
  691. char ah,al;
  692. ah = getch(); putch(ah);
  693. al = getch(); putch(al);
  694. if(ah >= 'a') {
  695. ah = ah - 'a' + 0x0a;
  696. } else if(ah >= '0') {
  697. ah -= '0';
  698. }
  699. if(al >= 'a') {
  700. al = al - 'a' + 0x0a;
  701. } else if(al >= '0') {
  702. al -= '0';
  703. }
  704. return (ah << 4) + al;
  705. }
  706. void puthex(char ch) {
  707. char ah,al;
  708. ah = (ch & 0xf0) >> 4;
  709. if(ah >= 0x0a) {
  710. ah = ah - 0x0a + 'a';
  711. } else {
  712. ah += '0';
  713. }
  714. al = (ch & 0x0f);
  715. if(al >= 0x0a) {
  716. al = al - 0x0a + 'a';
  717. } else {
  718. al += '0';
  719. }
  720. putch(ah);
  721. putch(al);
  722. }
  723. void putch(char ch)
  724. {
  725. #ifdef __AVR_ATmega128__
  726. if(bootuart == 1) {
  727. while (!(UCSR0A & _BV(UDRE0)));
  728. UDR0 = ch;
  729. }
  730. else if (bootuart == 2) {
  731. while (!(UCSR1A & _BV(UDRE1)));
  732. UDR1 = ch;
  733. }
  734. #elif defined __AVR_ATmega168__
  735. while (!(UCSR0A & _BV(UDRE0)));
  736. UDR0 = ch;
  737. #else
  738. /* m8,16,32,169,8515,8535,163 */
  739. while (!(UCSRA & _BV(UDRE)));
  740. UDR = ch;
  741. #endif
  742. }
  743. char getch(void)
  744. {
  745. #ifdef __AVR_ATmega128__
  746. if(bootuart == 1) {
  747. while(!(UCSR0A & _BV(RXC0)));
  748. return UDR0;
  749. }
  750. else if(bootuart == 2) {
  751. while(!(UCSR1A & _BV(RXC1)));
  752. return UDR1;
  753. }
  754. return 0;
  755. #elif defined __AVR_ATmega168__
  756. uint32_t count = 0;
  757. while(!(UCSR0A & _BV(RXC0))){
  758. /* 20060803 DojoCorp:: Addon coming from the previous Bootloader*/
  759. /* HACKME:: here is a good place to count times*/
  760. count++;
  761. if (count > MAX_TIME_COUNT)
  762. app_start();
  763. }
  764. return UDR0;
  765. #else
  766. /* m8,16,32,169,8515,8535,163 */
  767. uint32_t count = 0;
  768. while(!(UCSRA & _BV(RXC))){
  769. /* 20060803 DojoCorp:: Addon coming from the previous Bootloader*/
  770. /* HACKME:: here is a good place to count times*/
  771. count++;
  772. if (count > MAX_TIME_COUNT)
  773. app_start();
  774. }
  775. return UDR;
  776. #endif
  777. }
  778. void getNch(uint8_t count)
  779. {
  780. uint8_t i;
  781. for(i=0;i<count;i++) {
  782. #ifdef __AVR_ATmega128__
  783. if(bootuart == 1) {
  784. while(!(UCSR0A & _BV(RXC0)));
  785. UDR0;
  786. }
  787. else if(bootuart == 2) {
  788. while(!(UCSR1A & _BV(RXC1)));
  789. UDR1;
  790. }
  791. #elif defined __AVR_ATmega168__
  792. while(!(UCSR0A & _BV(RXC0)));
  793. UDR0;
  794. #else
  795. /* m8,16,32,169,8515,8535,163 */
  796. /* 20060803 DojoCorp:: Addon coming from the previous Bootloader*/
  797. //while(!(UCSRA & _BV(RXC)));
  798. //UDR;
  799. uint8_t i;
  800. for(i=0;i<count;i++) {
  801. getch(); // need to handle time out
  802. }
  803. #endif
  804. }
  805. }
  806. void byte_response(uint8_t val)
  807. {
  808. if (getch() == ' ') {
  809. putch(0x14);
  810. putch(val);
  811. putch(0x10);
  812. } else {
  813. if (++error_count == MAX_ERROR_COUNT)
  814. app_start();
  815. }
  816. }
  817. void nothing_response(void)
  818. {
  819. if (getch() == ' ') {
  820. putch(0x14);
  821. putch(0x10);
  822. } else {
  823. if (++error_count == MAX_ERROR_COUNT)
  824. app_start();
  825. }
  826. }
  827. void flash_led(uint8_t count)
  828. {
  829. /* flash onboard LED three times to signal entering of bootloader */
  830. /* l needs to be volatile or the delay loops below might get
  831. optimized away if compiling with optimizations (DAM). */
  832. volatile uint32_t l;
  833. if (count == 0) {
  834. count = 3;
  835. }
  836. for (i = 0; i < count; ++i) {
  837. LED_PORT |= _BV(LED);
  838. for(l = 0; l < (F_CPU / 1000); ++l);
  839. LED_PORT &= ~_BV(LED);
  840. for(l = 0; l < (F_CPU / 1000); ++l);
  841. }
  842. }
  843. /* end of file ATmegaBOOT.c */