Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fuses, calibration, sernum and tempsense memories to XMEGAs #1829

Merged
merged 34 commits into from
Jul 15, 2024

Conversation

stefanrueger
Copy link
Collaborator

... so that XMEGAs now also have the fuses memory encompassing all fuseX memories just like the modern AVRs. Also provides the sub-memories in prodsig. As bonus adds sernum to those 4 classic parts that have prodisig.

Perhaps neater to test this PR after multi-memory files have been tested, so can check out avrdude -U ALL:r:backup and then avrdude -U ALL:w:backup. As always be prepared to lose chips when testing without having carefully checked what the command does under -c dryrun. However, only testing with a real programmer will reveal whether the programmer understands the new memories. Backing up and restoring with -U ALL is a great way of checking out how well a pogrammer implementation deals with exotic memories.

This commit makes the parent of XMEGA parts one of three common ones:
 - .xmega-ab for XMEGA-A or XMEGA-B parts
 - .xmega-cd for XMEGA-C or XMEGA-D parts
 - .xmega-e for  XMEGA-E parts

The commit does not change any feature of parts or memories.
In order to prove this carry out before this commit

$ avrdude -p */At >/tmp/1

After this commit, the following command should not have any output:

$ avrdude -p */At | sort | comm -3 - /tmp/1 | grep -v family.common.values

(The only feature changes in the .xmega-* prototypes.)
@mcuee mcuee added the enhancement New feature or request label Jun 29, 2024
@stefanrueger
Copy link
Collaborator Author

OK, backup and restore with the new memories seems to work, at least for the AVRISP mkII programmer, see below. Can someone check all other PDI programmers, please? JTAG and the like... @MCUdude?

Also this PR adds sernum to m324pb, m328pb, t102 and t104. Can someone with an m328pb please check whether ALL backup and restore works? @mcuee?

The new multi-memory files and -U command are really helpful to smoke out whether a programmer can deal with all memories.

Backup

$ avrdude -p x256A3u -c avrispmkii -U ALL:r:x256a3u-real-backup.hex
avrdude: AVR device initialized and ready to accept instructions
avrdude: device signature = 0x1e9842 (probably x256a3u)
avrdude: Note: programmer supports page erase for Xmega devices.
         Each page will be erased before programming it, but no chip erase is performed.
         To disable page erases, specify the -D option; for a chip-erase, use the -e option.
         reading multiple memories ...
         eeprom         | ################################################## | 100% 0.11 s 
         flash          | ################################################## | 100% 3.01 s 
         application    | ################################################## | 100% 2.95 s 
         apptable       | ################################################## | 100% 0.10 s 
         boot           | ################################################## | 100% 0.09 s 
         fuses          | ################################################## | 100% 0.00 s 
         fuse0          | ################################################## | 100% 0.00 s 
         fuse1          | ################################################## | 100% 0.00 s 
         fuse2          | ################################################## | 100% 0.00 s 
         fuse4          | ################################################## | 100% 0.00 s 
         fuse5          | ################################################## | 100% 0.00 s 
         lock           | ################################################## | 100% 0.00 s 
         prodsig/sigrow | ################################################## | 100% 0.02 s 
         calibration    | ################################################## | 100% 0.00 s 
         sernum         | ################################################## | 100% 0.01 s 
         tempsense      | ################################################## | 100% 0.00 s 
         usersig        | ################################################## | 100% 0.01 s 
         signature      | ################################################## | 100% 0.00 s 
         writing 553560 bytes to output file x256a3u-real-backup.hex

avrdude done.  Thank you.

Restore

$ avrdude -p x256A3u -c avrispmkii -U ALL:w:x256a3u-real-backup.hex
avrdude: AVR device initialized and ready to accept instructions
avrdude: device signature = 0x1e9842 (probably x256a3u)
avrdude: Note: programmer supports page erase for Xmega devices.
         Each page will be erased before programming it, but no chip erase is performed.
         To disable page erases, specify the -D option; for a chip-erase, use the -e option.
avrdude: reading 275007 bytes for multiple memories from input file x256a3u-real-backup.hex
avrdude: 4096 bytes eeprom in 1 section [0, 0xfff]: 128 pages and 0 pad bytes
         writing 4096 bytes to eeprom ...
         writing | ################################################## | 100% 2.04 s 
         reading | ################################################## | 100% 0.11 s 
         4096 bytes of eeprom verified
avrdude: 270336 bytes flash in 1 section [0, 0x41fff]: 528 pages and 0 pad bytes
         writing 270336 bytes to flash ...
         writing | ################################################## | 100% 17.20 s 
         reading | ################################################## | 100% 3.00 s 
         270336 bytes of flash verified
avrdude: 262144 bytes application in 1 section [0, 0x3ffff]: 512 pages and 0 pad bytes
         writing 262144 bytes to application ...
         writing | ################################################## | 100% 16.66 s 
         reading | ################################################## | 100% 2.95 s 
         262144 bytes of application verified
avrdude: 8192 bytes apptable in 1 section [0, 0x1fff]: 16 pages and 0 pad bytes
         writing 8192 bytes to apptable ...
         writing | ################################################## | 100% 0.52 s 
         reading | ################################################## | 100% 0.09 s 
         8192 bytes of apptable verified
avrdude: 8192 bytes boot in 1 section [0, 0x1fff]: 16 pages and 0 pad bytes
         writing 8192 bytes to boot ...
         writing | ################################################## | 100% 0.52 s 
         reading | ################################################## | 100% 0.09 s 
         8192 bytes of boot verified
avrdude: 7 bytes fuses in 1 section [0, 6]
         writing 7 bytes to fuses ..., 7 bytes written, 7 verified
avrdude: 1 byte fuse0 in 1 section [0, 0]
         writing 1 byte to fuse0 (0xff), 1 byte written, 1 verified
avrdude: 1 byte fuse1 in 1 section [0, 0]
         writing 1 byte to fuse1 (0x00), 1 byte written, 1 verified
avrdude: 1 byte fuse2 in 1 section [0, 0]
         writing 1 byte to fuse2 (0xbf), 1 byte written, 1 verified
avrdude: 1 byte fuse4 in 1 section [0, 0]
         writing 1 byte to fuse4 (0xfe), 1 byte written, 1 verified
avrdude: 1 byte fuse5 in 1 section [0, 0]
         writing 1 byte to fuse5 (0xff), 1 byte written, 1 verified
avrdude: 1 byte lock in 1 section [0, 0]
         writing 1 byte to lock (0xff), 1 byte written, 1 verified
avrdude: 512 bytes usersig in 1 section [0, 0x1ff]: 1 page and 0 pad bytes
         writing 512 bytes to usersig ...
         writing | ################################################## | 100% 0.03 s 
         reading | ################################################## | 100% 0.01 s 
         512 bytes of usersig verified

avrdude done.  Thank you.

@stefanrueger
Copy link
Collaborator Author

stefanrueger commented Jul 6, 2024

@mcuee @MCUdude @dl8dtl Although this PR deserves tests against more PDI programmer and tests for classic m328pb whether the programmers can deal with sernum I suggest merging soon as to avoid merge conflics with future work. We need to do thorough testing before the 8.0 release, and I suggest doing so with a single backup/restore/verify line using a good matrix of parts and programmers:

avrdude -c ... -p ... -U ALL:r:backup.hex -U ALL:w:backup.hex -U ALL:v:backup

This will smoke out programmers that cannot deal with some memories regardless.

@mcuee
Copy link
Collaborator

mcuee commented Jul 6, 2024

Yes I will test againt ATmega328PB over this weekend.

@mcuee
Copy link
Collaborator

mcuee commented Jul 7, 2024

@stefanrueger

Here is the test result for ATmega328PB. There is a problem with sernum.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1829.exe -C .\avrdude_pr1829.conf -c usbasp -p m328pb -U ALL:r:backup.hex -U ALL:w:backup.hex -U ALL:v:backup.hex
avrdude_pr1829: AVR device initialized and ready to accept instructions
avrdude_pr1829: device signature = 0x1e9516 (probably m328pb)
avrdude_pr1829: Note: carrying out an erase cycle as flash memory needs programming (-U ALL:w:...)
                specify the -D option to disable this feature
avrdude_pr1829: erasing chip

avrdude_pr1829: processing -U ALL:r:backup.hex:I
                reading multiple memories ...
                eeprom         | ################################################## | 100% 0.71 s
                flash          | ################################################## | 100% 6.03 s
                lfuse          | ################################################## | 100% 0.00 s
                hfuse          | ################################################## | 100% 0.00 s
                efuse          | ################################################## | 100% 0.00 s
                lock           | ################################################## | 100% 0.00 s
                prodsig/sigrow | ################################################## | 100% 0.10 s
                sernum         | -------------------------------------------------- | 0% 0.00 s
avrdude_pr1829 error: unable to read byte at address 0x0000
avrdude_pr1829 error: read operation not supported for memory sernum
avrdude_pr1829 warning: unable to read sernum (ret = -2), skipping...
                signature      | ################################################## | 100% 0.01 s
                calibration    | ################################################## | 100% 0.00 s
                writing 33824 bytes to output file backup.hex

avrdude_pr1829 done.  Thank you.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1829.exe -C .\avrdude_pr1829.conf -c usbasp -p m328pb -U ALL:w:backup.hex -U ALL:v:backup.hex
avrdude_pr1829: AVR device initialized and ready to accept instructions
avrdude_pr1829: device signature = 0x1e9516 (probably m328pb)
avrdude_pr1829: Note: carrying out an erase cycle as flash memory needs programming (-U ALL:w:...)
                specify the -D option to disable this feature
avrdude_pr1829: erasing chip

avrdude_pr1829: processing -U ALL:w:backup.hex:i
avrdude_pr1829: reading 33824 bytes for multiple memories from input file backup.hex
avrdude_pr1829: 1024 bytes eeprom in 1 section [0, 0x3ff]: 256 pages and 0 pad bytes
                writing 1024 bytes to eeprom ...
                writing | ################################################## | 100% 11.70 s
                reading | ################################################## | 100% 0.68 s
                1024 bytes of eeprom verified
avrdude_pr1829: 32768 bytes flash in 1 section [0, 0x7fff]: 256 pages and 0 pad bytes
                writing 32768 bytes to flash ...
                writing | ################################################## | 100% 8.16 s
                reading | ################################################## | 100% 6.05 s
                32768 bytes of flash verified
avrdude_pr1829: 1 byte lfuse in 1 section [0, 0]
                writing 1 byte to lfuse (0xff), 1 byte written, 1 verified
avrdude_pr1829: 1 byte hfuse in 1 section [0, 0]
                writing 1 byte to hfuse (0xd6), 1 byte written, 1 verified
avrdude_pr1829: 1 byte efuse in 1 section [0, 0]
                writing 1 byte to efuse (0xf5), 1 byte written, 1 verified
avrdude_pr1829: 1 byte lock in 1 section [0, 0]
                writing 1 byte to lock (0xff), 1 byte written, 1 verified

avrdude_pr1829: processing -U ALL:v:backup.hex:i
avrdude_pr1829: verifying 33824 bytes of multiple memories against input file backup.hex
                reading | ################################################## | 100% 0.67 s
                1024 bytes of eeprom verified
                reading | ################################################## | 100% 6.06 s
                32768 bytes of flash verified
                1 byte of lfuse verified
                1 byte of hfuse verified
                1 byte of efuse verified
                1 byte of lock verified
                24 bytes of prodsig/sigrow verified
