I use ATmega644P and need both UART.
This modified loader use any 3 I/O line (RX, TX, LED) for load code from Arduino IDE.
Test with ATmega644P (internal RC 8MHz), 38400 baud rates.
PC5 = TX line, PC4 = RX line. LED on PB1.
#include <inttypes.h> #include <avr/io.h> #include <avr/pgmspace.h> #include <avr/interrupt.h> #include <avr/wdt.h> #include <avr/boot.h> #ifdef ADABOOT #define START_LED_FLASHES 2 #define NUM_LED_FLASHES 3 #define ERROR_LED_FLASHES 5 #define ADABOOT_VER 1 #endif #ifndef BAUD_RATE #define BAUD_RATE 38400 #endif #define MAX_ERROR_COUNT 5 /* SW_MAJOR and MINOR needs to be updated from time to time to avoid warning message from AVR Studio */ /* never allow AVR Studio to do an update */ #define HW_VER 0x02 #define SW_MAJOR 0x01 #define SW_MINOR 0x10 /* soft RX/TX line definition */ #define RX_DDR DDRC #define RX_PORT PORTC #define RX_PIN PINC #define RX _BV(PINC4) #define TX_DDR DDRC #define TX_PORT PORTC #define TX _BV(PINC5) /* onboard LED is used to indicate, that the bootloader was entered (3x flashing) */ /* if monitor functions are included, LED goes on after monitor was entered */ #define LED_DDR DDRB #define LED_PORT PORTB #define LED_PIN PINB #define LED _BV(PINB1) /* define various device id's */ /* manufacturer byte is always the same */ #define SIG1 0x1E #if defined(__AVR_ATmega644P__) #define SIG2 0x96 #define SIG3 0x0A #elif defined(__AVR_ATmega644__) #define SIG2 0x96 #define SIG3 0x09 #elif defined(__AVR_ATmega324P__) #define SIG2 0x95 #define SIG3 0x08 #endif #define PAGE_SIZE 0x080U //128 words #define PAGE_SIZE_BYTES 0x100U //256 bytes /* function prototypes */ void putch(uint8_t); char getch(void); void getNch(uint8_t); void byte_response(uint8_t); void nothing_response(void); void response(uint8_t, uint8_t); void flash_led(uint8_t); char gethex(void); void puthex(char); char getchx(uint8_t); void delay_us(uint8_t); void waitRX(void); #if F_CPU == 8000000L #ifdef BAUD_RATE #if BAUD_RATE == 38400 #define RX_GAIN_FIRST 10 #define RX_GAIN_NEXT 32 #define RX_GAIN_LAST 26 #define TX_GAIN_INITIAL 70 #define TX_GAIN_FIRST 35 #define TX_GAIN_NEXT 33 #define TX_GAIN_LAST 16 #else #error Only 38400 baud rate supported #endif #else #error Baud rate not defined #endif #else #error Only 8Mhz clock supported #endif union address_union { uint16_t word; uint8_t byte[2]; } address; union length_union { uint16_t word; uint8_t byte[2]; } length; uint8_t writeToEEPROM; uint8_t buff[PAGE_SIZE_BYTES]; uint8_t error_count; void (*app_start)(void) = 0x0000; /* main program starts here */ int main(void) { uint8_t ch; uint16_t w; asm volatile("nop\n\t"); #ifdef ADABOOT // BBR/LF 10/8/2007 & 9/13/2008 ch = MCUSR; MCUSR = 0; WDTCSR |= _BV(WDCE) | _BV(WDE); WDTCSR = 0; // Check if the WDT was used to reset, in which case we dont bootload and skip straight to the code. woot. if (! (ch & _BV(EXTRF))) // if its a not an external reset... app_start(); // skip bootloader #endif TX_DDR |= TX; // TX line set to high TX_PORT |= TX; // Enable pullup to RX line RX_DDR &= ~RX; // RX line set to input RX_PORT |= RX; // Enable pullup to RX line /* set LED pin as output */ LED_DDR |= LED; #ifdef ADABOOT /* flash onboard LED to signal entering of bootloader */ /* ADABOOT will do two series of flashes. first 4 - signifying ADABOOT */ /* then a pause and another flash series signifying ADABOOT sub-version */ flash_led(NUM_LED_FLASHES); flash_led(ADABOOT_VER); // BBR 9/13/2008 #endif error_count = 0; /* Wait for RX line stay high */ for(w = 8; w; --w) { if(!(RX_PIN & RX)) w = 8; delay_us(RX_GAIN_NEXT/2); } for (;;) { if(error_count >= MAX_ERROR_COUNT) { flash_led(ERROR_LED_FLASHES); app_start(); } /* get character from UART */ ch = getch(); /* Ping device */ /* Erase device, don't care as we will erase one page at a time anyway. */ /* Enter programming mode */ if(ch == '0' || ch == 'R' || ch == 'P') { nothing_response(); } /* Request programmer ID */ /* 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. */ else if(ch == '1') { if (getch() == ' ') { putch(0x14); putch('A'); putch('V'); putch('R'); putch(' '); putch('I'); putch('S'); putch('P'); putch(0x10); } else ++error_count; } /* AVR ISP/STK500 board commands DON'T CARE so default nothing_response */ else if(ch=='@') { if (getch() > 0x85) getch(); nothing_response(); } /* Get device signature bytes */ else if(ch == 'u') { if (getch() == ' ') { putch(0x14); putch(SIG1); putch(SIG2); putch(SIG3); putch(0x10); } else ++error_count; } /* AVR ISP/STK500 board requests */ else if(ch=='A') { ch = getch(); if(ch == 0x80) byte_response(HW_VER); // Hardware version else if(ch == 0x81) byte_response(SW_MAJOR); // Software major version else if(ch == 0x82) byte_response(SW_MINOR); // Software minor version else if(ch == 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 } /* Device Parameters DON'T CARE, DEVICE IS FIXED */ else if(ch == 'B') { getNch(20); nothing_response(); } /* Parallel programming stuff DON'T CARE */ else if(ch == 'E') { getNch(5); nothing_response(); } /* Universal SPI programming command, disabled. Would be used for fuses and lock bits. */ else if(ch == 'V') { getNch(4); byte_response(0x00); } /* Read oscillator calibration byte */ else if(ch == 'v') { byte_response(0x00); } /* Leave programming mode */ else if(ch == 'Q') { nothing_response(); #ifdef ADABOOT // autoreset via watchdog (sneaky!) BBR/LF 9/13/2008 WDTCSR = _BV(WDE); while (1); // 16 ms #endif } /* Set address, little endian. EEPROM in bytes, FLASH in words */ /* Perhaps extra address bytes may be added in future to support > 128kB FLASH. */ /* This might explain why little endian was used here, big endian used everywhere else. */ else if(ch == 'U') { address.byte[0] = getch(); address.byte[1] = getch(); nothing_response(); } /* Write memory, length is big endian and is in bytes */ else if(ch == 'd') { length.byte[1] = getch(); length.byte[0] = getch(); writeToEEPROM = 0; if (getch() == 'E') writeToEEPROM = 1; for (w = 0; w < length.word; w++) { // Store data in buffer, can't keep up with serial data stream whilst programming pages buff[w] = getch(); } if (getch() == ' ') { while (w < PAGE_SIZE) buff[w++] = 0; if (writeToEEPROM) { //Write to EEPROM one byte at a time for(w = 0; w < length.word; w++) { while(EECR & (1<<EEPE)); EEAR = (uint16_t)(void *)address.word; EEDR = buff[w]; EECR |= (1<<EEMPE); EECR |= (1<<EEPE); address.word++; } } else { //address * 2 -> byte location address.word = address.word << 1; //Even up an odd number of bytes if ((length.byte[0] & 0x01)) length.word++; // HACKME: EEPE used to be EEWE // Wait for previous EEPROM writes to complete // while(bit_is_set(EECR,EEPE)); while(EECR & (1<<EEPE)); asm volatile( "clr r17 \n\t" //page_word_count "lds r30,address \n\t" //Address of FLASH location (in bytes) "lds r31,address+1 \n\t" "ldi r28,lo8(buff) \n\t" //Start of buffer array in RAM "ldi r29,hi8(buff) \n\t" "lds r24,length \n\t" //Length of data to be written (in bytes) "lds r25,length+1 \n\t" "length_loop: \n\t" //Main loop, repeat for number of words in block "cpi r17,0x00 \n\t" //If page_word_count=0 then erase page "brne no_page_erase \n\t" "wait_spm1: \n\t" "lds r16,%0 \n\t" //Wait for previous spm to complete "andi r16,1 \n\t" "cpi r16,1 \n\t" "breq wait_spm1 \n\t" "ldi r16,0x03 \n\t" //Erase page pointed to by Z "sts %0,r16 \n\t" "spm \n\t" "wait_spm2: \n\t" "lds r16,%0 \n\t" //Wait for previous spm to complete "andi r16,1 \n\t" "cpi r16,1 \n\t" "breq wait_spm2 \n\t" "ldi r16,0x11 \n\t" //Re-enable RWW section "sts %0,r16 \n\t" "spm \n\t" "no_page_erase: \n\t" "ld r0,Y+ \n\t" //Write 2 bytes into page buffer "ld r1,Y+ \n\t" "wait_spm3: \n\t" "lds r16,%0 \n\t" //Wait for previous spm to complete "andi r16,1 \n\t" "cpi r16,1 \n\t" "breq wait_spm3 \n\t" "ldi r16,0x01 \n\t" //Load r0,r1 into FLASH page buffer "sts %0,r16 \n\t" "spm \n\t" "inc r17 \n\t" //page_word_count++ "cpi r17,%1 \n\t" "brlo same_page \n\t" //Still same page in FLASH "write_page: \n\t" "clr r17 \n\t" //New page, write current one first "wait_spm4: \n\t" "lds r16,%0 \n\t" //Wait for previous spm to complete "andi r16,1 \n\t" "cpi r16,1 \n\t" "breq wait_spm4 \n\t" "ldi r16,0x05 \n\t" //Write page pointed to by Z "sts %0,r16 \n\t" "spm \n\t" "wait_spm5: \n\t" "lds r16,%0 \n\t" //Wait for previous spm to complete "andi r16,1 \n\t" "cpi r16,1 \n\t" "breq wait_spm5 \n\t" "ldi r16,0x11 \n\t" //Re-enable RWW section "sts %0,r16 \n\t" "spm \n\t" "same_page: \n\t" "adiw r30,2 \n\t" //Next word in FLASH "sbiw r24,2 \n\t" //length-2 "breq final_write \n\t" //Finished "rjmp length_loop \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" (SPMCSR) : "M" (PAGE_SIZE) : "r0","r16","r17","r24","r25","r28","r29","r30","r31" ); } putch(0x14); putch(0x10); } } /* Read memory block mode, length is big endian. */ else if(ch == 't') { length.byte[1] = getch(); length.byte[0] = getch(); if (getch() == 'E') writeToEEPROM = 1; else writeToEEPROM = 0; // Command terminator if (getch() == ' ') { address.word = address.word << 1; // address * 2 -> byte location putch(0x14); for (w = 0; w < length.word; w++) { if (writeToEEPROM) { // Byte access EEPROM read while(EECR & (1<<EEPE)); EEAR = (uint16_t)(void *)address.word; EECR |= (1<<EERE); putch(EEDR); address.word++; } else { putch(pgm_read_byte_near(address.word)); address.word++; } } putch(0x10); } } else ++error_count; } } void delay_us(uint8_t delay) { while(--delay) { asm volatile( "nop\n\t" "nop\n\t" "nop\n\t" ); } } void putch(uint8_t ch) { delay_us(TX_GAIN_INITIAL); TX_PORT &= ~TX; delay_us(TX_GAIN_FIRST); for(uint8_t mask = 0x01; mask; mask <<= 1) { if(ch & mask) TX_PORT |= TX; else TX_PORT &= ~TX; delay_us(TX_GAIN_NEXT); } TX_PORT |= TX; delay_us(TX_GAIN_LAST); } char getch(void) { return getchx(1); } char getchx(uint8_t timeout) { uint32_t count = 0; uint8_t data = 0; uint8_t not_mask; LED_PORT &= ~LED; // toggle LED to show activity - BBR/LF 10/3/2007 & 9/13/2008 uint8_t mask = 0x01; while(RX_PIN & RX) // Wait for low level on RX pin { if(timeout) { count++; if (count > MAX_TIME_COUNT) { flash_led(START_LED_FLASHES); app_start(); } } } delay_us(RX_GAIN_FIRST); for(; mask; mask <<= 1) { delay_us(RX_GAIN_NEXT); not_mask = ~mask; if(RX_PIN & RX) data |= mask; else data &= not_mask; } delay_us(RX_GAIN_LAST); LED_PORT |= LED; // toggle LED to show activity - BBR/LF 10/3/2007 & 9/13/2008 return data; } void getNch(uint8_t count) { do { getchx(0); } while(--count); } void byte_response(uint8_t val) { if (getch() == ' ') { putch(0x14); putch(val); putch(0x10); } else ++error_count; } void nothing_response(void) { if (getch() == ' ') { putch(0x14); putch(0x10); } else ++error_count; } #ifdef ADABOOT void flash_led(uint8_t count) { /* flash onboard LED count times to signal entering of bootloader */ /* l needs to be volatile or the delay loops below might get */ /* optimized away if compiling with optimizations (DAM). */ volatile uint32_t l; if (count == 0) { count = ADABOOT; } int8_t i; for (i = 0; i < count; ++i) { LED_PORT |= LED; // LED on for(l = 0; l < (F_CPU / 800); ++l); // delay NGvalue was 1000 for both loops - BBR LED_PORT &= ~LED; // LED off for(l = 0; l < (F_CPU / 250); ++l); // delay asymmteric for ADA BOOT BBR } for(l = 0; l < (F_CPU / 100); ++l); // pause ADA BOOT BBR } #else void flash_led(uint8_t count) { /* flash onboard LED three times to signal entering of bootloader */ /* l needs to be volatile or the delay loops below might get optimized away if compiling with optimizations (DAM). */ volatile uint32_t l; if (count == 0) { count = 3; } int8_t i; for (i = 0; i < count; ++i) { LED_PORT |= LED; for(l = 0; l < (F_CPU / 1000); ++l); LED_PORT &= ~LED; for(l = 0; l < (F_CPU / 1000); ++l); } } #endif
0 comment(s) so far