My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ATmegaBOOT.c 31KB

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