avrdude_pr1829 error: unable to read byte at address 0x0000
avrdude_pr1829 error: read operation not supported for memory sernum
avrdude_pr1829 error: unable to read all of sernum, rc = -2
                3 bytes of signature verified
                1 byte of calibration verified

avrdude_pr1829 done.  Thank you.

@mcuee
Copy link
Collaborator

mcuee commented Jul 7, 2024

@stefanrueger

For -c dryrun, just wondering if we can lower the warning message to info for verification mismatches of prodsig.

PS C:\work\avr\avrdude_test\avrdude_bin> 
.\avrdude_pr1829.exe -C .\avrdude_pr1829.conf -c dryrun -p m328pb -U ALL:r:backup.hex
 -U ALL:w:backup.hex -U ALL:v:backup.hex
avrdude_pr1829: AVR device initialized and ready to accept instructions
avrdude_pr1829: device signature = 0x1e9516 (probably m328pb)
avrdude_pr1829: Note: carrying out an erase cycle as flash memory needs programming (-U ALL:w:...)
                specify the -D option to disable this feature
avrdude_pr1829: erasing chip

avrdude_pr1829: processing -U ALL:r:backup.hex:I
                reading multiple memories ...
                eeprom         | ################################################## | 100% 0.34 s
                flash          | ################################################## | 100% 0.34 s
                lfuse          | ################################################## | 100% 0.00 s
                hfuse          | ################################################## | 100% 0.00 s
                efuse          | ################################################## | 100% 0.00 s
                lock           | ################################################## | 100% 0.00 s
                prodsig/sigrow | ################################################## | 100% 0.08 s
                sernum         | ################################################## | 100% 0.03 s
                signature      | ################################################## | 100% 0.00 s
                calibration    | ################################################## | 100% 0.00 s
                writing 33834 bytes to output file backup.hex

avrdude_pr1829: processing -U ALL:w:backup.hex:a
avrdude_pr1829: reading 33824 bytes for multiple memories from input file backup.hex
avrdude_pr1829: 1024 bytes eeprom in 1 section [0, 0x3ff]: 256 pages and 0 pad bytes
                writing 1024 bytes to eeprom ...
                writing | ################################################## | 100% 0.32 s
                reading | ################################################## | 100% 0.32 s
                1024 bytes of eeprom verified
avrdude_pr1829: 32768 bytes flash in 1 section [0, 0x7fff]: 256 pages and 0 pad bytes
                writing 32768 bytes to flash ...
                writing | ################################################## | 100% 0.32 s
                reading | ################################################## | 100% 0.32 s
                32768 bytes of flash verified
avrdude_pr1829: 1 byte lfuse in 1 section [0, 0]
                writing 1 byte to lfuse (0x62), 1 byte written, 1 verified
avrdude_pr1829: 1 byte hfuse in 1 section [0, 0]
                writing 1 byte to hfuse (0xd9), 1 byte written, 1 verified
avrdude_pr1829: 1 byte efuse in 1 section [0, 0]
                writing 1 byte to efuse (0xf7), 1 byte written, 1 verified
avrdude_pr1829: 1 byte lock in 1 section [0, 0]
                writing 1 byte to lock (0xff), 1 byte written, 1 verified

avrdude_pr1829: processing -U ALL:v:backup.hex:a
avrdude_pr1829: verifying 33824 bytes of multiple memories against input file backup.hex
                reading | ################################################## | 100% 0.32 s
                1024 bytes of eeprom verified
                reading | ################################################## | 100% 0.32 s
                32768 bytes of flash verified
                1 byte of lfuse verified
                1 byte of hfuse verified
                1 byte of efuse verified
                1 byte of lock verified
avrdude_pr1829 warning: prodsig verification mismatch
               device 0xff != input 0x55 at addr 0x000e (read only location: ignored)
               device 0xff != input 0x54 at addr 0x000f (read only location: ignored)
               device 0xff != input 0x53 at addr 0x0010 (read only location: ignored)
               device 0xff != input 0x52 at addr 0x0011 (read only location: ignored)
               device 0xff != input 0x51 at addr 0x0012 (read only location: ignored)
               device 0xff != input 0x50 at addr 0x0013 (read only location: ignored)
               device 0xff != input 0x4f at addr 0x0014 (read only location: ignored)
               device 0xff != input 0x4e at addr 0x0015 (read only location: ignored)
               device 0xff != input 0x4d at addr 0x0016 (read only location: ignored)
               device 0xff != input 0x4c at addr 0x0017 (read only location: ignored)
                24 bytes of prodsig/sigrow verified
                10 bytes of sernum verified
                3 bytes of signature verified
                1 byte of calibration verified

avrdude_pr1829 done.  Thank you.

@stefanrueger
Copy link
Collaborator Author

stefanrueger commented Jul 7, 2024

