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

Fix jtag3_page_erase for targets with UPDI #1112

Merged
merged 2 commits into from
Oct 17, 2022

Conversation

MCUdude
Copy link
Collaborator

@MCUdude MCUdude commented Oct 6, 2022

Very handy now that #1106 is merged.

Tested and proved to work with an ATmega4808 and an ATxmega128A3U using a Power Debugger, and an AVR128DB48 using a pkobn_updi (Curiosity Nano board).

Here are some reference benchmarks.

Commands:

echo erase | ./avrdude -p avr128db48 -c pkobn_updi -t 
echo 'read flash 0x1000 16' | ./avrdude -p avr128db48 -c pkobn_updi -t
echo 'write flash 0x1000 "hello, world\n"' | ./avrdude -p avr128db48 -c pkobn_updi -t
echo 'read flash 0x1000 16' | ./avrdude -p avr128db48 -c pkobn_updi -t
echo 'write flash 0x1000 "Hello, world\n"' | ./avrdude -p avr128db48 -c pkobn_updi -t
echo 'read flash 0x1000 16' | ./avrdude -p avr128db48 -c pkobn_updi -t
echo 'write flash 0x1000 "hello, world\n"' | ./avrdude -p avr128db48 -c pkobn_updi -t
echo 'read flash 0x1000 16' | ./avrdude -p avr128db48 -c pkobn_updi -t
Without jtag3 UPDI page erase fix
$ echo erase | ./avrdude -p avr128db48 -c pkobn_updi -t 

         Vtarget                      : 3.31 V
         PDI/UPDI clock Xmega/megaAVR : 100 kHz

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e970c (probably avr128db48)
>>> erase 
avrdude: erasing chip

avrdude done.  Thank you.

$ echo 'read flash 0x1000 16' | ./avrdude -p avr128db48 -c pkobn_updi -t

         Vtarget                      : 3.31 V
         PDI/UPDI clock Xmega/megaAVR : 100 kHz

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e970c (probably avr128db48)
>>> read flash 0x1000 16 

Reading | ################################################## | 100% 0.09s

1000  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|


avrdude done.  Thank you.

$ echo 'write flash 0x1000 "hello, world\n"' | ./avrdude -p avr128db48 -c pkobn_updi -t

         Vtarget                      : 3.31 V
         PDI/UPDI clock Xmega/megaAVR : 100 kHz

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e970c (probably avr128db48)
>>> write flash 0x1000 "hello, world\n" 

Writing | ################################################## | 100% 0.09s

avrdude: writing cache to device... done

avrdude done.  Thank you.

$ echo 'read flash 0x1000 16' | ./avrdude -p avr128db48 -c pkobn_updi -t

         Vtarget                      : 3.31 V
         PDI/UPDI clock Xmega/megaAVR : 100 kHz

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e970c (probably avr128db48)
>>> read flash 0x1000 16 

Reading | ################################################## | 100% 0.09s

1000  68 65 6c 6c 6f 2c 20 77  6f 72 6c 64 0a 00 ff ff  |hello, world ...|


avrdude done.  Thank you.

$ echo 'write flash 0x1000 "Hello, world\n"' | ./avrdude -p avr128db48 -c pkobn_updi -t

         Vtarget                      : 3.31 V
         PDI/UPDI clock Xmega/megaAVR : 100 kHz

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e970c (probably avr128db48)
>>> write flash 0x1000 "Hello, world\n" 

Writing | ################################################## | 100% 0.09s

avrdude: writing cache to device... done

avrdude done.  Thank you.

$ echo 'read flash 0x1000 16' | ./avrdude -p avr128db48 -c pkobn_updi -t

         Vtarget                      : 3.31 V
         PDI/UPDI clock Xmega/megaAVR : 100 kHz

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e970c (probably avr128db48)
>>> read flash 0x1000 16 

Reading | ################################################## | 100% 0.09s

1000  48 65 6c 6c 6f 2c 20 77  6f 72 6c 64 0a 00 ff ff  |Hello, world ...|


avrdude done.  Thank you.

$ echo 'write flash 0x1000 "hello, world\n"' | ./avrdude -p avr128db48 -c pkobn_updi -t

         Vtarget                      : 3.31 V
         PDI/UPDI clock Xmega/megaAVR : 100 kHz

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e970c (probably avr128db48)
>>> write flash 0x1000 "hello, world\n" 

Writing | ################################################## | 100% 0.09s

