mirror of
https://github.com/arduino/Arduino.git
synced 2025-01-18 07:52:14 +01:00
Reduced bootloader to less than 1024 bytes (512 words); high fuse byte now 0xca instead of 0xc8; bootloader segment now starts at (byte 0x1C00 instead of 0x1800; word 0xE00 instead of 0xC00)
This commit is contained in:
parent
11aca24b1f
commit
420dcfe697
@ -1,12 +1,12 @@
|
|||||||
/**********************************************************/
|
/**********************************************************/
|
||||||
/* Serial Bootloader for Atmel megaAVR Controllers */
|
/* Serial Bootloader for Atmel mega8 AVR Controller */
|
||||||
/* */
|
/* */
|
||||||
/* ATmegaBOOT.c */
|
/* ATmegaBOOT.c */
|
||||||
/* */
|
/* */
|
||||||
/* Copyright (c) 2003, Jason P. Kyle */
|
/* Copyright (c) 2003, Jason P. Kyle */
|
||||||
/* All rights reserved. */
|
|
||||||
/* */
|
/* */
|
||||||
/* Hacked by DojoCorp - ZGZ - MMX - IVR */
|
/* Hacked by DojoCorp - ZGZ - MMX - IVR */
|
||||||
|
/* Hacked by David A. Mellis */
|
||||||
/* */
|
/* */
|
||||||
/* This program is free software; you can redistribute it */
|
/* This program is free software; you can redistribute it */
|
||||||
/* and/or modify it under the terms of the GNU General */
|
/* and/or modify it under the terms of the GNU General */
|
||||||
@ -28,12 +28,7 @@
|
|||||||
/* Licence can be viewed at */
|
/* Licence can be viewed at */
|
||||||
/* http://www.fsf.org/licenses/gpl.txt */
|
/* http://www.fsf.org/licenses/gpl.txt */
|
||||||
/* */
|
/* */
|
||||||
/* Target = Atmel AVR m128,m64,m32,m16,m8,m162,m163,m169, */
|
/* Target = Atmel AVR m8 */
|
||||||
/* m8515,m8535. ATmega161 has a very small boot block so */
|
|
||||||
/* isn't supported. */
|
|
||||||
/* */
|
|
||||||
/* Tested with m8 - */
|
|
||||||
/* */
|
|
||||||
/**********************************************************/
|
/**********************************************************/
|
||||||
|
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
@ -41,23 +36,21 @@
|
|||||||
#include <avr/pgmspace.h>
|
#include <avr/pgmspace.h>
|
||||||
#include <avr/eeprom.h>
|
#include <avr/eeprom.h>
|
||||||
#include <avr/interrupt.h>
|
#include <avr/interrupt.h>
|
||||||
|
#include <avr/delay.h>
|
||||||
|
|
||||||
#define F_CPU 16000000
|
//#define F_CPU 16000000
|
||||||
|
|
||||||
/* We, Malmoitians, like slow interaction
|
/* We, Malmoitians, like slow interaction
|
||||||
* therefore the slow baud rate ;-)
|
* therefore the slow baud rate ;-)
|
||||||
*/
|
*/
|
||||||
#define BAUD_RATE 9600
|
//#define BAUD_RATE 9600
|
||||||
|
|
||||||
/* 6.000.000 is more or less 8 seconds at the
|
/* 6.000.000 is more or less 8 seconds at the
|
||||||
* speed configured here
|
* speed configured here
|
||||||
*/
|
*/
|
||||||
#define MAX_TIME_COUNT 6000000
|
//#define MAX_TIME_COUNT 6000000
|
||||||
#define MAX_TIME_COUNT_MORATORY 1600000
|
#define MAX_TIME_COUNT (F_CPU>>1)
|
||||||
|
///#define MAX_TIME_COUNT_MORATORY 1600000
|
||||||
#define DECRYPT 0
|
|
||||||
#define ENCRYPT 1
|
|
||||||
/* #define DES_ENCRYPTION */
|
|
||||||
|
|
||||||
/* SW_MAJOR and MINOR needs to be updated from time to time to avoid warning message from AVR Studio */
|
/* SW_MAJOR and MINOR needs to be updated from time to time to avoid warning message from AVR Studio */
|
||||||
#define HW_VER 0x02
|
#define HW_VER 0x02
|
||||||
@ -68,18 +61,18 @@
|
|||||||
// avr-gcc compiler v3.1.x and older doesn't support outb() and inb()
|
// avr-gcc compiler v3.1.x and older doesn't support outb() and inb()
|
||||||
// if necessary, convert outb and inb to outp and inp
|
// if necessary, convert outb and inb to outp and inp
|
||||||
#ifndef outb
|
#ifndef outb
|
||||||
#define outb(sfr,val) (_SFR_BYTE(sfr) = (val))
|
#define outb(sfr,val) (_SFR_BYTE(sfr) = (val))
|
||||||
#endif
|
#endif
|
||||||
#ifndef inb
|
#ifndef inb
|
||||||
#define inb(sfr) _SFR_BYTE(sfr)
|
#define inb(sfr) _SFR_BYTE(sfr)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* defines for future compatibility */
|
/* defines for future compatibility */
|
||||||
#ifndef cbi
|
#ifndef cbi
|
||||||
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
|
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
|
||||||
#endif
|
#endif
|
||||||
#ifndef sbi
|
#ifndef sbi
|
||||||
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
|
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Adjust to suit whatever pin your hardware uses to enter the bootloader */
|
/* Adjust to suit whatever pin your hardware uses to enter the bootloader */
|
||||||
@ -87,15 +80,6 @@
|
|||||||
#define eeprom_rw(addr) eeprom_read_word ((uint16_t *)(addr))
|
#define eeprom_rw(addr) eeprom_read_word ((uint16_t *)(addr))
|
||||||
#define eeprom_wb(addr, val) eeprom_write_byte ((uint8_t *)(addr), (uint8_t)(val))
|
#define eeprom_wb(addr, val) eeprom_write_byte ((uint8_t *)(addr), (uint8_t)(val))
|
||||||
|
|
||||||
/* other ATmegas have only one UART, so only one pin is defined to enter bootloader */
|
|
||||||
#define BL_DDR DDRD
|
|
||||||
#define BL_PORT PORTD
|
|
||||||
#define BL_PIN PIND
|
|
||||||
#define BL PIND6
|
|
||||||
|
|
||||||
/* define pin for onboard LED */
|
|
||||||
/* depends on product */
|
|
||||||
|
|
||||||
/* Onboard LED is connected to pin PB5 */
|
/* Onboard LED is connected to pin PB5 */
|
||||||
#define LED_DDR DDRB
|
#define LED_DDR DDRB
|
||||||
#define LED_PORT PORTB
|
#define LED_PORT PORTB
|
||||||
@ -131,12 +115,12 @@ struct flags_struct {
|
|||||||
} flags;
|
} flags;
|
||||||
|
|
||||||
uint8_t buff[256];
|
uint8_t buff[256];
|
||||||
uint8_t address_high;
|
//uint8_t address_high;
|
||||||
|
|
||||||
uint8_t pagesz=0x80;
|
uint8_t pagesz=0x80;
|
||||||
|
|
||||||
uint8_t i;
|
uint8_t i;
|
||||||
uint8_t bootuart0=0,bootuart1=0;
|
//uint8_t bootuart0=0,bootuart1=0;
|
||||||
|
|
||||||
|
|
||||||
void (*app_start)(void) = 0x0000;
|
void (*app_start)(void) = 0x0000;
|
||||||
@ -146,23 +130,23 @@ int main(void)
|
|||||||
uint8_t ch,ch2;
|
uint8_t ch,ch2;
|
||||||
uint16_t w;
|
uint16_t w;
|
||||||
|
|
||||||
cbi(BL_DDR,BL);
|
//cbi(BL_DDR,BL);
|
||||||
sbi(BL_PORT,BL);
|
//sbi(BL_PORT,BL);
|
||||||
|
|
||||||
asm volatile("nop\n\t");
|
asm volatile("nop\n\t");
|
||||||
|
|
||||||
/* check if flash is programmed already, if not start bootloader anyway */
|
/* check if flash is programmed already, if not start bootloader anyway */
|
||||||
if(pgm_read_byte_near(0x0000) != 0xFF) {
|
//if(pgm_read_byte_near(0x0000) != 0xFF) {
|
||||||
|
|
||||||
/* check if bootloader pin is set low */
|
/* check if bootloader pin is set low */
|
||||||
//if(bit_is_set(BL_PIN,BL)) app_start();
|
//if(bit_is_set(BL_PIN,BL)) app_start();
|
||||||
}
|
//}
|
||||||
|
|
||||||
/* initialize UART(s) depending on CPU defined */
|
/* initialize UART(s) depending on CPU defined */
|
||||||
/* m8 */
|
/* m8 */
|
||||||
UBRRH = (((F_CPU/BAUD_RATE)/16)-1)>>8; // set baud rate
|
UBRRH = (((F_CPU/BAUD_RATE)/16)-1)>>8; // set baud rate
|
||||||
UBRRL = (((F_CPU/BAUD_RATE)/16)-1);
|
UBRRL = (((F_CPU/BAUD_RATE)/16)-1);
|
||||||
UCSRB =(1<<RXEN)|(1<<TXEN); // enable Rx & Tx
|
UCSRB = (1<<RXEN)|(1<<TXEN); // enable Rx & Tx
|
||||||
UCSRC = (1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0); // config USART; 8N1
|
UCSRC = (1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0); // config USART; 8N1
|
||||||
|
|
||||||
//UBRRL = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
|
//UBRRL = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
|
||||||
@ -176,27 +160,34 @@ int main(void)
|
|||||||
works on with uisp and avrdude on the mac (at least). */
|
works on with uisp and avrdude on the mac (at least). */
|
||||||
//putch('\0');
|
//putch('\0');
|
||||||
|
|
||||||
uint32_t l;
|
//uint32_t l;
|
||||||
uint32_t time_count;
|
//uint32_t time_count;
|
||||||
time_count=0;
|
//time_count=0;
|
||||||
|
|
||||||
/* set LED pin as output */
|
/* set LED pin as output */
|
||||||
sbi(LED_DDR,LED);
|
sbi(LED_DDR,LED);
|
||||||
|
for (i = 0; i < 16; i++) {
|
||||||
|
outb(LED_PORT, inb(LED_PORT) ^ _BV(LED));
|
||||||
|
_delay_loop_2(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
//for (l=0; l<40000000; l++)
|
||||||
|
//outb(LED_PORT, inb(LED_PORT) ^= _BV(LED));
|
||||||
|
|
||||||
/* flash onboard LED three times to signal entering of bootloader */
|
/* flash onboard LED three times to signal entering of bootloader */
|
||||||
for(i=0; i<3; ++i) {
|
//for(i=0; i<3; ++i) {
|
||||||
for(l=0; l<40000000; ++l);
|
//for(l=0; l<40000000; ++l);
|
||||||
sbi(LED_PORT,LED);
|
//sbi(LED_PORT,LED);
|
||||||
for(l=0; l<40000000; ++l);
|
//for(l=0; l<40000000; ++l);
|
||||||
cbi(LED_PORT,LED);
|
//cbi(LED_PORT,LED);
|
||||||
}
|
//}
|
||||||
|
|
||||||
/* see comment at previous call to putch() */
|
/* see comment at previous call to putch() */
|
||||||
//putch('\0'); // this line is needed for the synchronization of the programmer
|
//putch('\0'); // this line is needed for the synchronization of the programmer
|
||||||
|
|
||||||
/* forever */
|
/* forever */
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if((inb(UCSRA) & _BV(RXC))){
|
//if((inb(UCSRA) & _BV(RXC))){
|
||||||
/* get character from UART */
|
/* get character from UART */
|
||||||
ch = getch();
|
ch = getch();
|
||||||
|
|
||||||
@ -207,30 +198,20 @@ int main(void)
|
|||||||
nothing_response();
|
nothing_response();
|
||||||
}
|
}
|
||||||
|
|
||||||
if(ch=='l') {
|
/* Request programmer ID */
|
||||||
if (getch() == 'o') {
|
|
||||||
if (getch() == 'l') {
|
|
||||||
if (getch() == 'a') {
|
|
||||||
app_start();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Request programmer ID */
|
|
||||||
/* Not using PROGMEM string due to boot block in m128 being beyond 64kB boundry */
|
/* Not using PROGMEM string due to boot block in m128 being beyond 64kB boundry */
|
||||||
/* Would need to selectively manipulate RAMPZ, and it's only 9 characters anyway so who cares. */
|
/* Would need to selectively manipulate RAMPZ, and it's only 9 characters anyway so who cares. */
|
||||||
else if(ch=='1') {
|
else if(ch=='1') {
|
||||||
if (getch() == ' ') {
|
if (getch() == ' ') {
|
||||||
putch(0x14);
|
putch(0x14);
|
||||||
putch('A');
|
putch('A');
|
||||||
putch('V');
|
putch('V');
|
||||||
putch('R');
|
putch('R');
|
||||||
putch(' ');
|
putch(' ');
|
||||||
putch('I');
|
putch('I');
|
||||||
putch('S');
|
putch('S');
|
||||||
putch('P');
|
putch('P');
|
||||||
putch(0x10);
|
putch(0x10);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -247,7 +228,7 @@ int main(void)
|
|||||||
if(ch2==0x80) byte_response(HW_VER); // Hardware version
|
if(ch2==0x80) byte_response(HW_VER); // Hardware version
|
||||||
else if(ch2==0x81) byte_response(SW_MAJOR); // Software major version
|
else if(ch2==0x81) byte_response(SW_MAJOR); // Software major version
|
||||||
else if(ch2==0x82) byte_response(SW_MINOR); // Software minor version
|
else if(ch2==0x82) byte_response(SW_MINOR); // Software minor version
|
||||||
else if(ch2==0x98) byte_response(0x03); // Unknown but seems to be required by avr studio 3.56
|
//else if(ch2==0x98) byte_response(0x03); // Unknown but seems to be required by avr studio 3.56
|
||||||
else byte_response(0x00); // Covers various unnecessary responses we don't care about
|
else byte_response(0x00); // Covers various unnecessary responses we don't care about
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -267,13 +248,13 @@ int main(void)
|
|||||||
else if(ch=='P') {
|
else if(ch=='P') {
|
||||||
nothing_response();
|
nothing_response();
|
||||||
// FIXME: modified only here by DojoCorp, Mumbai, India, 20050626
|
// FIXME: modified only here by DojoCorp, Mumbai, India, 20050626
|
||||||
time_count=0; // exted the delay once entered prog.mode
|
//time_count=0; // exted the delay once entered prog.mode
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Leave programming mode */
|
/* Leave programming mode */
|
||||||
else if(ch=='Q') {
|
else if(ch=='Q') {
|
||||||
nothing_response();
|
nothing_response();
|
||||||
time_count=MAX_TIME_COUNT_MORATORY; // once the programming is done,
|
//time_count=MAX_TIME_COUNT_MORATORY; // once the programming is done,
|
||||||
// we should start the application
|
// we should start the application
|
||||||
// but uisp has problems with this,
|
// but uisp has problems with this,
|
||||||
// therefore we just change the times
|
// therefore we just change the times
|
||||||
@ -307,105 +288,121 @@ int main(void)
|
|||||||
flags.eeprom = 0;
|
flags.eeprom = 0;
|
||||||
if (getch() == 'E') flags.eeprom = 1;
|
if (getch() == 'E') flags.eeprom = 1;
|
||||||
for (w=0;w<length.word;w++) {
|
for (w=0;w<length.word;w++) {
|
||||||
buff[w] = getch(); // Store data in buffer, can't keep up with serial data stream whilst programming pages
|
buff[w] = getch(); // Store data in buffer, can't keep up with serial data stream whilst programming pages
|
||||||
}
|
}
|
||||||
if (getch() == ' ') {
|
if (getch() == ' ') {
|
||||||
if (flags.eeprom) { //Write to EEPROM one byte at a time
|
if (flags.eeprom) { //Write to EEPROM one byte at a time
|
||||||
for(w=0;w<length.word;w++) {
|
for(w=0;w<length.word;w++) {
|
||||||
eeprom_wb(address.word,buff[w]);
|
eeprom_wb(address.word,buff[w]);
|
||||||
address.word++;
|
address.word++;
|
||||||
}
|
}
|
||||||
}
|
} else { //Write to FLASH one page at a time
|
||||||
else { //Write to FLASH one page at a time
|
//if (address.byte[1]>127) address_high = 0x01; //Only possible with m128, m256 will need 3rd address byte. FIXME
|
||||||
if (address.byte[1]>127) address_high = 0x01; //Only possible with m128, m256 will need 3rd address byte. FIXME
|
//else address_high = 0x00;
|
||||||
else address_high = 0x00;
|
|
||||||
|
//address.word = address.word << 1; //address * 2 -> byte location
|
||||||
address.word = address.word << 1; //address * 2 -> byte location
|
//if ((length.byte[0] & 0x01)) length.word++; //Even up an odd number of bytes
|
||||||
/* if ((length.byte[0] & 0x01) == 0x01) length.word++; //Even up an odd number of bytes */
|
cli(); //Disable interrupts, just to be sure
|
||||||
if ((length.byte[0] & 0x01)) length.word++; //Even up an odd number of bytes
|
while(bit_is_set(EECR,EEWE)); //Wait for previous EEPROM writes to complete
|
||||||
cli(); //Disable interrupts, just to be sure
|
asm volatile(
|
||||||
while(bit_is_set(EECR,EEWE)); //Wait for previous EEPROM writes to complete
|
"clr r17 \n\t" //page_word_count
|
||||||
asm volatile("clr r17 \n\t" //page_word_count
|
"lds r30,address \n\t" //Address of FLASH location (in words)
|
||||||
"lds r30,address \n\t" //Address of FLASH location (in bytes)
|
"lds r31,address+1 \n\t"
|
||||||
"lds r31,address+1 \n\t"
|
"lsl r30 \n\t" //address * 2 -> byte location
|
||||||
"ldi r28,lo8(buff) \n\t" //Start of buffer array in RAM
|
"rol r31 \n\t"
|
||||||
"ldi r29,hi8(buff) \n\t"
|
"ldi r28,lo8(buff) \n\t" //Start of buffer array in RAM
|
||||||
"lds r24,length \n\t" //Length of data to be written (in bytes)
|
"ldi r29,hi8(buff) \n\t"
|
||||||
"lds r25,length+1 \n\t"
|
"lds r24,length \n\t" //Length of data to be written (in bytes)
|
||||||
"length_loop: \n\t" //Main loop, repeat for number of words in block
|
"lds r25,length+1 \n\t"
|
||||||
"cpi r17,0x00 \n\t" //If page_word_count=0 then erase page
|
"sbrs r24,0 \n\t" //Even up an odd number of bytes
|
||||||
"brne no_page_erase \n\t"
|
"rjmp length_loop \n\t"
|
||||||
"wait_spm1: \n\t"
|
"adiw r24,1 \n\t"
|
||||||
"lds r16,%0 \n\t" //Wait for previous spm to complete
|
"length_loop: \n\t" //Main loop, repeat for number of words in block
|
||||||
"andi r16,1 \n\t"
|
"cpi r17,0x00 \n\t" //If page_word_count=0 then erase page
|
||||||
"cpi r16,1 \n\t"
|
"brne no_page_erase \n\t"
|
||||||
"breq wait_spm1 \n\t"
|
"rcall wait_spm \n\t"
|
||||||
"ldi r16,0x03 \n\t" //Erase page pointed to by Z
|
// "wait_spm1: \n\t"
|
||||||
"sts %0,r16 \n\t"
|
// "lds r16,%0 \n\t" //Wait for previous spm to complete
|
||||||
"spm \n\t"
|
// "andi r16,1 \n\t"
|
||||||
"wait_spm2: \n\t"
|
// "cpi r16,1 \n\t"
|
||||||
"lds r16,%0 \n\t" //Wait for previous spm to complete
|
// "breq wait_spm1 \n\t"
|
||||||
"andi r16,1 \n\t"
|
"ldi r16,0x03 \n\t" //Erase page pointed to by Z
|
||||||
"cpi r16,1 \n\t"
|
"sts %0,r16 \n\t"
|
||||||
"breq wait_spm2 \n\t"
|
"spm \n\t"
|
||||||
|
"rcall wait_spm \n\t"
|
||||||
"ldi r16,0x11 \n\t" //Re-enable RWW section
|
// "wait_spm2: \n\t"
|
||||||
"sts %0,r16 \n\t"
|
// "lds r16,%0 \n\t" //Wait for previous spm to complete
|
||||||
"spm \n\t"
|
// "andi r16,1 \n\t"
|
||||||
"no_page_erase: \n\t"
|
// "cpi r16,1 \n\t"
|
||||||
"ld r0,Y+ \n\t" //Write 2 bytes into page buffer
|
// "breq wait_spm2 \n\t"
|
||||||
"ld r1,Y+ \n\t"
|
"ldi r16,0x11 \n\t" //Re-enable RWW section
|
||||||
|
"sts %0,r16 \n\t"
|
||||||
"wait_spm3: \n\t"
|
"spm \n\t"
|
||||||
"lds r16,%0 \n\t" //Wait for previous spm to complete
|
"no_page_erase: \n\t"
|
||||||
"andi r16,1 \n\t"
|
"ld r0,Y+ \n\t" //Write 2 bytes into page buffer
|
||||||
"cpi r16,1 \n\t"
|
"ld r1,Y+ \n\t"
|
||||||
"breq wait_spm3 \n\t"
|
|
||||||
"ldi r16,0x01 \n\t" //Load r0,r1 into FLASH page buffer
|
"rcall wait_spm \n\t"
|
||||||
"sts %0,r16 \n\t"
|
// "wait_spm3: \n\t"
|
||||||
"spm \n\t"
|
// "lds r16,%0 \n\t" //Wait for previous spm to complete
|
||||||
|
// "andi r16,1 \n\t"
|
||||||
"inc r17 \n\t" //page_word_count++
|
// "cpi r16,1 \n\t"
|
||||||
"cpi r17,%1 \n\t"
|
// "breq wait_spm3 \n\t"
|
||||||
"brlo same_page \n\t" //Still same page in FLASH
|
"ldi r16,0x01 \n\t" //Load r0,r1 into FLASH page buffer
|
||||||
"write_page: \n\t"
|
"sts %0,r16 \n\t"
|
||||||
"clr r17 \n\t" //New page, write current one first
|
"spm \n\t"
|
||||||
"wait_spm4: \n\t"
|
|
||||||
"lds r16,%0 \n\t" //Wait for previous spm to complete
|
"inc r17 \n\t" //page_word_count++
|
||||||
"andi r16,1 \n\t"
|
"cpi r17,%1 \n\t"
|
||||||
"cpi r16,1 \n\t"
|
"brlo same_page \n\t" //Still same page in FLASH
|
||||||
"breq wait_spm4 \n\t"
|
"write_page: \n\t"
|
||||||
"ldi r16,0x05 \n\t" //Write page pointed to by Z
|
"clr r17 \n\t" //New page, write current one first
|
||||||
"sts %0,r16 \n\t"
|
"rcall wait_spm \n\t"
|
||||||
"spm \n\t"
|
// "wait_spm4: \n\t"
|
||||||
"wait_spm5: \n\t"
|
// "lds r16,%0 \n\t" //Wait for previous spm to complete
|
||||||
"lds r16,%0 \n\t" //Wait for previous spm to complete
|
// "andi r16,1 \n\t"
|
||||||
"andi r16,1 \n\t"
|
// "cpi r16,1 \n\t"
|
||||||
"cpi r16,1 \n\t"
|
// "breq wait_spm4 \n\t"
|
||||||
"breq wait_spm5 \n\t"
|
"ldi r16,0x05 \n\t" //Write page pointed to by Z
|
||||||
"ldi r16,0x11 \n\t" //Re-enable RWW section
|
"sts %0,r16 \n\t"
|
||||||
"sts %0,r16 \n\t"
|
"spm \n\t"
|
||||||
"spm \n\t"
|
"rcall wait_spm \n\t"
|
||||||
"same_page: \n\t"
|
// "wait_spm5: \n\t"
|
||||||
"adiw r30,2 \n\t" //Next word in FLASH
|
// "lds r16,%0 \n\t" //Wait for previous spm to complete
|
||||||
"sbiw r24,2 \n\t" //length-2
|
// "andi r16,1 \n\t"
|
||||||
"breq final_write \n\t" //Finished
|
// "cpi r16,1 \n\t"
|
||||||
"rjmp length_loop \n\t"
|
// "breq wait_spm5 \n\t"
|
||||||
"final_write: \n\t"
|
"ldi r16,0x11 \n\t" //Re-enable RWW section
|
||||||
"cpi r17,0 \n\t"
|
"sts %0,r16 \n\t"
|
||||||
"breq block_done \n\t"
|
"spm \n\t"
|
||||||
"adiw r24,2 \n\t" //length+2, fool above check on length after short page write
|
"same_page: \n\t"
|
||||||
"rjmp write_page \n\t"
|
"adiw r30,2 \n\t" //Next word in FLASH
|
||||||
"block_done: \n\t"
|
"sbiw r24,2 \n\t" //length-2
|
||||||
"clr __zero_reg__ \n\t" //restore zero register
|
"breq final_write \n\t" //Finished
|
||||||
: "=m" (SPMCR) : "M" (PAGE_SIZE) : "r0","r16","r17","r24","r25","r28","r29","r30","r31");
|
"rjmp length_loop \n\t"
|
||||||
|
|
||||||
/* Should really add a wait for RWW section to be enabled, don't actually need it since we never */
|
"wait_spm: \n\t"
|
||||||
/* exit the bootloader without a power cycle anyhow */
|
"lds r16,%0 \n\t" //Wait for previous spm to complete
|
||||||
}
|
"andi r16,1 \n\t"
|
||||||
putch(0x14);
|
"cpi r16,1 \n\t"
|
||||||
putch(0x10);
|
"breq wait_spm \n\t"
|
||||||
}
|
"ret \n\t"
|
||||||
|
|
||||||
|
"final_write: \n\t"
|
||||||
|
"cpi r17,0 \n\t"
|
||||||
|
"breq block_done \n\t"
|
||||||
|
"adiw r24,2 \n\t" //length+2, fool above check on length after short page write
|
||||||
|
"rjmp write_page \n\t"
|
||||||
|
"block_done: \n\t"
|
||||||
|
"clr __zero_reg__ \n\t" //restore zero register
|
||||||
|
: "=m" (SPMCR) : "M" (PAGE_SIZE) : "r0","r16","r17","r24","r25","r28","r29","r30","r31");
|
||||||
|
|
||||||
|
/* Should really add a wait for RWW section to be enabled, don't actually need it since we never */
|
||||||
|
/* exit the bootloader without a power cycle anyhow */
|
||||||
|
}
|
||||||
|
putch(0x14);
|
||||||
|
putch(0x10);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Read memory block mode, length is big endian. */
|
/* Read memory block mode, length is big endian. */
|
||||||
@ -414,34 +411,32 @@ int main(void)
|
|||||||
length.byte[0] = getch();
|
length.byte[0] = getch();
|
||||||
if (getch() == 'E') flags.eeprom = 1;
|
if (getch() == 'E') flags.eeprom = 1;
|
||||||
else {
|
else {
|
||||||
flags.eeprom = 0;
|
flags.eeprom = 0;
|
||||||
address.word = address.word << 1; // address * 2 -> byte location
|
address.word = address.word << 1; // address * 2 -> byte location
|
||||||
}
|
}
|
||||||
if (getch() == ' ') { // Command terminator
|
if (getch() == ' ') { // Command terminator
|
||||||
putch(0x14);
|
putch(0x14);
|
||||||
for (w=0;w < length.word;w++) { // Can handle odd and even lengths okay
|
for (w=0;w < length.word;w++) { // Can handle odd and even lengths okay
|
||||||
if (flags.eeprom) { // Byte access EEPROM read
|
if (flags.eeprom) { // Byte access EEPROM read
|
||||||
putch(eeprom_rb(address.word));
|
putch(eeprom_rb(address.word));
|
||||||
address.word++;
|
address.word++;
|
||||||
}
|
} else {
|
||||||
else {
|
if (!flags.rampz) putch(pgm_read_byte_near(address.word));
|
||||||
|
address.word++;
|
||||||
if (!flags.rampz) putch(pgm_read_byte_near(address.word));
|
}
|
||||||
address.word++;
|
}
|
||||||
}
|
putch(0x10);
|
||||||
}
|
|
||||||
putch(0x10);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Get device signature bytes */
|
/* Get device signature bytes */
|
||||||
else if(ch=='u') {
|
else if(ch=='u') {
|
||||||
if (getch() == ' ') {
|
if (getch() == ' ') {
|
||||||
putch(0x14);
|
putch(0x14);
|
||||||
putch(SIG1);
|
putch(SIG1);
|
||||||
putch(SIG2);
|
putch(SIG2);
|
||||||
putch(SIG3);
|
putch(SIG3);
|
||||||
putch(0x10);
|
putch(0x10);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -449,17 +444,13 @@ int main(void)
|
|||||||
else if(ch=='v') {
|
else if(ch=='v') {
|
||||||
byte_response(0x00);
|
byte_response(0x00);
|
||||||
}
|
}
|
||||||
}
|
// } else {
|
||||||
else {
|
// time_count++;
|
||||||
time_count++;
|
// if (time_count>=MAX_TIME_COUNT) {
|
||||||
if (time_count>=MAX_TIME_COUNT) {
|
// app_start();
|
||||||
app_start();
|
// }
|
||||||
}
|
// }
|
||||||
}
|
} /* end of forever loop */
|
||||||
|
|
||||||
}
|
|
||||||
/* end of forever loop */
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void putch(char ch)
|
void putch(char ch)
|
||||||
@ -472,8 +463,12 @@ void putch(char ch)
|
|||||||
char getch(void)
|
char getch(void)
|
||||||
{
|
{
|
||||||
/* m8 */
|
/* m8 */
|
||||||
|
uint32_t count = 0;
|
||||||
while(!(inb(UCSRA) & _BV(RXC))) {
|
while(!(inb(UCSRA) & _BV(RXC))) {
|
||||||
/* HACKME:: here is a good place to count times*/
|
/* HACKME:: here is a good place to count times*/
|
||||||
|
count++;
|
||||||
|
if (count > MAX_TIME_COUNT)
|
||||||
|
app_start();
|
||||||
}
|
}
|
||||||
return (inb(UDR));
|
return (inb(UDR));
|
||||||
}
|
}
|
||||||
@ -483,8 +478,9 @@ void getNch(uint8_t count)
|
|||||||
uint8_t i;
|
uint8_t i;
|
||||||
for(i=0;i<count;i++) {
|
for(i=0;i<count;i++) {
|
||||||
/* m8 */
|
/* m8 */
|
||||||
while(!(inb(UCSRA) & _BV(RXC)));
|
//while(!(inb(UCSRA) & _BV(RXC)));
|
||||||
inb(UDR);
|
//inb(UDR);
|
||||||
|
getch(); // need to handle time out
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,19 +4,12 @@
|
|||||||
# program name should not be changed...
|
# program name should not be changed...
|
||||||
PROGRAM = ATmegaBOOT
|
PROGRAM = ATmegaBOOT
|
||||||
|
|
||||||
# enter the product name for which you want to build the bootloader/monitor
|
PRODUCT=atmega8
|
||||||
# currently CRUMB8, CRUMB128 and PROBOMEGA128 are supported
|
|
||||||
PRODUCT = CRUMB8
|
|
||||||
|
|
||||||
# enter the parameters for the UISP isp tool
|
# enter the parameters for the UISP isp tool
|
||||||
#ISPPARAMS = -dprog=stk200 -dlpt=0x378
|
ISPPARAMS = -dprog=stk500 -dserial=$(SERIAL) -dspeed=115200
|
||||||
ISPPARAMS = -dprog=stk500 -dserial=/dev/com1 -dspeed=115200
|
|
||||||
|
|
||||||
|
|
||||||
############################################################
|
|
||||||
# You should not have to change anything below here.
|
|
||||||
############################################################
|
|
||||||
|
|
||||||
#DIRAVR = /usr/local/avr
|
#DIRAVR = /usr/local/avr
|
||||||
DIRAVRBIN = $(DIRAVR)/bin
|
DIRAVRBIN = $(DIRAVR)/bin
|
||||||
DIRAVRUTILS = $(DIRAVR)/utils/bin
|
DIRAVRUTILS = $(DIRAVR)/utils/bin
|
||||||
@ -24,45 +17,21 @@ DIRINC = .
|
|||||||
DIRLIB = $(DIRAVR)/avr/lib
|
DIRLIB = $(DIRAVR)/avr/lib
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
ifeq ($(PRODUCT),CRUMB8)
|
|
||||||
MCU_TARGET = atmega8
|
MCU_TARGET = atmega8
|
||||||
LDSECTION = --section-start=.text=0x1800
|
LDSECTION = --section-start=.text=0x1c00
|
||||||
FUSE_L = 0xdf
|
FUSE_L = 0xdf
|
||||||
FUSE_H = 0xc8
|
FUSE_H = 0xca
|
||||||
ISPFUSES = $(DIRAVRBIN)/uisp -dpart=ATmega8 $(ISPPARAMS) --wr_fuse_l=$(FUSE_L) --wr_fuse_h=$(FUSE_H)
|
ISPFUSES = $(DIRAVRBIN)/uisp -dpart=ATmega8 $(ISPPARAMS) --wr_fuse_l=$(FUSE_L) --wr_fuse_h=$(FUSE_H)
|
||||||
ISPFLASH = $(DIRAVRBIN)/uisp -dpart=ATmega8 $(ISPPARAMS) --erase --upload if=$(PROGRAM).hex -v
|
ISPFLASH = $(DIRAVRBIN)/uisp -dpart=ATmega8 $(ISPPARAMS) --erase --upload if=$(PROGRAM).hex -v
|
||||||
endif
|
|
||||||
|
|
||||||
ifeq ($(PRODUCT),CRUMB128)
|
|
||||||
MCU_TARGET = atmega128
|
|
||||||
LDSECTION = --section-start=.text=0x1E000
|
|
||||||
FUSE_L = 0xdf
|
|
||||||
FUSE_H = 0xc8
|
|
||||||
FUSE_E = 0xff
|
|
||||||
ISPFUSES = ./uisp -dpart=ATmega128 $(ISPPARAMS) --wr_fuse_l=$(FUSE_L) --wr_fuse_h=$(FUSE_H) --wr_fuse_e=$(FUSE_E)
|
|
||||||
ISPFLASH = ./uisp -dpart=ATmega128 $(ISPPARAMS) --erase --upload if=$(PROGRAM).hex -v
|
|
||||||
endif
|
|
||||||
|
|
||||||
ifeq ($(PRODUCT),PROBOMEGA128)
|
|
||||||
MCU_TARGET = atmega128
|
|
||||||
LDSECTION = --section-start=.text=0x1E000
|
|
||||||
FUSE_L = 0xdf
|
|
||||||
FUSE_H = 0xc8
|
|
||||||
FUSE_E = 0xff
|
|
||||||
ISPFUSES = ./uisp -dpart=ATmega128 $(ISPPARAMS) --wr_fuse_l=$(FUSE_L) --wr_fuse_h=$(FUSE_H) --wr_fuse_e=$(FUSE_E)
|
|
||||||
ISPFLASH = ./uisp -dpart=ATmega128 $(ISPPARAMS) --erase --upload if=$(PROGRAM).hex -v
|
|
||||||
endif
|
|
||||||
|
|
||||||
|
|
||||||
OBJ = $(PROGRAM).o
|
OBJ = $(PROGRAM).o
|
||||||
OPTIMIZE = -O3
|
OPTIMIZE = -Os
|
||||||
|
|
||||||
DEFS =
|
DEFS = -DF_CPU=16000000 -DBAUD_RATE=9600
|
||||||
LIBS =
|
LIBS =
|
||||||
|
|
||||||
CC = $(DIRAVRBIN)/avr-gcc
|
CC = $(DIRAVRBIN)/avr-gcc
|
||||||
|
|
||||||
|
|
||||||
# Override is only needed by avr-lib build system.
|
# Override is only needed by avr-lib build system.
|
||||||
@ -72,8 +41,9 @@ override LDFLAGS = -Wl,-Map,$(PROGRAM).map,$(LDSECTION)
|
|||||||
|
|
||||||
OBJCOPY = $(DIRAVRBIN)/avr-objcopy
|
OBJCOPY = $(DIRAVRBIN)/avr-objcopy
|
||||||
OBJDUMP = $(DIRAVRBIN)/avr-objdump
|
OBJDUMP = $(DIRAVRBIN)/avr-objdump
|
||||||
|
SIZE = $(DIRAVRBIN)/avr-size
|
||||||
|
|
||||||
all: $(PROGRAM).elf lst text
|
all: $(PROGRAM).elf lst text asm size
|
||||||
|
|
||||||
isp: $(PROGRAM).hex
|
isp: $(PROGRAM).hex
|
||||||
$(ISPFUSES)
|
$(ISPFUSES)
|
||||||
@ -83,14 +53,23 @@ $(PROGRAM).elf: $(OBJ)
|
|||||||
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS)
|
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS)
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
|
rm -rf *.s
|
||||||
rm -rf *.o *.elf
|
rm -rf *.o *.elf
|
||||||
rm -rf *.lst *.map
|
rm -rf *.lst *.map
|
||||||
|
|
||||||
|
asm: $(PROGRAM).s
|
||||||
|
|
||||||
|
%.s: %.c
|
||||||
|
$(CC) -S $(CFLAGS) -g1 $^
|
||||||
|
|
||||||
lst: $(PROGRAM).lst
|
lst: $(PROGRAM).lst
|
||||||
|
|
||||||
%.lst: %.elf
|
%.lst: %.elf
|
||||||
$(OBJDUMP) -h -S $< > $@
|
$(OBJDUMP) -h -S $< > $@
|
||||||
|
|
||||||
|
size: $(PROGRAM).hex
|
||||||
|
$(SIZE) $^
|
||||||
|
|
||||||
# Rules for building the .text rom images
|
# Rules for building the .text rom images
|
||||||
|
|
||||||
text: hex bin srec
|
text: hex bin srec
|
||||||
|
2
build/linux/dist/bootloader/burn
vendored
2
build/linux/dist/bootloader/burn
vendored
@ -25,6 +25,6 @@ PORT=/dev/ttyS0
|
|||||||
#PORT=/dev/tty.USA19QW1b1P1.1
|
#PORT=/dev/tty.USA19QW1b1P1.1
|
||||||
|
|
||||||
uisp -dpart=ATmega8 -dprog=stk500 -dserial=$PORT -dspeed=115200 --wr_lock=0xFF
|
uisp -dpart=ATmega8 -dprog=stk500 -dserial=$PORT -dspeed=115200 --wr_lock=0xFF
|
||||||
uisp -dpart=ATmega8 -dprog=stk500 -dserial=$PORT -dspeed=115200 --wr_fuse_l=0xdf --wr_fuse_h=0xc8
|
uisp -dpart=ATmega8 -dprog=stk500 -dserial=$PORT -dspeed=115200 --wr_fuse_l=0xdf --wr_fuse_h=0xca
|
||||||
uisp -dpart=ATmega8 -dprog=stk500 -dserial=$PORT -dspeed=115200 --erase --upload if=ATMegaBOOT.hex -v
|
uisp -dpart=ATmega8 -dprog=stk500 -dserial=$PORT -dspeed=115200 --erase --upload if=ATMegaBOOT.hex -v
|
||||||
uisp -dpart=ATmega8 -dprog=stk500 -dserial=$PORT -dspeed=115200 --wr_lock=0xCF
|
uisp -dpart=ATmega8 -dprog=stk500 -dserial=$PORT -dspeed=115200 --wr_lock=0xCF
|
||||||
|
18
build/macosx/dist/bootloader/burn.command
vendored
18
build/macosx/dist/bootloader/burn.command
vendored
@ -17,13 +17,19 @@
|
|||||||
# parametre
|
# parametre
|
||||||
#
|
#
|
||||||
|
|
||||||
#BINDIR=/usr/local/avr/bin
|
BINDIR=../tools/avr/bin
|
||||||
BINDIR=../build/macosx/work/tools/avr/bin
|
PORT=/dev/tty.USA19QW?b1P1.1
|
||||||
#PORT=/dev/tty.usbserial0
|
|
||||||
#PORT=/dev/tty.USA19QW3b1P1.1
|
|
||||||
PORT=/dev/tty.USA19QW1b1P1.1
|
|
||||||
|
|
||||||
|
# unlock bootloader segment
|
||||||
$BINDIR/uisp -dpart=ATmega8 -dprog=stk500 -dserial=$PORT -dspeed=115200 --wr_lock=0xFF
|
$BINDIR/uisp -dpart=ATmega8 -dprog=stk500 -dserial=$PORT -dspeed=115200 --wr_lock=0xFF
|
||||||
$BINDIR/uisp -dpart=ATmega8 -dprog=stk500 -dserial=$PORT -dspeed=115200 --wr_fuse_l=0xdf --wr_fuse_h=0xc8
|
|
||||||
|
# set fuses
|
||||||
|
# bootloader size of 512 words; from 0xE00-0xFFF
|
||||||
|
# clock speed of 16 MHz, external quartz
|
||||||
|
$BINDIR/uisp -dpart=ATmega8 -dprog=stk500 -dserial=$PORT -dspeed=115200 --wr_fuse_l=0xdf --wr_fuse_h=0xca
|
||||||
|
|
||||||
|
# upload bootloader
|
||||||
$BINDIR/uisp -dpart=ATmega8 -dprog=stk500 -dserial=$PORT -dspeed=115200 --erase --upload if=ATMegaBOOT.hex -v
|
$BINDIR/uisp -dpart=ATmega8 -dprog=stk500 -dserial=$PORT -dspeed=115200 --erase --upload if=ATMegaBOOT.hex -v
|
||||||
|
|
||||||
|
# lock bootloader segment
|
||||||
$BINDIR/uisp -dpart=ATmega8 -dprog=stk500 -dserial=$PORT -dspeed=115200 --wr_lock=0xCF
|
$BINDIR/uisp -dpart=ATmega8 -dprog=stk500 -dserial=$PORT -dspeed=115200 --wr_lock=0xCF
|
||||||
|
2
build/windows/dist/bootloader/burn.bat
vendored
2
build/windows/dist/bootloader/burn.bat
vendored
@ -1,6 +1,6 @@
|
|||||||
REM TODO: need way to specify serial port.
|
REM TODO: need way to specify serial port.
|
||||||
|
|
||||||
..\tools\avr\bin\uisp -dpart=ATmega8 -dprog=stk500 -dserial=com1 -dspeed=115200 --wr_lock=0xFF
|
..\tools\avr\bin\uisp -dpart=ATmega8 -dprog=stk500 -dserial=com1 -dspeed=115200 --wr_lock=0xFF
|
||||||
..\tools\avr\bin\uisp -dpart=ATmega8 -dprog=stk500 -dserial=com1 -dspeed=115200 --wr_fuse_l=0xdf --wr_fuse_h=0xc8
|
..\tools\avr\bin\uisp -dpart=ATmega8 -dprog=stk500 -dserial=com1 -dspeed=115200 --wr_fuse_l=0xdf --wr_fuse_h=0xca
|
||||||
..\tools\avr\bin\uisp -dpart=ATmega8 -dprog=stk500 -dserial=com1 -dspeed=115200 --erase --upload --verify if=ATMegaBOOT.hex
|
..\tools\avr\bin\uisp -dpart=ATmega8 -dprog=stk500 -dserial=com1 -dspeed=115200 --erase --upload --verify if=ATMegaBOOT.hex
|
||||||
..\tools\avr\bin\uisp -dpart=ATmega8 -dprog=stk500 -dserial=com1 -dspeed=115200 --wr_lock=0xCF
|
..\tools\avr\bin\uisp -dpart=ATmega8 -dprog=stk500 -dserial=com1 -dspeed=115200 --wr_lock=0xCF
|
||||||
|
Loading…
x
Reference in New Issue
Block a user