123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071 |
- #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
- #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_ || defined __AVR_ATmega644P__
- #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__ || defined __AVR_ATmega644P__
- #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__)
- #define MONITOR 1
- #endif
- #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_ATmega644P__
- #define SIG2 0x96
- #define SIG3 0x0A
- #define PAGE_SIZE 0x080U
- #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 pagesz=0x80;
- 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__ || defined __AVR_ATmega644P__
-
-
- 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__) || defined(__AVR_ATmega644P__)
- 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__) || defined(__AVR_ATmega644P__)
-
- 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__) || defined(__AVR_ATmega644P__)
- 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__ || __AVR_ATmega644P__
- : "=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__) || defined(__AVR_ATmega644P__)
- 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__) || defined(__AVR_ATmega644P__)
- 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__) || defined(__AVR_ATmega644P__)
- 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__) || defined(__AVR_ATmega644P__)
- 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 */
|