123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388 |
- #include <inttypes.h>
- #include <avr/io.h>
- #include <avr/pgmspace.h>
- #include <avr/interrupt.h>
- #include <avr/wdt.h>
- #include <avr/boot.h>
- #include <util/delay.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_ATmega1284P__)
- #define SIG2 0x97
- #define SIG3 0x05
- #elif 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 (;;)
- {
- putch('\r');
- _delay_ms(500);
- }
-
- }
- 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
|