/***************************************************************************** * * AVRPROG compatible boot-loader * Version : 0.5 (7. Apr. 2004) * Compiler : avr-gcc 3.3.1 / avr-libc 1.0 * size : depends on features and startup ( minmal features < 512 words) * by : Martin Thomas, Kaiserslautern, Germany * eversmith@heizung-thomas.de * * License : none. free code and free to use and modify * BUT: Please send me bug-reports if you find out that * something has been done wrong. You may mention where * you've got the source in the documention of your * project if you're using this bootloader. * * - based on the Butterfly Bootloader-Code * Copyright (C) 1996-1998 Atmel Corporation * Author(s) : BBrandal, PKastnes, ARodland, LHM * The orignal code has been made available by ATMEL together with the * Butterfly application code. Since ATMEL.NO had no problem with * the application gcc-port they hopefully will not have any concerns about * publishing this port. A lot of things have been change but the ATMEL * "skeleton" is still in this code. Make sure to keep the copyright notice * in derived work to avoid trouble. * * - based on boot.h from the avr-libc (c) Eric Weddington * **************************************************************************** * * Many functions used by "AVRPROG" (fuses) have been disabled by ATMEL in * the original source code of the Butterfly Boot-loader not by me. * I will try to make all of them available in later versions but i.e the * ATmega169 is not completly supported by AVRPROG 1.37. * * The boot interrupt vector is included (this bootloader is completly in * ".text" section). If you need this space for further functions you have to * add a separate section for the bootloader-functions and add an attribute * for this section to _all_ function prototypes of functions in the loader. * With this the interrupt vector will be placed at .0000 and the bootloader * code (without interrupt vector) at the adress you define in the linker * options for the newly created section. See the avr-libc FAQ, the avr- * libc's avr/boot.h documentation and the makefile for further details. * * See the makefile for information how to adopt the linker-settings to * the selected Boot Size (_Bxxx below) * * With BOOT_SIMPLE this bootloader has 0x3DE bytes size and should fit * into a 512word bootloader-section. * ****************************************************************************/ /* Does not work so far: - fuse high byte read (or parse in AVRPROG?) - lock bits set */ // programmers-notepad tabsize 4 #define VERSION_HIGH '0' #define VERSION_LOW '5' #include #include #include #include /* Port and Pin in this port that has to grounded (active low) to start the bootloader */ #define BLPORT PORTC #define BLPIN PINC #define BLPNUM PINC0 /* Select startup-mode * SIMPLE-Mode - Jump to bootloader main BL-loop if key is pressed (Pin grounded) "during" reset or jump to the application if the pin is not grounded (=pulled up by internal pull-up-resistor) * POWERSAVE-Mode - Startup is separated in two loops which makes power-saving a little easier if no firmware is on the chip. Needs more memory * BOOTICE-Mode - to flash the JTAGICE upgrade.ebn file. No startup-sequence in this mode. Jump directly to the parser-loop on reset XTAL in BOOTICEMODE must be 7372800 Hz to be compatible with the org. JTAGICE-Firmware */ #define START_SIMPLE //#define START_POWERSAVE //#define START_BOOTICE #ifndef START_BOOTICE #define XTAL 3686400 #else #warning "BOOTICE mode - External Crystal/Oszillator must be 7,3728 MHz" #define XTAL 7372800 #endif // UART handling - some definitions from P. Fleury's Library - thanks #define BAUDRATE 19200 #include "uart.h" /* enable/disable readout of fuse and lock-bits (will not work for Mega169 since not supported by AVRPROG 1.37 */ // #define ENABLEREADFUSELOCK /* enable/disable write of lock-bits WARNING: lock-bits can not be reseted by bootloader (as far as I know) Only protection no unprotection, "chip erase" from bootloader only clears the flash but does no real "chip erase" (this is not possible with a bootloader as far as I know) */ // #define WRITELOCKBITS /* Select Boot Size (select one, comment out the others) */ // NO! #define _B128 // NO! #define _B256 // MAYBE: #define _B512 #define _B1024 //#define _B2048 #include "chipdef.h" // this uses the latest version from avr-libc CVS not the one that comes // with WINAVR Sep./03 (6.Apr.04: it's like the boot.h from the Apr/04 // WINAVR but the lock-bit-mask has been changed. Found out during the // development of a STK500-plugin-compatible-bootloader) #include "myboot.h" // functions not found in boot.h #include "lowlevel.h" #define UART_RX_BUFFER_SIZE SPM_PAGESIZE unsigned char gBuffer[UART_RX_BUFFER_SIZE]; #define eeprom_is_ready() bit_is_clear(EECR, EEWE) #define my_eeprom_busy_wait() do{}while(!eeprom_is_ready()) unsigned char BufferLoad(unsigned int , unsigned char ) ; void BlockRead(unsigned int , unsigned char ) ; unsigned short address; unsigned char device; void send_boot(void) { sendchar('A'); sendchar('V'); sendchar('R'); sendchar('B'); sendchar('O'); sendchar('O'); sendchar('T'); } void (*jump_to_app)(void) = 0x0000; int main(void) { unsigned short tempi; char val; #ifdef START_POWERSAVE char OK = 1; #endif MCUCR = (1<> 8) & 0xFF); // Report buffer size in bytes sendchar(UART_RX_BUFFER_SIZE & 0xFF); } else if(val=='B') // Start buffer load { tempi = recchar() << 8; // Load high byte of buffersize tempi |= recchar(); // Load low byte of buffersize val = recchar(); // Load memory type ('E' or 'F') sendchar (BufferLoad(tempi,val)); // Start downloading of buffer } else if(val == 'g') // Block read { tempi = (recchar() << 8) | recchar(); val = recchar(); // Get memtype BlockRead(tempi,val); // Perform the block read } /* else if(val=='c') //Write program memory, low byte { ldata=recchar(); sendchar('\r'); } else if(val== 'C') //Write program memory, high byte { data=ldata|(recchar()<<8); if (device == devtype) { fill_temp_buffer(data,(address)); //call asm routine. } address=address+2; sendchar('\r'); } */ else if(val=='e') //Chip erase { if (device == devtype) { // erase only main section (bootloader protection) for(address=0;address < APP_END;address += SPM_PAGESIZE) //Application section = 60 pages { //write_page(address,(1<>8)); //send MSB sendchar((char)intval); //send LSB address=address+2; } */ /* else if (val == 'D') // write EEPROM { if (device == devtype) { EEARL = address; EEARH = (address >> 8); address++; EEDR = recchar(); EECR |= (1<> 8); address++; EECR |= (1<>=1; do { EEARL = address; // Setup EEPROM address EEARH = (address >> 8); address++; // Select next byte // EEDR = recchar(); // Load data to write EEDR=gBuffer[cnt++]; EECR |= (1<>=1; // not needed here - hmm, somehow inconsistant TODO do { EEARL = address; // Setup EEPROM address EEARH = (address >> 8); address++; // Select next EEPROM byte EECR |= (1<> 8)); //send MSB address += 2; // Select next word in memory size -= 2; // Subtract two bytes from number of bytes to read } while (size); // Repeat until all block has been read } // Re-enable interrupts (if they were ever enabled). /*SREG=sreg;*/ }