Thanks for testing @mcuee . I think I will have to abandon this PR and redo it after merging other PRs. This PR had a merge conflict that was not properly addressed. [edit: turned out the PR wasn't complete]

Although, on the surface of it, every entry of a classic part has been
changed, and some radically so by different parenting, there are only
subtle changes in AVRDUDE's internal representation:
 - 15 parts that did not have an io memory entry now have one
 - Some ISP opcodes were replaced with equivalent ones (x maps to 0)

This has been checked using the development options, particularly -p*/At,
before and after the changes.

The benefit of the new avrdude.conf is that every (non-TPI) classic part
is now derived from a common .classic or .classic-nocal stub. This enables
putting a common prodsig and sernum memory there. Generally, parenting
off now only happens between related chips. All in all, more systematic.
And it's some 10% smaller, too.
@stefanrueger
Copy link
Collaborator Author

All classic parts with calibration memory now export a prodsig and a sernum memory. I checked out a few parts and some seem to have data in sernum that could be varying from part to part and some don't.

@MCUdude
Copy link
Collaborator

MCUdude commented Jul 14, 2024

PR seems to work great!

It's interesting that the xmega calibration memory also contains the device signature. It's also odd that the serial number only consists of 0's. Maybe this is a JTAG3 related issue?

$ ./avrdude -cxplainedpro_pdi -px128a1u -t
avrdude: AVR device initialized and ready to accept instructions
avrdude: device signature = 0x1e974c (probably x128a1u)
avrdude> read fuses
Reading | ################################################## | 100% 0.01 s 
0000  ff 00 ff ff f3 f7 00                              |.......         |
avrdude> read fuse0
Reading | ################################################## | 100% 0.00 s 
0000  ff                                                |.               |
avrdude> read fuse1
Reading | ################################################## | 100% 0.00 s 
0000  00                                                |.               |
avrdude> read fuse2
Reading | ################################################## | 100% 0.00 s 
0000  ff                                                |.               |
avrdude> read fuse5
Reading | ################################################## | 100% 0.00 s 
0000  f7                                                |.               |
avrdude> read fuse6
avrdude error: (read) memory fuse6 not defined for part ATxmega128A1U
avrdude> read sig
Reading | ################################################## | 100% 0.00 s 
0000  1e 97 4c                                          |..L             |
avrdude> read cal
Reading | ################################################## | 100% 0.01 s 
0000  1e 97 4c 0b                                       |..L             |
avrdude> read sernum
Reading | ################################################## | 100% 0.03 s 
0000  00 00 00 00 00 00 00 00  00 00 00 00 00 00        |..............  |
avrdude> q

avrdude done.  Thank you.

EDIT:
I can confirm that there is an issue with the sernum memory on JTAG3 programmers. The PICkit4 in PDI mode couldn't read the serial number either. However, the AVRISPmkII works perfectly fine:

$ ./avrdude -cavrispmkii -px128a3u -t
avrdude: AVR device initialized and ready to accept instructions
avrdude: device signature = 0x1e9742 (probably x128a3u)
avrdude> read sernum
Reading | ################################################## | 100% 0.01 s 
0000  31 4a 30 37 33 30 ff ff  05 ff 0e 00 07 00        |1J0730........  |

@MCUdude
Copy link
Collaborator

MCUdude commented Jul 14, 2024

JTAG3 programmers are, however, able to read prodsig that already contains sernum. So it might just be a simple fix somewhere:

$ ./avrdude -cpickit4_pdi -px128a3u -t
ravrdude: AVR device initialized and ready to accept instructions
avrdude: device signature = 0x1e9742 (probably x128a3u)
avrdude> read prodsig
Reading | ################################################## | 100% 0.50 s 
0000  0c 40 75 0a 40 9f ff 01  31 4a 30 37 33 30 ff ff  | @u @...1J0730..|
0010  05 ff 0e 00 07 00 ff ff  ff ff 7e f8 24 40 ff ff  |..........~.$@..|
0020  00 00 00 ff 00 00 00 ff  ff ff ff ff ff ff 9f 09  |............... |
0030  ff ff 81 10                                       |....            |
avrdude> read sernum
Reading | ################################################## | 100% 0.12 s 
0000  00 00 00 00 00 00 00 00  00 00 00 00 00 00        |..............  |
avrdude> q

avrdude done.  Thank you.

@stefanrueger
Copy link
Collaborator Author

Thanks for testing @MCUdude ! Yes, the tests reveal a problem with the code.

simple fix somewhere

I need your help here.

XMEGAs don't have the signature in the prodsig area; it's available in the MCU I/O registers (go figure) at a very different address from prodsig, which houses the calibration bytes and sernum. So, the simple fix is to provide the correct JTAG command for the correct address region. I expect, as soon as that's done correctly, it works (TM). Trouble is, the commands are all called differently, depending on whether it's a TPI, non-TPI classic, XMEGA or UPDI part. And the other trouble is there are a number of programming interfaces (JTAG, ISP, TPI, debugWire, HVSP, HVPP, ...). And the next trouble is there are a few programmers with different FW. And the next trouble is the programmers all share files and get their access functions changed live by different initialisation in the same file depending on what part they see.

To cut a long short: I am a bit lost. Can you figure out which byte read code runs when you read from sernum or prodsig in the -c xplainedpro_pdi programmer? That will be a great help!

@stefanrueger
Copy link
Collaborator Author

It's interesting that the xmega calibration memory also contains the device signature. It's also odd that the serial number only consists of 0's. Maybe this is a JTAG3 related issue?

The programmer reads from the wrong memory area...

@MCUdude
Copy link
Collaborator

MCUdude commented Jul 14, 2024

To cut a long short: I am a bit lost. Can you figure out which byte read code runs when you read from sernum or prodsig in the -c xplainedpro_pdi programmer? That will be a great help!

I added a few print statements here and there, and here's the result. It doesn't print anything special when I'm running read sernum, just like it's cached or something:

modified jtag3_read_byte()
tatic int jtag3_read_byte(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *mem,
                           unsigned long addr, unsigned char * value) {
  unsigned char cmd[12];
  unsigned char *resp, *cache_ptr = NULL;
  int status, unsupp = 0;
  unsigned long paddr = 0UL, *paddr_ptr = NULL;
  unsigned int pagesize = 0;

  pmsg_notice2("jtag3_read_byte(.., %s, 0x%lx, ...)\n", mem->desc, addr);

  paddr = jtag3_memaddr(pgm, p, mem, addr);
  if (paddr != addr)
    imsg_debug("addr 0x%lx mapped to address 0x%lx\n", addr, paddr);
  paddr = 0;

  if (mem->size < 1) {
    pmsg_error("cannot read byte from %s %s owing to its size %d\n", p->desc, mem->desc, mem->size);
    return -1;
  }
  if (addr >= (unsigned long) mem->size) {
    pmsg_error("cannot read byte from %s %s as address 0x%04lx outside range [0, 0x%04x]\n",
      p->desc, mem->desc, addr, mem->size-1);
    return -1;
  }

  if (!(pgm->flag & PGM_FL_IS_DW))
    if ((status = jtag3_program_enable(pgm)) < 0)
      return status;

  cmd[0] = SCOPE_AVR;
  cmd[1] = CMD3_READ_MEMORY;
  cmd[2] = 0;

  cmd[3] = p->prog_modes & (PM_PDI | PM_UPDI)? MTYPE_FLASH: MTYPE_FLASH_PAGE;
  if (mem_is_in_flash(mem)) {
    addr += mem->offset & (512 * 1024 - 1); /* max 512 KiB flash @@@ could be max 8M */
    pagesize = PDATA(pgm)->flash_pagesize;
    paddr = addr & ~(pagesize - 1);
    paddr_ptr = &PDATA(pgm)->flash_pageaddr;
    cache_ptr = PDATA(pgm)->flash_pagecache;
    msg_info("mem_is_in_flash\n");
  } else if (mem_is_eeprom(mem)) {
    if ( (pgm->flag & PGM_FL_IS_DW) || (p->prog_modes & PM_PDI) || (p->prog_modes & PM_UPDI) ) {
      cmd[3] = MTYPE_EEPROM;
      msg_info("mem_is_eeprom, MTYPE_EEPROM\n");
    } else {
      cmd[3] = MTYPE_EEPROM_PAGE;
    }
    pagesize = mem->page_size;
    paddr = addr & ~(pagesize - 1);
    paddr_ptr = &PDATA(pgm)->eeprom_pageaddr;
    cache_ptr = PDATA(pgm)->eeprom_pagecache;
  } else if (mem_is_a_fuse(mem) || mem_is_fuses(mem)) {
    cmd[3] = MTYPE_FUSE_BITS;
    msg_info("mem_is_a_fuse\n");
    if(!(p->prog_modes & PM_UPDI) && mem_is_a_fuse(mem))
      addr = mem_fuse_offset(mem);
    if (pgm->flag & PGM_FL_IS_DW)
      unsupp = 1;
  } else if (mem_is_lock(mem)) {
    msg_info("mem_is_lock\n");
    cmd[3] = MTYPE_LOCK_BITS;
    if (pgm->flag & PGM_FL_IS_DW)
      unsupp = 1;
  } else if (mem_is_userrow(mem)) {
    msg_info("mem_is_userrow\n");
    cmd[3] = MTYPE_USERSIG;
  } else if (mem_is_sigrow(mem)) {
    if (p->prog_modes & (PM_PDI | PM_UPDI)) {
      cmd[3] = MTYPE_PRODSIG;
      msg_info("mem_is_sigrow\n");
    } else {
      cmd[3] = addr&1? MTYPE_OSCCAL_BYTE: MTYPE_SIGN_JTAG;
      addr /= 2;
      if (pgm->flag & PGM_FL_IS_DW)
        unsupp = 1;
    }
  } else if ((p->prog_modes & PM_Classic) && mem_is_calibration(mem)) { // Classic part calibration
    cmd[3] = MTYPE_OSCCAL_BYTE;
    if (pgm->flag & PGM_FL_IS_DW)
      unsupp = 1;
  } else if (mem_is_io(mem) || mem_is_sram(mem)) {
    cmd[3] = MTYPE_SRAM;
    msg_info("mem_is_io || mem_is_sram\n");
  } else if (mem_is_sib(mem)) {
    if(addr >= AVR_SIBLEN) {
      pmsg_error("cannot read byte from %s sib as address 0x%04lx outside range [0, 0x%04x]\n",
        p->desc, addr, AVR_SIBLEN-1);
      return -1;
    }
    if(!*PDATA(pgm)->sib_string) {
      pmsg_error("cannot read byte from %s sib as memory not initialised\n", p->desc);
      return -1;
    }
    *value = PDATA(pgm)->sib_string[addr];
    return 0;
  } else if (mem_is_signature(mem)) {
    msg_info("mem_is_signature\n");
    cmd[3] = MTYPE_SIGN_JTAG;

    /*
     * dW can read out the signature on JTAGICE3, but only allows
     * for a full three-byte read.  We cache them in a local
     * variable to avoid multiple reads.  This optimization does not
     * harm for other connection types either.
     */
    u32_to_b4(cmd + 8, 3);
    u32_to_b4(cmd + 4, jtag3_memaddr(pgm, p, mem, addr));

    if (addr == 0) {
      if ((status = jtag3_command(pgm, cmd, 12, &resp, "read memory")) < 0)
        return status;

      PDATA(pgm)->signature_cache[0] = resp[4];
      PDATA(pgm)->signature_cache[1] = resp[5];
      *value = resp[3];
      mmt_free(resp);
      return 0;
    } else if (addr <= 2) {
      *value = PDATA(pgm)->signature_cache[addr - 1];
      return 0;
    } else {
      /* should not happen */
      msg_error("address out of range for signature memory: %lu\n", addr);
      return -1;
    }
  } else if(mem_is_in_sigrow(mem)) { // sigrow submemories but not signature nor sigrow itself
    cmd[3] = (p->prog_modes & PM_PDI)? MTYPE_SIGN_JTAG: MTYPE_PRODSIG;
    AVRMEM *sigrow = avr_locate_sigrow(p);
    if(sigrow)
      addr += mem->offset - sigrow->offset; // Adjust offset for parent memory
  } else {
    pmsg_error("unknown memory %s\n", mem->desc);
    return -1;
  }

  /*
   * If the respective memory area is not supported under debugWire,
   * leave here.
   */
  if (unsupp) {
    *value = 42;
    return -1;
  }

  /*
   * To improve the read speed, we used paged reads for flash and
   * EEPROM, and cache the results in a page cache.
   *
   * Page cache validation is based on "{flash,eeprom}_pageaddr"
   * (holding the base address of the most recent cache fill
   * operation).  This variable is set to (unsigned long)-1L when the
   * cache needs to be invalidated.
   */
  if (pagesize && paddr == *paddr_ptr) {
    *value = cache_ptr[addr & (pagesize - 1)];
    return 0;
  }

  if (pagesize) {
    u32_to_b4(cmd + 8, pagesize);
    u32_to_b4(cmd + 4, jtag3_memaddr(pgm, p, mem, paddr));

  } else {
    u32_to_b4(cmd + 8, 1);
    u32_to_b4(cmd + 4, jtag3_memaddr(pgm, p, mem, addr));
  }

  if ((status = jtag3_command(pgm, cmd, 12, &resp, "read memory")) < 0)
    return status;

  if (resp[1] != RSP3_DATA ||
      status < (int) (pagesize? pagesize: 1) + 4) {
    pmsg_error("wrong/short reply to read memory command\n");
    mmt_free(resp);
    return -1;
  }

  if (pagesize) {
    *paddr_ptr = paddr;
    memcpy(cache_ptr, resp + 3, pagesize);
    *value = cache_ptr[addr & (pagesize - 1)];
  } else
    *value = resp[3];

  mmt_free(resp);
  return 0;
}
$ ./avrdude -cxplainedpro_pdi -patxmega128a1u -t
mem_is_io || mem_is_sram
avrdude: AVR device initialized and ready to accept instructions
mem_is_signature
mem_is_signature
mem_is_signature
avrdude: device signature = 0x1e974c (probably x128a1u)
avrdude> read sernum
Reading | ################################################## | 100% 0.03 s 
0000  00 00 00 00 00 00 00 00  00 00 00 00 00 00        |..............  |
avrdude> read cal
Reading | ################################################## | 100% 0.01 s 
0000  1e 97 4c 0b                                       |..L             |
avrdude> read prodsig
Reading |                                                    | 0% 0.00 s mem_is_sigrow
mem_is_sigrow
Reading | #                                                  | 1% 0.01 s mem_is_sigrow
Reading | ##                                                 | 3% 0.01 s mem_is_sigrow
Reading | ##                                                 | 4% 0.01 s mem_is_sigrow
Reading | ###                                                | 6% 0.01 s mem_is_sigrow
Reading | ####                                               | 7% 0.01 s mem_is_sigrow
Reading | #####                                              | 9% 0.02 s mem_is_sigrow
Reading | #####                                              | 10% 0.02 s mem_is_sigrow
Reading | ######                                             | 12% 0.02 s mem_is_sigrow
Reading | #######                                            | 14% 0.02 s mem_is_sigrow
Reading | ########                                           | 15% 0.02 s mem_is_sigrow
Reading | #########                                          | 17% 0.03 s mem_is_sigrow
Reading | #########                                          | 18% 0.03 s mem_is_sigrow
Reading | ##########                                         | 20% 0.03 s mem_is_sigrow
Reading | ###########                                        | 21% 0.03 s mem_is_sigrow
Reading | ############                                       | 23% 0.03 s mem_is_sigrow
Reading | #############                                      | 25% 0.04 s mem_is_sigrow
Reading | #############                                      | 26% 0.04 s mem_is_sigrow
Reading | ##############                                     | 28% 0.04 s mem_is_sigrow
Reading | ###############                                    | 29% 0.04 s mem_is_sigrow
Reading | ################                                   | 31% 0.04 s mem_is_sigrow
Reading | ################                                   | 32% 0.05 s mem_is_sigrow
Reading | #################                                  | 34% 0.05 s mem_is_sigrow
Reading | ##################                                 | 35% 0.05 s mem_is_sigrow
Reading | ###################                                | 37% 0.05 s mem_is_sigrow
Reading | ####################                               | 39% 0.05 s mem_is_sigrow
Reading | ####################                               | 40% 0.06 s mem_is_sigrow
Reading | #####################                              | 42% 0.06 s mem_is_sigrow
Reading | ######################                             | 43% 0.06 s mem_is_sigrow
Reading | #######################                            | 45% 0.07 s mem_is_sigrow
Reading | #######################                            | 46% 0.07 s mem_is_sigrow
Reading | ########################                           | 48% 0.07 s mem_is_sigrow
Reading | #########################                          | 50% 0.07 s mem_is_sigrow
Reading | ##########################                         | 51% 0.07 s mem_is_sigrow
Reading | ###########################                        | 53% 0.08 s mem_is_sigrow
Reading | ###########################                        | 54% 0.08 s mem_is_sigrow
Reading | ############################                       | 56% 0.08 s mem_is_sigrow
Reading | #############################                      | 57% 0.08 s mem_is_sigrow
Reading | ##############################                     | 59% 0.08 s mem_is_sigrow
Reading | ##############################                     | 60% 0.09 s mem_is_sigrow
Reading | ###############################                    | 62% 0.09 s mem_is_sigrow
Reading | ################################                   | 64% 0.09 s mem_is_sigrow
Reading | #################################                  | 65% 0.09 s mem_is_sigrow
Reading | ##################################                 | 67% 0.09 s mem_is_sigrow
Reading | ##################################                 | 68% 0.10 s mem_is_sigrow
Reading | ###################################                | 70% 0.10 s mem_is_sigrow
Reading | ####################################               | 71% 0.10 s mem_is_sigrow
Reading | #####################################              | 73% 0.10 s mem_is_sigrow
Reading | ######################################             | 75% 0.10 s mem_is_sigrow
Reading | ######################################             | 76% 0.11 s mem_is_sigrow
Reading | #######################################            | 78% 0.11 s mem_is_sigrow
Reading | ########################################           | 79% 0.11 s mem_is_sigrow
Reading | #########################################          | 81% 0.11 s mem_is_sigrow
Reading | #########################################          | 82% 0.12 s mem_is_sigrow
Reading | ##########################################         | 84% 0.12 s mem_is_sigrow
Reading | ###########################################        | 85% 0.12 s mem_is_sigrow
Reading | ############################################       | 87% 0.12 s mem_is_sigrow
Reading | #############################################      | 89% 0.13 s mem_is_sigrow
Reading | #############################################      | 90% 0.13 s mem_is_sigrow
Reading | ##############################################     | 92% 0.13 s mem_is_sigrow
Reading | ###############################################    | 93% 0.13 s mem_is_sigrow
Reading | ################################################   | 95% 0.13 s mem_is_sigrow
Reading | ################################################   | 96% 0.14 s mem_is_sigrow
Reading | ################################################## | 100% 0.14 s 
0000  0a 40 5c 05 40 ff ff 00  31 55 32 36 36 33 ff ff  | @\.@...1U2663..|
0010  09 ff 04 00 07 00 ff ff  ff ff cf 07 1b 40 ff ff  | ............@..|
0020  44 04 00 ff 44 04 00 ff  ff ff ff ff ff ff fe 09  |D...D.......... |
0030  0e 92 0c 29 04 2d 11 89  ff ff ff ff ff ff ff ff  |.. ).-..........|

