手上多余一大堆Mega8,最近刚好在熟悉ISP,想自己做一批来测试,重新写了Bootload,把代码贴出来给大家自己做,只用了512个字节,使用0x1c00开始地址作为Bootload地址,开机进入Bootload,10秒钟重启一次,如有有代码自动进入用户程序。 先上代码:
include <inttypes.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/eeprom.h>
#include <avr/interrupt.h>
#define MAX_TIME_COUNT (F_CPU>>1)
#define HW_VER 0x02
#define SW_MAJOR 0x01
#define SW_MINOR 0x12
#ifndef outb
#define outb(sfr,val) (_SFR_BYTE(sfr) = (val))
#endif
#ifndef inb
#define inb(sfr) _SFR_BYTE(sfr)
#endif
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
#define eeprom_rb(addr) eeprom_read_byte ((uint8_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 LED_DDR DDRB
#define LED_PORT PORTB
#define LED_PIN PIND
#define LED PIND5
#define SIG1 0x1E
#define SIG2 0x93
#define SIG3 0x07
#define PAGE_SIZE 0x20U
void putch(char);
char getch(void);
void getNch(uint8_t);
void byte_response(uint8_t);
void nothing_response(void);
#if 1
union address_union {
uint16_t word;
uint8_t byte[2];
} address;
union length_union {
uint16_t word;
uint8_t byte[2];
} length;
#endif
struct flags_struct {
unsigned eeprom : 1;
} flags;
uint8_t buff[256];
void (*app_start)(void) = 0x0000;
void putch(char ch)
{
while (!(inb(UCSRA) & _BV(UDRE)));
outb(UDR,ch);
}
char getch(void)
{
uint32_t count = 0;
while(!(inb(UCSRA) & _BV(RXC))) {
count++;
if (count > MAX_TIME_COUNT){
putch('!');
app_start();
}
}
return (inb(UDR));
}
void getNch(uint8_t count)
{
uint8_t i;
for(i=0;i<count;i++) {
getch();
}
}
void byte_response(uint8_t val)
{
if (getch() ==0x20) {
putch(0x14);
putch(val);
putch(0x10);
}
}
void nothing_response(void)
{
if (getch() == 0x20) {
putch(0x14);
putch(0x10);
}
}
int main(void)
{
uint8_t ch,ch2;
uint16_t w;
asm volatile("nop\n\t");
UBRRH = (((F_CPU/BAUD_RATE)/16)-1)>>8;
UBRRL = (((F_CPU/BAUD_RATE)/16)-1);
UCSRB = (1<<RXEN)|(1<<TXEN);
UCSRC = (1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0);
putch('\0');
for (;;) {
ch = getch();
if((ch=='P')||(ch=='Q')||(ch=='R')||(ch=='0')) {
nothing_response();
if (ch=='Q')app_start();
}
else if(ch=='1') {
if (getch() == ' ') {
putch(0x14);
putch('A');
putch('V');
putch('R');
putch(' ');
putch('I');
putch('S');
putch('P');
putch(0x10);
}
}
else if(ch=='@') {
ch2 = getch();
if (ch2>0x85) getch();
nothing_response();
}
else if(ch=='A') {
ch2 = getch();
if(ch2==0x80) byte_response(HW_VER);
else if(ch2==0x81) byte_response(SW_MAJOR);
else if(ch2==0x82) byte_response(SW_MINOR);
else byte_response(0x00);
}
else if(ch=='B') {
getNch(20);
nothing_response();
}
else if(ch=='E') {
getNch(5);
nothing_response();
}
else if(ch=='V') {
getNch(4);
byte_response(00);
}
else if(ch=='d') {
length.byte[1] = getch();
length.byte[0]= getch();
flags.eeprom = 0;
if (getch() == 'E') flags.eeprom = 1;
for (w=0;w<length.word;w++) {
buff[w] = getch();
}
if (getch() ==0x20)
{
#ifdef EERPOM
if (flags.eeprom) {
for(w=0;w<length.word;w++) {
eeprom_wb(address.word,buff[w]);
address.word++;
}
} else
#else
if (flags.eeprom==0)
#endif
{
#if 1
cli();
while(bit_is_set(EECR,EEWE));
asm volatile(
"clr r17 \n\t"
"lds r30,address \n\t"
"lds r31,address+1 \n\t"
"lsl r30 \n\t"
"rol r31 \n\t"
"ldi r28,lo8(buff) \n\t"
"ldi r29,hi8(buff) \n\t"
"lds r24,length \n\t"
"lds r25,length+1 \n\t"
"sbrs r24,0 \n\t"
"rjmp length_loop \n\t"
"adiw r24,1 \n\t"
"length_loop: \n\t"
"cpi r17,0x00 \n\t"
"brne no_page_erase \n\t"
"rcall wait_spm \n\t"
"ldi r16,0x03 \n\t"
"sts %0,r16 \n\t"
"spm \n\t"
"rcall wait_spm \n\t"
"ldi r16,0x11 \n\t"
"sts %0,r16 \n\t"
"spm \n\t"
"no_page_erase: \n\t"
"ld r0,Y+ \n\t"
"ld r1,Y+ \n\t"
"rcall wait_spm \n\t"
"ldi r16,0x01 \n\t"
"sts %0,r16 \n\t"
"spm \n\t"
"inc r17 \n\t"
"cpi r17,%1 \n\t"
"brlo same_page \n\t"
"write_page: \n\t"
"clr r17 \n\t"
"rcall wait_spm \n\t"
"ldi r16,0x05 \n\t"
"sts %0,r16 \n\t"
"spm \n\t"
"rcall wait_spm \n\t"
"ldi r16,0x11 \n\t"
"sts %0,r16 \n\t"
"spm \n\t"
"same_page: \n\t"
"adiw r30,2 \n\t"
"sbiw r24,2 \n\t"
"breq final_write \n\t"
"rjmp length_loop \n\t"
"wait_spm: \n\t"
"lds r16,%0 \n\t"
"andi r16,1 \n\t"
"cpi r16,1 \n\t"
"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"
"rjmp write_page \n\t"
"block_done: \n\t"
"clr __zero_reg__ \n\t"
: "=m" (SPMCR) : "M" (PAGE_SIZE) : "r0","r16","r17","r24","r25","r28","r29","r30","r31");
#endif
}
putch(0x14);
putch(0x10);
}
}
else if(ch=='U') {
address.byte[0] = getch();
address.byte[1]=getch();
nothing_response();
}
else if(ch=='t') {
length.byte[1] = getch();
length.byte[0] =getch();
#if EERPOM
if (getch() == 'E')
flags.eeprom = 1;
else
#else
if (getch() == 'F')
#endif
{
flags.eeprom = 0;
address.word = address.word << 1;
}
if (getch() == 0x20) {
putch(0x14);
for (w=0;w <length.word;w++) {
#if EERPOM
if (flags.eeprom) {
putch(eeprom_rb(address.word));
address.word++;
} else
#else
if (flags.eeprom==0)
#endif
{
if (address.word<0x1c00)
{
putch(pgm_read_byte_near(address.word));
}else{
putch(0xff);
}
address.word++;
}
}
putch(0x10);
}
}
else if(ch=='u') {
if (getch() == ' ') {
putch(0x14);
putch(SIG1);
putch(SIG2);
putch(SIG3);
putch(0x10);
}
}
else if(ch=='v') {
byte_response(0x00);
}
}
}
接下来是Makefile:
# Makefile for ATmegaBOOT
# E.Lins, 2004-10-14
# program name should not be changed...
PROGRAM = ATmegaBOOT
PRODUCT=atmega8
# enter the parameters for the UISP isp tool
ISPPARAMS = -dprog=stk500 -dserial=$(SERIAL) -dspeed=19200
#DIRAVR = F:\2022\AVR\miniuxAVR\arduino-lite\bin\win32\WinAVR-20090313
DIRAVR = ../../../bin/win32/WinAVR-20090313
DIRAVRBIN = $(DIRAVR)/bin
DIRAVRUTILS = $(DIRAVR)/utils/bin
DIRINC = $(DIRAVR)/include
DIRLIB = $(DIRAVR)/avr/lib
MCU_TARGET = atmega8
LDSECTION = --section-start=.text=0x1C00
#FUSE_L = 0xdf
#FUSE_H = 0xca
FUSE_L = 0xFF
FUSE_H = 0xDD
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
OBJ = $(PROGRAM).o
OPTIMIZE = -Os
DEFS = -DF_CPU=7372800 -DBAUD_RATE=19200
LIBS =
CC = $(DIRAVRBIN)/avr-gcc
# Override is only needed by avr-lib build system.
override CFLAGS = -g -Wall $(OPTIMIZE) -mmcu=$(MCU_TARGET) -D$(PRODUCT) $(DEFS) -I$(DIRINC)
override LDFLAGS = -Wl,-Map,$(PROGRAM).map,$(LDSECTION)
OBJCOPY = $(DIRAVRBIN)/avr-objcopy
OBJDUMP = $(DIRAVRBIN)/avr-objdump
SIZE = $(DIRAVRBIN)/avr-size
all: $(PROGRAM).elf lst text asm size
isp: $(PROGRAM).hex
$(ISPFUSES)
$(ISPFLASH)
$(PROGRAM).elf: $(OBJ)
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS)
clean:
rm -rf *.s
rm -rf *.o *.elf
rm -rf *.lst *.map
asm: $(PROGRAM).s
%.s: %.c
$(CC) -S $(CFLAGS) -g1 $^
lst: $(PROGRAM).lst
%.lst: %.elf
$(OBJDUMP) -h -S $< > $@
size: $(PROGRAM).hex
$(SIZE) $^
# Rules for building the .text rom images
text: hex bin srec
hex: $(PROGRAM).hex
bin: $(PROGRAM).bin
srec: $(PROGRAM).srec
%.hex: %.elf
$(OBJCOPY) -j .text -j .data -O ihex $< $@
%.srec: %.elf
$(OBJCOPY) -j .text -j .data -O srec $< $@
%.bin: %.elf
$(OBJCOPY) -j .text -j .data -O binary $< $@
然后使用 make 编译出hex
用烧录器烧录或者ISP
配置位如图:
下载完代开Arduino
开发板选择 Arduino NG or older 处理器 Atmega8 编程器选择 STK500
这样使用 19200下载到开发板就可以了!
在Arduino\hardware\arduino\avr目录下有个broads.txt
由于我使用的是7.372800 19200BPS这里面需要改动
##############################################################
atmegang.name=Arduino NG or older
atmegang.upload.tool=avrdude atmegang.upload.protocol=arduino atmegang.upload.speed=19200
atmegang.bootloader.tool=avrdude atmegang.bootloader.unlock_bits=0x3F atmegang.bootloader.lock_bits=0x0F
atmegang.build.mcu=atmegang atmegang.build.f_cpu=7372800L atmegang.build.board=AVR_NG atmegang.build.core=arduino atmegang.build.variant=standard
Arduino NG or older w/ ATmega168
--------------------------------
atmegang.menu.cpu.atmega168=ATmega168
atmegang.menu.cpu.atmega168.upload.maximum_size=14336 atmegang.menu.cpu.atmega168.upload.maximum_data_size=1024
atmegang.menu.cpu.atmega168.bootloader.low_fuses=0xff atmegang.menu.cpu.atmega168.bootloader.high_fuses=0xdd atmegang.menu.cpu.atmega168.bootloader.extended_fuses=0xF8 atmegang.menu.cpu.atmega168.bootloader.file=atmega/ATmegaBOOT_168_ng.hex
atmegang.menu.cpu.atmega168.build.mcu=atmega168
Arduino NG or older w/ ATmega8
------------------------------
atmegang.menu.cpu.atmega8=ATmega8
atmegang.menu.cpu.atmega8.upload.maximum_size=7168 atmegang.menu.cpu.atmega8.upload.maximum_data_size=1024
atmegang.menu.cpu.atmega8.bootloader.low_fuses=0xdf atmegang.menu.cpu.atmega8.bootloader.high_fuses=0xca atmegang.menu.cpu.atmega8.bootloader.extended_fuses=0xF8 atmegang.menu.cpu.atmega8.bootloader.file=atmega8/ATmegaBOOT-prod-firmware-2009-11-07.hex
atmegang.menu.cpu.atmega8.build.mcu=atmega8
############################################################## 这样就完成了。
|