avrdude: writing cache to device... avrdude: jtag3_page_erase: not an Xmega device

Reading | ################################################## | 100% 21.94s
Erasing | ################################################## | 100% 0.02s
Writing | ################################################## | 100% 0.24s

avrdude done.  Thank you.

$ echo 'read flash 0x1000 16' | ./avrdude -p avr128db48 -c pkobn_updi -t

         Vtarget                      : 3.31 V
         PDI/UPDI clock Xmega/megaAVR : 100 kHz

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e970c (probably avr128db48)
>>> read flash 0x1000 16 

Reading | ################################################## | 100% 0.09s

1000  68 65 6c 6c 6f 2c 20 77  6f 72 6c 64 0a 00 ff ff  |hello, world ...|


avrdude done.  Thank you.
With jtag3 UPDI page erase fix
echo erase | ./avrdude -p avr128db48 -c pkobn_updi -t 

         Vtarget                      : 3.31 V
         PDI/UPDI clock Xmega/megaAVR : 100 kHz

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e970c (probably avr128db48)
>>> erase 
avrdude: erasing chip

avrdude done.  Thank you.

$ echo 'read flash 0x1000 16' | ./avrdude -p avr128db48 -c pkobn_updi -t

         Vtarget                      : 3.31 V
         PDI/UPDI clock Xmega/megaAVR : 100 kHz

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e970c (probably avr128db48)
>>> read flash 0x1000 16 

Reading | ################################################## | 100% 0.09s

1000  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|


avrdude done.  Thank you.

$ echo 'write flash 0x1000 "hello, world\n"' | ./avrdude -p avr128db48 -c pkobn_updi -t

         Vtarget                      : 3.31 V
         PDI/UPDI clock Xmega/megaAVR : 100 kHz

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e970c (probably avr128db48)
>>> write flash 0x1000 "hello, world\n" 

Writing | ################################################## | 100% 0.09s

avrdude: writing cache to device... done

avrdude done.  Thank you.

$ echo 'read flash 0x1000 16' | ./avrdude -p avr128db48 -c pkobn_updi -t

         Vtarget                      : 3.31 V
         PDI/UPDI clock Xmega/megaAVR : 100 kHz

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e970c (probably avr128db48)
>>> read flash 0x1000 16 

Reading | ################################################## | 100% 0.09s

1000  68 65 6c 6c 6f 2c 20 77  6f 72 6c 64 0a 00 ff ff  |hello, world ...|


avrdude done.  Thank you.

$ echo 'write flash 0x1000 "Hello, world\n"' | ./avrdude -p avr128db48 -c pkobn_updi -t

         Vtarget                      : 3.31 V
         PDI/UPDI clock Xmega/megaAVR : 100 kHz

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e970c (probably avr128db48)
>>> write flash 0x1000 "Hello, world\n" 

Writing | ################################################## | 100% 0.09s

avrdude: writing cache to device... done

avrdude done.  Thank you.

$ echo 'read flash 0x1000 16' | ./avrdude -p avr128db48 -c pkobn_updi -t

         Vtarget                      : 3.31 V
         PDI/UPDI clock Xmega/megaAVR : 100 kHz

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e970c (probably avr128db48)
>>> read flash 0x1000 16 

Reading | ################################################## | 100% 0.09s

1000  48 65 6c 6c 6f 2c 20 77  6f 72 6c 64 0a 00 ff ff  |Hello, world ...|


avrdude done.  Thank you.

$ echo 'write flash 0x1000 "hello, world\n"' | ./avrdude -p avr128db48 -c pkobn_updi -t

         Vtarget                      : 3.31 V
         PDI/UPDI clock Xmega/megaAVR : 100 kHz

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e970c (probably avr128db48)
>>> write flash 0x1000 "hello, world\n" 

Writing | ################################################## | 100% 0.09s

avrdude: writing cache to device... done

avrdude done.  Thank you.

$ echo 'read flash 0x1000 16' | ./avrdude -p avr128db48 -c pkobn_updi -t

         Vtarget                      : 3.31 V
         PDI/UPDI clock Xmega/megaAVR : 100 kHz

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e970c (probably avr128db48)
>>> read flash 0x1000 16 

Reading | ################################################## | 100% 0.09s

1000  68 65 6c 6c 6f 2c 20 77  6f 72 6c 64 0a 00 ff ff  |hello, world ...|