@MCUdude
Copy link
Collaborator

MCUdude commented Jul 14, 2024

Ignore my previous post. Here's the output when reading sernum:

$ ./avrdude -cxplainedpro_pdi -patxmega128a1u -t
mem_is_io || mem_is_sram
avrdude: AVR device initialized and ready to accept instructions
mem_is_signature
mem_is_signature
mem_is_signature
avrdude: device signature = 0x1e974c (probably x128a1u)
avrdude> read sernum
Reading |                                                    | 0% 0.00 s   mem_is_in_sigrow(). addr: 0x8, mem->offset: 0x8e0208, sigrow->offset: 0x8e0200
  mem_is_in_sigrow(). addr: 0x9, mem->offset: 0x8e0208, sigrow->offset: 0x8e0200
Reading | ####                                               | 7% 0.00 s   mem_is_in_sigrow(). addr: 0xa, mem->offset: 0x8e0208, sigrow->offset: 0x8e0200
Reading | #######                                            | 14% 0.01 s   mem_is_in_sigrow(). addr: 0xb, mem->offset: 0x8e0208, sigrow->offset: 0x8e0200
Reading | ###########                                        | 21% 0.01 s   mem_is_in_sigrow(). addr: 0xc, mem->offset: 0x8e0208, sigrow->offset: 0x8e0200
Reading | ##############                                     | 28% 0.01 s   mem_is_in_sigrow(). addr: 0xd, mem->offset: 0x8e0208, sigrow->offset: 0x8e0200
Reading | ##################                                 | 35% 0.01 s   mem_is_in_sigrow(). addr: 0xe, mem->offset: 0x8e0208, sigrow->offset: 0x8e0200
Reading | #####################                              | 42% 0.01 s   mem_is_in_sigrow(). addr: 0xf, mem->offset: 0x8e0208, sigrow->offset: 0x8e0200
Reading | #########################                          | 50% 0.02 s   mem_is_in_sigrow(). addr: 0x10, mem->offset: 0x8e0208, sigrow->offset: 0x8e0200
Reading | #############################                      | 57% 0.02 s   mem_is_in_sigrow(). addr: 0x11, mem->offset: 0x8e0208, sigrow->offset: 0x8e0200
Reading | ################################                   | 64% 0.02 s   mem_is_in_sigrow(). addr: 0x12, mem->offset: 0x8e0208, sigrow->offset: 0x8e0200
Reading | ####################################               | 71% 0.02 s   mem_is_in_sigrow(). addr: 0x13, mem->offset: 0x8e0208, sigrow->offset: 0x8e0200
Reading | #######################################            | 78% 0.02 s   mem_is_in_sigrow(). addr: 0x14, mem->offset: 0x8e0208, sigrow->offset: 0x8e0200
Reading | ###########################################        | 85% 0.02 s   mem_is_in_sigrow(). addr: 0x15, mem->offset: 0x8e0208, sigrow->offset: 0x8e0200
Reading | ################################################## | 100% 0.03 s 
0000  00 00 00 00 00 00 00 00  00 00 00 00 00 00        |..............  |
avrdude> read prodsig
Reading |                                                    | 0% 0.00 s mem_is_sigrow
mem_is_sigrow
Reading | #                                                  | 1% 0.00 s mem_is_sigrow
Reading | ##                                                 | 3% 0.01 s mem_is_sigrow
Reading | ##                                                 | 4% 0.01 s mem_is_sigrow
Reading | ###                                                | 6% 0.01 s mem_is_sigrow
Reading | ####                                               | 7% 0.01 s mem_is_sigrow
Reading | #####                                              | 9% 0.01 s mem_is_sigrow
Reading | #####                                              | 10% 0.02 s mem_is_sigrow
Reading | ######                                             | 12% 0.02 s mem_is_sigrow
Reading | #######                                            | 14% 0.02 s mem_is_sigrow
Reading | ########                                           | 15% 0.02 s mem_is_sigrow
Reading | #########                                          | 17% 0.03 s mem_is_sigrow
Reading | #########                                          | 18% 0.03 s mem_is_sigrow
Reading | ##########                                         | 20% 0.03 s mem_is_sigrow
Reading | ###########                                        | 21% 0.03 s mem_is_sigrow
Reading | ############                                       | 23% 0.03 s mem_is_sigrow
Reading | #############                                      | 25% 0.04 s mem_is_sigrow
Reading | #############                                      | 26% 0.04 s mem_is_sigrow
Reading | ##############                                     | 28% 0.04 s mem_is_sigrow
Reading | ###############                                    | 29% 0.04 s mem_is_sigrow
Reading | ################                                   | 31% 0.04 s mem_is_sigrow
Reading | ################                                   | 32% 0.05 s mem_is_sigrow
Reading | #################                                  | 34% 0.05 s mem_is_sigrow
Reading | ##################                                 | 35% 0.05 s mem_is_sigrow
Reading | ###################                                | 37% 0.05 s mem_is_sigrow
Reading | ####################                               | 39% 0.06 s mem_is_sigrow
Reading | ####################                               | 40% 0.06 s mem_is_sigrow
Reading | #####################                              | 42% 0.06 s mem_is_sigrow
Reading | ######################                             | 43% 0.06 s mem_is_sigrow
Reading | #######################                            | 45% 0.07 s mem_is_sigrow
Reading | #######################                            | 46% 0.07 s mem_is_sigrow
Reading | ########################                           | 48% 0.07 s mem_is_sigrow
Reading | #########################                          | 50% 0.07 s mem_is_sigrow
Reading | ##########################                         | 51% 0.07 s mem_is_sigrow
Reading | ###########################                        | 53% 0.08 s mem_is_sigrow
Reading | ###########################                        | 54% 0.08 s mem_is_sigrow
Reading | ############################                       | 56% 0.08 s mem_is_sigrow
Reading | #############################                      | 57% 0.08 s mem_is_sigrow
Reading | ##############################                     | 59% 0.08 s mem_is_sigrow
Reading | ##############################                     | 60% 0.09 s mem_is_sigrow
Reading | ###############################                    | 62% 0.09 s mem_is_sigrow
Reading | ################################                   | 64% 0.09 s mem_is_sigrow
Reading | #################################                  | 65% 0.09 s mem_is_sigrow
Reading | ##################################                 | 67% 0.09 s mem_is_sigrow
Reading | ##################################                 | 68% 0.10 s mem_is_sigrow
Reading | ###################################                | 70% 0.10 s mem_is_sigrow
Reading | ####################################               | 71% 0.10 s mem_is_sigrow
Reading | #####################################              | 73% 0.10 s mem_is_sigrow
Reading | ######################################             | 75% 0.11 s mem_is_sigrow
Reading | ######################################             | 76% 0.11 s mem_is_sigrow
Reading | #######################################            | 78% 0.11 s mem_is_sigrow
Reading | ########################################           | 79% 0.11 s mem_is_sigrow
Reading | #########################################          | 81% 0.12 s mem_is_sigrow
Reading | #########################################          | 82% 0.12 s mem_is_sigrow
Reading | ##########################################         | 84% 0.12 s mem_is_sigrow
Reading | ###########################################        | 85% 0.12 s mem_is_sigrow
Reading | ############################################       | 87% 0.12 s mem_is_sigrow
Reading | #############################################      | 89% 0.13 s mem_is_sigrow
Reading | #############################################      | 90% 0.13 s mem_is_sigrow
Reading | ##############################################     | 92% 0.13 s mem_is_sigrow
Reading | ###############################################    | 93% 0.13 s mem_is_sigrow
Reading | ################################################   | 95% 0.13 s mem_is_sigrow
Reading | ################################################   | 96% 0.14 s mem_is_sigrow
Reading | ################################################## | 100% 0.14 s 
0000  0a 40 5c 05 40 ff ff 00  31 55 32 36 36 33 ff ff  | @\.@...1U2663..|
0010  09 ff 04 00 07 00 ff ff  ff ff cf 07 1b 40 ff ff  | ............@..|
0020  44 04 00 ff 44 04 00 ff  ff ff ff ff ff ff fe 09  |D...D.......... |
0030  0e 92 0c 29 04 2d 11 89  ff ff ff ff ff ff ff ff  |.. ).-..........|
avrdude> q

