ATmegaBOOT_168.c 30 KB

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