avrdude done.  Thank you.

Very handy to have now that avrdudes#1106 is merged
Copy link
Collaborator

@stefanrueger stefanrueger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coding looks good. If XMEGA page erase still works and UPDI parts can now have pages erased that is great.

I would double check XMEGA apptable page erase (both in theory and testing in practice). I suspect the routine fails to treat apptable correctly (and if so this would have been wrong before), but maybe it does treat it correctly.

Perhaps line 1703 might be changed from if (strcmp(m->desc, "flash") == 0) to sth like if(avr_mem_is_flash_type(m->desc)) instead; then also the else if (strcmp(m->desc, "boot") == 0) branch can go. My suggestion may still be not sufficient in case that apptable has yet a different XMEGA_ERASE_APPTABLE_PAGE command (I don't know!)

@stefanrueger stefanrueger added the enhancement New feature or request label Oct 6, 2022
@stefanrueger
Copy link
Collaborator

And what about eeprom page erases? Would this PR do them correctly for UPDI parts?

@MCUdude
Copy link
Collaborator Author

MCUdude commented Oct 6, 2022

Coding looks good. If XMEGA page erase still works and UPDI parts can now have pages erased that is great.

Thanks! Xmega page erase (still) works fine.

Perhaps line 1703 might be changed from if (strcmp(m->desc, "flash") == 0) to sth like if(avr_mem_is_flash_type(m->desc)) instead; then also the else if (strcmp(m->desc, "boot") == 0) branch can go. My suggestion may still be not sufficient in case that apptable has yet a different XMEGA_ERASE_APPTABLE_PAGE command (I don't know!)

Thanks for the tip! I'll do it this way, and check if I can get apptable page erase working as well.

And what about eeprom page erases? Would this PR do them correctly for UPDI parts?

Yes, for AVRs with UPDI that have an EEPROM page defined (like ATmega4808). When trying to EEPROM page an AVR Dx, it only erases one byte; the address passed to jtag3_page_erase. This is probably because AVR Dx's EEPROM isn't page oriented.

@MCUdude
Copy link
Collaborator Author

MCUdude commented Oct 6, 2022

Sorry, but I've not been able to page-erase the app table memory on my ATxmega128A3U using a Power Debugger. I'm not sure it's even possible. I've tried many different commands (XMEGA_ERASE_APP_PAGE, XMEGA_ERASE_BOOT_PAGE, XMEGA_ERASE_EEPROM_PAGE, XMEGA_ERASE_USERSIG), but nothing works.

@stefanrueger
Copy link
Collaborator

Is it possible to erase an apptable page by erasing flash at apptable_offset + apptable_address?

@MCUdude
Copy link
Collaborator Author

MCUdude commented Oct 7, 2022

Is it possible to erase an apptable page by erasing flash at apptable_offset + apptable_address?

Sorry, I tried a bunch of different combinations but was not able to page erase the app table. I can probably contact Microchip and check if the Xmega and/or JTAG3 programmer supports this.

@mcuee
Copy link
Collaborator

mcuee commented Oct 7, 2022

@MCUdude
This PR looks good. Just wondering if you can do similar things with JTAG2 based UPDI programmer like jtag2updi.

Ref:

@stefanrueger
Copy link
Collaborator

was not able to page erase the app table

@MCUdude Here my understanding (might well be wrong): The ATxmega128A3U has flash with 0x22000 bytes, where the apptable of size 8192 is located at 0x1e000. So, naively I would expect the page erase of flash at 0x1e000 to be the same as the page erase of apptable at address 0. PR #1113 now has an experimental pgerase memory addr command for the terminal. Can you see what happens when you pgerase flash 0x1e000? That should work, and if it does, the page_erase() for apptable at address addr could, in the worst case, carry out a page erase of flash at addr + 0x1e000. This offset should be the same as locate_mem(p, "apptable")->offset - locate_mem(p, "flash")->offset.

@mcuee
Copy link
Collaborator

mcuee commented Oct 8, 2022

Tested with ATtiny817 Xplained Mini and this PR works fine.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_git -qq -p t817 -c xplainedmini_updi -t
                 Vtarget                      : 5.00 V
avrdude> dump flash 0 0x10
>>> dump flash 0 0x10
0000  01 c0 d8 c0 11 24 80 91  40 00 88 23 69 f0 28 2f  |.....$..@..#i.(/|

avrdude> dump flash 0x1000 0x10
>>> dump flash 0x1000 0x10
1000  68 65 6c 6c 6f 2c 20 77  6f 72 6c 64 0a 00 ff ff  |hello, world ...|

avrdude> write flash 0x1000 "HELLO WORLD\n"
>>> write flash 0x1000 "HELLO WORLD\n"
avrdude> flush
>>> flush
avrdude_git.exe: synching cache to device... avrdude_git.exe: jtag3_page_erase: not an Xmega device
reading/chip erase/writing cycle needed ... done
avrdude> dump flash 0x1000 0x10
>>> dump flash 0x1000 0x10
1000  48 45 4c 4c 4f 20 57 4f  52 4c 44 0a 00 00 ff ff  |HELLO WORLD ....|

avrdude> quit
>>> quit
PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1112 -qq -p t817 -c xplainedmini_updi -t
                    Vtarget                      : 5.00 V
avrdude> dump flash 0x1000 0x10
>>> dump flash 0x1000 0x10
1000  48 45 4c 4c 4f 20 57 4f  52 4c 44 0a 00 00 ff ff  |HELLO WORLD ....|

avrdude> write flash 0x1000 "Hello, world\n"
>>> write flash 0x1000 "Hello, world\n"
avrdude> flush
>>> flush
avrdude_pr1112.exe: synching cache to device... avrdude>
avrdude> dump flash 0x1000 0x10
>>> dump flash 0x1000 0x10
1000  48 65 6c 6c 6f 2c 20 77  6f 72 6c 64 0a 00 ff ff  |Hello, world ...|

avrdude> quit
>>> quit


@mcuee
Copy link
Collaborator

mcuee commented Oct 8, 2022

@stefanrueger

I think this can be merged and then you can add the pgerase memory addr terminal command for JTAG3 UPDI.

Ref:

@MCUdude
Copy link
Collaborator Author

MCUdude commented Oct 8, 2022

@stefanrueger I'm for some reason suddenly having a hard time writing to apptable...


$  ./avrdude -cpowerdebugger_pdi -patxmega128a3u -t
avrdude: usbhid_open(): No device found
avrdude: usbdev_open(): error claiming interface 0: Permission denied
avrdude: usbdev_open(): error claiming interface 1: Permission denied
avrdude: usbdev_open(): no usable interface found
avrdude: jtag3_open_common(): Did not find any device matching VID 0x03eb and PID list: 0x2144
avrdude: opening programmer "powerdebugger_pdi" on port "usb" failed

avrdude done.  Thank you.

Hanss-MacBook-Pro-2:src hans$  ./avrdude -cpowerdebugger_pdi -patxmega128a3u -t

         Vtarget                      : 3.30 V
         Vout set                     : 0.00 V
         Vout measured                : 1.47 V
         Ch A voltage                 : 1.465 V
         Ch A current                 : -1.062 mA
         Ch B voltage                 : 1.525 V
         Ch B current                 : 0.000 mA
         JTAG clock megaAVR/program   : 1000 kHz
         JTAG clock megaAVR/debug     : 1000 kHz
         JTAG clock Xmega             : 1000 kHz
         PDI/UPDI clock Xmega/megaAVR : 900 kHz
avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e9742 (probably x128a3u)
avrdude> write apptable 0 0x100 0x55 ...
>>> write apptable 0 0x100 0x55 ... 

Caching | ################################################## | 100% 0.01s

avrdude> flush
>>> flush 
Segmentation fault: 11

@mcuee
Copy link
Collaborator

mcuee commented Oct 9, 2022

@stefanrueger I'm for some reason suddenly having a hard time writing to apptable...

> $  ./avrdude -cpowerdebugger_pdi -patxmega128a3u -t
> avrdude: usbhid_open(): No device found
> avrdude: usbdev_open(): error claiming interface 0: Permission denied
> avrdude: usbdev_open(): error claiming interface 1: Permission denied
> avrdude: usbdev_open(): no usable interface found
> avrdude: jtag3_open_common(): Did not find any device matching VID 0x03eb and PID list: 0x2144
> avrdude: opening programmer "powerdebugger_pdi" on port "usb" failed

By right you need to use hidapi under macOS and can not fall back to libusb. You may have to rebuild avrdude.

@MCUdude
Copy link
Collaborator Author

MCUdude commented Oct 9, 2022

By right you need to use hidapi under macOS and can not fall back to libusb. You may have to rebuild avrdude.

I believe I'm building with hidapi:

Configuration summary:
----------------------
DO HAVE    libelf
DO HAVE    libusb
DO HAVE    libusb_1_0
DO HAVE    libftdi1
DO HAVE    libftdi (but prefer to use libftdi1)
DON'T HAVE libhid
DO HAVE    libhidapi
DO HAVE    pthread
DISABLED   doc
DISABLED   parport
DISABLED   linuxgpio
DISABLED   linuxspi

I'm also able to read and write to flash in terminal mode just fine

@mcuee
Copy link
Collaborator

mcuee commented Oct 10, 2022

By right you need to use hidapi under macOS and can not fall back to libusb. You may have to rebuild avrdude.

I believe I'm building with hidapi:

That is good to know. Then the error message is strange. It seems to fail to use hidapi and fall back to libusb in your first result and then the error message will appear.

I'm also able to read and write to flash in terminal mode just fine

Do you still see the error message some times when using the terminal mode? It is kind of strange.

@MCUdude
Copy link
Collaborator Author

MCUdude commented Oct 10, 2022

I'm also able to read and write to flash in terminal mode just fine

Do you still see the error message some times when using the terminal mode? It is kind of strange.

Nope, I didn't see it until I started messing with the apptable memory. Everything else works flawlessly.

@mcuee
Copy link
Collaborator

mcuee commented Oct 10, 2022

Not so sure if the xmega issue is only specific to jtag3 or not, my AVRISP mkii clone seems to be okay.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_git -c avrispmkii -p x32a4u -qq -t
avrdude> part
>>> part

AVR Part                      : ATxmega32A4U
RESET disposition             : dedicated
RETRY pulse                   : SCK
Serial program mode           : yes
Parallel program mode         : yes
Memory Detail                 :

                                  Block Poll               Page                       Polled
  Memory Type Alias    Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
  ----------- -------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
  fuse1                   0     0     0    0 no          1    1      0     0     0 0x00 0x00
  fuse2                   0     0     0    0 no          1    1      0     0     0 0x00 0x00
  fuse4                   0     0     0    0 no          1    1      0     0     0 0x00 0x00
  fuse5                   0     0     0    0 no          1    1      0     0     0 0x00 0x00
  lock                    0     0     0    0 no          1    1      0     0     0 0x00 0x00
  signature               0     0     0    0 no          3    1      0     0     0 0x00 0x00
  prodsig                 0     0     0    0 no         50   50      0     0     0 0x00 0x00
  data                    0     0     0    0 no          0    1      0     0     0 0x00 0x00
  eeprom                  0     0     0    0 no       1024   32      0     0     0 0x00 0x00
  flash                   0     0     0    0 no      36864  256      0     0     0 0x00 0x00
  application             0     0     0    0 no      32768  256      0     0     0 0x00 0x00
  apptable                0     0     0    0 no       4096  256      0     0     0 0x00 0x00
  boot                    0     0     0    0 no       4096  256      0     0     0 0x00 0x00
  usersig                 0     0     0    0 no        256  256      0     0     0 0x00 0x00

avrdude> write apptable 0 0xaa 0x55
>>> write apptable 0 0xaa 0x55
avrdude> flush
>>> flush
avrdude_git.exe: synching cache to device... done
avrdude> dump apptable 0 0x10
>>> dump apptable 0 0x10
0000  aa 55 ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |.U..............|

avrdude> write apptable 0 0xff 0xff 0xaa 0xaa 0x55 0x55
>>> write apptable 0 0xff 0xff 0xaa 0xaa 0x55 0x55
avrdude> flush
>>> flush
avrdude_git.exe: synching cache to device... avrdude>

avrdude> dump apptable 0 0x10
>>> dump apptable 0 0x10
0000  ff ff aa aa 55 55 ff ff  ff ff ff ff ff ff ff ff  |....UU..........|
avrdude> quit
>>> quit

@mcuee
Copy link
Collaborator

mcuee commented Oct 15, 2022

@MCUdude and @stefanrueger
I think this PR can be merged. We can track xmega problems in another issue.

@stefanrueger
Copy link
Collaborator

I'm for some reason suddenly having a hard time writing to apptable...

Might that also be a workflow/make (missing make clean) issue? Let's check again when all the PRs are integrated in the next few days.

@stefanrueger stefanrueger merged commit 08f4f6c into avrdudes:main Oct 17, 2022
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.

3 participants