avrdude done.  Thank you.
Modified jtag3_read_byte()
static int jtag3_read_byte(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *mem,
                           unsigned long addr, unsigned char * value) {
  unsigned char cmd[12];
  unsigned char *resp, *cache_ptr = NULL;
  int status, unsupp = 0;
  unsigned long paddr = 0UL, *paddr_ptr = NULL;
  unsigned int pagesize = 0;

  pmsg_notice2("jtag3_read_byte(.., %s, 0x%lx, ...)\n", mem->desc, addr);

  paddr = jtag3_memaddr(pgm, p, mem, addr);
  if (paddr != addr)
    imsg_debug("addr 0x%lx mapped to address 0x%lx\n", addr, paddr);
  paddr = 0;

  if (mem->size < 1) {
    pmsg_error("cannot read byte from %s %s owing to its size %d\n", p->desc, mem->desc, mem->size);
    return -1;
  }
  if (addr >= (unsigned long) mem->size) {
    pmsg_error("cannot read byte from %s %s as address 0x%04lx outside range [0, 0x%04x]\n",
      p->desc, mem->desc, addr, mem->size-1);
    return -1;
  }

  if (!(pgm->flag & PGM_FL_IS_DW))
    if ((status = jtag3_program_enable(pgm)) < 0)
      return status;

  cmd[0] = SCOPE_AVR;
  cmd[1] = CMD3_READ_MEMORY;
  cmd[2] = 0;

  cmd[3] = p->prog_modes & (PM_PDI | PM_UPDI)? MTYPE_FLASH: MTYPE_FLASH_PAGE;
  if (mem_is_in_flash(mem)) {
    addr += mem->offset & (512 * 1024 - 1); /* max 512 KiB flash @@@ could be max 8M */
    pagesize = PDATA(pgm)->flash_pagesize;
    paddr = addr & ~(pagesize - 1);
    paddr_ptr = &PDATA(pgm)->flash_pageaddr;
    cache_ptr = PDATA(pgm)->flash_pagecache;
    msg_info("mem_is_in_flash\n");
  } else if (mem_is_eeprom(mem)) {
    if ( (pgm->flag & PGM_FL_IS_DW) || (p->prog_modes & PM_PDI) || (p->prog_modes & PM_UPDI) ) {
      cmd[3] = MTYPE_EEPROM;
      msg_info("mem_is_eeprom, MTYPE_EEPROM\n");
    } else {
      cmd[3] = MTYPE_EEPROM_PAGE;
    }
    pagesize = mem->page_size;
    paddr = addr & ~(pagesize - 1);
    paddr_ptr = &PDATA(pgm)->eeprom_pageaddr;
    cache_ptr = PDATA(pgm)->eeprom_pagecache;
  } else if (mem_is_a_fuse(mem) || mem_is_fuses(mem)) {
    cmd[3] = MTYPE_FUSE_BITS;
    msg_info("mem_is_a_fuse\n");
    if(!(p->prog_modes & PM_UPDI) && mem_is_a_fuse(mem))
      addr = mem_fuse_offset(mem);
    if (pgm->flag & PGM_FL_IS_DW)
      unsupp = 1;
  } else if (mem_is_lock(mem)) {
    msg_info("mem_is_lock\n");
    cmd[3] = MTYPE_LOCK_BITS;
    if (pgm->flag & PGM_FL_IS_DW)
      unsupp = 1;
  } else if (mem_is_userrow(mem)) {
    msg_info("mem_is_userrow\n");
    cmd[3] = MTYPE_USERSIG;
  } else if (mem_is_sigrow(mem)) {
    if (p->prog_modes & (PM_PDI | PM_UPDI)) {
      cmd[3] = MTYPE_PRODSIG;
      msg_info("mem_is_sigrow\n");
    } else {
      cmd[3] = addr&1? MTYPE_OSCCAL_BYTE: MTYPE_SIGN_JTAG;
      addr /= 2;
      if (pgm->flag & PGM_FL_IS_DW)
        unsupp = 1;
    }
  } else if ((p->prog_modes & PM_Classic) && mem_is_calibration(mem)) { // Classic part calibration
    cmd[3] = MTYPE_OSCCAL_BYTE;
    if (pgm->flag & PGM_FL_IS_DW)
      unsupp = 1;
  } else if (mem_is_io(mem) || mem_is_sram(mem)) {
    cmd[3] = MTYPE_SRAM;
    msg_info("mem_is_io || mem_is_sram\n");
  } else if (mem_is_sib(mem)) {
    if(addr >= AVR_SIBLEN) {
      pmsg_error("cannot read byte from %s sib as address 0x%04lx outside range [0, 0x%04x]\n",
        p->desc, addr, AVR_SIBLEN-1);
      return -1;
    }
    if(!*PDATA(pgm)->sib_string) {
      pmsg_error("cannot read byte from %s sib as memory not initialised\n", p->desc);
      return -1;
    }
    *value = PDATA(pgm)->sib_string[addr];
    return 0;
  } else if (mem_is_signature(mem)) {
    msg_info("mem_is_signature\n");
    cmd[3] = MTYPE_SIGN_JTAG;

    /*
     * dW can read out the signature on JTAGICE3, but only allows
     * for a full three-byte read.  We cache them in a local
     * variable to avoid multiple reads.  This optimization does not
     * harm for other connection types either.
     */
    u32_to_b4(cmd + 8, 3);
    u32_to_b4(cmd + 4, jtag3_memaddr(pgm, p, mem, addr));

    if (addr == 0) {
      if ((status = jtag3_command(pgm, cmd, 12, &resp, "read memory")) < 0)
        return status;

      PDATA(pgm)->signature_cache[0] = resp[4];
      PDATA(pgm)->signature_cache[1] = resp[5];
      *value = resp[3];
      mmt_free(resp);
      return 0;
    } else if (addr <= 2) {
      *value = PDATA(pgm)->signature_cache[addr - 1];
      return 0;
    } else {
      /* should not happen */
      msg_error("address out of range for signature memory: %lu\n", addr);
      return -1;
    }
  } else if(mem_is_in_sigrow(mem)) { // sigrow submemories but not signature nor sigrow itself
    cmd[3] = (p->prog_modes & PM_PDI)? MTYPE_SIGN_JTAG: MTYPE_PRODSIG;
    AVRMEM *sigrow = avr_locate_sigrow(p);
    if(sigrow)
      addr += mem->offset - sigrow->offset; // Adjust offset for parent memory
    
    msg_info("  mem_is_in_sigrow(). addr: 0x%lx, mem->offset: 0x%lx, sigrow->offset: 0x%lx\n", addr, mem->offset, sigrow->offset);
  } else {
    pmsg_error("unknown memory %s\n", mem->desc);
    return -1;
  }

  /*
   * If the respective memory area is not supported under debugWire,
   * leave here.
   */
  if (unsupp) {
    *value = 42;
    return -1;
  }

  /*
   * To improve the read speed, we used paged reads for flash and
   * EEPROM, and cache the results in a page cache.
   *
   * Page cache validation is based on "{flash,eeprom}_pageaddr"
   * (holding the base address of the most recent cache fill
   * operation).  This variable is set to (unsigned long)-1L when the
   * cache needs to be invalidated.
   */
  if (pagesize && paddr == *paddr_ptr) {
    *value = cache_ptr[addr & (pagesize - 1)];
    return 0;
  }

  if (pagesize) {
    u32_to_b4(cmd + 8, pagesize);
    u32_to_b4(cmd + 4, jtag3_memaddr(pgm, p, mem, paddr));

  } else {
    u32_to_b4(cmd + 8, 1);
    u32_to_b4(cmd + 4, jtag3_memaddr(pgm, p, mem, addr));
  }

  if ((status = jtag3_command(pgm, cmd, 12, &resp, "read memory")) < 0)
    return status;

  if (resp[1] != RSP3_DATA ||
      status < (int) (pagesize? pagesize: 1) + 4) {
    pmsg_error("wrong/short reply to read memory command\n");
    mmt_free(resp);
    return -1;
  }

  if (pagesize) {
    *paddr_ptr = paddr;
    memcpy(cache_ptr, resp + 3, pagesize);
    *value = cache_ptr[addr & (pagesize - 1)];
  } else
    *value = resp[3];

  mmt_free(resp);
  return 0;
}

@stefanrueger
Copy link
Collaborator Author

stefanrueger commented Jul 14, 2024

@MCUdude Brilliant! I think this gets us closer: check out the latest commit

@MCUdude
Copy link
Collaborator

MCUdude commented Jul 14, 2024

There we go!

$ ./avrdude -cxplainedpro_pdi -patxmega128a1u -t
avrdude: AVR device initialized and ready to accept instructions
avrdude: device signature = 0x1e974c (probably x128a1u)
avrdude> read sernum
Reading | ################################################## | 100% 0.03 s 
0000  31 55 32 36 36 33 ff ff  09 ff 04 00 07 00        |1U2663.. .....  |
avrdude> q

avrdude done.  Thank you.

@stefanrueger
Copy link
Collaborator Author

Thanks! @MCUdude. The jtag3.c code is a bit fickle as it morphs depending on the parts, pgm i/f etc.

Would you be able to give the jtag3.c programmers a thorough spin re TPI, other classic parts, PDI and UPDI parts and see whether it reads the correct memory regions? There may be another, similar treadmine lurking!

@MCUdude
Copy link
Collaborator

MCUdude commented Jul 14, 2024

TPI (ATtiny9 + PICkit4):
$ ./avrdude -cpickit4_tpi -pattiny9 -t
avrdude: AVR device initialized and ready to accept instructions
avrdude: device signature = 0x1e9008 (probably t9)
avrdude> read flash
Reading | ################################################## | 100% 0.16 s 
0000  44 61 74 61 20 69 6e 20  66 6c 61 73 68 00 44 61  |Data in flash.Da|
0010  74 61 20 69 6e 20 66 6c  61 73 68 00 44 61 74 61  |ta in flash.Data|
0020  20 69 6e 20 66 6c 61 73  68 00 44 61 74 61 20 69  | in flash.Data i|
0030  6e 20 66 6c 61 73 68 00  44 61 74 61 20 69 6e 20  |n flash.Data in |
0040  66 6c 61 73 68 00 44 61  74 61 20 69 6e 20 66 6c  |flash.Data in fl|
0050  61 73 68 00 44 61 74 61  20 69 6e 20 66 6c 61 73  |ash.Data in flas|
0060  68 00 44 61 74 61 20 69  6e 20 66 6c 61 73 68 00  |h.Data in flash.|
0070  44 61 74 61 20 69 6e 20  66 6c 61 73 68 00 44 61  |Data in flash.Da|
0080  74 61 20 69 6e 20 66 6c  61 73 68 00 44 61 74 61  |ta in flash.Data|
0090  20 69 6e 20 66 6c 61 73  68 00 44 61 74 61 20 69  | in flash.Data i|
00a0  6e 20 66 6c 61 73 68 00  44 61 74 61 20 69 6e 20  |n flash.Data in |
00b0  66 6c 61 73 68 00 44 61  74 61 20 69 6e 20 66 6c  |flash.Data in fl|
00c0  61 73 68 00 44 61 74 61  20 69 6e 20 66 6c 61 73  |ash.Data in flas|
00d0  68 00 44 61 74 61 20 69  6e 20 66 6c 61 73 68 00  |h.Data in flash.|
00e0  44 61 74 61 20 69 6e 20  66 6c 61 73 68 00 44 61  |Data in flash.Da|
00f0  74 61 20 69 6e 20 66 6c  61 73 68 00 44 61 74 61  |ta in flash.Data|
avrdude> read cal
Reading | ################################################## | 100% 0.01 s 
0000  8a                                                |.               |
avrdude> read sig
Reading | ################################################## | 100% 0.01 s 
0000  1e 90 08                                          |...             |
avrdude> c
config rstdisbl=external_reset # 1
config wdton=wdt_programmable # 1
config ckout=co_disabled # 1
config lb=no_lock # 3
avrdude> read fuse
Reading | ################################################## | 100% 0.01 s 
0000  ff                                                |.               |
avrdude> read io
Reading | ################################################## | 100% 0.29 s 
0000  01 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
0010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
0020  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
0030  00 00 00 00 00 00 03 00  00 8a 00 03 00 00 00 00  |................|
avrdude> q

