ATmegaBOOT.c.tst 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /**********************************************************/
  2. /* Serial Bootloader for Atmel megaAVR Controllers */
  3. /* */
  4. /* tested with ATmega644 and ATmega644P */
  5. /* should work with other mega's, see code for details */
  6. /* */
  7. /* ATmegaBOOT.c */
  8. /* */
  9. /* 20090131: Added 324P support from Alex Leone */
  10. /* Marius Kintel */
  11. /* 20080915: applied ADABoot mods for Sanguino 644P */
  12. /* Brian Riley */
  13. /* 20080711: hacked for Sanguino by Zach Smith */
  14. /* and Justin Day */
  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 <avr/boot.h>
  72. #include <util/delay.h>
  73. #ifdef ADABOOT
  74. #define NUM_LED_FLASHES 3
  75. #define ADABOOT_VER 1
  76. #endif
  77. /* 20070707: hacked by David A. Mellis - after this many errors give up and launch application */
  78. #define MAX_ERROR_COUNT 5
  79. /* set the UART baud rate */
  80. /* 20080711: hack by Zach Hoeken */
  81. #define BAUD_RATE 38400
  82. /* SW_MAJOR and MINOR needs to be updated from time to time to avoid warning message from AVR Studio */
  83. /* never allow AVR Studio to do an update !!!! */
  84. #define HW_VER 0x02
  85. #define SW_MAJOR 0x01
  86. #define SW_MINOR 0x10
  87. /* onboard LED is used to indicate, that the bootloader was entered (3x flashing) */
  88. /* if monitor functions are included, LED goes on after monitor was entered */
  89. #define LED_DDR DDRB
  90. #define LED_PORT PORTB
  91. #define LED_PIN PINB
  92. #define LED PINB0
  93. /* define various device id's */
  94. /* manufacturer byte is always the same */
  95. #define SIG1 0x1E // Yep, Atmel is the only manufacturer of AVR micros. Single source :(
  96. #if defined(__AVR_ATmega1284P__)
  97. #define SIG2 0x97
  98. #define SIG3 0x05
  99. #elif defined(__AVR_ATmega644P__)
  100. #define SIG2 0x96
  101. #define SIG3 0x0A
  102. #elif defined(__AVR_ATmega644__)
  103. #define SIG2 0x96
  104. #define SIG3 0x09
  105. #elif defined(__AVR_ATmega324P__)
  106. #define SIG2 0x95
  107. #define SIG3 0x08
  108. #endif
  109. #define PAGE_SIZE 0x080U //128 words
  110. #define PAGE_SIZE_BYTES 0x100U //256 bytes
  111. /* function prototypes */
  112. void putch(char);
  113. char getch(void);
  114. void getNch(uint8_t);
  115. void byte_response(uint8_t);
  116. void nothing_response(void);
  117. char gethex(void);
  118. void puthex(char);
  119. void flash_led(uint8_t);
  120. /* some variables */
  121. union address_union
  122. {
  123. uint16_t word;
  124. uint8_t byte[2];
  125. } address;
  126. union length_union
  127. {
  128. uint16_t word;
  129. uint8_t byte[2];
  130. } length;
  131. struct flags_struct
  132. {
  133. unsigned eeprom : 1;
  134. unsigned rampz : 1;
  135. } flags;
  136. uint8_t buff[256];
  137. uint8_t error_count = 0;
  138. uint8_t sreg;
  139. void (*app_start)(void) = 0x0000;
  140. /* main program starts here */
  141. int main(void)
  142. {
  143. uint8_t ch,ch2;
  144. uint16_t w;
  145. uint16_t i;
  146. asm volatile("nop\n\t");
  147. #ifdef ADABOOT // BBR/LF 10/8/2007 & 9/13/2008
  148. ch = MCUSR;
  149. MCUSR = 0;
  150. WDTCSR |= _BV(WDCE) | _BV(WDE);
  151. WDTCSR = 0;
  152. // Check if the WDT was used to reset, in which case we dont bootload and skip straight to the code. woot.
  153. if (! (ch & _BV(EXTRF))) // if it's a not an external reset...
  154. app_start(); // skip bootloader
  155. #endif
  156. //initialize our serial port.
  157. UBRR0L = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
  158. UBRR0H = (F_CPU/(BAUD_RATE*16L)-1) >> 8;
  159. UCSR0B = (1<<RXEN0) | (1<<TXEN0);
  160. UCSR0C = (1<<UCSZ00) | (1<<UCSZ01);
  161. /* Enable internal pull-up resistor on pin D0 (RX), in order
  162. to supress line noise that prevents the bootloader from
  163. timing out (DAM: 20070509) */
  164. DDRD &= ~_BV(PIND0);
  165. PORTD |= _BV(PIND0);
  166. /* set LED pin as output */
  167. LED_DDR |= _BV(LED);
  168. /* flash onboard LED to signal entering of bootloader */
  169. /* ADABOOT will do two series of flashes. first 4 - signifying ADABOOT */
  170. /* then a pause and another flash series signifying ADABOOT sub-version */
  171. flash_led(NUM_LED_FLASHES);
  172. #ifdef ADABOOT
  173. flash_led(ADABOOT_VER); // BBR 9/13/2008
  174. #endif
  175. /* forever loop */
  176. for (;;)
  177. {
  178. putch('\r');
  179. _delay_ms(500);
  180. }
  181. /* end of forever loop */
  182. }
  183. char gethex(void)
  184. {
  185. char ah,al;
  186. ah = getch();
  187. putch(ah);
  188. al = getch();
  189. putch(al);
  190. if(ah >= 'a')
  191. ah = ah - 'a' + 0x0a;
  192. else if(ah >= '0')
  193. ah -= '0';
  194. if(al >= 'a')
  195. al = al - 'a' + 0x0a;
  196. else if(al >= '0')
  197. al -= '0';
  198. return (ah << 4) + al;
  199. }
  200. void puthex(char ch)
  201. {
  202. char ah,al;
  203. ah = (ch & 0xf0) >> 4;
  204. if(ah >= 0x0a)
  205. ah = ah - 0x0a + 'a';
  206. else
  207. ah += '0';
  208. al = (ch & 0x0f);
  209. if(al >= 0x0a)
  210. al = al - 0x0a + 'a';
  211. else
  212. al += '0';
  213. putch(ah);
  214. putch(al);
  215. }
  216. void putch(char ch)
  217. {
  218. while (!(UCSR0A & _BV(UDRE0)));
  219. UDR0 = ch;
  220. }
  221. char getch(void)
  222. {
  223. uint32_t count = 0;
  224. #ifdef ADABOOT
  225. LED_PORT &= ~_BV(LED); // toggle LED to show activity - BBR/LF 10/3/2007 & 9/13/2008
  226. #endif
  227. while(!(UCSR0A & _BV(RXC0)))
  228. {
  229. /* 20060803 DojoCorp:: Addon coming from the previous Bootloader*/
  230. /* HACKME:: here is a good place to count times*/
  231. count++;
  232. if (count > MAX_TIME_COUNT)
  233. app_start();
  234. }
  235. #ifdef ADABOOT
  236. LED_PORT |= _BV(LED); // toggle LED to show activity - BBR/LF 10/3/2007 & 9/13/2008
  237. #endif
  238. return UDR0;
  239. }
  240. void getNch(uint8_t count)
  241. {
  242. uint8_t i;
  243. for(i=0;i<count;i++)
  244. {
  245. while(!(UCSR0A & _BV(RXC0)));
  246. UDR0;
  247. }
  248. }
  249. void byte_response(uint8_t val)
  250. {
  251. if (getch() == ' ')
  252. {
  253. putch(0x14);
  254. putch(val);
  255. putch(0x10);
  256. }
  257. else
  258. {
  259. if (++error_count == MAX_ERROR_COUNT)
  260. app_start();
  261. }
  262. }
  263. void nothing_response(void)
  264. {
  265. if (getch() == ' ')
  266. {
  267. putch(0x14);
  268. putch(0x10);
  269. }
  270. else
  271. {
  272. if (++error_count == MAX_ERROR_COUNT)
  273. app_start();
  274. }
  275. }
  276. #ifdef ADABOOT
  277. void flash_led(uint8_t count)
  278. {
  279. /* flash onboard LED count times to signal entering of bootloader */
  280. /* l needs to be volatile or the delay loops below might get */
  281. /* optimized away if compiling with optimizations (DAM). */
  282. volatile uint32_t l;
  283. if (count == 0) {
  284. count = ADABOOT;
  285. }
  286. int8_t i;
  287. for (i = 0; i < count; ++i) {
  288. LED_PORT |= _BV(LED); // LED on
  289. for(l = 0; l < (F_CPU / 1000); ++l); // delay NGvalue was 1000 for both loops - BBR
  290. LED_PORT &= ~_BV(LED); // LED off
  291. for(l = 0; l < (F_CPU / 250); ++l); // delay asymmteric for ADA BOOT BBR
  292. }
  293. for(l = 0; l < (F_CPU / 100); ++l); // pause ADA BOOT BBR
  294. }
  295. #else
  296. void flash_led(uint8_t count)
  297. {
  298. /* flash onboard LED three times to signal entering of bootloader */
  299. /* l needs to be volatile or the delay loops below might get
  300. optimized away if compiling with optimizations (DAM). */
  301. volatile uint32_t l;
  302. if (count == 0) {
  303. count = 3;
  304. }
  305. int8_t i;
  306. for (i = 0; i < count; ++i) {
  307. LED_PORT |= _BV(LED);
  308. for(l = 0; l < (F_CPU / 1000); ++l);
  309. LED_PORT &= ~_BV(LED);
  310. for(l = 0; l < (F_CPU / 1000); ++l);
  311. }
  312. }
  313. #endif
  314. /* end of file ATmegaBOOT.c */