2006-05-01 19:19:59 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* AVRPROG compatible boot-loader
|
2006-05-01 19:20:40 +02:00
|
|
|
* Version : 0.75 (Feb. 2006)
|
2006-05-19 21:00:23 +02:00
|
|
|
* Compiler : avr-gcc 3.4.1 / avr-libc 1.0.2
|
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
|
|
|
|
*
|
|
|
|
* License : Copyright (c) 2005 Martin Thomas
|
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
|
2006-05-01 19:19:59 +02:00
|
|
|
* work. No warranty.
|
|
|
|
*
|
2006-05-01 19:20:40 +02:00
|
|
|
* Additional code and improvements contributed by:
|
|
|
|
* - Uwe Bonnes
|
|
|
|
* - Bjoern Riemer
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Tested with ATmega8, ATmega16, ATmega32, ATmega128, AT90CAN128
|
2006-05-01 19:19:59 +02:00
|
|
|
*
|
|
|
|
* - based on the Butterfly Bootloader-Code
|
|
|
|
* Copyright (C) 1996-1998 Atmel Corporation
|
|
|
|
* Author(s) : BBrandal, PKastnes, ARodland, LHM
|
2006-05-19 21:00:23 +02:00
|
|
|
* The orignal code has been made available by ATMEL together with the
|
|
|
|
* Butterfly application code. Since ATMEL.NO had no problem with
|
2006-05-01 19:19:59 +02:00
|
|
|
* 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
|
2006-05-19 21:00:23 +02:00
|
|
|
* "skeleton" is still in this code. Make sure to keep the copyright notice
|
2006-05-01 19:19:59 +02:00
|
|
|
* in derived work to avoid trouble.
|
|
|
|
*
|
|
|
|
* - based on boot.h from the avr-libc (c) Eric Weddington
|
|
|
|
*
|
|
|
|
****************************************************************************
|
|
|
|
*
|
|
|
|
* 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
|
2006-05-19 21:00:23 +02:00
|
|
|
* 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
|
2006-05-01 19:19:59 +02:00
|
|
|
* 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.
|
|
|
|
*
|
2006-05-19 21:00:23 +02:00
|
|
|
* See the makefile for information how to adopt the linker-settings to
|
2006-05-01 19:19:59 +02:00
|
|
|
* the selected Boot Size (_Bxxx below)
|
|
|
|
*
|
|
|
|
* With BOOT_SIMPLE this bootloader has 0x3DE bytes size and should fit
|
|
|
|
* into a 512word bootloader-section.
|
|
|
|
*
|
2006-05-19 21:00:23 +02:00
|
|
|
* Set AVR clock-frequency and the baudrate below, set MCU-type in
|
2006-05-01 19:19:59 +02:00
|
|
|
* makefile.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
/*
|
|
|
|
Does not work reliably so far:
|
|
|
|
- lock bits set
|
|
|
|
*/
|
|
|
|
|
|
|
|
// programmers-notepad tabsize 4
|
|
|
|
#define VERSION_HIGH '0'
|
|
|
|
#define VERSION_LOW '7'
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <avr/io.h>
|
|
|
|
#include <avr/interrupt.h>
|
|
|
|
#include <avr/wdt.h>
|
|
|
|
#include <avr/boot.h>
|
|
|
|
#include <avr/pgmspace.h>
|
|
|
|
|
|
|
|
#ifndef XTAL
|
|
|
|
#warning "Set XTAL in Makefile"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef START_BOOTICE
|
|
|
|
#warning "Using START_BOOTICE"
|
|
|
|
#if XTAL == 7372800
|
|
|
|
#else
|
|
|
|
#warning "BOOTICE mode - External Crystal/Oszillator must be 7,3728 MHz"
|
|
|
|
#undef XTAL
|
|
|
|
#define XTAL 7372800
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// UART handling - some definitions from P. Fleury's Library - thanks
|
|
|
|
#include "uart.h"
|
|
|
|
|
2006-05-19 21:00:23 +02:00
|
|
|
/* enable/disable readout of fuse and lock-bits
|
2006-05-01 19:19:59 +02:00
|
|
|
(will not work for Mega169 since not supported by AVRPROG 1.37 */
|
2006-05-19 21:00:23 +02:00
|
|
|
#define ENABLEREADFUSELOCK
|
2006-05-01 19:19:59 +02:00
|
|
|
|
2006-05-19 21:00:23 +02:00
|
|
|
/* enable/disable write of lock-bits
|
2006-05-01 19:19:59 +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
|
|
|
|
|
|
|
|
#include "chipdef.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())
|
|
|
|
|
|
|
|
uint32_t address;
|
|
|
|
unsigned char device;
|
|
|
|
|
2006-05-19 21:52:00 +02:00
|
|
|
unsigned char BufferLoad(unsigned int size, unsigned char mem)
|
|
|
|
{
|
|
|
|
unsigned int data, cnt;
|
|
|
|
uint32_t tempaddress;
|
|
|
|
|
|
|
|
for (cnt = 0; cnt < UART_RX_BUFFER_SIZE; cnt++) {
|
|
|
|
if (cnt < size)
|
|
|
|
gBuffer[cnt] = recchar();
|
|
|
|
else
|
|
|
|
gBuffer[cnt] = 0xFF;
|
|
|
|
}
|
|
|
|
cnt = 0;
|
|
|
|
|
|
|
|
tempaddress = address; // Store address in page
|
|
|
|
|
|
|
|
my_eeprom_busy_wait();
|
|
|
|
|
2006-05-19 21:52:42 +02:00
|
|
|
if (device == DEVTYPE) {
|
2006-05-19 21:52:00 +02:00
|
|
|
// Flash
|
|
|
|
if (mem == 'F') {
|
|
|
|
do {
|
|
|
|
data = gBuffer[cnt++];
|
|
|
|
data |= (gBuffer[cnt++] << 8);
|
|
|
|
boot_page_fill(address, data); // call asm routine.
|
|
|
|
|
|
|
|
address = address +2; // Select next word in memory
|
|
|
|
size -= 2; // Reduce number of bytes to write by two
|
|
|
|
} while(size); // Loop until all bytes written
|
|
|
|
|
|
|
|
/* commented out since not compatible with mega8 -
|
|
|
|
secondary benefit: saves memory
|
|
|
|
tempaddress &= 0xFF80; // Ensure the address points to the first byte in the page
|
|
|
|
*/
|
|
|
|
|
|
|
|
boot_page_write(tempaddress);
|
|
|
|
boot_spm_busy_wait();
|
|
|
|
boot_rww_enable(); // Re-enable the RWW section
|
|
|
|
|
|
|
|
/* commented out since not compatible with mega8
|
|
|
|
if (address != (address & 0xFF80))
|
|
|
|
{ // Ensure that the address points to the beginning of the next page
|
|
|
|
address &= 0xFF80;
|
|
|
|
address += SPM_PAGESIZE;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
} // End FLASH
|
|
|
|
|
|
|
|
// Start EEPROM
|
|
|
|
if (mem == 'E') {
|
|
|
|
address >>= 1;
|
|
|
|
do {
|
|
|
|
EEARL = address; // Setup EEPROM address
|
|
|
|
EEARH = (address >> 8);
|
|
|
|
address++; // Select next byte
|
|
|
|
EEDR = gBuffer[cnt++];
|
|
|
|
|
|
|
|
EECR |= (1<<EEMWE); // Write data into EEPROM
|
|
|
|
EECR |= (1<<EEWE);
|
|
|
|
|
|
|
|
while (EECR & (1<<EEWE)); // Wait for EEPROM write to finish
|
|
|
|
size--; // Decreas number of bytes to write
|
|
|
|
} while(size); // Loop until all bytes written
|
|
|
|
}
|
|
|
|
return '\r'; // Report programming OK
|
|
|
|
}
|
|
|
|
return 0; // Report programming failed
|
|
|
|
}
|
|
|
|
|
|
|
|
void BlockRead(unsigned int size, unsigned char mem)
|
|
|
|
{
|
|
|
|
unsigned int data;
|
|
|
|
|
|
|
|
my_eeprom_busy_wait();
|
|
|
|
|
|
|
|
// Read EEPROM
|
|
|
|
if (mem == 'E') {
|
|
|
|
// address>>=1; // not needed here - hmm, somehow inconsistant TODO
|
|
|
|
do {
|
|
|
|
EEARL = address; // Setup EEPROM address
|
|
|
|
EEARH = (address >> 8);
|
|
|
|
address++; // Select next EEPROM byte
|
|
|
|
EECR |= (1<<EERE); // Read EEPROM
|
|
|
|
sendchar(EEDR); // Transmit EEPROM data to PC
|
|
|
|
size--; // Decrease number of bytes to read
|
|
|
|
} while (size); // Repeat until all block has been read
|
|
|
|
// Read Flash
|
|
|
|
} else {
|
|
|
|
do {
|
|
|
|
#if defined(RAMPZ)
|
|
|
|
data = pgm_read_word_far(address);
|
|
|
|
#else
|
|
|
|
data = pgm_read_word_near((uint16_t)address);
|
|
|
|
#endif
|
|
|
|
sendchar((unsigned char)data); //send LSB
|
|
|
|
sendchar((unsigned char)(data >> 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char read_fuse_lock(unsigned short addr, unsigned char mode)
|
|
|
|
{
|
|
|
|
unsigned char retval;
|
|
|
|
|
|
|
|
asm volatile
|
|
|
|
(
|
|
|
|
"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 */ \
|
|
|
|
: "=m" (SPM_REG),
|
|
|
|
"=r" (retval)
|
|
|
|
: "r" (mode),
|
|
|
|
"r" (addr)
|
|
|
|
: "r30", "r31", "r0"
|
|
|
|
);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2006-05-01 19:19:59 +02:00
|
|
|
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 tempi;
|
|
|
|
char val;
|
2006-05-19 21:00:23 +02:00
|
|
|
|
2006-05-19 21:52:00 +02:00
|
|
|
#ifdef START_POWERSAVE
|
2006-05-19 21:00:23 +02:00
|
|
|
char OK = 1;
|
2006-05-19 21:52:00 +02:00
|
|
|
#endif
|
2006-05-19 21:00:23 +02:00
|
|
|
|
2006-05-01 19:19:59 +02:00
|
|
|
cli();
|
2006-05-19 21:00:23 +02:00
|
|
|
|
2006-05-19 21:52:00 +02:00
|
|
|
MCUCR = (1<<IVCE); // move interruptvectors to the Boot sector
|
|
|
|
MCUCR = (1<<IVSEL); // device specific !
|
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 21:52:00 +02:00
|
|
|
USART_Init(UART_BAUD_SELECT(BAUDRATE,XTAL),UARTSINGLE); // single speed
|
|
|
|
// USART_Init(UART_BAUD_SELECT(BAUDRATE/2,XTAL),UARTDOUBLE); // double speed
|
2006-05-19 21:00:23 +02:00
|
|
|
|
2006-05-19 21:52:00 +02:00
|
|
|
#ifdef 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
|
|
|
|
MCUCR = (1<<IVCE); // move interruptvectors to the Application sector
|
|
|
|
MCUCR = (0<<IVSEL); // device specific !
|
|
|
|
jump_to_app(); // Jump to application sector
|
2006-05-19 21:00:23 +02:00
|
|
|
|
2006-05-19 21:52:00 +02:00
|
|
|
} else {
|
|
|
|
val = recchar();
|
|
|
|
/* ESC */
|
|
|
|
if (val == 0x1B) {
|
|
|
|
// AVRPROG connection
|
|
|
|
// Wait for signon
|
|
|
|
while (val != 'S')
|
2006-05-01 19:19:59 +02:00
|
|
|
val = recchar();
|
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
|
2006-05-19 21:00:23 +02:00
|
|
|
MCUCR = (1<<IVCE);
|
2006-05-19 21:52:00 +02:00
|
|
|
MCUCR = (0<<IVSEL); //move interruptvectors to the Application sector
|
|
|
|
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
|
|
|
|
|
|
|
// Timer-Setup for ATmega8
|
2006-05-01 19:20:40 +02:00
|
|
|
// - verify that the configuration is valid for the target AVR
|
2006-05-19 21:00:23 +02:00
|
|
|
|
2006-05-01 19:20:40 +02:00
|
|
|
#define F_OSC XTAL
|
2006-05-19 21:00:23 +02:00
|
|
|
#define UART0_STATUS UCSRA
|
2006-05-01 19:20:40 +02:00
|
|
|
#define UART0_DATA UDR
|
|
|
|
#define MY_WAIT 900
|
|
|
|
// wait ca 1 sec (900ms)
|
|
|
|
TCCR1A = 0; // timer setup
|
|
|
|
// F_OSC / 8 / 1000 -> 1ms
|
|
|
|
#if (((F_OSC / 8 / 1000)*MY_WAIT) < 65535)
|
|
|
|
#warning Information: setting prescaler to 8
|
|
|
|
#define WAIT_VALUE ((F_OSC / 8 / 1000)*MY_WAIT)
|
2006-05-19 21:00:23 +02:00
|
|
|
TCCR1B |= _BV(CS01);
|
2006-05-01 19:20:40 +02:00
|
|
|
#elif ((((F_OSC / 64 / 1000)*MY_WAIT) < 65535))
|
|
|
|
#warning Information: setting prescaler to 64
|
|
|
|
#define WAIT_VALUE ((F_OSC / 64 / 1000)*MY_WAIT)
|
2006-05-19 21:00:23 +02:00
|
|
|
TCCR1B |= _BV(CS01)| _BV(CS00);
|
2006-05-01 19:20:40 +02:00
|
|
|
#elif ((((F_OSC / 256 / 1000)*MY_WAIT) < 65535))
|
|
|
|
#warning Information: setting prescaler to 256
|
|
|
|
#define WAIT_VALUE ((F_OSC / 256 / 1000)*MY_WAIT)
|
2006-05-19 21:00:23 +02:00
|
|
|
TCCR1B |= _BV(CS02);
|
2006-05-01 19:20:40 +02:00
|
|
|
#else //((((F_OSC / 1024 / 1000)*MY_WAIT) < 65535))
|
|
|
|
#warning Information: setting prescaler to 1024
|
|
|
|
#define WAIT_VALUE ((F_OSC / 1024 / 1000)*MY_WAIT)
|
|
|
|
TCCR1B |= _BV(CS00) |_BV(CS02); //1024 prescaler
|
|
|
|
#endif
|
2006-05-19 21:00:23 +02:00
|
|
|
|
2006-05-19 21:52:00 +02:00
|
|
|
while (1) {
|
|
|
|
if (UART0_STATUS & (1<<RXC)) {
|
2006-05-01 19:20:40 +02:00
|
|
|
if (UART0_DATA == 'S')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (TCNT1 >= WAIT_VALUE){
|
2006-05-19 21:52:00 +02:00
|
|
|
BLPORT &= ~(1<<BLPNUM); // set to default
|
2006-05-19 21:00:23 +02:00
|
|
|
MCUCR = (1<<IVCE);
|
2006-05-19 21:52:00 +02:00
|
|
|
MCUCR = (0<<IVSEL); // move interruptvectors to the Application sector
|
|
|
|
TCCR1B = 0; // timer off
|
|
|
|
jump_to_app(); // Jump to application sector
|
2006-05-01 19:20:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
TCCR1B = 0; // timer off
|
|
|
|
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(;;) {
|
|
|
|
val = recchar();
|
|
|
|
// 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
|
|
|
|
} else if (val == 'A') {
|
|
|
|
address = recchar(); //read address 8 MSB
|
|
|
|
address = (address<<8) | recchar();
|
2006-05-19 21:00:23 +02:00
|
|
|
|
2006-05-19 21:52:00 +02:00
|
|
|
address = address<<1; // !! convert from word address to byte address
|
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-19 21:52:00 +02:00
|
|
|
sendchar((UART_RX_BUFFER_SIZE >> 8) & 0xFF); // Report buffer size in bytes
|
2006-05-01 19:19:59 +02:00
|
|
|
sendchar(UART_RX_BUFFER_SIZE & 0xFF);
|
|
|
|
|
2006-05-19 21:52:00 +02:00
|
|
|
// Start buffer load
|
|
|
|
} else if (val == 'B') {
|
|
|
|
tempi = recchar() << 8; // Load high byte of buffersize
|
2006-05-01 19:19:59 +02:00
|
|
|
tempi |= recchar(); // Load low byte of buffersize
|
|
|
|
val = recchar(); // Load memory type ('E' or 'F')
|
2006-05-19 21:52:00 +02:00
|
|
|
sendchar (BufferLoad(tempi, val)); // Start downloading of buffer
|
2006-05-19 21:00:23 +02:00
|
|
|
|
2006-05-19 21:52:00 +02:00
|
|
|
// Block read
|
|
|
|
} else if (val == 'g') {
|
2006-05-01 19:19:59 +02:00
|
|
|
tempi = (recchar() << 8) | recchar();
|
|
|
|
val = recchar(); // Get memtype
|
2006-05-19 21:52:00 +02:00
|
|
|
BlockRead(tempi, val); // Perform the block read
|
2006-05-01 19:19:59 +02:00
|
|
|
|
2006-05-19 21:52:00 +02:00
|
|
|
// Chip erase
|
|
|
|
} else if (val == 'e') {
|
2006-05-19 21:52:42 +02:00
|
|
|
if (device == DEVTYPE) {
|
2006-05-01 19:19:59 +02:00
|
|
|
// erase only main section (bootloader protection)
|
2006-05-19 21:00:23 +02:00
|
|
|
address = 0;
|
2006-05-19 21:52:00 +02:00
|
|
|
while (APP_END > address) {
|
2006-05-01 19:15:27 +02:00
|
|
|
boot_page_erase(address); // Perform page erase
|
2006-05-01 19:19:59 +02:00
|
|
|
boot_spm_busy_wait(); // Wait until the memory is erased.
|
|
|
|
address += SPM_PAGESIZE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
boot_rww_enable();
|
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') {
|
2006-05-01 19:19:59 +02:00
|
|
|
wdt_enable(WDTO_15MS); // Enable Watchdog Timer to give reset
|
|
|
|
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-01 19:19:59 +02:00
|
|
|
// write_lock_bits(recchar());
|
|
|
|
boot_lock_bits_set(recchar()); // boot.h takes care of mask
|
|
|
|
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') {
|
|
|
|
sendchar(read_fuse_lock(0x0000, _BV(BLBSET) | _BV(SPMEN)));
|
|
|
|
|
|
|
|
// read lock bits
|
|
|
|
} else if (val == 'r') {
|
|
|
|
sendchar(read_fuse_lock(0x0001, _BV(BLBSET) | _BV(SPMEN)));
|
|
|
|
|
|
|
|
// read high fuse bits
|
|
|
|
} else if (val == 'N') {
|
|
|
|
sendchar(read_fuse_lock(0x0003, _BV(BLBSET) | _BV(SPMEN)));
|
|
|
|
|
|
|
|
// read extended fuse bits
|
|
|
|
} else if (val == 'Q') {
|
|
|
|
sendchar(read_fuse_lock(0x0002, _BV(BLBSET) | _BV(SPMEN)));
|
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
|
|
|
|
} else if ((val == 'x') || (val == 'y')) {
|
2006-05-01 19:19:59 +02:00
|
|
|
recchar();
|
|
|
|
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-01 19:19:59 +02:00
|
|
|
device = recchar();
|
|
|
|
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-19 21:52:00 +02:00
|
|
|
// Return Signature Byte
|
|
|
|
} else if (val == 's') {
|
2006-05-19 21:52:42 +02:00
|
|
|
sendchar(SIG_BYTE1);
|
|
|
|
sendchar(SIG_BYTE2);
|
|
|
|
sendchar(SIG_BYTE3);
|
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
|
|
|
}
|