1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090 |
- #include <inttypes.h>
- #include <avr/io.h>
- #include <avr/pgmspace.h>
- #include <avr/interrupt.h>
- #include <avr/wdt.h>
- #include <util/delay.h>
- #if !defined(__AVR_ATmega168__) || !defined(__AVR_ATmega328P__)
- #include <avr/eeprom.h>
- #endif
- #define MAX_ERROR_COUNT 5
- #define NUM_LED_FLASHES 3
- #ifndef BAUD_RATE
- #define BAUD_RATE 19200
- #endif
- #define HW_VER 0x02
- #define SW_MAJOR 0x01
- #define SW_MINOR 0x10
- #ifdef __AVR_ATmega128__
- #define BL_DDR DDRF
- #define BL_PORT PORTF
- #define BL_PIN PINF
- #define BL0 PINF7
- #define BL1 PINF6
- #elif defined __AVR_ATmega1280__
- #elif defined __AVR_ATmega1284P__
- #else
- #define BL_DDR DDRD
- #define BL_PORT PORTD
- #define BL_PIN PIND
- #define BL PIND6
- #endif
- #if defined __AVR_ATmega128__ || defined __AVR_ATmega1280__
- #define LED_DDR DDRB
- #define LED_PORT PORTB
- #define LED_PIN PINB
- #define LED PINB7
- #elif defined __AVR_ATmega1284P__
- #define LED_DDR DDRB
- #define LED_PORT PORTB
- #define LED_PIN PINB
- #define LED PINB0
- #else
- #define LED_DDR DDRB
- #define LED_PORT PORTB
- #define LED_PIN PINB
- #define LED PINB5
- #endif
- #if defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__)
- #endif
- #undef MONITOR
- #define SIG1 0x1E
- #if defined __AVR_ATmega1280__
- #define SIG2 0x97
- #define SIG3 0x03
- #define PAGE_SIZE 0x80U
- #elif defined __AVR_ATmega1284P__
- #define SIG2 0x97
- #define SIG3 0x05
- #define PAGE_SIZE 0x080U
- #elif defined __AVR_ATmega1281__
- #define SIG2 0x97
- #define SIG3 0x04
- #define PAGE_SIZE 0x80U
- #elif defined __AVR_ATmega128__
- #define SIG2 0x97
- #define SIG3 0x02
- #define PAGE_SIZE 0x80U
- #elif defined __AVR_ATmega64__
- #define SIG2 0x96
- #define SIG3 0x02
- #define PAGE_SIZE 0x80U
- #elif defined __AVR_ATmega32__
- #define SIG2 0x95
- #define SIG3 0x02
- #define PAGE_SIZE 0x40U
- #elif defined __AVR_ATmega16__
- #define SIG2 0x94
- #define SIG3 0x03
- #define PAGE_SIZE 0x40U
- #elif defined __AVR_ATmega8__
- #define SIG2 0x93
- #define SIG3 0x07
- #define PAGE_SIZE 0x20U
- #elif defined __AVR_ATmega88__
- #define SIG2 0x93
- #define SIG3 0x0a
- #define PAGE_SIZE 0x20U
- #elif defined __AVR_ATmega168__
- #define SIG2 0x94
- #define SIG3 0x06
- #define PAGE_SIZE 0x40U
- #elif defined __AVR_ATmega328P__
- #define SIG2 0x95
- #define SIG3 0x0F
- #define PAGE_SIZE 0x40U
- #elif defined __AVR_ATmega162__
- #define SIG2 0x94
- #define SIG3 0x04
- #define PAGE_SIZE 0x40U
- #elif defined __AVR_ATmega163__
- #define SIG2 0x94
- #define SIG3 0x02
- #define PAGE_SIZE 0x40U
- #elif defined __AVR_ATmega169__
- #define SIG2 0x94
- #define SIG3 0x05
- #define PAGE_SIZE 0x40U
- #elif defined __AVR_ATmega8515__
- #define SIG2 0x93
- #define SIG3 0x06
- #define PAGE_SIZE 0x20U
- #elif defined __AVR_ATmega8535__
- #define SIG2 0x93
- #define SIG3 0x08
- #define PAGE_SIZE 0x20U
- #endif
- void putch(char);
- char getch(void);
- void getNch(uint8_t);
- void byte_response(uint8_t);
- void nothing_response(void);
- char gethex(void);
- void puthex(char);
- void flash_led(uint8_t);
- union address_union
- {
- uint16_t word;
- uint8_t byte[2];
- } address;
- union length_union
- {
- uint16_t word;
- uint8_t byte[2];
- } length;
- struct flags_struct
- {
- unsigned eeprom : 1;
- unsigned rampz : 1;
- } flags;
- uint8_t buff[256];
- uint8_t address_high;
- uint8_t i;
- uint8_t bootuart = 0;
- uint8_t error_count = 0;
- void (*app_start)(void) = 0x0000;
- int main(void)
- {
- uint8_t ch,ch2;
- uint16_t w;
- #ifdef WATCHDOG_MODS
- ch = MCUSR;
- MCUSR = 0;
- WDTCSR |= _BV(WDCE) | _BV(WDE);
- WDTCSR = 0;
-
- if (! (ch & _BV(EXTRF)))
- app_start();
- #else
- asm volatile("nop\n\t");
- #endif
-
-
- #ifdef __AVR_ATmega128__
- BL_DDR &= ~_BV(BL0);
- BL_DDR &= ~_BV(BL1);
- BL_PORT |= _BV(BL0);
- BL_PORT |= _BV(BL1);
- #else
-
- #endif
- #ifdef __AVR_ATmega128__
-
- if(bit_is_clear(BL_PIN, BL0))
- {
- bootuart = 1;
- }
- else if(bit_is_clear(BL_PIN, BL1))
- {
- bootuart = 2;
- }
- #endif
- #if defined __AVR_ATmega1280__ || defined __AVR_ATmega1284P__
-
-
- bootuart = 1;
- #endif
-
- if(pgm_read_byte_near(0x0000) != 0xFF)
- {
- #ifdef __AVR_ATmega128__
-
- if(!bootuart)
- {
- app_start();
- }
- #else
-
-
-
-
-
- #endif
- }
- #ifdef __AVR_ATmega128__
-
- if(!bootuart)
- {
- bootuart = 1;
- }
- #endif
-
- #if defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1284P__)
- if(bootuart == 1)
- {
- UBRR0L = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
- UBRR0H = (F_CPU/(BAUD_RATE*16L)-1) >> 8;
- UCSR0A = 0x00;
- UCSR0C = 0x06;
- UCSR0B = _BV(TXEN0)|_BV(RXEN0);
- }
- if(bootuart == 2)
- {
- UBRR1L = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
- UBRR1H = (F_CPU/(BAUD_RATE*16L)-1) >> 8;
- UCSR1A = 0x00;
- UCSR1C = 0x06;
- UCSR1B = _BV(TXEN1)|_BV(RXEN1);
- }
- #elif defined __AVR_ATmega163__
- UBRR = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
- UBRRHI = (F_CPU/(BAUD_RATE*16L)-1) >> 8;
- UCSRA = 0x00;
- UCSRB = _BV(TXEN)|_BV(RXEN);
- #elif defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
- #ifdef DOUBLE_SPEED
- UCSR0A = (1<<U2X0);
- UBRR0L = (uint8_t)(F_CPU/(BAUD_RATE*8L)-1);
- UBRR0H = (F_CPU/(BAUD_RATE*8L)-1) >> 8;
- #else
- UBRR0L = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
- UBRR0H = (F_CPU/(BAUD_RATE*16L)-1) >> 8;
- #endif
- UCSR0B = (1<<RXEN0) | (1<<TXEN0);
- UCSR0C = (1<<UCSZ00) | (1<<UCSZ01);
-
- DDRD &= ~_BV(PIND0);
- PORTD |= _BV(PIND0);
- #elif defined __AVR_ATmega8__
-
- UBRRH = (((F_CPU/BAUD_RATE)/16)-1)>>8;
- UBRRL = (((F_CPU/BAUD_RATE)/16)-1);
- UCSRB = (1<<RXEN)|(1<<TXEN);
- UCSRC = (1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0);
- #else
-
- UBRRL = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
- UBRRH = (F_CPU/(BAUD_RATE*16L)-1) >> 8;
- UCSRA = 0x00;
- UCSRC = 0x06;
- UCSRB = _BV(TXEN)|_BV(RXEN);
- #endif
- #if defined __AVR_ATmega1280__
-
-
- DDRE &= ~_BV(PINE0);
- PORTE |= _BV(PINE0);
- #endif
-
- LED_DDR |= _BV(LED);
-
- #if defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1284P__)
-
- flash_led(NUM_LED_FLASHES + bootuart);
- #else
- flash_led(NUM_LED_FLASHES);
- #endif
-
-
-
- for (;;)
- {
-
- ch = getch();
-
-
- if(ch=='0')
- {
- nothing_response();
- }
-
-
-
- else if(ch=='1')
- {
- if (getch() == ' ')
- {
- putch(0x14);
- putch('A');
- putch('V');
- putch('R');
- putch(' ');
- putch('I');
- putch('S');
- putch('P');
- putch(0x10);
- }
- else
- {
- if (++error_count == MAX_ERROR_COUNT)
- app_start();
- }
- }
-
- else if(ch=='@')
- {
- ch2 = getch();
- if (ch2>0x85) getch();
- nothing_response();
- }
-
- else if(ch=='A')
- {
- ch2 = getch();
- if(ch2==0x80) byte_response(HW_VER);
- else if(ch2==0x81) byte_response(SW_MAJOR);
- else if(ch2==0x82) byte_response(SW_MINOR);
- else if(ch2==0x98) byte_response(0x03);
- else byte_response(0x00);
- }
-
- else if(ch=='B')
- {
- getNch(20);
- nothing_response();
- }
-
- else if(ch=='E')
- {
- getNch(5);
- nothing_response();
- }
-
-
- else if(ch=='P' || ch=='R')
- {
- nothing_response();
- }
-
- else if(ch=='Q')
- {
- nothing_response();
- #ifdef WATCHDOG_MODS
-
- WDTCSR = _BV(WDE);
- while (1);
- #endif
- }
-
-
-
- else if(ch=='U')
- {
- address.byte[0] = getch();
- address.byte[1] = getch();
- nothing_response();
- }
-
- else if(ch=='V')
- {
- if (getch() == 0x30)
- {
- getch();
- ch = getch();
- getch();
- if (ch == 0)
- {
- byte_response(SIG1);
- }
- else if (ch == 1)
- {
- byte_response(SIG2);
- }
- else
- {
- byte_response(SIG3);
- }
- }
- else
- {
- getNch(3);
- byte_response(0x00);
- }
- }
-
- else if(ch=='d')
- {
- length.byte[1] = getch();
- length.byte[0] = getch();
- flags.eeprom = 0;
- if (getch() == 'E') flags.eeprom = 1;
- for (w=0; w<length.word; w++)
- {
- buff[w] = getch();
- }
- if (getch() == ' ')
- {
- if (flags.eeprom)
- {
- address.word <<= 1;
- for(w=0; w<length.word; w++)
- {
- #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
- while(EECR & (1<<EEPE));
- EEAR = (uint16_t)(void *)address.word;
- EEDR = buff[w];
- EECR |= (1<<EEMPE);
- EECR |= (1<<EEPE);
- #else
- eeprom_write_byte((void *)address.word,buff[w]);
- #endif
- address.word++;
- }
- }
- else
- {
- if (address.byte[1]>127) address_high = 0x01;
- else address_high = 0x00;
- #if defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega1284P__)
- RAMPZ = address_high;
- #endif
- address.word = address.word << 1;
-
- if ((length.byte[0] & 0x01)) length.word++;
- cli();
- #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega1284P__)
- while(bit_is_set(EECR,EEPE));
- #else
- while(bit_is_set(EECR,EEWE));
- #endif
- asm volatile(
- "clr r17 \n\t"
- "lds r30,address \n\t"
- "lds r31,address+1 \n\t"
- "ldi r28,lo8(buff) \n\t"
- "ldi r29,hi8(buff) \n\t"
- "lds r24,length \n\t"
- "lds r25,length+1 \n\t"
- "length_loop: \n\t"
- "cpi r17,0x00 \n\t"
- "brne no_page_erase \n\t"
- "wait_spm1: \n\t"
- "lds r16,%0 \n\t"
- "andi r16,1 \n\t"
- "cpi r16,1 \n\t"
- "breq wait_spm1 \n\t"
- "ldi r16,0x03 \n\t"
- "sts %0,r16 \n\t"
- "spm \n\t"
- #ifdef __AVR_ATmega163__
- ".word 0xFFFF \n\t"
- "nop \n\t"
- #endif
- "wait_spm2: \n\t"
- "lds r16,%0 \n\t"
- "andi r16,1 \n\t"
- "cpi r16,1 \n\t"
- "breq wait_spm2 \n\t"
- "ldi r16,0x11 \n\t"
- "sts %0,r16 \n\t"
- "spm \n\t"
- #ifdef __AVR_ATmega163__
- ".word 0xFFFF \n\t"
- "nop \n\t"
- #endif
- "no_page_erase: \n\t"
- "ld r0,Y+ \n\t"
- "ld r1,Y+ \n\t"
- "wait_spm3: \n\t"
- "lds r16,%0 \n\t"
- "andi r16,1 \n\t"
- "cpi r16,1 \n\t"
- "breq wait_spm3 \n\t"
- "ldi r16,0x01 \n\t"
- "sts %0,r16 \n\t"
- "spm \n\t"
- "inc r17 \n\t"
- "cpi r17,%1 \n\t"
- "brlo same_page \n\t"
- "write_page: \n\t"
- "clr r17 \n\t"
- "wait_spm4: \n\t"
- "lds r16,%0 \n\t"
- "andi r16,1 \n\t"
- "cpi r16,1 \n\t"
- "breq wait_spm4 \n\t"
- #ifdef __AVR_ATmega163__
- "andi r30,0x80 \n\t"
- #endif
- "ldi r16,0x05 \n\t"
- "sts %0,r16 \n\t"
- "spm \n\t"
- #ifdef __AVR_ATmega163__
- ".word 0xFFFF \n\t"
- "nop \n\t"
- "ori r30,0x7E \n\t"
- #endif
- "wait_spm5: \n\t"
- "lds r16,%0 \n\t"
- "andi r16,1 \n\t"
- "cpi r16,1 \n\t"
- "breq wait_spm5 \n\t"
- "ldi r16,0x11 \n\t"
- "sts %0,r16 \n\t"
- "spm \n\t"
- #ifdef __AVR_ATmega163__
- ".word 0xFFFF \n\t"
- "nop \n\t"
- #endif
- "same_page: \n\t"
- "adiw r30,2 \n\t"
- "sbiw r24,2 \n\t"
- "breq final_write \n\t"
- "rjmp length_loop \n\t"
- "final_write: \n\t"
- "cpi r17,0 \n\t"
- "breq block_done \n\t"
- "adiw r24,2 \n\t"
- "rjmp write_page \n\t"
- "block_done: \n\t"
- "clr __zero_reg__ \n\t"
- #if defined __AVR_ATmega168__ || __AVR_ATmega328P__ || __AVR_ATmega128__ || __AVR_ATmega1280__ || __AVR_ATmega1281__ || __AVR_ATmega1284P__
- : "=m" (SPMCSR) : "M" (PAGE_SIZE) : "r0","r16","r17","r24","r25","r28","r29","r30","r31"
- #else
- : "=m" (SPMCR) : "M" (PAGE_SIZE) : "r0","r16","r17","r24","r25","r28","r29","r30","r31"
- #endif
- );
- /* Should really add a wait for RWW section to be enabled, don't actually need it since we never */
- /* exit the bootloader without a power cycle anyhow */
- }
- putch(0x14);
- putch(0x10);
- }
- else
- {
- if (++error_count == MAX_ERROR_COUNT)
- app_start();
- }
- }
- /* Read memory block mode, length is big endian. */
- else if(ch=='t')
- {
- length.byte[1] = getch();
- length.byte[0] = getch();
- #if defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1284P__)
- if (address.word>0x7FFF) flags.rampz = 1; // No go with m256, FIXME
- else flags.rampz = 0;
- #endif
- address.word = address.word << 1; // address * 2 -> byte location
- if (getch() == 'E') flags.eeprom = 1;
- else flags.eeprom = 0;
- if (getch() == ' ') // Command terminator
- {
- putch(0x14);
- for (w=0; w < length.word; w++) // Can handle odd and even lengths okay
- {
- if (flags.eeprom) // Byte access EEPROM read
- {
- #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
- while(EECR & (1<<EEPE));
- EEAR = (uint16_t)(void *)address.word;
- EECR |= (1<<EERE);
- putch(EEDR);
- #else
- putch(eeprom_read_byte((void *)address.word));
- #endif
- address.word++;
- }
- else
- {
- if (!flags.rampz) putch(pgm_read_byte_near(address.word));
- #if defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1284P__)
- else putch(pgm_read_byte_far(address.word + 0x10000));
- // Hmmmm, yuck FIXME when m256 arrvies
- #endif
- address.word++;
- }
- }
- putch(0x10);
- }
- }
- /* Get device signature bytes */
- else if(ch=='u')
- {
- if (getch() == ' ')
- {
- putch(0x14);
- putch(SIG1);
- putch(SIG2);
- putch(SIG3);
- putch(0x10);
- }
- else
- {
- if (++error_count == MAX_ERROR_COUNT)
- app_start();
- }
- }
- /* Read oscillator calibration byte */
- else if(ch=='v')
- {
- byte_response(0x00);
- }
- #if defined MONITOR
- /* here come the extended monitor commands by Erik Lins */
- /* check for three times exclamation mark pressed */
- else if(ch=='!')
- {
- ch = getch();
- if(ch=='!')
- {
- ch = getch();
- if(ch=='!')
- {
- PGM_P welcome = "";
- #if defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__)
- uint16_t extaddr;
- #endif
- uint8_t addrl, addrh;
- #ifdef CRUMB128
- welcome = "ATmegaBOOT / Crumb128 - (C) J.P.Kyle, E.Lins - 050815\n\r";
- #elif defined PROBOMEGA128
- welcome = "ATmegaBOOT / PROBOmega128 - (C) J.P.Kyle, E.Lins - 050815\n\r";
- #elif defined SAVVY128
- welcome = "ATmegaBOOT / Savvy128 - (C) J.P.Kyle, E.Lins - 050815\n\r";
- #elif defined __AVR_ATmega1280__
- welcome = "ATmegaBOOT / Arduino Mega - (C) Arduino LLC - 090930\n\r";
- #endif
- /* turn on LED */
- LED_DDR |= _BV(LED);
- LED_PORT &= ~_BV(LED);
- /* print a welcome message and command overview */
- for(i=0; welcome[i] != '\0'; ++i)
- {
- putch(welcome[i]);
- }
- /* test for valid commands */
- for(;;)
- {
- putch('\n');
- putch('\r');
- putch(':');
- putch(' ');
- ch = getch();
- putch(ch);
- /* toggle LED */
- if(ch == 't')
- {
- if(bit_is_set(LED_PIN,LED))
- {
- LED_PORT &= ~_BV(LED);
- putch('1');
- }
- else
- {
- LED_PORT |= _BV(LED);
- putch('0');
- }
- }
- /* read byte from address */
- else if(ch == 'r')
- {
- ch = getch();
- putch(ch);
- addrh = gethex();
- addrl = gethex();
- putch('=');
- ch = *(uint8_t *)((addrh << 8) + addrl);
- puthex(ch);
- }
- /* write a byte to address */
- else if(ch == 'w')
- {
- ch = getch();
- putch(ch);
- addrh = gethex();
- addrl = gethex();
- ch = getch();
- putch(ch);
- ch = gethex();
- *(uint8_t *)((addrh << 8) + addrl) = ch;
- }
- /* read from uart and echo back */
- else if(ch == 'u')
- {
- for(;;)
- {
- putch(getch());
- }
- }
- #if defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__)
- /* external bus loop */
- else if(ch == 'b')
- {
- putch('b');
- putch('u');
- putch('s');
- MCUCR = 0x80;
- XMCRA = 0;
- XMCRB = 0;
- extaddr = 0x1100;
- for(;;)
- {
- ch = *(volatile uint8_t *)extaddr;
- if(++extaddr == 0)
- {
- extaddr = 0x1100;
- }
- }
- }
- #endif
- else if(ch == 'j')
- {
- app_start();
- }
- } /* end of monitor functions */
- }
- }
- }
- /* end of monitor */
- #endif
- else if (++error_count == MAX_ERROR_COUNT)
- {
- app_start();
- }
- } /* end of forever loop */
- }
- char gethexnib(void)
- {
- char a;
- a = getch();
- putch(a);
- if(a >= 'a')
- {
- return (a - 'a' + 0x0a);
- }
- else if(a >= '0')
- {
- return(a - '0');
- }
- return a;
- }
- char gethex(void)
- {
- return (gethexnib() << 4) + gethexnib();
- }
- void puthex(char ch)
- {
- char ah;
- ah = ch >> 4;
- if(ah >= 0x0a)
- {
- ah = ah - 0x0a + 'a';
- }
- else
- {
- ah += '0';
- }
- ch &= 0x0f;
- if(ch >= 0x0a)
- {
- ch = ch - 0x0a + 'a';
- }
- else
- {
- ch += '0';
- }
- putch(ah);
- putch(ch);
- }
- void putch(char ch)
- {
- #if defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1284P__)
- if(bootuart == 1)
- {
- while (!(UCSR0A & _BV(UDRE0)));
- UDR0 = ch;
- }
- else if (bootuart == 2)
- {
- while (!(UCSR1A & _BV(UDRE1)));
- UDR1 = ch;
- }
- #elif defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
- while (!(UCSR0A & _BV(UDRE0)));
- UDR0 = ch;
- #else
- /* m8,16,32,169,8515,8535,163 */
- while (!(UCSRA & _BV(UDRE)));
- UDR = ch;
- #endif
- }
- char getch(void)
- {
- #if defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1284P__)
- uint32_t count = 0;
- if(bootuart == 1)
- {
- while(!(UCSR0A & _BV(RXC0)))
- {
- /* 20060803 DojoCorp:: Addon coming from the previous Bootloader*/
- /* HACKME:: here is a good place to count times*/
- count++;
- if (count > MAX_TIME_COUNT)
- app_start();
- }
- return UDR0;
- }
- else if(bootuart == 2)
- {
- while(!(UCSR1A & _BV(RXC1)))
- {
- /* 20060803 DojoCorp:: Addon coming from the previous Bootloader*/
- /* HACKME:: here is a good place to count times*/
- count++;
- if (count > MAX_TIME_COUNT)
- app_start();
- }
- return UDR1;
- }
- return 0;
- #elif defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
- uint32_t count = 0;
- while(!(UCSR0A & _BV(RXC0)))
- {
- /* 20060803 DojoCorp:: Addon coming from the previous Bootloader*/
- /* HACKME:: here is a good place to count times*/
- count++;
- if (count > MAX_TIME_COUNT)
- app_start();
- }
- return UDR0;
- #else
- /* m8,16,32,169,8515,8535,163 */
- uint32_t count = 0;
- while(!(UCSRA & _BV(RXC)))
- {
- /* 20060803 DojoCorp:: Addon coming from the previous Bootloader*/
- /* HACKME:: here is a good place to count times*/
- count++;
- if (count > MAX_TIME_COUNT)
- app_start();
- }
- return UDR;
- #endif
- }
- void getNch(uint8_t count)
- {
- while(count--)
- {
- #if defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1284P__)
- if(bootuart == 1)
- {
- while(!(UCSR0A & _BV(RXC0)));
- UDR0;
- }
- else if(bootuart == 2)
- {
- while(!(UCSR1A & _BV(RXC1)));
- UDR1;
- }
- #elif defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
- getch();
- #else
- /* m8,16,32,169,8515,8535,163 */
- /* 20060803 DojoCorp:: Addon coming from the previous Bootloader*/
- //while(!(UCSRA & _BV(RXC)));
- //UDR;
- getch(); // need to handle time out
- #endif
- }
- }
- void byte_response(uint8_t val)
- {
- if (getch() == ' ')
- {
- putch(0x14);
- putch(val);
- putch(0x10);
- }
- else
- {
- if (++error_count == MAX_ERROR_COUNT)
- app_start();
- }
- }
- void nothing_response(void)
- {
- if (getch() == ' ')
- {
- putch(0x14);
- putch(0x10);
- }
- else
- {
- if (++error_count == MAX_ERROR_COUNT)
- app_start();
- }
- }
- void flash_led(uint8_t count)
- {
- while (count--)
- {
- LED_PORT |= _BV(LED);
- _delay_ms(100);
- LED_PORT &= ~_BV(LED);
- _delay_ms(100);
- }
- }
- /* end of file ATmegaBOOT.c */
|