2006-05-01 19:19:59 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* AVRPROG compatible boot-loader
|
2009-02-05 15:32:15 +01:00
|
|
|
* Version : 0.83 (Apr. 2008)
|
|
|
|
* Compiler : avr-gcc 4.1.2 / avr-libc 1.4.6
|
2006-05-01 19:19:59 +02:00
|
|
|
* size : depends on features and startup ( minmal features < 512 words)
|
|
|
|
* by : Martin Thomas, Kaiserslautern, Germany
|
|
|
|
* eversmith@heizung-thomas.de
|
2006-05-22 14:17:47 +02:00
|
|
|
* Additional code and improvements contributed by:
|
|
|
|
* - Uwe Bonnes
|
|
|
|
* - Bjoern Riemer
|
|
|
|
* - Olaf Rempel
|
2006-05-01 19:19:59 +02:00
|
|
|
*
|
2009-02-05 15:32:15 +01:00
|
|
|
* License : Copyright (c) 2006-2008 M. Thomas, U. Bonnes, O. Rempel
|
2006-05-19 21:00:23 +02:00
|
|
|
* Free to use. You have to mention the copyright
|
|
|
|
* owners in source-code and documentation of derived
|
2009-02-05 15:32:15 +01:00
|
|
|
* work. No warranty! (Yes, you can insert the BSD
|
|
|
|
* license here)
|
2006-05-01 19:20:40 +02:00
|
|
|
*
|
2009-02-05 15:32:15 +01:00
|
|
|
* Tested with ATmega8, ATmega16, ATmega162, ATmega32, ATmega324P,
|
|
|
|
* ATmega644, ATmega644P, ATmega128, AT90CAN128
|
2006-05-01 19:19:59 +02:00
|
|
|
*
|
2006-05-22 14:17:47 +02:00
|
|
|
* - Initial versions have been based on the Butterfly bootloader-code
|
|
|
|
* by Atmel Corporation (Authors: BBrandal, PKastnes, ARodland, LHM)
|
2006-05-01 19:19:59 +02:00
|
|
|
*
|
|
|
|
****************************************************************************
|
|
|
|
*
|
2009-02-05 15:32:15 +01:00
|
|
|
* See the makefile and readme.txt for information on how to adapt
|
|
|
|
* the linker-settings to the selected Boot Size (BOOTSIZE=xxxx) and
|
|
|
|
* the MCU-type. Other configurations futher down in this file.
|
2006-05-01 19:19:59 +02:00
|
|
|
*
|
2006-05-26 20:19:10 +02:00
|
|
|
* With BOOT_SIMPLE, minimal features and discarded int-vectors
|
2009-02-05 15:32:15 +01:00
|
|
|
* this bootloader has should fit into a a 512 word (1024, 0x400 bytes)
|
|
|
|
* bootloader-section.
|
2006-05-01 19:19:59 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
/*
|
2006-05-31 14:30:05 +02:00
|
|
|
TODOs:
|
|
|
|
- check lock-bits set
|
|
|
|
- __bad_interrupt still linked even with modified
|
|
|
|
linker-scripts which needs a default-handler,
|
|
|
|
"wasted": 3 words for AVR5 (>8kB), 2 words for AVR4
|
2009-02-05 15:32:15 +01:00
|
|
|
- Check watchdog-disable-function in avr-libc.
|
2006-05-01 19:19:59 +02:00
|
|
|
*/
|
2006-05-31 14:30:05 +02:00
|
|
|
// tabsize: 4
|
2006-05-01 19:19:59 +02:00
|
|
|
|
2006-05-19 22:19:18 +02:00
|
|
|
/* MCU frequency */
|
2006-05-31 14:30:05 +02:00
|
|
|
#ifndef F_CPU
|
|
|
|
// #define F_CPU 7372800
|
|
|
|
#define F_CPU (7372800/2)
|
|
|
|
#endif
|
2006-05-19 22:19:18 +02:00
|
|
|
|
|
|
|
/* UART Baudrate */
|
2006-05-31 14:30:05 +02:00
|
|
|
// #define BAUDRATE 9600
|
|
|
|
// #define BAUDRATE 19200
|
2006-05-19 22:19:18 +02:00
|
|
|
#define BAUDRATE 115200
|
|
|
|
|
|
|
|
/* use "Double Speed Operation" */
|
|
|
|
//#define UART_DOUBLESPEED
|
|
|
|
|
2009-02-05 15:32:15 +01:00
|
|
|
/* use second UART on mega128 / can128 / mega162 / mega324p / mega644p */
|
2006-05-19 22:19:18 +02:00
|
|
|
//#define UART_USE_SECOND
|
|
|
|
|
2006-05-31 14:30:05 +02:00
|
|
|
/* Device-Type:
|
|
|
|
For AVRProg the BOOT-option is prefered
|
|
|
|
which is the "correct" value for a bootloader.
|
|
|
|
avrdude may only detect the part-code for ISP */
|
|
|
|
#define DEVTYPE DEVTYPE_BOOT
|
2009-02-05 15:32:15 +01:00
|
|
|
// #define DEVTYPE DEVTYPE_ISP
|
2006-05-31 14:30:05 +02:00
|
|
|
|
2006-05-20 00:30:03 +02:00
|
|
|
/*
|
|
|
|
* Pin "STARTPIN" on port "STARTPORT" in this port has to grounded
|
|
|
|
* (active low) to start the bootloader
|
|
|
|
*/
|
2009-02-05 15:32:15 +01:00
|
|
|
#define BLPORT PORTC
|
|
|
|
#define BLDDR DDRC
|
|
|
|
#define BLPIN PINC
|
|
|
|
#define BLPNUM PINC7
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Define if Watchdog-Timer should be disable at startup
|
|
|
|
*/
|
|
|
|
#define DISABLE_WDT_AT_STARTUP
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Watchdog-reset is issued at exit
|
|
|
|
* define the timeout-value here (see avr-libc manual)
|
|
|
|
*/
|
|
|
|
#define EXIT_WDT_TIME WDTO_250MS
|
2006-05-20 00:30:03 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Select startup-mode
|
|
|
|
* SIMPLE-Mode - Jump to bootloader main BL-loop if key is
|
|
|
|
* pressed (Pin grounded) "during" reset or jump to the
|
2006-05-31 14:30:05 +02:00
|
|
|
* application if the pin is not grounded. The internal
|
|
|
|
* pull-up resistor is enabled during the startup and
|
|
|
|
* gets disabled before the application is started.
|
2006-05-20 00:30:03 +02:00
|
|
|
* 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
|
|
|
|
* F_CPU in BOOTICEMODE must be 7372800 Hz to be compatible
|
|
|
|
* with the org. JTAGICE-Firmware
|
2006-05-31 14:30:05 +02:00
|
|
|
* WAIT-mode waits 1 sec for the defined character if nothing
|
|
|
|
* is recived then the user prog is started.
|
2006-05-20 00:30:03 +02:00
|
|
|
*/
|
2006-05-26 20:19:10 +02:00
|
|
|
#define START_SIMPLE
|
|
|
|
//#define START_WAIT
|
2006-05-20 00:30:03 +02:00
|
|
|
//#define START_POWERSAVE
|
|
|
|
//#define START_BOOTICE
|
|
|
|
|
2006-05-31 14:30:05 +02:00
|
|
|
/* character to start the bootloader in mode START_WAIT */
|
|
|
|
#define START_WAIT_UARTCHAR 'S'
|
|
|
|
|
2009-02-05 15:32:15 +01:00
|
|
|
/* wait-time for START_WAIT mode ( t = WAIT_TIME * 10ms ) */
|
|
|
|
#define WAIT_VALUE 100 /* here: 100*10ms = 1000ms = 1sec */
|
2006-05-26 19:54:29 +02:00
|
|
|
|
2006-05-20 00:30:03 +02:00
|
|
|
/*
|
|
|
|
* enable/disable readout of fuse and lock-bits
|
2006-05-31 14:30:05 +02:00
|
|
|
* (AVRPROG has to detect the AVR correctly by device-code
|
|
|
|
* to show the correct information).
|
2006-05-20 00:30:03 +02:00
|
|
|
*/
|
|
|
|
//#define ENABLEREADFUSELOCK
|
|
|
|
|
2009-02-05 15:32:15 +01:00
|
|
|
/* enable/disable write of lock-bits
|
2006-05-20 00:30:03 +02:00
|
|
|
* 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)
|
|
|
|
* Keep this undefined!
|
|
|
|
*/
|
|
|
|
//#define WRITELOCKBITS
|
|
|
|
|
2009-02-05 15:32:15 +01:00
|
|
|
/*
|
|
|
|
* define the following if the bootloader should not output
|
|
|
|
* itself at flash read (will fake an empty boot-section)
|
|
|
|
*/
|
|
|
|
#define READ_PROTECT_BOOTLOADER
|
|
|
|
|
|
|
|
|
2006-05-31 14:30:05 +02:00
|
|
|
#define VERSION_HIGH '0'
|
|
|
|
#define VERSION_LOW '8'
|
|
|
|
|
|
|
|
#define GET_LOCK_BITS 0x0001
|
|
|
|
#define GET_LOW_FUSE_BITS 0x0000
|
|
|
|
#define GET_HIGH_FUSE_BITS 0x0003
|
|
|
|
#define GET_EXTENDED_FUSE_BITS 0x0002
|
|
|
|
|
|
|
|
|
2006-05-20 00:04:32 +02:00
|
|
|
#include <stdint.h>
|
2006-05-01 19:19:59 +02:00
|
|
|
#include <avr/io.h>
|
|
|
|
#include <avr/wdt.h>
|
|
|
|
#include <avr/boot.h>
|
|
|
|
#include <avr/pgmspace.h>
|
2009-02-05 15:32:15 +01:00
|
|
|
#include <avr/eeprom.h>
|
|
|
|
#include <avr/interrupt.h>
|
|
|
|
#include <util/delay.h>
|
2006-05-01 19:19:59 +02:00
|
|
|
|
|
|
|
#include "chipdef.h"
|
|
|
|
|
2006-05-20 00:06:27 +02:00
|
|
|
uint8_t gBuffer[SPM_PAGESIZE];
|
2006-05-01 19:19:59 +02:00
|
|
|
|
2006-05-31 14:30:05 +02:00
|
|
|
#if defined(BOOTLOADERHASNOVECTORS)
|
|
|
|
#warning "This Bootloader does not link interrupt vectors - see makefile"
|
|
|
|
/* make the linker happy - it wants to see __vector_default */
|
|
|
|
// void __vector_default(void) { ; }
|
|
|
|
void __vector_default(void) { ; }
|
|
|
|
#endif
|
|
|
|
|
2006-05-26 19:31:18 +02:00
|
|
|
static void sendchar(uint8_t data)
|
2006-05-19 22:19:18 +02:00
|
|
|
{
|
2006-05-26 19:29:55 +02:00
|
|
|
while (!(UART_STATUS & (1<<UART_TXREADY)));
|
2006-05-19 22:19:18 +02:00
|
|
|
UART_DATA = data;
|
|
|
|
}
|
|
|
|
|
2006-05-26 19:31:18 +02:00
|
|
|
static uint8_t recvchar(void)
|
2006-05-19 22:19:18 +02:00
|
|
|
{
|
2006-05-26 19:29:55 +02:00
|
|
|
while (!(UART_STATUS & (1<<UART_RXREADY)));
|
2006-05-19 22:19:18 +02:00
|
|
|
return UART_DATA;
|
|
|
|
}
|
|
|
|
|
2006-05-20 00:06:27 +02:00
|
|
|
static inline void eraseFlash(void)
|
2006-05-19 21:52:00 +02:00
|
|
|
{
|
2006-05-20 00:06:27 +02:00
|
|
|
// erase only main section (bootloader protection)
|
|
|
|
uint32_t addr = 0;
|
|
|
|
while (APP_END > addr) {
|
|
|
|
boot_page_erase(addr); // Perform page erase
|
|
|
|
boot_spm_busy_wait(); // Wait until the memory is erased.
|
|
|
|
addr += SPM_PAGESIZE;
|
|
|
|
}
|
|
|
|
boot_rww_enable();
|
|
|
|
}
|
2006-05-19 21:52:00 +02:00
|
|
|
|
2006-05-20 00:06:27 +02:00
|
|
|
static inline void recvBuffer(pagebuf_t size)
|
|
|
|
{
|
|
|
|
pagebuf_t cnt;
|
2006-05-26 19:33:50 +02:00
|
|
|
uint8_t *tmp = gBuffer;
|
|
|
|
|
2009-02-05 15:32:15 +01:00
|
|
|
for (cnt = 0; cnt < sizeof(gBuffer); cnt++) {
|
2006-05-26 19:33:50 +02:00
|
|
|
*tmp++ = (cnt < size) ? recvchar() : 0xFF;
|
2009-02-05 15:32:15 +01:00
|
|
|
}
|
2006-05-19 21:52:00 +02:00
|
|
|
}
|
|
|
|
|
2006-05-20 00:06:27 +02:00
|
|
|
static inline uint16_t writeFlashPage(uint16_t waddr, pagebuf_t size)
|
|
|
|
{
|
2006-05-26 19:26:19 +02:00
|
|
|
uint32_t pagestart = (uint32_t)waddr<<1;
|
2006-05-20 00:06:27 +02:00
|
|
|
uint32_t baddr = pagestart;
|
|
|
|
uint16_t data;
|
2006-05-26 19:33:50 +02:00
|
|
|
uint8_t *tmp = gBuffer;
|
2006-05-20 00:06:27 +02:00
|
|
|
|
|
|
|
do {
|
2006-05-26 19:33:50 +02:00
|
|
|
data = *tmp++;
|
|
|
|
data |= *tmp++ << 8;
|
2006-05-20 00:06:27 +02:00
|
|
|
boot_page_fill(baddr, data); // call asm routine.
|
|
|
|
|
|
|
|
baddr += 2; // Select next word in memory
|
|
|
|
size -= 2; // Reduce number of bytes to write by two
|
|
|
|
} while (size); // Loop until all bytes written
|
|
|
|
|
|
|
|
boot_page_write(pagestart);
|
|
|
|
boot_spm_busy_wait();
|
2009-02-05 15:32:15 +01:00
|
|
|
boot_rww_enable(); // Re-enable the RWW section
|
2006-05-20 00:06:27 +02:00
|
|
|
|
|
|
|
return baddr>>1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint16_t writeEEpromPage(uint16_t address, pagebuf_t size)
|
|
|
|
{
|
2006-05-26 19:33:50 +02:00
|
|
|
uint8_t *tmp = gBuffer;
|
2006-05-20 00:06:27 +02:00
|
|
|
|
|
|
|
do {
|
2009-02-05 15:32:15 +01:00
|
|
|
eeprom_write_byte( (uint8_t*)address, *tmp++ );
|
2006-05-20 00:06:27 +02:00
|
|
|
address++; // Select next byte
|
|
|
|
size--; // Decreas number of bytes to write
|
|
|
|
} while (size); // Loop until all bytes written
|
|
|
|
|
2009-02-05 15:32:15 +01:00
|
|
|
// eeprom_busy_wait();
|
|
|
|
|
2006-05-20 00:06:27 +02:00
|
|
|
return address;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint16_t readFlashPage(uint16_t waddr, pagebuf_t size)
|
2006-05-19 21:52:00 +02:00
|
|
|
{
|
2006-05-26 19:26:19 +02:00
|
|
|
uint32_t baddr = (uint32_t)waddr<<1;
|
2006-05-20 00:04:32 +02:00
|
|
|
uint16_t data;
|
2006-05-19 21:52:00 +02:00
|
|
|
|
2006-05-20 00:06:27 +02:00
|
|
|
do {
|
2009-02-05 15:32:15 +01:00
|
|
|
#ifndef READ_PROTECT_BOOTLOADER
|
|
|
|
#warning "Bootloader not read-protected"
|
2006-05-19 21:52:00 +02:00
|
|
|
#if defined(RAMPZ)
|
2006-05-20 00:06:27 +02:00
|
|
|
data = pgm_read_word_far(baddr);
|
2006-05-19 21:52:00 +02:00
|
|
|
#else
|
2006-05-20 00:06:27 +02:00
|
|
|
data = pgm_read_word_near(baddr);
|
2009-02-05 15:32:15 +01:00
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
// don't read bootloader
|
|
|
|
if ( baddr < APP_END ) {
|
|
|
|
#if defined(RAMPZ)
|
|
|
|
data = pgm_read_word_far(baddr);
|
|
|
|
#else
|
|
|
|
data = pgm_read_word_near(baddr);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
data = 0xFFFF; // fake empty
|
|
|
|
}
|
2006-05-19 21:52:00 +02:00
|
|
|
#endif
|
2006-05-20 00:06:27 +02:00
|
|
|
sendchar(data); // send LSB
|
|
|
|
sendchar((data >> 8)); // send MSB
|
|
|
|
baddr += 2; // Select next word in memory
|
|
|
|
size -= 2; // Subtract two bytes from number of bytes to read
|
2009-02-05 15:32:15 +01:00
|
|
|
} while (size); // Repeat until block has been read
|
2006-05-20 00:06:27 +02:00
|
|
|
|
|
|
|
return baddr>>1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint16_t readEEpromPage(uint16_t address, pagebuf_t size)
|
|
|
|
{
|
|
|
|
do {
|
2009-02-05 15:32:15 +01:00
|
|
|
sendchar( eeprom_read_byte( (uint8_t*)address ) );
|
|
|
|
address++;
|
2006-05-20 00:06:27 +02:00
|
|
|
size--; // Decrease number of bytes to read
|
2009-02-05 15:32:15 +01:00
|
|
|
} while (size); // Repeat until block has been read
|
2006-05-20 00:06:27 +02:00
|
|
|
|
|
|
|
return address;
|
2006-05-19 21:52:00 +02:00
|
|
|
}
|
|
|
|
|
2006-05-31 14:30:05 +02:00
|
|
|
#if defined(ENABLEREADFUSELOCK)
|
2006-05-26 19:37:18 +02:00
|
|
|
static uint8_t read_fuse_lock(uint16_t addr)
|
2006-05-19 21:52:00 +02:00
|
|
|
{
|
2006-05-26 19:37:18 +02:00
|
|
|
uint8_t mode = (1<<BLBSET) | (1<<SPMEN);
|
2006-05-20 00:04:32 +02:00
|
|
|
uint8_t retval;
|
2006-05-19 21:52:00 +02:00
|
|
|
|
|
|
|
asm volatile
|
|
|
|
(
|
2006-05-26 19:37:18 +02:00
|
|
|
"movw r30, %3\n\t" /* Z to addr */ \
|
|
|
|
"sts %0, %2\n\t" /* set mode in SPM_REG */ \
|
|
|
|
"lpm\n\t" /* load fuse/lock value into r0 */ \
|
|
|
|
"mov %1,r0\n\t" /* save return value */ \
|
2006-05-19 21:52:00 +02:00
|
|
|
: "=m" (SPM_REG),
|
|
|
|
"=r" (retval)
|
|
|
|
: "r" (mode),
|
|
|
|
"r" (addr)
|
|
|
|
: "r30", "r31", "r0"
|
|
|
|
);
|
|
|
|
return retval;
|
|
|
|
}
|
2006-05-31 14:30:05 +02:00
|
|
|
#endif
|
2006-05-19 21:52:00 +02:00
|
|
|
|
2006-05-26 19:31:18 +02:00
|
|
|
static void send_boot(void)
|
2006-05-01 19:19:59 +02:00
|
|
|
{
|
|
|
|
sendchar('A');
|
|
|
|
sendchar('V');
|
|
|
|
sendchar('R');
|
|
|
|
sendchar('B');
|
|
|
|
sendchar('O');
|
|
|
|
sendchar('O');
|
|
|
|
sendchar('T');
|
|
|
|
}
|
|
|
|
|
2006-05-26 19:31:18 +02:00
|
|
|
static void (*jump_to_app)(void) = 0x0000;
|
2006-05-01 19:19:59 +02:00
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
2006-05-20 00:06:27 +02:00
|
|
|
uint16_t address = 0;
|
|
|
|
uint8_t device = 0, val;
|
2006-05-19 21:00:23 +02:00
|
|
|
|
2009-02-05 15:32:15 +01:00
|
|
|
#ifdef DISABLE_WDT_AT_STARTUP
|
|
|
|
#ifdef WDT_OFF_SPECIAL
|
|
|
|
#warning "using target specific watchdog_off"
|
|
|
|
bootloader_wdt_off();
|
|
|
|
#else
|
|
|
|
cli();
|
|
|
|
wdt_reset();
|
|
|
|
wdt_disable();
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2006-05-19 21:52:00 +02:00
|
|
|
#ifdef START_POWERSAVE
|
2006-05-20 00:04:32 +02:00
|
|
|
uint8_t OK = 1;
|
2006-05-19 21:52:00 +02:00
|
|
|
#endif
|
2006-05-19 21:00:23 +02:00
|
|
|
|
2006-05-19 21:52:00 +02:00
|
|
|
BLDDR &= ~(1<<BLPNUM); // set as Input
|
2006-05-01 19:19:59 +02:00
|
|
|
BLPORT |= (1<<BLPNUM); // Enable pullup
|
2006-05-19 21:00:23 +02:00
|
|
|
|
2006-05-19 22:19:18 +02:00
|
|
|
// Set baud rate
|
|
|
|
UART_BAUD_HIGH = (UART_CALC_BAUDRATE(BAUDRATE)>>8) & 0xFF;
|
|
|
|
UART_BAUD_LOW = (UART_CALC_BAUDRATE(BAUDRATE) & 0xFF);
|
|
|
|
|
|
|
|
#ifdef UART_DOUBLESPEED
|
2009-02-05 15:32:15 +01:00
|
|
|
UART_STATUS = ( 1<<UART_DOUBLE );
|
2006-05-19 22:19:18 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
UART_CTRL = UART_CTRL_DATA;
|
|
|
|
UART_CTRL2 = UART_CTRL2_DATA;
|
2009-02-05 15:32:15 +01:00
|
|
|
|
2006-05-31 14:30:05 +02:00
|
|
|
#if defined(START_POWERSAVE)
|
2006-05-19 21:00:23 +02:00
|
|
|
/*
|
2006-05-01 19:19:59 +02:00
|
|
|
This is an adoption of the Butterfly Bootloader startup-sequence.
|
|
|
|
It may look a little strange but separating the login-loop from
|
|
|
|
the main parser-loop gives a lot a possibilities (timeout, sleep-modes
|
|
|
|
etc.).
|
2006-05-19 21:00:23 +02:00
|
|
|
*/
|
2006-05-19 21:52:00 +02:00
|
|
|
for(;OK;) {
|
|
|
|
if ((BLPIN & (1<<BLPNUM))) {
|
2006-05-01 19:19:59 +02:00
|
|
|
// jump to main app if pin is not grounded
|
2006-05-19 21:52:00 +02:00
|
|
|
BLPORT &= ~(1<<BLPNUM); // set to default
|
|
|
|
jump_to_app(); // Jump to application sector
|
2006-05-19 21:00:23 +02:00
|
|
|
|
2006-05-19 21:52:00 +02:00
|
|
|
} else {
|
2006-05-26 19:26:19 +02:00
|
|
|
val = recvchar();
|
2006-05-19 21:52:00 +02:00
|
|
|
/* ESC */
|
|
|
|
if (val == 0x1B) {
|
|
|
|
// AVRPROG connection
|
|
|
|
// Wait for signon
|
|
|
|
while (val != 'S')
|
2006-05-26 19:26:19 +02:00
|
|
|
val = recvchar();
|
2006-05-19 21:52:00 +02:00
|
|
|
|
|
|
|
send_boot(); // Report signon
|
2006-05-01 19:19:59 +02:00
|
|
|
OK = 0;
|
2006-05-19 21:52:00 +02:00
|
|
|
|
|
|
|
} else {
|
2006-05-01 19:19:59 +02:00
|
|
|
sendchar('?');
|
2006-05-19 21:52:00 +02:00
|
|
|
}
|
2006-05-01 19:19:59 +02:00
|
|
|
}
|
|
|
|
// Power-Save code here
|
|
|
|
}
|
2006-05-19 21:00:23 +02:00
|
|
|
|
2006-05-19 21:52:00 +02:00
|
|
|
#elif defined(START_SIMPLE)
|
2006-05-19 21:00:23 +02:00
|
|
|
|
2006-05-19 21:52:00 +02:00
|
|
|
if ((BLPIN & (1<<BLPNUM))) {
|
|
|
|
// jump to main app if pin is not grounded
|
|
|
|
BLPORT &= ~(1<<BLPNUM); // set to default
|
|
|
|
jump_to_app(); // Jump to application sector
|
2006-05-01 19:19:59 +02:00
|
|
|
}
|
2006-05-01 19:20:40 +02:00
|
|
|
|
2006-05-19 21:52:00 +02:00
|
|
|
#elif defined(START_WAIT)
|
2006-05-19 21:00:23 +02:00
|
|
|
|
2006-05-26 19:54:29 +02:00
|
|
|
uint16_t cnt = 0;
|
2006-05-19 21:00:23 +02:00
|
|
|
|
2006-05-19 21:52:00 +02:00
|
|
|
while (1) {
|
2006-05-26 19:54:29 +02:00
|
|
|
if (UART_STATUS & (1<<UART_RXREADY))
|
2006-05-31 14:30:05 +02:00
|
|
|
if (UART_DATA == START_WAIT_UARTCHAR)
|
2006-05-01 19:20:40 +02:00
|
|
|
break;
|
2006-05-26 19:54:29 +02:00
|
|
|
|
|
|
|
if (cnt++ >= WAIT_VALUE) {
|
2006-05-19 21:52:00 +02:00
|
|
|
BLPORT &= ~(1<<BLPNUM); // set to default
|
|
|
|
jump_to_app(); // Jump to application sector
|
2006-05-01 19:20:40 +02:00
|
|
|
}
|
2006-05-26 19:54:29 +02:00
|
|
|
|
|
|
|
_delay_ms(10);
|
2006-05-01 19:20:40 +02:00
|
|
|
}
|
|
|
|
send_boot();
|
2006-05-19 21:00:23 +02:00
|
|
|
|
2006-05-19 21:52:00 +02:00
|
|
|
#elif defined(START_BOOTICE)
|
|
|
|
#warning "BOOTICE mode - no startup-condition"
|
2006-05-01 19:19:59 +02:00
|
|
|
|
2006-05-19 21:52:00 +02:00
|
|
|
#else
|
|
|
|
#error "Select START_ condition for bootloader in main.c"
|
|
|
|
#endif
|
2006-05-19 21:00:23 +02:00
|
|
|
|
2006-05-19 21:52:00 +02:00
|
|
|
for(;;) {
|
2006-05-19 22:19:18 +02:00
|
|
|
val = recvchar();
|
2006-05-19 21:52:00 +02:00
|
|
|
// Autoincrement?
|
|
|
|
if (val == 'a') {
|
|
|
|
sendchar('Y'); // Autoincrement is quicker
|
2006-05-19 21:00:23 +02:00
|
|
|
|
2006-05-19 21:52:00 +02:00
|
|
|
//write address
|
2009-02-05 15:32:15 +01:00
|
|
|
} else if (val == 'A') {
|
2006-05-19 22:19:18 +02:00
|
|
|
address = recvchar(); //read address 8 MSB
|
|
|
|
address = (address<<8) | recvchar();
|
2006-05-01 19:19:59 +02:00
|
|
|
sendchar('\r');
|
2006-05-19 21:00:23 +02:00
|
|
|
|
2006-05-19 21:52:00 +02:00
|
|
|
// Buffer load support
|
|
|
|
} else if (val == 'b') {
|
2006-05-01 19:19:59 +02:00
|
|
|
sendchar('Y'); // Report buffer load supported
|
2006-05-20 00:06:27 +02:00
|
|
|
sendchar((sizeof(gBuffer) >> 8) & 0xFF); // Report buffer size in bytes
|
|
|
|
sendchar(sizeof(gBuffer) & 0xFF);
|
2006-05-01 19:19:59 +02:00
|
|
|
|
2006-05-19 21:52:00 +02:00
|
|
|
// Start buffer load
|
|
|
|
} else if (val == 'B') {
|
2006-05-20 00:06:27 +02:00
|
|
|
pagebuf_t size;
|
2006-05-20 00:04:32 +02:00
|
|
|
size = recvchar() << 8; // Load high byte of buffersize
|
|
|
|
size |= recvchar(); // Load low byte of buffersize
|
2006-05-19 22:19:18 +02:00
|
|
|
val = recvchar(); // Load memory type ('E' or 'F')
|
2006-05-20 00:06:27 +02:00
|
|
|
recvBuffer(size);
|
|
|
|
|
|
|
|
if (device == DEVTYPE) {
|
|
|
|
if (val == 'F') {
|
|
|
|
address = writeFlashPage(address, size);
|
|
|
|
} else if (val == 'E') {
|
|
|
|
address = writeEEpromPage(address, size);
|
|
|
|
}
|
|
|
|
sendchar('\r');
|
|
|
|
} else {
|
|
|
|
sendchar(0);
|
|
|
|
}
|
2006-05-19 21:00:23 +02:00
|
|
|
|
2006-05-19 21:52:00 +02:00
|
|
|
// Block read
|
|
|
|
} else if (val == 'g') {
|
2006-05-20 00:06:27 +02:00
|
|
|
pagebuf_t size;
|
2006-05-20 00:04:32 +02:00
|
|
|
size = recvchar() << 8; // Load high byte of buffersize
|
|
|
|
size |= recvchar(); // Load low byte of buffersize
|
2006-05-19 22:19:18 +02:00
|
|
|
val = recvchar(); // Get memtype
|
2006-05-20 00:06:27 +02:00
|
|
|
|
|
|
|
if (val == 'F') {
|
|
|
|
address = readFlashPage(address, size);
|
|
|
|
} else if (val == 'E') {
|
|
|
|
address = readEEpromPage(address, size);
|
|
|
|
}
|
2006-05-01 19:19:59 +02:00
|
|
|
|
2006-05-19 21:52:00 +02:00
|
|
|
// Chip erase
|
|
|
|
} else if (val == 'e') {
|
2009-02-05 15:32:15 +01:00
|
|
|
if (device == DEVTYPE) {
|
2006-05-20 00:06:27 +02:00
|
|
|
eraseFlash();
|
2009-02-05 15:32:15 +01:00
|
|
|
}
|
2006-05-19 21:00:23 +02:00
|
|
|
sendchar('\r');
|
2006-05-01 19:19:59 +02:00
|
|
|
|
2006-05-19 21:52:00 +02:00
|
|
|
// Exit upgrade
|
|
|
|
} else if (val == 'E') {
|
2009-02-05 15:32:15 +01:00
|
|
|
wdt_enable(EXIT_WDT_TIME); // Enable Watchdog Timer to give reset
|
2006-05-01 19:19:59 +02:00
|
|
|
sendchar('\r');
|
2006-05-19 21:52:00 +02:00
|
|
|
|
|
|
|
#ifdef WRITELOCKBITS
|
|
|
|
#warning "Extension 'WriteLockBits' enabled"
|
|
|
|
// TODO: does not work reliably
|
|
|
|
// write lockbits
|
|
|
|
} else if (val == 'l') {
|
2006-05-19 21:52:42 +02:00
|
|
|
if (device == DEVTYPE) {
|
2006-05-26 19:26:19 +02:00
|
|
|
// write_lock_bits(recvchar());
|
|
|
|
boot_lock_bits_set(recvchar()); // boot.h takes care of mask
|
2006-05-01 19:19:59 +02:00
|
|
|
boot_spm_busy_wait();
|
|
|
|
}
|
|
|
|
sendchar('\r');
|
2006-05-19 21:52:00 +02:00
|
|
|
#endif
|
|
|
|
// Enter programming mode
|
|
|
|
} else if (val == 'P') {
|
2006-05-01 19:19:59 +02:00
|
|
|
sendchar('\r');
|
2006-05-19 21:00:23 +02:00
|
|
|
|
2006-05-19 21:52:00 +02:00
|
|
|
// Leave programming mode
|
|
|
|
} else if (val == 'L') {
|
2006-05-01 19:19:59 +02:00
|
|
|
sendchar('\r');
|
2006-05-19 21:00:23 +02:00
|
|
|
|
2006-05-19 21:52:00 +02:00
|
|
|
// return programmer type
|
|
|
|
} else if (val == 'p') {
|
|
|
|
sendchar('S'); // always serial programmer
|
2006-05-19 21:00:23 +02:00
|
|
|
|
2006-05-01 19:19:59 +02:00
|
|
|
#ifdef ENABLEREADFUSELOCK
|
|
|
|
#warning "Extension 'ReadFuseLock' enabled"
|
2006-05-19 21:52:00 +02:00
|
|
|
// read "low" fuse bits
|
|
|
|
} else if (val == 'F') {
|
2006-05-26 19:37:18 +02:00
|
|
|
sendchar(read_fuse_lock(GET_LOW_FUSE_BITS));
|
2006-05-19 21:52:00 +02:00
|
|
|
|
|
|
|
// read lock bits
|
|
|
|
} else if (val == 'r') {
|
2006-05-26 19:37:18 +02:00
|
|
|
sendchar(read_fuse_lock(GET_LOCK_BITS));
|
2006-05-19 21:52:00 +02:00
|
|
|
|
|
|
|
// read high fuse bits
|
|
|
|
} else if (val == 'N') {
|
2006-05-26 19:37:18 +02:00
|
|
|
sendchar(read_fuse_lock(GET_HIGH_FUSE_BITS));
|
2006-05-19 21:52:00 +02:00
|
|
|
|
|
|
|
// read extended fuse bits
|
|
|
|
} else if (val == 'Q') {
|
2006-05-26 19:37:18 +02:00
|
|
|
sendchar(read_fuse_lock(GET_EXTENDED_FUSE_BITS));
|
2006-05-19 21:00:23 +02:00
|
|
|
#endif
|
2006-05-01 19:19:59 +02:00
|
|
|
|
2006-05-19 21:52:00 +02:00
|
|
|
// Return device type
|
|
|
|
} else if (val == 't') {
|
2006-05-19 21:52:42 +02:00
|
|
|
sendchar(DEVTYPE);
|
2006-05-01 19:19:59 +02:00
|
|
|
sendchar(0);
|
|
|
|
|
2006-05-19 21:52:00 +02:00
|
|
|
// clear and set LED ignored
|
2009-02-05 15:32:15 +01:00
|
|
|
} else if ((val == 'x') || (val == 'y')) {
|
2006-05-19 22:19:18 +02:00
|
|
|
recvchar();
|
2006-05-01 19:19:59 +02:00
|
|
|
sendchar('\r');
|
2006-05-19 21:00:23 +02:00
|
|
|
|
2006-05-19 21:52:00 +02:00
|
|
|
// set device
|
|
|
|
} else if (val == 'T') {
|
2006-05-19 22:19:18 +02:00
|
|
|
device = recvchar();
|
2006-05-01 19:19:59 +02:00
|
|
|
sendchar('\r');
|
2006-05-19 21:00:23 +02:00
|
|
|
|
2006-05-19 21:52:00 +02:00
|
|
|
// Return software identifier
|
|
|
|
} else if (val == 'S') {
|
2006-05-01 19:19:59 +02:00
|
|
|
send_boot();
|
2006-05-19 21:00:23 +02:00
|
|
|
|
2006-05-19 21:52:00 +02:00
|
|
|
// Return Software Version
|
|
|
|
} else if (val == 'V') {
|
2006-05-01 19:19:59 +02:00
|
|
|
sendchar(VERSION_HIGH);
|
|
|
|
sendchar(VERSION_LOW);
|
|
|
|
|
2006-05-31 14:30:05 +02:00
|
|
|
// Return Signature Bytes (it seems that
|
|
|
|
// AVRProg expects the "Atmel-byte" 0x1E last
|
|
|
|
// but shows it first in the dialog-window)
|
2006-05-19 21:52:00 +02:00
|
|
|
} else if (val == 's') {
|
2006-05-19 21:52:42 +02:00
|
|
|
sendchar(SIG_BYTE3);
|
2006-05-31 14:30:05 +02:00
|
|
|
sendchar(SIG_BYTE2);
|
|
|
|
sendchar(SIG_BYTE1);
|
2006-05-01 19:19:59 +02:00
|
|
|
|
2006-05-19 21:52:00 +02:00
|
|
|
/* ESC */
|
|
|
|
} else if(val != 0x1b) {
|
2006-05-01 19:19:59 +02:00
|
|
|
sendchar('?');
|
|
|
|
}
|
|
|
|
}
|
2006-05-19 21:52:00 +02:00
|
|
|
return 0;
|
2006-05-01 19:19:59 +02:00
|
|
|
}
|