ATmegaBOOT_168.c 26 KB

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