--- stk500-orig.c 2011-09-15 15:37:04.000000000 +0100 +++ stk500.c 2013-12-06 22:45:30.000000000 +0000 @@ -689,14 +689,31 @@ } -static int stk500_loadaddr(PROGRAMMER * pgm, unsigned int addr) +static int stk500_loadaddr(PROGRAMMER * pgm, unsigned int addr, char ext_addr) { unsigned char buf[16]; int tries; + static unsigned char prev_ext_byte = 0xff; /* Ensures it is set first time */ + unsigned char ext_byte; tries = 0; retry: tries++; + + /* To support flash > 64K words the correct Extended Address Byte has to be programmed. Use cmnd-universal */ + if (ext_addr) { + ext_byte = (addr >> 16) & 0xff; + if (ext_byte != prev_ext_byte) { + /* 'Load Extended Address Byte' 0x4d,0x00 see Atmel datasheets, 'Serial Programming Instructions' */ + buf[0] = 0x4d; + buf[1] = 0x00; + buf[2] = ext_byte; + buf[3] = 0x00; + stk500_cmd(pgm, buf, buf); + prev_ext_byte = ext_byte; + } + } + buf[0] = Cmnd_STK_LOAD_ADDRESS; buf[1] = addr & 0xff; buf[2] = (addr >> 8) & 0xff; @@ -751,6 +768,7 @@ unsigned int n; unsigned int i; int flash; + char use_ext_addr = 0; if (page_size == 0) { // MIB510 uses page size of 256 bytes @@ -765,6 +783,9 @@ if (strcmp(m->desc, "flash") == 0) { memtype = 'F'; flash = 1; + /* To support flash > 128K (64K words) use an Extended Address Byte */ + if (m->size > 131072) + use_ext_addr = 1; } else if (strcmp(m->desc, "eeprom") == 0) { memtype = 'E'; @@ -822,7 +843,7 @@ tries = 0; retry: tries++; - stk500_loadaddr(pgm, addr/a_div); + stk500_loadaddr(pgm, addr/a_div, use_ext_addr); /* build command block and avoid multiple send commands as it leads to a crash of the silabs usb serial driver on mac os x */ @@ -895,9 +916,13 @@ int tries; unsigned int n; int block_size; + char use_ext_addr = 0; if (strcmp(m->desc, "flash") == 0) { memtype = 'F'; + /* To support flash > 128K (64K words) use an Extended Address Byte */ + if (m->size > 131072) + use_ext_addr = 1; } else if (strcmp(m->desc, "eeprom") == 0) { memtype = 'E'; @@ -939,7 +964,7 @@ tries = 0; retry: tries++; - stk500_loadaddr(pgm, addr/a_div); + stk500_loadaddr(pgm, addr/a_div, use_ext_addr); buf[0] = Cmnd_STK_READ_PAGE; buf[1] = (block_size >> 8) & 0xff; buf[2] = block_size & 0xff;