avrdude done.  Thank you.
UPDI (AVR64DU28 + PICKit4):
$ ./avrdude -cpickit4_updi -pavr64du28 -t
avrdude: AVR device initialized and ready to accept instructions
avrdude: device signature = 0x1e9622 (probably 64du28)
avrdude> read flash
Reading | ################################################## | 100% 0.09 s 
0000  48 65 6c 6c 6f 20 66 6c  61 73 68 00 48 65 6c 6c  |Hello flash.Hell|
0010  6f 20 66 6c 61 73 68 00  48 65 6c 6c 6f 20 66 6c  |o flash.Hello fl|
0020  61 73 68 00 48 65 6c 6c  6f 20 66 6c 61 73 68 00  |ash.Hello flash.|
0030  48 65 6c 6c 6f 20 66 6c  61 73 68 00 48 65 6c 6c  |Hello flash.Hell|
0040  6f 20 66 6c 61 73 68 00  48 65 6c 6c 6f 20 66 6c  |o flash.Hello fl|
0050  61 73 68 00 48 65 6c 6c  6f 20 66 6c 61 73 68 00  |ash.Hello flash.|
0060  48 65 6c 6c 6f 20 66 6c  61 73 68 00 48 65 6c 6c  |Hello flash.Hell|
0070  6f 20 66 6c 61 73 68 00  48 65 6c 6c 6f 20 66 6c  |o flash.Hello fl|
0080  61 73 68 00 48 65 6c 6c  6f 20 66 6c 61 73 68 00  |ash.Hello flash.|
0090  48 65 6c 6c 6f 20 66 6c  61 73 68 00 48 65 6c 6c  |Hello flash.Hell|
00a0  6f 20 66 6c 61 73 68 00  48 65 6c 6c 6f 20 66 6c  |o flash.Hello fl|
00b0  61 73 68 00 48 65 6c 6c  6f 20 66 6c 61 73 68 00  |ash.Hello flash.|
00c0  48 65 6c 6c 6f 20 66 6c  61 73 68 00 48 65 6c 6c  |Hello flash.Hell|
00d0  6f 20 66 6c 61 73 68 00  48 65 6c 6c 6f 20 66 6c  |o flash.Hello fl|
00e0  61 73 68 00 48 65 6c 6c  6f 20 66 6c 61 73 68 00  |ash.Hello flash.|
00f0  48 65 6c 6c 6f 20 66 6c  61 73 68 00 48 65 6c 6c  |Hello flash.Hell|
avrdude> read eeprom
Reading | ################################################## | 100% 1.76 s 
0000  48 65 6c 6c 6f 20 45 45  50 52 4f 4d 00 48 65 6c  |Hello EEPROM.Hel|
0010  6c 6f 20 45 45 50 52 4f  4d 00 48 65 6c 6c 6f 20  |lo EEPROM.Hello |
0020  45 45 50 52 4f 4d 00 48  65 6c 6c 6f 20 45 45 50  |EEPROM.Hello EEP|
0030  52 4f 4d 00 48 65 6c 6c  6f 20 45 45 50 52 4f 4d  |ROM.Hello EEPROM|
0040  00 48 65 6c 6c 6f 20 45  45 50 52 4f 4d 00 48 65  |.Hello EEPROM.He|
0050  6c 6c 6f 20 45 45 50 52  4f 4d 00 48 65 6c 6c 6f  |llo EEPROM.Hello|
0060  20 45 45 50 52 4f 4d 00  48 65 6c 6c 6f 20 45 45  | EEPROM.Hello EE|
0070  50 52 4f 4d 00 48 65 6c  6c 6f 20 45 45 50 52 4f  |PROM.Hello EEPRO|
0080  4d 00 48 65 6c 6c 6f 20  45 45 50 52 4f 4d 00 48  |M.Hello EEPROM.H|
0090  65 6c 6c 6f 20 45 45 50  52 4f 4d 00 48 65 6c 6c  |ello EEPROM.Hell|
00a0  6f 20 45 45 50 52 4f 4d  00 48 65 6c 6c 6f 20 45  |o EEPROM.Hello E|
00b0  45 50 52 4f 4d 00 48 65  6c 6c 6f 20 45 45 50 52  |EPROM.Hello EEPR|
00c0  4f 4d 00 48 65 6c 6c 6f  20 45 45 50 52 4f 4d 00  |OM.Hello EEPROM.|
00d0  48 65 6c 6c 6f 20 45 45  50 52 4f 4d 00 48 65 6c  |Hello EEPROM.Hel|
00e0  6c 6f 20 45 45 50 52 4f  4d 00 48 65 6c 6c 6f 20  |lo EEPROM.Hello |
00f0  45 45 50 52 4f 4d 00 48  65 6c 6c 6f 20 45 45 50  |EEPROM.Hello EEP|
avrdude> read fuses
Reading | ################################################## | 100% 0.11 s 
0000  00 00 00 ff ff d1 08 00  00 ff 03 00 ff ff ff ff  |................|
avrdude> read prodsig
Reading | ################################################## | 100% 0.88 s 
0000  1e 96 22 ff cb 03 4b 03  ff ff ff ff ff ff ff ff  |.."...K.........|
0010  42 41 23 17 71 00 77 08  00 99 01 51 00 00 00 00  |BA#.q.w....Q....|
0020  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0030  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0040  74 67 fc cc cb ae 87 74  ff ff ff ff 02 ff ff ff  |tg.....t........|
0050  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0060  6e 5f 94 a8 ff ff ff ff  ff ff ff ff ff ff ff ff  |n_..............|
0070  ff ff ff ff ff ff ff ff  aa 55 aa 55 ff ff ff ff  |.........U.U....|
avrdude> read sig
Reading | ################################################## | 100% 0.01 s 
0000  1e 96 22                                          |.."             |
avrdude> read tempsense
Reading | ################################################## | 100% 0.03 s 
0000  ff ff ff ff                                       |....            |
avrdude> read sernum
Reading | ################################################## | 100% 0.11 s 
0000  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
avrdude> read bootrow
Reading | ################################################## | 100% 0.05 s 
0000  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0010  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0020  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0030  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0040  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0050  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0060  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0070  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0080  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0090  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00a0  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00b0  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00c0  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00d0  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00e0  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00f0  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
avrdude> write bootrow 0 "Hello bootrow"
Caching | ################################################## | 100% 0.00 s 
avrdude> flush
avrdude: synching cache to device ... 
Writing | -------------------------------------------------- | 0% 0.22 s 
avrdude error: verification mismatch at bootrow page addr 0x0000
avrdude> read sib
Reading | ################################################## | 100% 0.00 s 
0000  41 56 52 20 20 20 20 20  50 3a 34 44 3a 31 2d 33  |AVR     P:4D:1-3|
0010  4d 32 20 28 41 33 2e 4b  56 30 30 53 2e 30 29 00  |M2 (A3.KV00S.0).|

It seems like tempsense and sernum are broken for UPDI parts. They did work before.

EDIT: I can confirm that this PR breaks tempsense and sernum. Git main works fine.

@MCUdude
Copy link
Collaborator

MCUdude commented Jul 14, 2024

FIY I've been back from vacation for 24 hours, but we're leaving again tomorrow morning, and will be away for five-six days. Will read emails, but don't count on me when it comes to testing until I'm back home.

@stefanrueger
Copy link
Collaborator Author

stefanrueger commented Jul 14, 2024

FIY I've been back from vacation for 24 hours, but we're leaving again tomorrow morning, and will be away for five-six days. Will read emails, but don't count on me when it comes to testing until I'm back home.

Enjoy your trip(s).

I think I've solved the regression re UPDI sernum etc. There is more to do: UPDI's bootrow and PDI's apptable (remember, we never solved that). Even so, I suggest to merge relatively soon and fix remaining problems as part of the testing for v8.0. Now that we have the ALL memory we really need to smoke out memory/prog_type/programmer combos that don't work. Otherwise terminal backup and restore is less useful (or even broken) from the get go.

@mcuee
Copy link
Collaborator

mcuee commented Jul 14, 2024

No more mismatch of prodsig for -c dryrun.

PS C:\work\avr\avrdude_test\avrdude_bin>
 .\avrdude_pr1829v1.exe -C .\avrdude_pr1829v1.conf -c dryrun -p m328pb
 -U ALL:r:backup.hex -U ALL:w:backup.hex -U ALL:v:backup.hex
avrdude_pr1829v1: AVR device initialized and ready to accept instructions
avrdude_pr1829v1: device signature = 0x1e9516 (probably m328pb)
avrdude_pr1829v1: Performing a chip erase as flash memory needs programming (-U ALL:w:...)
avrdude_pr1829v1: erasing chip

avrdude_pr1829v1: processing -U ALL:r:backup.hex:I
                  reading multiple memories ...
                  eeprom         | ################################################## | 100% 0.34 s
                  flash          | ################################################## | 100% 0.34 s
                  lfuse          | ################################################## | 100% 0.00 s
                  hfuse          | ################################################## | 100% 0.00 s
                  efuse          | ################################################## | 100% 0.00 s
                  lock           | ################################################## | 100% 0.00 s
                  prodsig/sigrow | ################################################## | 100% 0.08 s
                  sernum         | ################################################## | 100% 0.03 s
                  signature      | ################################################## | 100% 0.00 s
                  calibration    | ################################################## | 100% 0.00 s
                  writing 33834 bytes to output file backup.hex

avrdude_pr1829v1: processing -U ALL:w:backup.hex:a
avrdude_pr1829v1: reading 33824 bytes for multiple memories from input file backup.hex
avrdude_pr1829v1: 1024 bytes eeprom in 1 section [0, 0x3ff]: 256 pages and 0 pad bytes
                  writing 1024 bytes to eeprom ...
                  writing | ################################################## | 100% 0.32 s
                  reading | ################################################## | 100% 0.32 s
                  1024 bytes of eeprom verified
avrdude_pr1829v1: 32768 bytes flash in 1 section [0, 0x7fff]: 256 pages and 0 pad bytes
                  writing 32768 bytes to flash ...
                  writing | ################################################## | 100% 0.32 s
                  reading | ################################################## | 100% 0.32 s
                  32768 bytes of flash verified
avrdude_pr1829v1: 1 byte lfuse in 1 section [0, 0]
                  writing 1 byte to lfuse (0x62), 1 byte written, 1 verified
avrdude_pr1829v1: 1 byte hfuse in 1 section [0, 0]
                  writing 1 byte to hfuse (0xd9), 1 byte written, 1 verified
avrdude_pr1829v1: 1 byte efuse in 1 section [0, 0]
                  writing 1 byte to efuse (0xf7), 1 byte written, 1 verified
avrdude_pr1829v1: 1 byte lock in 1 section [0, 0]
                  writing 1 byte to lock (0xff), 1 byte written, 1 verified

avrdude_pr1829v1: processing -U ALL:v:backup.hex:a
avrdude_pr1829v1: verifying 33824 bytes of multiple memories against input file backup.hex
                  reading | ################################################## | 100% 0.32 s
                  1024 bytes of eeprom verified
                  reading | ################################################## | 100% 0.32 s
                  32768 bytes of flash verified
                  1 byte of lfuse verified
                  1 byte of hfuse verified
                  1 byte of efuse verified
                  1 byte of lock verified
                  24 bytes of prodsig/sigrow verified
                  10 bytes of sernum verified
                  3 bytes of signature verified
                  1 byte of calibration verified

avrdude_pr1829v1 done.  Thank you.

@mcuee
Copy link
Collaborator

mcuee commented Jul 14, 2024

This PR works fine with -c usbasp and ATmega328PB.

PS C:\work\avr\avrdude_test\avrdude_bin> rm .\backup.hex
PS C:\work\avr\avrdude_test\avrdude_bin>
 .\avrdude_pr1829v1.exe -C .\avrdude_pr1829v1.conf -c dryrun -p m328pb
 -U ALL:r:backup.hex -U ALL:w:backup.hex -U ALL:v:backup.hex
avrdude_pr1829v1: AVR device initialized and ready to accept instructions
avrdude_pr1829v1: device signature = 0x1e9516 (probably m328pb)
avrdude_pr1829v1: Performing a chip erase as flash memory needs programming (-U ALL:w:...)
avrdude_pr1829v1: erasing chip

avrdude_pr1829v1: processing -U ALL:r:backup.hex:I
                  reading multiple memories ...
                  eeprom         | ################################################## | 100% 0.34 s
                  flash          | ################################################## | 100% 0.33 s
                  lfuse          | ################################################## | 100% 0.00 s
                  hfuse          | ################################################## | 100% 0.00 s
                  efuse          | ################################################## | 100% 0.00 s
                  lock           | ################################################## | 100% 0.00 s
                  prodsig/sigrow | ################################################## | 100% 0.08 s
                  sernum         | ################################################## | 100% 0.03 s
                  signature      | ################################################## | 100% 0.00 s
                  calibration    | ################################################## | 100% 0.00 s
                  writing 33834 bytes to output file backup.hex

