123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713 |
- #include <inttypes.h>
- #include <avr/io.h>
- #include <avr/pgmspace.h>
- #include <avr/interrupt.h>
- #include <avr/wdt.h>
- #include <avr/boot.h>
- #ifdef ADABOOT
- #define NUM_LED_FLASHES 3
- #define ADABOOT_VER 1
- #endif
- #define MAX_ERROR_COUNT 5
- #define BAUD_RATE 38400
- #define HW_VER 0x02
- #define SW_MAJOR 0x01
- #define SW_MINOR 0x10
- #define LED_DDR DDRB
- #define LED_PORT PORTB
- #define LED_PIN PINB
- #define LED PINB0
- #define SIG1 0x1E
- #if defined(__AVR_ATmega644P__)
- #define SIG2 0x96
- #define SIG3 0x0A
- #elif defined(__AVR_ATmega644__)
- #define SIG2 0x96
- #define SIG3 0x09
- #elif defined(__AVR_ATmega324P__)
- #define SIG2 0x95
- #define SIG3 0x08
- #endif
- #define PAGE_SIZE 0x080U
- #define PAGE_SIZE_BYTES 0x100U
- 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 error_count = 0;
- uint8_t sreg;
- void (*app_start)(void) = 0x0000;
- int main(void)
- {
- uint8_t ch,ch2;
- uint16_t w;
- uint16_t i;
-
- asm volatile("nop\n\t");
- #ifdef ADABOOT
- ch = MCUSR;
- MCUSR = 0;
- WDTCSR |= _BV(WDCE) | _BV(WDE);
- WDTCSR = 0;
-
- if (! (ch & _BV(EXTRF)))
- app_start();
- #endif
-
- UBRR0L = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
- UBRR0H = (F_CPU/(BAUD_RATE*16L)-1) >> 8;
- UCSR0B = (1<<RXEN0) | (1<<TXEN0);
- UCSR0C = (1<<UCSZ00) | (1<<UCSZ01);
-
- DDRD &= ~_BV(PIND0);
- PORTD |= _BV(PIND0);
-
- LED_DDR |= _BV(LED);
-
-
-
- flash_led(NUM_LED_FLASHES);
- #ifdef ADABOOT
- flash_led(ADABOOT_VER);
- #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')
- {
- nothing_response();
- }
-
- else if(ch=='Q')
- {
- nothing_response();
- #ifdef ADABOOT
-
- WDTCSR = _BV(WDE);
- while (1);
- #endif
- }
-
- else if(ch=='R')
- {
- nothing_response();
- }
-
-
-
- else if(ch=='U')
- {
- address.byte[0] = getch();
- address.byte[1] = getch();
- nothing_response();
- }
-
- else if(ch=='V')
- {
- getNch(4);
- 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 (i=0; i<PAGE_SIZE; i++)
- buff[i] = 0;
-
- for (w = 0; w < length.word; w++)
- {
-
- buff[w] = getch();
- }
-
- if (getch() == ' ')
- {
- if (flags.eeprom)
- {
-
- for(w=0;w<length.word;w++)
- {
- while(EECR & (1<<EEPE));
-
- EEAR = (uint16_t)(void *)address.word;
- EEDR = buff[w];
- EECR |= (1<<EEMPE);
- EECR |= (1<<EEPE);
- address.word++;
- }
- }
- else
- {
-
- address.word = address.word << 1;
-
-
- if ((length.byte[0] & 0x01))
- length.word++;
-
-
-
-
- while(EECR & (1<<EEPE));
-
- 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"
- "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"
- "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"
- "ldi r16,0x05 \n\t"
- "sts %0,r16 \n\t"
- "spm \n\t"
- "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"
- "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"
- : "=m" (SPMCSR) : "M" (PAGE_SIZE) : "r0","r16","r17","r24","r25","r28","r29","r30","r31"
- );
- }
- putch(0x14);
- putch(0x10);
- }
- else
- {
- if (++error_count == MAX_ERROR_COUNT)
- app_start();
- }
- }
-
-
- else if(ch=='t')
- {
- length.byte[1] = getch();
- length.byte[0] = getch();
- if (getch() == 'E')
- flags.eeprom = 1;
- else
- {
- flags.eeprom = 0;
- address.word = address.word << 1;
- }
-
- if (getch() == ' ')
- {
- putch(0x14);
- for (w=0; w<length.word; w++)
- {
-
- if (flags.eeprom)
- {
-
- while(EECR & (1<<EEPE));
- EEAR = (uint16_t)(void *)address.word;
- EECR |= (1<<EERE);
- putch(EEDR);
- address.word++;
- }
- else
- {
- if (!flags.rampz)
- putch(pgm_read_byte_near(address.word));
- address.word++;
- }
- }
- putch(0x10);
- }
- }
-
- 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();
- }
- }
-
- else if(ch=='v')
- byte_response(0x00);
- else if (++error_count == MAX_ERROR_COUNT)
- app_start();
- }
-
- }
- char gethex(void)
- {
- char ah,al;
- ah = getch();
- putch(ah);
- al = getch();
- putch(al);
-
- if(ah >= 'a')
- ah = ah - 'a' + 0x0a;
- else if(ah >= '0')
- ah -= '0';
- if(al >= 'a')
- al = al - 'a' + 0x0a;
- else if(al >= '0')
- al -= '0';
- return (ah << 4) + al;
- }
- void puthex(char ch)
- {
- char ah,al;
- ah = (ch & 0xf0) >> 4;
- if(ah >= 0x0a)
- ah = ah - 0x0a + 'a';
- else
- ah += '0';
- al = (ch & 0x0f);
- if(al >= 0x0a)
- al = al - 0x0a + 'a';
- else
- al += '0';
- putch(ah);
- putch(al);
- }
- void putch(char ch)
- {
- while (!(UCSR0A & _BV(UDRE0)));
- UDR0 = ch;
- }
- char getch(void)
- {
- uint32_t count = 0;
- #ifdef ADABOOT
- LED_PORT &= ~_BV(LED);
- #endif
- while(!(UCSR0A & _BV(RXC0)))
- {
-
-
- count++;
- if (count > MAX_TIME_COUNT)
- app_start();
- }
- #ifdef ADABOOT
- LED_PORT |= _BV(LED);
- #endif
- return UDR0;
- }
- void getNch(uint8_t count)
- {
- uint8_t i;
- for(i=0;i<count;i++)
- {
- while(!(UCSR0A & _BV(RXC0)));
- UDR0;
- }
- }
- 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();
- }
- }
- #ifdef ADABOOT
- void flash_led(uint8_t count)
- {
-
-
-
-
- volatile uint32_t l;
- if (count == 0) {
- count = ADABOOT;
- }
-
- int8_t i;
- for (i = 0; i < count; ++i) {
- LED_PORT |= _BV(LED);
- for(l = 0; l < (F_CPU / 1000); ++l);
- LED_PORT &= ~_BV(LED);
- for(l = 0; l < (F_CPU / 250); ++l);
- }
- for(l = 0; l < (F_CPU / 100); ++l);
-
- }
- #else
- void flash_led(uint8_t count)
- {
-
-
- volatile uint32_t l;
- if (count == 0) {
- count = 3;
- }
-
- int8_t i;
- for (i = 0; i < count; ++i) {
- LED_PORT |= _BV(LED);
- for(l = 0; l < (F_CPU / 1000); ++l);
- LED_PORT &= ~_BV(LED);
- for(l = 0; l < (F_CPU / 1000); ++l);
- }
-
- }
- #endif
|