ATmegaBOOT.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /**********************************************************/
  2. /* Serial Bootloader for Atmel mega8 AVR Controller */
  3. /* */
  4. /* ATmegaBOOT.c */
  5. /* */
  6. /* Copyright (c) 2003, Jason P. Kyle */
  7. /* */
  8. /* Hacked by DojoCorp - ZGZ - MMX - IVR */
  9. /* Hacked by David A. Mellis */
  10. /* */
  11. /* This program is free software; you can redistribute it */
  12. /* and/or modify it under the terms of the GNU General */
  13. /* Public License as published by the Free Software */
  14. /* Foundation; either version 2 of the License, or */
  15. /* (at your option) any later version. */
  16. /* */
  17. /* This program is distributed in the hope that it will */
  18. /* be useful, but WITHOUT ANY WARRANTY; without even the */
  19. /* implied warranty of MERCHANTABILITY or FITNESS FOR A */
  20. /* PARTICULAR PURPOSE. See the GNU General Public */
  21. /* License for more details. */
  22. /* */
  23. /* You should have received a copy of the GNU General */
  24. /* Public License along with this program; if not, write */
  25. /* to the Free Software Foundation, Inc., */
  26. /* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  27. /* */
  28. /* Licence can be viewed at */
  29. /* http://www.fsf.org/licenses/gpl.txt */
  30. /* */
  31. /* Target = Atmel AVR m8 */
  32. /**********************************************************/
  33. #include <inttypes.h>
  34. #include <avr/io.h>
  35. #include <avr/pgmspace.h>
  36. #include <avr/eeprom.h>
  37. #include <avr/interrupt.h>
  38. #include <util/delay.h>
  39. //#define F_CPU 16000000
  40. /* We, Malmoitians, like slow interaction
  41. * therefore the slow baud rate ;-)
  42. */
  43. //#define BAUD_RATE 9600
  44. /* 6.000.000 is more or less 8 seconds at the
  45. * speed configured here
  46. */
  47. //#define MAX_TIME_COUNT 6000000
  48. #define MAX_TIME_COUNT (F_CPU>>1)
  49. ///#define MAX_TIME_COUNT_MORATORY 1600000
  50. /* SW_MAJOR and MINOR needs to be updated from time to time to avoid warning message from AVR Studio */
  51. #define HW_VER 0x02
  52. #define SW_MAJOR 0x01
  53. #define SW_MINOR 0x12
  54. // AVR-GCC compiler compatibility
  55. // avr-gcc compiler v3.1.x and older doesn't support outb() and inb()
  56. // if necessary, convert outb and inb to outp and inp
  57. #ifndef outb
  58. #define outb(sfr,val) (_SFR_BYTE(sfr) = (val))
  59. #endif
  60. #ifndef inb
  61. #define inb(sfr) _SFR_BYTE(sfr)
  62. #endif
  63. /* defines for future compatibility */
  64. #ifndef cbi
  65. #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
  66. #endif
  67. #ifndef sbi
  68. #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
  69. #endif
  70. /* Adjust to suit whatever pin your hardware uses to enter the bootloader */
  71. #define eeprom_rb(addr) eeprom_read_byte ((uint8_t *)(addr))
  72. #define eeprom_rw(addr) eeprom_read_word ((uint16_t *)(addr))
  73. #define eeprom_wb(addr, val) eeprom_write_byte ((uint8_t *)(addr), (uint8_t)(val))
  74. /* Onboard LED is connected to pin PB5 */
  75. #define LED_DDR DDRB
  76. #define LED_PORT PORTB
  77. #define LED_PIN PINB
  78. #define LED PINB5
  79. #define SIG1 0x1E // Yep, Atmel is the only manufacturer of AVR micros. Single source :(
  80. #define SIG2 0x93
  81. #define SIG3 0x07
  82. #define PAGE_SIZE 0x20U //32 words
  83. void putch(char);
  84. char getch(void);
  85. void getNch(uint8_t);
  86. void byte_response(uint8_t);
  87. void nothing_response(void);
  88. union address_union {
  89. uint16_t word;
  90. uint8_t byte[2];
  91. } address;
  92. union length_union {
  93. uint16_t word;
  94. uint8_t byte[2];
  95. } length;
  96. struct flags_struct {
  97. unsigned eeprom : 1;
  98. unsigned rampz : 1;
  99. } flags;
  100. uint8_t buff[256];
  101. //uint8_t address_high;
  102. uint8_t pagesz=0x80;
  103. uint8_t i;
  104. //uint8_t bootuart0=0,bootuart1=0;
  105. void (*app_start)(void) = 0x0000;
  106. int main(void)
  107. {
  108. uint8_t ch,ch2;
  109. uint16_t w;
  110. //cbi(BL_DDR,BL);
  111. //sbi(BL_PORT,BL);
  112. asm volatile("nop\n\t");
  113. /* check if flash is programmed already, if not start bootloader anyway */
  114. //if(pgm_read_byte_near(0x0000) != 0xFF) {
  115. /* check if bootloader pin is set low */
  116. //if(bit_is_set(BL_PIN,BL)) app_start();
  117. //}
  118. /* initialize UART(s) depending on CPU defined */
  119. /* m8 */
  120. UBRRH = (((F_CPU/BAUD_RATE)/16)-1)>>8; // set baud rate
  121. UBRRL = (((F_CPU/BAUD_RATE)/16)-1);
  122. UCSRB = (1<<RXEN)|(1<<TXEN); // enable Rx & Tx
  123. UCSRC = (1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0); // config USART; 8N1
  124. //UBRRL = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
  125. //UBRRH = (F_CPU/(BAUD_RATE*16L)-1) >> 8;
  126. //UCSRA = 0x00;
  127. //UCSRC = 0x86;
  128. //UCSRB = _BV(TXEN)|_BV(RXEN);
  129. /* this was giving uisp problems, so I removed it; without it, the boot
  130. works on with uisp and avrdude on the mac (at least). */
  131. //putch('\0');
  132. //uint32_t l;
  133. //uint32_t time_count;
  134. //time_count=0;
  135. /* set LED pin as output */
  136. sbi(LED_DDR,LED);
  137. for (i = 0; i < 16; i++) {
  138. outb(LED_PORT, inb(LED_PORT) ^ _BV(LED));
  139. _delay_loop_2(0);
  140. }
  141. //for (l=0; l<40000000; l++)
  142. //outb(LED_PORT, inb(LED_PORT) ^= _BV(LED));
  143. /* flash onboard LED three times to signal entering of bootloader */
  144. //for(i=0; i<3; ++i) {
  145. //for(l=0; l<40000000; ++l);
  146. //sbi(LED_PORT,LED);
  147. //for(l=0; l<40000000; ++l);
  148. //cbi(LED_PORT,LED);
  149. //}
  150. /* see comment at previous call to putch() */
  151. //putch('\0'); // this line is needed for the synchronization of the programmer
  152. /* forever */
  153. for (;;) {
  154. //if((inb(UCSRA) & _BV(RXC))){
  155. /* get character from UART */
  156. ch = getch();
  157. /* A bunch of if...else if... gives smaller code than switch...case ! */
  158. /* Hello is anyone home ? */
  159. if(ch=='0') {
  160. nothing_response();
  161. }
  162. /* Request programmer ID */
  163. /* Not using PROGMEM string due to boot block in m128 being beyond 64kB boundry */
  164. /* Would need to selectively manipulate RAMPZ, and it's only 9 characters anyway so who cares. */
  165. else if(ch=='1') {
  166. if (getch() == ' ') {
  167. putch(0x14);
  168. putch('A');
  169. putch('V');
  170. putch('R');
  171. putch(' ');
  172. putch('I');
  173. putch('S');
  174. putch('P');
  175. putch(0x10);
  176. }
  177. }
  178. /* AVR ISP/STK500 board commands DON'T CARE so default nothing_response */
  179. else if(ch=='@') {
  180. ch2 = getch();
  181. if (ch2>0x85) getch();
  182. nothing_response();
  183. }
  184. /* AVR ISP/STK500 board requests */
  185. else if(ch=='A') {
  186. ch2 = getch();
  187. if(ch2==0x80) byte_response(HW_VER); // Hardware version
  188. else if(ch2==0x81) byte_response(SW_MAJOR); // Software major version
  189. else if(ch2==0x82) byte_response(SW_MINOR); // Software minor version
  190. //else if(ch2==0x98) byte_response(0x03); // Unknown but seems to be required by avr studio 3.56
  191. else byte_response(0x00); // Covers various unnecessary responses we don't care about
  192. }
  193. /* Device Parameters DON'T CARE, DEVICE IS FIXED */
  194. else if(ch=='B') {
  195. getNch(20);
  196. nothing_response();
  197. }
  198. /* Parallel programming stuff DON'T CARE */
  199. else if(ch=='E') {
  200. getNch(5);
  201. nothing_response();
  202. }
  203. /* Enter programming mode */
  204. else if(ch=='P') {
  205. nothing_response();
  206. // FIXME: modified only here by DojoCorp, Mumbai, India, 20050626
  207. //time_count=0; // exted the delay once entered prog.mode
  208. }
  209. /* Leave programming mode */
  210. else if(ch=='Q') {
  211. nothing_response();
  212. //time_count=MAX_TIME_COUNT_MORATORY; // once the programming is done,
  213. // we should start the application
  214. // but uisp has problems with this,
  215. // therefore we just change the times
  216. // and give the programmer 1 sec to react
  217. }
  218. /* Erase device, don't care as we will erase one page at a time anyway. */
  219. else if(ch=='R') {
  220. nothing_response();
  221. }
  222. /* Set address, little endian. EEPROM in bytes, FLASH in words */
  223. /* Perhaps extra address bytes may be added in future to support > 128kB FLASH. */
  224. /* This might explain why little endian was used here, big endian used everywhere else. */
  225. else if(ch=='U') {
  226. address.byte[0] = getch();
  227. address.byte[1] = getch();
  228. nothing_response();
  229. }
  230. /* Universal SPI programming command, disabled. Would be used for fuses and lock bits. */
  231. else if(ch=='V') {
  232. getNch(4);
  233. byte_response(0x00);
  234. }
  235. /* Write memory, length is big endian and is in bytes */
  236. else if(ch=='d') {
  237. length.byte[1] = getch();
  238. length.byte[0] = getch();
  239. flags.eeprom = 0;
  240. if (getch() == 'E') flags.eeprom = 1;
  241. for (w=0;w<length.word;w++) {
  242. buff[w] = getch(); // Store data in buffer, can't keep up with serial data stream whilst programming pages
  243. }
  244. if (getch() == ' ') {
  245. if (flags.eeprom) { //Write to EEPROM one byte at a time
  246. for(w=0;w<length.word;w++) {
  247. eeprom_wb(address.word,buff[w]);
  248. address.word++;
  249. }
  250. } else { //Write to FLASH one page at a time
  251. //if (address.byte[1]>127) address_high = 0x01; //Only possible with m128, m256 will need 3rd address byte. FIXME
  252. //else address_high = 0x00;
  253. //address.word = address.word << 1; //address * 2 -> byte location
  254. //if ((length.byte[0] & 0x01)) length.word++; //Even up an odd number of bytes
  255. cli(); //Disable interrupts, just to be sure
  256. while(bit_is_set(EECR,EEWE)); //Wait for previous EEPROM writes to complete
  257. asm volatile(
  258. "clr r17 \n\t" //page_word_count
  259. "lds r30,address \n\t" //Address of FLASH location (in words)
  260. "lds r31,address+1 \n\t"
  261. "lsl r30 \n\t" //address * 2 -> byte location
  262. "rol r31 \n\t"
  263. "ldi r28,lo8(buff) \n\t" //Start of buffer array in RAM
  264. "ldi r29,hi8(buff) \n\t"
  265. "lds r24,length \n\t" //Length of data to be written (in bytes)
  266. "lds r25,length+1 \n\t"
  267. "sbrs r24,0 \n\t" //Even up an odd number of bytes
  268. "rjmp length_loop \n\t"
  269. "adiw r24,1 \n\t"
  270. "length_loop: \n\t" //Main loop, repeat for number of words in block
  271. "cpi r17,0x00 \n\t" //If page_word_count=0 then erase page
  272. "brne no_page_erase \n\t"
  273. "rcall wait_spm \n\t"
  274. // "wait_spm1: \n\t"
  275. // "lds r16,%0 \n\t" //Wait for previous spm to complete
  276. // "andi r16,1 \n\t"
  277. // "cpi r16,1 \n\t"
  278. // "breq wait_spm1 \n\t"
  279. "ldi r16,0x03 \n\t" //Erase page pointed to by Z
  280. "sts %0,r16 \n\t"
  281. "spm \n\t"
  282. "rcall wait_spm \n\t"
  283. // "wait_spm2: \n\t"
  284. // "lds r16,%0 \n\t" //Wait for previous spm to complete
  285. // "andi r16,1 \n\t"
  286. // "cpi r16,1 \n\t"
  287. // "breq wait_spm2 \n\t"
  288. "ldi r16,0x11 \n\t" //Re-enable RWW section
  289. "sts %0,r16 \n\t"
  290. "spm \n\t"
  291. "no_page_erase: \n\t"
  292. "ld r0,Y+ \n\t" //Write 2 bytes into page buffer
  293. "ld r1,Y+ \n\t"
  294. "rcall wait_spm \n\t"
  295. // "wait_spm3: \n\t"
  296. // "lds r16,%0 \n\t" //Wait for previous spm to complete
  297. // "andi r16,1 \n\t"
  298. // "cpi r16,1 \n\t"
  299. // "breq wait_spm3 \n\t"
  300. "ldi r16,0x01 \n\t" //Load r0,r1 into FLASH page buffer
  301. "sts %0,r16 \n\t"
  302. "spm \n\t"
  303. "inc r17 \n\t" //page_word_count++
  304. "cpi r17,%1 \n\t"
  305. "brlo same_page \n\t" //Still same page in FLASH
  306. "write_page: \n\t"
  307. "clr r17 \n\t" //New page, write current one first
  308. "rcall wait_spm \n\t"
  309. // "wait_spm4: \n\t"
  310. // "lds r16,%0 \n\t" //Wait for previous spm to complete
  311. // "andi r16,1 \n\t"
  312. // "cpi r16,1 \n\t"
  313. // "breq wait_spm4 \n\t"
  314. "ldi r16,0x05 \n\t" //Write page pointed to by Z
  315. "sts %0,r16 \n\t"
  316. "spm \n\t"
  317. "rcall wait_spm \n\t"
  318. // "wait_spm5: \n\t"
  319. // "lds r16,%0 \n\t" //Wait for previous spm to complete
  320. // "andi r16,1 \n\t"
  321. // "cpi r16,1 \n\t"
  322. // "breq wait_spm5 \n\t"
  323. "ldi r16,0x11 \n\t" //Re-enable RWW section
  324. "sts %0,r16 \n\t"
  325. "spm \n\t"
  326. "same_page: \n\t"
  327. "adiw r30,2 \n\t" //Next word in FLASH
  328. "sbiw r24,2 \n\t" //length-2
  329. "breq final_write \n\t" //Finished
  330. "rjmp length_loop \n\t"
  331. "wait_spm: \n\t"
  332. "lds r16,%0 \n\t" //Wait for previous spm to complete
  333. "andi r16,1 \n\t"
  334. "cpi r16,1 \n\t"
  335. "breq wait_spm \n\t"
  336. "ret \n\t"
  337. "final_write: \n\t"
  338. "cpi r17,0 \n\t"
  339. "breq block_done \n\t"
  340. "adiw r24,2 \n\t" //length+2, fool above check on length after short page write
  341. "rjmp write_page \n\t"
  342. "block_done: \n\t"
  343. "clr __zero_reg__ \n\t" //restore zero register
  344. : "=m" (SPMCR) : "M" (PAGE_SIZE) : "r0","r16","r17","r24","r25","r28","r29","r30","r31");
  345. /* Should really add a wait for RWW section to be enabled, don't actually need it since we never */
  346. /* exit the bootloader without a power cycle anyhow */
  347. }
  348. putch(0x14);
  349. putch(0x10);
  350. }
  351. }
  352. /* Read memory block mode, length is big endian. */
  353. else if(ch=='t') {
  354. length.byte[1] = getch();
  355. length.byte[0] = getch();
  356. if (getch() == 'E') flags.eeprom = 1;
  357. else {
  358. flags.eeprom = 0;
  359. address.word = address.word << 1; // address * 2 -> byte location
  360. }
  361. if (getch() == ' ') { // Command terminator
  362. putch(0x14);
  363. for (w=0;w < length.word;w++) { // Can handle odd and even lengths okay
  364. if (flags.eeprom) { // Byte access EEPROM read
  365. putch(eeprom_rb(address.word));
  366. address.word++;
  367. } else {
  368. if (!flags.rampz) putch(pgm_read_byte_near(address.word));
  369. address.word++;
  370. }
  371. }
  372. putch(0x10);
  373. }
  374. }
  375. /* Get device signature bytes */
  376. else if(ch=='u') {
  377. if (getch() == ' ') {
  378. putch(0x14);
  379. putch(SIG1);
  380. putch(SIG2);
  381. putch(SIG3);
  382. putch(0x10);
  383. }
  384. }
  385. /* Read oscillator calibration byte */
  386. else if(ch=='v') {
  387. byte_response(0x00);
  388. }
  389. // } else {
  390. // time_count++;
  391. // if (time_count>=MAX_TIME_COUNT) {
  392. // app_start();
  393. // }
  394. // }
  395. } /* end of forever loop */
  396. }
  397. void putch(char ch)
  398. {
  399. /* m8 */
  400. while (!(inb(UCSRA) & _BV(UDRE)));
  401. outb(UDR,ch);
  402. }
  403. char getch(void)
  404. {
  405. /* m8 */
  406. uint32_t count = 0;
  407. while(!(inb(UCSRA) & _BV(RXC))) {
  408. /* HACKME:: here is a good place to count times*/
  409. count++;
  410. if (count > MAX_TIME_COUNT)
  411. app_start();
  412. }
  413. return (inb(UDR));
  414. }
  415. void getNch(uint8_t count)
  416. {
  417. uint8_t i;
  418. for(i=0;i<count;i++) {
  419. /* m8 */
  420. //while(!(inb(UCSRA) & _BV(RXC)));
  421. //inb(UDR);
  422. getch(); // need to handle time out
  423. }
  424. }
  425. void byte_response(uint8_t val)
  426. {
  427. if (getch() == ' ') {
  428. putch(0x14);
  429. putch(val);
  430. putch(0x10);
  431. }
  432. }
  433. void nothing_response(void)
  434. {
  435. if (getch() == ' ') {
  436. putch(0x14);
  437. putch(0x10);
  438. }
  439. }
  440. /* end of file ATmegaBOOT.c */