avrdude_pr1829v1: processing -U ALL:w:backup.hex:a
avrdude_pr1829v1: reading 33824 bytes for multiple memories from input file backup.hex
avrdude_pr1829v1: 1024 bytes eeprom in 1 section [0, 0x3ff]: 256 pages and 0 pad bytes
                  writing 1024 bytes to eeprom ...
                  writing | ################################################## | 100% 0.31 s
                  reading | ################################################## | 100% 0.31 s
                  1024 bytes of eeprom verified
avrdude_pr1829v1: 32768 bytes flash in 1 section [0, 0x7fff]: 256 pages and 0 pad bytes
                  writing 32768 bytes to flash ...
                  writing | ################################################## | 100% 0.31 s
                  reading | ################################################## | 100% 0.31 s
                  32768 bytes of flash verified
avrdude_pr1829v1: 1 byte lfuse in 1 section [0, 0]
                  writing 1 byte to lfuse (0x62), 1 byte written, 1 verified
avrdude_pr1829v1: 1 byte hfuse in 1 section [0, 0]
                  writing 1 byte to hfuse (0xd9), 1 byte written, 1 verified
avrdude_pr1829v1: 1 byte efuse in 1 section [0, 0]
                  writing 1 byte to efuse (0xf7), 1 byte written, 1 verified
avrdude_pr1829v1: 1 byte lock in 1 section [0, 0]
                  writing 1 byte to lock (0xff), 1 byte written, 1 verified

avrdude_pr1829v1: processing -U ALL:v:backup.hex:a
avrdude_pr1829v1: verifying 33824 bytes of multiple memories against input file backup.hex
                  reading | ################################################## | 100% 0.31 s
                  1024 bytes of eeprom verified
                  reading | ################################################## | 100% 0.31 s
                  32768 bytes of flash verified
                  1 byte of lfuse verified
                  1 byte of hfuse verified
                  1 byte of efuse verified
                  1 byte of lock verified
                  24 bytes of prodsig/sigrow verified
                  10 bytes of sernum verified
                  3 bytes of signature verified
                  1 byte of calibration verified

avrdude_pr1829v1 done.  Thank you.

@mcuee
Copy link
Collaborator

mcuee commented Jul 14, 2024

For -c dryrun, no issues with ATxmega32A4U.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1829v1.exe -C .\avrdude_pr1829v1.conf -c dryrun -p atxmega32a4u -U ALL:r:backup.hex -U ALL:w:backup.hex -U ALL:v:backup.hex
avrdude_pr1829v1: AVR device initialized and ready to accept instructions
avrdude_pr1829v1: device signature = 0x1e9541 (probably x32a4u)
avrdude_pr1829v1: NOT erasing chip as page erase will be used for new flash contents

avrdude_pr1829v1: processing -U ALL:r:backup.hex:I
                  reading multiple memories ...
                  eeprom         | ################################################## | 100% 0.11 s
                  flash          | ################################################## | 100% 0.33 s
                  application    | ################################################## | 100% 0.32 s
                  apptable       | ################################################## | 100% 0.05 s
                  boot           | ################################################## | 100% 0.05 s
                  fuses          | ################################################## | 100% 0.02 s
                  fuse0          | ################################################## | 100% 0.00 s
                  fuse1          | ################################################## | 100% 0.00 s
                  fuse2          | ################################################## | 100% 0.00 s
                  fuse4          | ################################################## | 100% 0.00 s
                  fuse5          | ################################################## | 100% 0.00 s
                  lock           | ################################################## | 100% 0.00 s
                  prodsig/sigrow | ################################################## | 100% 0.17 s
                  calibration    | ################################################## | 100% 0.01 s
                  sernum         | ################################################## | 100% 0.05 s
                  tempsense      | ################################################## | 100% 0.01 s
                  usersig        | ################################################## | 100% 0.00 s
                  signature      | ################################################## | 100% 0.00 s
                  writing 79192 bytes to output file backup.hex

avrdude_pr1829v1: processing -U ALL:w:backup.hex:a
avrdude_pr1829v1: reading 38207 bytes for multiple memories from input file backup.hex
avrdude_pr1829v1: 1024 bytes eeprom in 1 section [0, 0x3ff]: 32 pages and 0 pad bytes
                  writing 1024 bytes to eeprom ...
                  writing | ################################################## | 100% 0.09 s
                  reading | ################################################## | 100% 0.09 s
                  1024 bytes of eeprom verified
avrdude_pr1829v1: 36864 bytes flash in 1 section [0, 0x8fff]: 144 pages and 0 pad bytes
                  writing 36864 bytes to flash ...
                  writing | ################################################## | 100% 0.30 s
                  reading | ################################################## | 100% 0.31 s
                  36864 bytes of flash verified
avrdude_pr1829v1: 32768 bytes application in 1 section [0, 0x7fff]: 128 pages and 0 pad bytes
                  writing 32768 bytes to application ...
                  writing | ################################################## | 100% 0.31 s
                  reading | ################################################## | 100% 0.31 s
                  32768 bytes of application verified
avrdude_pr1829v1: 4096 bytes apptable in 1 section [0, 0xfff]: 16 pages and 0 pad bytes
                  writing 4096 bytes to apptable ...
                  writing | ################################################## | 100% 0.05 s
                  reading | ################################################## | 100% 0.05 s
                  4096 bytes of apptable verified
avrdude_pr1829v1: 4096 bytes boot in 1 section [0, 0xfff]: 16 pages and 0 pad bytes
                  writing 4096 bytes to boot ...
                  writing | ################################################## | 100% 0.05 s
                  reading | ################################################## | 100% 0.05 s
                  4096 bytes of boot verified
avrdude_pr1829v1: 7 bytes fuses in 1 section [0, 6]
                  writing 7 bytes to fuses ..., 7 bytes written, 7 verified
avrdude_pr1829v1: 1 byte fuse0 in 1 section [0, 0]
                  writing 1 byte to fuse0 (0xff), 1 byte written, 1 verified
avrdude_pr1829v1: 1 byte fuse1 in 1 section [0, 0]
                  writing 1 byte to fuse1 (0x00), 1 byte written, 1 verified
avrdude_pr1829v1: 1 byte fuse2 in 1 section [0, 0]
                  writing 1 byte to fuse2 (0xff), 1 byte written, 1 verified
avrdude_pr1829v1: 1 byte fuse4 in 1 section [0, 0]
                  writing 1 byte to fuse4 (0xfe), 1 byte written, 1 verified
avrdude_pr1829v1: 1 byte fuse5 in 1 section [0, 0]
                  writing 1 byte to fuse5 (0xff), 1 byte written, 1 verified
avrdude_pr1829v1: 1 byte lock in 1 section [0, 0]
                  writing 1 byte to lock (0xff), 1 byte written, 1 verified
avrdude_pr1829v1: 256 bytes usersig in 1 section [0, 0xff]: 1 page and 0 pad bytes
                  writing 256 bytes to usersig ...
                  writing | ################################################## | 100% 0.00 s
                  reading | ################################################## | 100% 0.00 s
                  256 bytes of usersig verified

avrdude_pr1829v1: processing -U ALL:v:backup.hex:a
avrdude_pr1829v1: verifying 38207 bytes of multiple memories against input file backup.hex
                  reading | ################################################## | 100% 0.10 s
                  1024 bytes of eeprom verified
                  reading | ################################################## | 100% 0.31 s
                  36864 bytes of flash verified
                  reading | ################################################## | 100% 0.31 s
                  32768 bytes of application verified
                  reading | ################################################## | 100% 0.05 s
                  4096 bytes of apptable verified
                  reading | ################################################## | 100% 0.05 s
                  4096 bytes of boot verified
                  7 bytes of fuses verified
                  1 byte of fuse0 verified
                  1 byte of fuse1 verified
                  1 byte of fuse2 verified
                  1 byte of fuse4 verified
                  1 byte of fuse5 verified
                  1 byte of lock verified
                  reading | ################################################## | 100% 0.16 s
                  52 bytes of prodsig/sigrow verified
                  4 bytes of calibration verified
                  14 bytes of sernum verified
                  2 bytes of tempsense verified
                  reading | ################################################## | 100% 0.00 s
                  256 bytes of usersig verified
                  3 bytes of signature verified

avrdude_pr1829v1 done.  Thank you.

@mcuee
Copy link
Collaborator

mcuee commented Jul 14, 2024

Somehow I can not get an AVRISP mkII clone to work properly with ATxmega32A4U. This may not be related to this PR though. I will try other clones.

PS C:\work\avr\avrdude_test\avrdude_bin> rm .\backup.hex
PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1829v1.exe -C .\avrdude_pr1829v1.conf -c avrispmkii -p atxmega32a4u
 -U ALL:r:backup.hex -U ALL:w:backup.hex -U ALL:v:backup.hex
avrdude_pr1829v1: AVR device initialized and ready to accept instructions
avrdude_pr1829v1: device signature = 0x1e9541 (probably x32a4u)
avrdude_pr1829v1: NOT erasing chip as page erase will be used for new flash contents

avrdude_pr1829v1: processing -U ALL:r:backup.hex:I
                  reading multiple memories ...
                  eeprom         | ################################################## | 100% 0.14 s
                  flash          | ################################################## | 100% 0.80 s
                  application    | ################################################## | 100% 0.75 s
                  apptable       | ################################################## | 100% 0.10 s
                  boot           | ################################################## | 100% 0.10 s
                  fuses          | ################################################## | 100% 0.03 s
                  fuse0          | ################################################## | 100% 0.00 s
                  fuse1          | ################################################## | 100% 0.00 s
                  fuse2          | ################################################## | 100% 0.01 s
                  fuse4          | ################################################## | 100% 0.00 s
                  fuse5          | ################################################## | 100% 0.00 s
                  lock           | ################################################## | 100% 0.00 s
                  prodsig/sigrow | ################################################## | 100% 0.20 s
                  calibration    | ################################################## | 100% 0.02 s
                  sernum         | ################################################## | 100% 0.05 s
                  tempsense      | ################################################## | 100% 0.01 s
                  usersig        | ################################################## | 100% 0.01 s
                  signature      | ################################################## | 100% 0.02 s
                  writing 79192 bytes to output file backup.hex

avrdude_pr1829v1: processing -U ALL:w:backup.hex:a
avrdude_pr1829v1: reading 38207 bytes for multiple memories from input file backup.hex
avrdude_pr1829v1: 1024 bytes eeprom in 1 section [0, 0x3ff]: 32 pages and 0 pad bytes
                  writing 1024 bytes to eeprom ...
                  writing | ################################################## | 100% 0.52 s
                  reading | ######################---------------------------- | 43% 1.93 s
avrdude_pr1829v1 error: unable to receive from USB
avrdude_pr1829v1 error: wrote -5 out of 1 bytes, err = Input/output error
avrdude_pr1829v1 error: unable to send command to serial port
avrdude_pr1829v1 error: wrote 0 out of 1 bytes, err = Input/output error
avrdude_pr1829v1 error: unable to send command to serial port
avrdude_pr1829v1 error: wrote 0 out of 1 bytes, err = Input/output error
avrdude_pr1829v1 error: unable to send command to serial port
avrdude_pr1829v1 error: wrote 0 out of 1 bytes, err = Input/output error
avrdude_pr1829v1 error: unable to send command to serial port
avrdude_pr1829v1 error: wrote 0 out of 1 bytes, err = Input/output error
avrdude_pr1829v1 error: unable to send command to serial port
avrdude_pr1829v1 error: wrote 0 out of 1 bytes, err = Input/output error

On the other hand, no issue if I just read back the eeprom or flash.

PS C:\work\avr\avrdude_test\avrdude_bin>
 .\avrdude_pr1829v1.exe -C .\avrdude_pr1829v1.conf -c avrispmkii -p atxmega32a4u
 -U eeprom:r:backup.hex:i -U eeprom:w:backup.hex:i -U eeprom:v:backup.hex:i
