--- stk500-orig.c 2011-09-15 15:37:04.000000000 +0100 +++ stk500.c 2013-12-08 20:06:54.000000000 +0000 @@ -45,6 +45,8 @@ #define STK500_XTAL 7372800U +static unsigned char ext_addr_byte; /* Record ext-addr byte set in the target device (if used) */ + static int stk500_getparm(PROGRAMMER * pgm, unsigned parm, unsigned * value); static int stk500_setparm(PROGRAMMER * pgm, unsigned parm, unsigned value); static void stk500_print_parms1(PROGRAMMER * pgm, const char * p); @@ -427,6 +429,7 @@ n_extparms = 3; tries = 0; + ext_addr_byte = 0xff; /* Ensures it is programmed before first memory address */ retry: tries++; @@ -689,14 +692,30 @@ } -static int stk500_loadaddr(PROGRAMMER * pgm, unsigned int addr) +static int stk500_loadaddr(PROGRAMMER * pgm, AVRMEM * mem, unsigned int addr) { unsigned char buf[16]; int tries; + unsigned char ext_byte; + OPCODE * lext; tries = 0; retry: tries++; + + /* To support flash > 64K words the correct Extended Address Byte is needed */ + lext = mem->op[AVR_OP_LOAD_EXT_ADDR]; + if (lext != NULL) { + ext_byte = (addr >> 16) & 0xff; + if (ext_byte != ext_addr_byte) { + /* Either this is the first addr load, or a 64K word boundary is crossed, so set the ext addr byte */ + avr_set_bits(lext, buf); + avr_set_addr(lext, buf, addr); + stk500_cmd(pgm, buf, buf); + ext_addr_byte = ext_byte; + } + } + buf[0] = Cmnd_STK_LOAD_ADDRESS; buf[1] = addr & 0xff; buf[2] = (addr >> 8) & 0xff; @@ -822,7 +841,7 @@ tries = 0; retry: tries++; - stk500_loadaddr(pgm, addr/a_div); + stk500_loadaddr(pgm, m, addr/a_div); /* build command block and avoid multiple send commands as it leads to a crash of the silabs usb serial driver on mac os x */ @@ -939,7 +958,7 @@ tries = 0; retry: tries++; - stk500_loadaddr(pgm, addr/a_div); + stk500_loadaddr(pgm, m, addr/a_div); buf[0] = Cmnd_STK_READ_PAGE; buf[1] = (block_size >> 8) & 0xff; buf[2] = block_size & 0xff;