123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724 |
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- #define OPTIBOOT_MAJVER 4
- #define OPTIBOOT_MINVER 5
-
- #define MAKESTR(a) #a
- #define MAKEVER(a, b) MAKESTR(a*256+b)
-
- asm(" .section .version\n"
- "optiboot_version: .word " MAKEVER(OPTIBOOT_MAJVER, OPTIBOOT_MINVER) "\n"
- " .section .text\n");
-
- #include <inttypes.h>
- #include <avr/io.h>
- #include <avr/pgmspace.h>
-
-
-
- #include "boot.h"
-
-
-
-
- #include "pin_defs.h"
- #include "stk500.h"
-
- #ifndef LED_START_FLASHES
- #define LED_START_FLASHES 0
- #endif
-
- #ifdef LUDICROUS_SPEED
- #define BAUD_RATE 230400L
- #endif
-
-
- #ifndef BAUD_RATE
- #if F_CPU >= 8000000L
- #define BAUD_RATE 115200L
- #elsif F_CPU >= 1000000L
- #define BAUD_RATE 9600L
- #elsif F_CPU >= 128000L
- #define BAUD_RATE 4800L
- #else
- #define BAUD_RATE 1200L
- #endif
- #endif
-
- #if 0
-
-
- #if (F_CPU/BAUD_RATE) > 280
- #ifndef SOFT_UART
- #define SOFT_UART
- #endif
- #endif
- #else
- #if (F_CPU + BAUD_RATE * 4L) / (BAUD_RATE * 8L) - 1 > 250
- #error Unachievable baud rate (too slow) BAUD_RATE
- #endif
- #if (F_CPU + BAUD_RATE * 4L) / (BAUD_RATE * 8L) - 1 < 3
- #error Unachievable baud rate (too fast) BAUD_RATE
- #endif
- #endif
-
-
- #define WATCHDOG_OFF (0)
- #define WATCHDOG_16MS (_BV(WDE))
- #define WATCHDOG_32MS (_BV(WDP0) | _BV(WDE))
- #define WATCHDOG_64MS (_BV(WDP1) | _BV(WDE))
- #define WATCHDOG_125MS (_BV(WDP1) | _BV(WDP0) | _BV(WDE))
- #define WATCHDOG_250MS (_BV(WDP2) | _BV(WDE))
- #define WATCHDOG_500MS (_BV(WDP2) | _BV(WDP0) | _BV(WDE))
- #define WATCHDOG_1S (_BV(WDP2) | _BV(WDP1) | _BV(WDE))
- #define WATCHDOG_2S (_BV(WDP2) | _BV(WDP1) | _BV(WDP0) | _BV(WDE))
- #ifndef __AVR_ATmega8__
- #define WATCHDOG_4S (_BV(WDP3) | _BV(WDE))
- #define WATCHDOG_8S (_BV(WDP3) | _BV(WDP0) | _BV(WDE))
- #endif
-
-
-
-
-
- int main(void) __attribute__ ((OS_main)) __attribute__ ((section (".init9")));
- void putch(char);
- uint8_t getch(void);
- static inline void getNch(uint8_t);
- void verifySpace();
- static inline void flash_led(uint8_t);
- uint8_t getLen();
- static inline void watchdogReset();
- void watchdogConfig(uint8_t x);
- #ifdef SOFT_UART
- void uartDelay() __attribute__ ((naked));
- #endif
- void appStart() __attribute__ ((naked));
-
-
- #if defined(__AVR_ATmega168__)
- #define RAMSTART (0x100)
- #define NRWWSTART (0x3800)
- #elif defined(__AVR_ATmega328P__)
- #define RAMSTART (0x100)
- #define NRWWSTART (0x7000)
- #elif defined (__AVR_ATmega644P__)
- #define RAMSTART (0x100)
- #define NRWWSTART (0xE000)
- #elif defined (__AVR_ATmega1284P__)
- #define RAMSTART (0x100)
- #define NRWWSTART (0xE000)
- #elif defined(__AVR_ATtiny84__)
- #define RAMSTART (0x100)
- #define NRWWSTART (0x0000)
- #elif defined(__AVR_ATmega1280__)
- #define RAMSTART (0x200)
- #define NRWWSTART (0xE000)
- #elif defined(__AVR_ATmega8__) || defined(__AVR_ATmega88__)
- #define RAMSTART (0x100)
- #define NRWWSTART (0x1800)
- #endif
-
-
-
-
- #define buff ((uint8_t*)(RAMSTART))
- #ifdef VIRTUAL_BOOT_PARTITION
- #define rstVect (*(uint16_t*)(RAMSTART+SPM_PAGESIZE*2+4))
- #define wdtVect (*(uint16_t*)(RAMSTART+SPM_PAGESIZE*2+6))
- #endif
-
-
- int main(void) {
- uint8_t ch;
-
-
-
- register uint16_t address = 0;
- register uint8_t length;
-
-
-
-
-
-
-
-
-
-
- asm volatile ("clr __zero_reg__");
- #ifdef __AVR_ATmega8__
- SP=RAMEND;
- #endif
-
-
- ch = MCUSR;
- MCUSR = 0;
- if (!(ch & _BV(EXTRF))) appStart();
-
- #if LED_START_FLASHES > 0
-
- TCCR1B = _BV(CS12) | _BV(CS10);
- #endif
- #ifndef SOFT_UART
- #ifdef __AVR_ATmega8__
- UCSRA = _BV(U2X);
- UCSRB = _BV(RXEN) | _BV(TXEN);
- UCSRC = _BV(URSEL) | _BV(UCSZ1) | _BV(UCSZ0);
- UBRRL = (uint8_t)( (F_CPU + BAUD_RATE * 4L) / (BAUD_RATE * 8L) - 1 );
- #else
- UCSR0A = _BV(U2X0);
- UCSR0B = _BV(RXEN0) | _BV(TXEN0);
- UCSR0C = _BV(UCSZ00) | _BV(UCSZ01);
- UBRR0L = (uint8_t)( (F_CPU + BAUD_RATE * 4L) / (BAUD_RATE * 8L) - 1 );
- #endif
- #endif
-
-
- watchdogConfig(WATCHDOG_1S);
-
-
- LED_DDR |= _BV(LED);
-
- #ifdef SOFT_UART
-
- UART_DDR |= _BV(UART_TX_BIT);
- #endif
-
- #if LED_START_FLASHES > 0
-
- flash_led(LED_START_FLASHES * 2);
- #endif
-
-
- for (;;) {
-
- ch = getch();
-
- if(ch == STK_GET_PARAMETER) {
- unsigned char which = getch();
- verifySpace();
- if (which == 0x82) {
-
-
- putch(OPTIBOOT_MINVER);
- } else if (which == 0x81) {
- putch(OPTIBOOT_MAJVER);
- } else {
-
-
- putch(0x03);
- }
- }
- else if(ch == STK_SET_DEVICE) {
-
- getNch(20);
- }
- else if(ch == STK_SET_DEVICE_EXT) {
-
- getNch(5);
- }
- else if(ch == STK_LOAD_ADDRESS) {
-
- uint16_t newAddress;
- newAddress = getch();
- newAddress = (newAddress & 0xff) | (getch() << 8);
- #ifdef RAMPZ
-
- RAMPZ = (newAddress & 0x8000) ? 1 : 0;
- #endif
- newAddress += newAddress;
- address = newAddress;
- verifySpace();
- }
- else if(ch == STK_UNIVERSAL) {
-
- getNch(4);
- putch(0x00);
- }
-
- else if(ch == STK_PROG_PAGE) {
-
- uint8_t *bufPtr;
- uint16_t addrPtr;
-
- getch();
- length = getch();
- getch();
-
-
- if (address < NRWWSTART) __boot_page_erase_short((uint16_t)(void*)address);
-
-
- bufPtr = buff;
- do *bufPtr++ = getch();
- while (--length);
-
-
-
- if (address >= NRWWSTART) __boot_page_erase_short((uint16_t)(void*)address);
-
-
- verifySpace();
-
-
-
- boot_spm_busy_wait();
-
- #ifdef VIRTUAL_BOOT_PARTITION
- if ((uint16_t)(void*)address == 0) {
-
-
-
-
- uint16_t vect = buff[0] | (buff[1]<<8);
- rstVect = vect;
- wdtVect = buff[8] | (buff[9]<<8);
- vect -= 4;
- buff[8] = vect & 0xff;
- buff[9] = vect >> 8;
-
-
- buff[0] = 0x7f;
- buff[1] = 0xce;
- }
- #endif
-
-
- bufPtr = buff;
- addrPtr = (uint16_t)(void*)address;
- ch = SPM_PAGESIZE / 2;
- do {
- uint16_t a;
- a = *bufPtr++;
- a |= (*bufPtr++) << 8;
- __boot_page_fill_short((uint16_t)(void*)addrPtr,a);
- addrPtr += 2;
- } while (--ch);
-
-
- __boot_page_write_short((uint16_t)(void*)address);
- boot_spm_busy_wait();
-
- #if defined(RWWSRE)
-
- boot_rww_enable();
- #endif
-
- }
-
- else if(ch == STK_READ_PAGE) {
-
- getch();
- length = getch();
- getch();
-
- verifySpace();
- #ifdef VIRTUAL_BOOT_PARTITION
- do {
-
- if (address == 0) ch=rstVect & 0xff;
- else if (address == 1) ch=rstVect >> 8;
- else if (address == 8) ch=wdtVect & 0xff;
- else if (address == 9) ch=wdtVect >> 8;
- else ch = pgm_read_byte_near(address);
- address++;
- putch(ch);
- } while (--length);
- #else
- #ifdef RAMPZ
-
-
-
- do {
- uint8_t result;
- __asm__ ("elpm %0,Z\n":"=r"(result):"z"(address));
- putch(result);
- address++;
- }
- while (--length);
- #else
- do putch(pgm_read_byte_near(address++));
- while (--length);
- #endif
- #endif
- }
-
-
- else if(ch == STK_READ_SIGN) {
-
- verifySpace();
- putch(SIGNATURE_0);
- putch(SIGNATURE_1);
- putch(SIGNATURE_2);
- }
- else if (ch == STK_LEAVE_PROGMODE) {
-
- watchdogConfig(WATCHDOG_16MS);
- verifySpace();
- }
- else {
-
- verifySpace();
- }
- putch(STK_OK);
- }
- }
-
- void putch(char ch) {
- #ifndef SOFT_UART
- while (!(UCSR0A & _BV(UDRE0)));
- UDR0 = ch;
- #else
- __asm__ __volatile__ (
- " com %[ch]\n"
- " sec\n"
- "1: brcc 2f\n"
- " cbi %[uartPort],%[uartBit]\n"
- " rjmp 3f\n"
- "2: sbi %[uartPort],%[uartBit]\n"
- " nop\n"
- "3: rcall uartDelay\n"
- " rcall uartDelay\n"
- " lsr %[ch]\n"
- " dec %[bitcnt]\n"
- " brne 1b\n"
- :
- :
- [bitcnt] "d" (10),
- [ch] "r" (ch),
- [uartPort] "I" (_SFR_IO_ADDR(UART_PORT)),
- [uartBit] "I" (UART_TX_BIT)
- :
- "r25"
- );
- #endif
- }
-
- uint8_t getch(void) {
- uint8_t ch;
-
- #ifdef LED_DATA_FLASH
- #ifdef __AVR_ATmega8__
- LED_PORT ^= _BV(LED);
- #else
- LED_PIN |= _BV(LED);
- #endif
- #endif
-
- #ifdef SOFT_UART
- __asm__ __volatile__ (
- "1: sbic %[uartPin],%[uartBit]\n"
- " rjmp 1b\n"
- " rcall uartDelay\n"
- "2: rcall uartDelay\n"
- " rcall uartDelay\n"
- " clc\n"
- " sbic %[uartPin],%[uartBit]\n"
- " sec\n"
- " dec %[bitCnt]\n"
- " breq 3f\n"
- " ror %[ch]\n"
- " rjmp 2b\n"
- "3:\n"
- :
- [ch] "=r" (ch)
- :
- [bitCnt] "d" (9),
- [uartPin] "I" (_SFR_IO_ADDR(UART_PIN)),
- [uartBit] "I" (UART_RX_BIT)
- :
- "r25"
- );
- #else
- while(!(UCSR0A & _BV(RXC0)))
- ;
- if (!(UCSR0A & _BV(FE0))) {
-
-
- watchdogReset();
- }
-
- ch = UDR0;
- #endif
-
- #ifdef LED_DATA_FLASH
- #ifdef __AVR_ATmega8__
- LED_PORT ^= _BV(LED);
- #else
- LED_PIN |= _BV(LED);
- #endif
- #endif
-
- return ch;
- }
-
- #ifdef SOFT_UART
-
-
- #define UART_B_VALUE (((F_CPU/BAUD_RATE)-20)/6)
- #if UART_B_VALUE > 255
- #error Baud rate too slow for soft UART
- #endif
-
- void uartDelay() {
- __asm__ __volatile__ (
- "ldi r25,%[count]\n"
- "1:dec r25\n"
- "brne 1b\n"
- "ret\n"
- ::[count] "M" (UART_B_VALUE)
- );
- }
- #endif
-
- void getNch(uint8_t count) {
- do getch(); while (--count);
- verifySpace();
- }
-
- void verifySpace() {
- if (getch() != CRC_EOP) {
- watchdogConfig(WATCHDOG_16MS);
- while (1)
- ;
- }
- putch(STK_INSYNC);
- }
-
- #if LED_START_FLASHES > 0
- void flash_led(uint8_t count) {
- do {
- TCNT1 = -(F_CPU/(1024*16));
- TIFR1 = _BV(TOV1);
- while(!(TIFR1 & _BV(TOV1)));
- #ifdef __AVR_ATmega8__
- LED_PORT ^= _BV(LED);
- #else
- LED_PIN |= _BV(LED);
- #endif
- watchdogReset();
- } while (--count);
- }
- #endif
-
-
- void watchdogReset() {
- __asm__ __volatile__ (
- "wdr\n"
- );
- }
-
- void watchdogConfig(uint8_t x) {
- WDTCSR = _BV(WDCE) | _BV(WDE);
- WDTCSR = x;
- }
-
- void appStart() {
- watchdogConfig(WATCHDOG_OFF);
- __asm__ __volatile__ (
- #ifdef VIRTUAL_BOOT_PARTITION
-
- "ldi r30,4\n"
- "clr r31\n"
- #else
-
- "clr r30\n"
- "clr r31\n"
- #endif
- "ijmp\n"
- );
- }
|