手上多余一大堆Mega8,最近刚好在熟悉ISP,想自己做一批来测试,重新写了Bootload,把代码贴出来给大家自己做,只用了512个字节,使用0x1c00开始地址作为Bootload地址,开机进入Bootload,10秒钟重启一次,如有有代码自动进入用户程序。
先上代码:
include #include #include #include #include //#include //#define EEPROM 0 //FUCS FF D9 00 FF 0x1800 //FUCS FF DA 00 FF 0x1C00 OKOK //#define F_CPU 16000000 /* We, Malmoitians, like slow interaction * therefore the slow baud rate ;-) */ //#define BAUD_RATE 9600 /* 6.000.000 is more or less 8 seconds at the * speed configured here */ //#define MAX_TIME_COUNT 6000000 #define MAX_TIME_COUNT (F_CPU>>1) //#define MAX_TIME_COUNT_MORATORY 1600000 /* SW_MAJOR and MINOR needs to be updated from time to time to avoid warning message from AVR Studio */ #define HW_VER 0x02 #define SW_MAJOR 0x01 #define SW_MINOR 0x12 // AVR-GCC compiler compatibility // avr-gcc compiler v3.1.x and older doesn't support outb() and inb() // if necessary, convert outb and inb to outp and inp #ifndef outb #define outb(sfr,val) (_SFR_BYTE(sfr) = (val)) #endif #ifndef inb #define inb(sfr) _SFR_BYTE(sfr) #endif /* defines for future compatibility */ #ifndef cbi #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) #endif #ifndef sbi #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) #endif /* Adjust to suit whatever pin your hardware uses to enter the bootloader */ #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)) /* Onboard LED is connected to pin PB5 */ #define LED_DDR DDRB #define LED_PORT PORTB #define LED_PIN PIND #define LED PIND5 #define SIG1 0x1E // Yep, Atmel is the only manufacturer of AVR micros. Single source :( #define SIG2 0x93 #define SIG3 0x07 #define PAGE_SIZE 0x20U //32 words 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 //uint16_t length; //uint16_t address; struct flags_struct { unsigned eeprom : 1; //unsigned rampz : 1; } flags; uint8_t buff[256]; //uint8_t address_high; //uint8_t pagesz=0x80; //uint8_t i; //uint8_t bootuart0=0,bootuart1=0; void (*app_start)(void) = 0x0000; void putch(char ch) { /* m8 */ while (!(inb(UCSRA) & _BV(UDRE))); outb(UDR,ch); } char getch(void) { /* m8 */ uint32_t count = 0; while(!(inb(UCSRA) & _BV(RXC))) { /* HACKME:: here is a good place to count times*/ count++; //putch('.'); if (count > MAX_TIME_COUNT){ putch('!'); app_start(); } } return (inb(UDR)); } void getNch(uint8_t count) { uint8_t i; for(i=0;i //while(!(inb(UCSRA) & _BV(RXC))); //inb(UDR); getch(); // need to handle time out } } 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; //cbi(BL_DDR,BL); //sbi(BL_PORT,BL); asm volatile("nopnt"); /* check if flash is programmed already, if not start bootloader anyway */ //if(pgm_read_byte_near(0x0000) != 0xFF) { /* check if bootloader pin is set low */ // if(bit_is_set(BL_PIN,BL)) app_start(); //} /* initialize UART(s) depending on CPU defined */ /* m8 */ UBRRH = (((F_CPU/BAUD_RATE)/16)-1)>>8; // set baud rate UBRRL = (((F_CPU/BAUD_RATE)/16)-1); UCSRB = (1< //UBRRL = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1); //UBRRH = (F_CPU/(BAUD_RATE*16L)-1) >> 8; //UCSRA = 0x00; //UCSRC = 0x86; //UCSRB = _BV(TXEN)|_BV(RXEN); /* this was giving uisp problems, so I removed it; without it, the boot works on with uisp and avrdude on the mac (at least). */ putch('