ATmegaBOOT_168.c 29 KB

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