avrdude_pr1829v1: AVR device initialized and ready to accept instructions
avrdude_pr1829v1: device signature = 0x1e9541 (probably x32a4u)

avrdude_pr1829v1: processing -U eeprom:r:backup.hex:i
                  reading eeprom memory ...
                  reading | ################################################## | 100% 0.13 s
                  writing 1024 bytes to output file backup.hex

avrdude_pr1829v1: processing -U eeprom:w:backup.hex:i
avrdude_pr1829v1: reading 1024 bytes for eeprom from input file backup.hex
                  in 1 section [0, 0x3ff]: 32 pages and 0 pad bytes
                  writing 1024 bytes to eeprom ...
                  writing | ################################################## | 100% 0.51 s
                  reading | ################################################## | 100% 0.14 s
                  1024 bytes of eeprom verified

avrdude_pr1829v1: processing -U eeprom:v:backup.hex:i
avrdude_pr1829v1: verifying 1024 bytes of eeprom against input file backup.hex
                  reading | ################################################## | 100% 0.13 s
                  1024 bytes of eeprom verified

avrdude_pr1829v1 done.  Thank you.

PS C:\work\avr\avrdude_test\avrdude_bin> rm .\backup.hex
PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1829v1.exe -C .\avrdude_pr1829v1.conf -c avrispmkii -p atxmega32a4u -U flash:r:backup.hex:i -U flash:w:backup.hex:i -U flash:v:backup.hex:i
avrdude_pr1829v1: AVR device initialized and ready to accept instructions
avrdude_pr1829v1: device signature = 0x1e9541 (probably x32a4u)
avrdude_pr1829v1: NOT erasing chip as page erase will be used for new flash contents

avrdude_pr1829v1: processing -U flash:r:backup.hex:i
                  reading flash memory ...
                  reading | ################################################## | 100% 0.79 s
                  writing 36864 bytes to output file backup.hex

avrdude_pr1829v1: processing -U flash:w:backup.hex:i
avrdude_pr1829v1: reading 36864 bytes for flash from input file backup.hex
                  in 1 section [0, 0x8fff]: 144 pages and 0 pad bytes
                  writing 36864 bytes to flash ...
                  writing | ################################################## | 100% 3.05 s
                  reading | ################################################## | 100% 0.78 s
                  36864 bytes of flash verified

avrdude_pr1829v1: processing -U flash:v:backup.hex:i
avrdude_pr1829v1: verifying 36864 bytes of flash against input file backup.hex
                  reading | ################################################## | 100% 0.78 s
                  36864 bytes of flash verified

avrdude_pr1829v1 done.  Thank you.

@mcuee
Copy link
Collaborator

mcuee commented Jul 15, 2024

Somehow I can not get an AVRISP mkII clone to work properly with ATxmega32A4U. This may not be related to this PR though. I will try other clones.

Looks like a temporary USB communication issue. Now it is okay. The reason is probably because of using jumper wires from the programmer to the ATxmega32AU4 breakout board.

PS C:\work\avr\avrdude_test\avrdude_bin> rm .\backup.hex
PS C:\work\avr\avrdude_test\avrdude_bin>
 .\avrdude_pr1829v1.exe -C .\avrdude_pr1829v1.conf -c avrispmkii -p atxmega32a4u
 -U ALL:r:backup.hex -U ALL:w:backup.hex -U ALL:v:backup.hex
avrdude_pr1829v1: AVR device initialized and ready to accept instructions
avrdude_pr1829v1: device signature = 0x1e9541 (probably x32a4u)
avrdude_pr1829v1: NOT erasing chip as page erase will be used for new flash contents

avrdude_pr1829v1: processing -U ALL:r:backup.hex:I
                  reading multiple memories ...
                  eeprom         | ################################################## | 100% 0.14 s
                  flash          | ################################################## | 100% 0.81 s
                  application    | ################################################## | 100% 0.75 s
                  apptable       | ################################################## | 100% 0.10 s
                  boot           | ################################################## | 100% 0.11 s
                  fuses          | ################################################## | 100% 0.03 s
                  fuse0          | ################################################## | 100% 0.00 s
                  fuse1          | ################################################## | 100% 0.00 s
                  fuse2          | ################################################## | 100% 0.00 s
                  fuse4          | ################################################## | 100% 0.00 s
                  fuse5          | ################################################## | 100% 0.00 s
                  lock           | ################################################## | 100% 0.01 s
                  prodsig/sigrow | ################################################## | 100% 0.21 s
                  calibration    | ################################################## | 100% 0.02 s
                  sernum         | ################################################## | 100% 0.05 s
                  tempsense      | ################################################## | 100% 0.01 s
                  usersig        | ################################################## | 100% 0.01 s
                  signature      | ################################################## | 100% 0.01 s
                  writing 79192 bytes to output file backup.hex

avrdude_pr1829v1: processing -U ALL:w:backup.hex:a
avrdude_pr1829v1: reading 38207 bytes for multiple memories from input file backup.hex
avrdude_pr1829v1: 1024 bytes eeprom in 1 section [0, 0x3ff]: 32 pages and 0 pad bytes
                  writing 1024 bytes to eeprom ...
                  writing | ################################################## | 100% 0.52 s
                  reading | ################################################## | 100% 0.14 s
                  1024 bytes of eeprom verified
avrdude_pr1829v1: 36864 bytes flash in 1 section [0, 0x8fff]: 144 pages and 0 pad bytes
                  writing 36864 bytes to flash ...
                  writing | ################################################## | 100% 2.99 s
                  reading | ################################################## | 100% 0.77 s
                  36864 bytes of flash verified
avrdude_pr1829v1: 32768 bytes application in 1 section [0, 0x7fff]: 128 pages and 0 pad bytes
                  writing 32768 bytes to application ...
                  writing | ################################################## | 100% 2.66 s
                  reading | ################################################## | 100% 0.73 s
                  32768 bytes of application verified
avrdude_pr1829v1: 4096 bytes apptable in 1 section [0, 0xfff]: 16 pages and 0 pad bytes
                  writing 4096 bytes to apptable ...
                  writing | ################################################## | 100% 0.31 s
                  reading | ################################################## | 100% 0.10 s
                  4096 bytes of apptable verified
avrdude_pr1829v1: 4096 bytes boot in 1 section [0, 0xfff]: 16 pages and 0 pad bytes
                  writing 4096 bytes to boot ...
                  writing | ################################################## | 100% 0.31 s
                  reading | ################################################## | 100% 0.10 s
                  4096 bytes of boot verified
avrdude_pr1829v1: 7 bytes fuses in 1 section [0, 6]
                  writing 7 bytes to fuses ..., 7 bytes written, 7 verified
avrdude_pr1829v1: 1 byte fuse0 in 1 section [0, 0]
                  writing 1 byte to fuse0 (0x55), 1 byte written, 1 verified
avrdude_pr1829v1: 1 byte fuse1 in 1 section [0, 0]
                  writing 1 byte to fuse1 (0x00), 1 byte written, 1 verified
avrdude_pr1829v1: 1 byte fuse2 in 1 section [0, 0]
                  writing 1 byte to fuse2 (0x63), 1 byte written
avrdude_pr1829v1 warning: ignoring mismatch in unused bits of fuse2
                 (device 0xff != input 0x63); to prevent this warning set
                 unused bits to 1 when writing (double check with datasheet)
, 1 verified
avrdude_pr1829v1: 1 byte fuse4 in 1 section [0, 0]
                  writing 1 byte to fuse4 (0x1e), 1 byte written, 1 verified
avrdude_pr1829v1: 1 byte fuse5 in 1 section [0, 0]
                  writing 1 byte to fuse5 (0x37), 1 byte written
avrdude_pr1829v1 warning: ignoring mismatch in unused bits of fuse5
                 (device 0xf7 != input 0x37); to prevent this warning set
                 unused bits to 1 when writing (double check with datasheet)
, 1 verified
avrdude_pr1829v1: 1 byte lock in 1 section [0, 0]
                  writing 1 byte to lock (0xff), 1 byte written, 1 verified
avrdude_pr1829v1: 256 bytes usersig in 1 section [0, 0xff]: 1 page and 0 pad bytes
                  writing 256 bytes to usersig ...
                  writing | ################################################## | 100% 0.02 s
                  reading | ################################################## | 100% 0.01 s
                  256 bytes of usersig verified

avrdude_pr1829v1: processing -U ALL:v:backup.hex:a
avrdude_pr1829v1: verifying 38207 bytes of multiple memories against input file backup.hex
                  reading | ################################################## | 100% 0.14 s
                  1024 bytes of eeprom verified
                  reading | ################################################## | 100% 0.79 s
                  36864 bytes of flash verified
                  reading | ################################################## | 100% 0.72 s
                  32768 bytes of application verified
                  reading | ################################################## | 100% 0.10 s
                  4096 bytes of apptable verified
                  reading | ################################################## | 100% 0.10 s
                  4096 bytes of boot verified
avrdude_pr1829v1 warning: ignoring mismatch in unused bits of fuses
                 (device 0xff != input 0x63); to prevent this warning set
                 unused bits to 1 when writing (double check with datasheet)
avrdude_pr1829v1 warning: ignoring mismatch in unused bits of fuses
                 (device 0xf7 != input 0x37); to prevent this warning set
                 unused bits to 1 when writing (double check with datasheet)
                  7 bytes of fuses verified
                  1 byte of fuse0 verified
                  1 byte of fuse1 verified
avrdude_pr1829v1 warning: ignoring mismatch in unused bits of fuse2
                 (device 0xff != input 0x63); to prevent this warning set
                 unused bits to 1 when writing (double check with datasheet)
                  1 byte of fuse2 verified
                  1 byte of fuse4 verified
avrdude_pr1829v1 warning: ignoring mismatch in unused bits of fuse5
                 (device 0xf7 != input 0x37); to prevent this warning set
                 unused bits to 1 when writing (double check with datasheet)
                  1 byte of fuse5 verified
                  1 byte of lock verified
                  reading | ################################################## | 100% 0.19 s
                  52 bytes of prodsig/sigrow verified
                  4 bytes of calibration verified
                  14 bytes of sernum verified
                  2 bytes of tempsense verified
                  reading | ################################################## | 100% 0.01 s
                  256 bytes of usersig verified
                  3 bytes of signature verified

avrdude_pr1829v1 done.  Thank you.

@stefanrueger
Copy link
Collaborator Author

Thanks @mcuee for testing. Any chance you have an ATtiny102 or 104 that you can test for the sernum memory? It's a TPI part.

Also, for future tests, I now recommend a slightly changed line:

avrdude -c ... -p ... -D -U ALL:r:backup.hex -T erase -U ALL:w:backup.hex -U ALL:v:backup.hex

Note the -D and -T erase between :r for backing up and :w for restoring. And still important to check whether the right memory regions were read/written (ie, it's good to inspect the .hex file).

@mcuee
Copy link
Collaborator

mcuee commented Jul 15, 2024

Thanks @mcuee for testing. Any chance you have an ATtiny102 or 104 that you can test for the sernum memory? It's a TPI part.

Unfortunately I do not have good setup to test TPI parts in general.
Let me check if I can find the Microchip ATtiny104 Xplained Nano board for testing.

@stefanrueger stefanrueger merged commit 2c57c2e into avrdudes:main Jul 15, 2024
13 checks passed
@stefanrueger stefanrueger deleted the additional-memories branch July 15, 2024 16:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants