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

#1052 Add JTAG support for PICkit4 and Snap. #1233

Merged
merged 3 commits into from
Dec 21, 2022

Conversation

janegilruud
Copy link
Contributor

No description provided.

id = "snap";
desc = "MPLAB(R) Snap in JTAG mode";
type = "jtagice3";
prog_modes = PM_JTAG | PM_XMEGAJTAG | PM_AVR32JTAG;
Copy link

Choose a reason for hiding this comment

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

PM_AVR32JTAG - not sure about that

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed.

@MCUdude
Copy link
Collaborator

MCUdude commented Dec 20, 2022

Wow, thanks a lot! I'll test it when I'm back home

@mcuee mcuee added the enhancement New feature or request label Dec 20, 2022
@MCUdude
Copy link
Collaborator

MCUdude commented Dec 20, 2022

Just curious, this won't cause any issues for older JTAG compatible gen3 programmers?

Semi off-topic:
A few lines down in jtag3.c there's this code in the JTAG initialization function. Is osccal needed, or should ur be passed for good measure?

avrdude/src/jtag3.c

Lines 1289 to 1295 in 159d126

unsigned char eecr = p->eecr? p->eecr: 0x3f; // Use default 0x3f if not set
md.eearh_address = eecr - 0x20 + 3;
md.eearl_address = eecr - 0x20 + 2;
md.eecr_address = eecr - 0x20;
md.eedr_address = eecr - 0x20 + 1;
md.spmcr_address = p->spmcr;
//md.osccal_address = p->osccal; // do we need it at all?

@xedbg
Copy link

xedbg commented Dec 20, 2022

There are a few fields in this configuration block which, if zero, can cause loops to never terminate, for example, which is why the check is mandatory in the newer debuggers (you get 0x33 back). Other missing fields might just yield bad results, so they are not mandatory to specify.
Since avrdude is a programmer only, most of this info is not required anyway.
In the case of OSCCAL, there are a handful of ancient devices which need special handling during debug since its address is shared.

@MCUdude
Copy link
Collaborator

MCUdude commented Dec 20, 2022

Tested and works perfectly using both the PICkit4 and SNAP! I can read and write to EEPROM and flash, and terminal mode works great.

$ ./avrdude -cpickit4 -p atmega16 -Uflash:w:blink-m16.hex 

         Vtarget                      : 5.07 V
         JTAG clock megaAVR/program   : 1000 kHz
         JTAG clock megaAVR/debug     : 100 kHz
         PDI/UPDI clock Xmega/megaAVR : 100 kHz
avrdude: AVR device initialized and ready to accept instructions
avrdude: device signature = 0x1e9403 (probably m16)
avrdude: Note: flash memory has been specified, an erase cycle will be performed.
         To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file blink-m16.hex for flash
         with 988 bytes in 1 section within [0, 0x3db]
         using 8 pages and 36 pad bytes
avrdude: writing 988 bytes flash ...

Writing | ################################################## | 100% 0.23 s 

avrdude: 988 bytes of flash written
avrdude: verifying flash memory against blink-m16.hex

Reading | ################################################## | 100% 0.17 s 

avrdude: 988 bytes of flash verified

avrdude done.  Thank you.

$ ./avrdude -csnap -p atmega16 -Uflash:w:blink-m16.hex 

         Vtarget                      : 5.06 V
         JTAG clock megaAVR/program   : 1000 kHz
         JTAG clock megaAVR/debug     : 100 kHz
         PDI/UPDI clock Xmega/megaAVR : 100 kHz
avrdude: AVR device initialized and ready to accept instructions
avrdude: device signature = 0x1e9403 (probably m16)
avrdude: Note: flash memory has been specified, an erase cycle will be performed.
         To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file blink-m16.hex for flash
         with 988 bytes in 1 section within [0, 0x3db]
         using 8 pages and 36 pad bytes
avrdude: writing 988 bytes flash ...

Writing | ################################################## | 100% 0.17 s 

avrdude: 988 bytes of flash written
avrdude: verifying flash memory against blink-m16.hex

Reading | ################################################## | 100% 0.12 s 

avrdude: 988 bytes of flash verified

avrdude done.  Thank you.

While you're at it, could you add JTAG and TPI to these two lines in the docs as well?
I promise there won't be another "one more thing 😉

avrdude.1:

The MPLAB(R) PICkit 4 and MPLAB(R) SNAP, are supported in ISP, PDI and UPDI mode.

avrdude.text:

The MPLAB(R) PICkit 4 and MPLAB(R) SNAP are supported in ISP, PDI and UPDI mode.

But regardless of the docs, this PR is working perfectly fine, and can be merged as it is!
Thank you @janegilruud and @xedbg for yet another early Christmas gift! 🎁

@xedbg
Copy link

xedbg commented Dec 20, 2022

God jul :)

@janegilruud
Copy link
Contributor Author

You're welcome, and God Jul! 🎅 😄

@MCUdude MCUdude self-requested a review December 20, 2022 23:25
Copy link
Collaborator

@MCUdude MCUdude left a comment

Choose a reason for hiding this comment

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

Thanks for updating the docs! PR will be merged within a day or two I think.
God jul til dere også 🎄

@mcuee
Copy link
Collaborator

mcuee commented Dec 21, 2022

This is good. The only thing is that it does not print out the serial number, which should hopefully be addressed by #1223.
Edit: yes the serial number is addressed in #1223, tested using PR #1223 with avrdude.conf from this PR.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1233 -C .\avrdude_pr1233.conf -c pickit4 -p m32a -v

avrdude_pr1233: Version 7.0-20221220 (a2ae465)
                Copyright the AVRDUDE authors;
                see https://github.com/avrdudes/avrdude/blob/main/AUTHORS

                System wide configuration file is C:\work\avr\avrdude_test\avrdude_bin\avrdude_pr1233.conf

                Using Port                    : usb
                Using Programmer              : pickit4
avrdude_pr1233: found CMSIS-DAP compliant device, using EDBG protocol
                AVR Part                      : ATmega32A
                Chip Erase delay              : 9000 us
                PAGEL                         : PD7
                BS2                           : PA0
                RESET disposition             : possible i/o
                RETRY pulse                   : SCK
                Serial program mode           : yes
                Parallel program mode         : yes
                Timeout                       : 200
                StabDelay                     : 100
                CmdexeDelay                   : 25
                SyncLoops                     : 32
                PollIndex                     : 3
                PollValue                     : 0x53
                Memory Detail                 :

                                                  Block Poll               Page                       Polled
                  Memory Type Alias    Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
                  ----------- -------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
                  eeprom                  4    10    64    0 no       1024    4      0  9000  9000 0xff 0xff
                  flash                  33     6    64    0 yes     32768  128    256  4500  4500 0xff 0xff
                  lfuse                   0     0     0    0 no          1    1      0  2000  2000 0x00 0x00
                  hfuse                   0     0     0    0 no          1    1      0  2000  2000 0x00 0x00
                  lock                    0     0     0    0 no          1    1      0  2000  2000 0x00 0x00
                  signature               0     0     0    0 no          3    1      0     0     0 0x00 0x00
                  calibration             0     0     0    0 no          4    1      0     0     0 0x00 0x00

                Programmer Type : JTAGICE3
                Description     : MPLAB(R) PICkit 4 in JTAG mode
                ICE HW version  : 4
                ICE FW version  : 1.14 (rel. 268)
                Serial number   :
                Vtarget         : 5.01 V
                JTAG clock megaAVR/program   : 1000 kHz
                JTAG clock megaAVR/debug     : 100 kHz
                PDI/UPDI clock Xmega/megaAVR : 100 kHz
avrdude_pr1233: JTAG ID returned: 0x3f 0x20 0x50 0xa9
avrdude_pr1233: AVR device initialized and ready to accept instructions
avrdude_pr1233: device signature = 0x1e9502 (probably m32a)

avrdude_pr1233 done.  Thank you.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1233 -C .\avrdude_pr1233.conf -c pickit4 -p m32a -qqt
avrdude> dump lfuse
0000  ff                                                |.               |

avrdude> dump hfuse
0000  19                                                |.               |

avrdude> dump lock
0000  ff                                                |.               |

avrdude> dump signature
0000  1e 95 02                                          |...             |

avrdude> dump calibration
0000  9e 9e 9d a0                                       |....            |

avrdude> write lfuse 0 0xbf
avrdude> write hfuse 0 0x11
avrdude> flush
avrdude> dump lfuse
0000  bf                                                |.               |

avrdude> dump hfuse
0000  11                                                |.               |

avrdude> dump flash 0 0x140
0000  0c 94 2a 00 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 cf e5 d8 e0  cd bf de bf c0 51 d0 40  |.............Q.@|
0060  0a ea 08 83 00 24 e0 e6  f0 e0 10 e0 e0 36 f1 07  | ....$.......6..|
0070  11 f0 01 92 fb cf 00 83  e4 e5 f0 e0 a0 e6 b0 e0  |................|
0080  10 e0 00 e0 0b bf e4 35  f1 07 21 f0 c8 95 31 96  |.... ..5..!...1.|
0090  0d 92 f9 cf 0e 94 4d 00  ff cf 1a 93 0a 93 80 ef  | .....M..... ...|
00a0  81 bb 8f ef 82 bb 0c c0  aa 24 40 ef 06 c0 42 bb  |...... [email protected].|
00b0  0c e2 11 e0 16 d0 44 0f  a3 94 84 e0 8a 15 b8 f7  | .....D.........|
00c0  f3 cf 22 96 08 95 4a 93  5a 93 44 27 55 27 03 c0  |.."...J.Z.D'U'..|
00d0  00 00 4f 5f 5f 4f 40 17  51 07 d0 f3 59 91 49 91  |[email protected].|
00e0  08 95 0e 94 88 00 66 27  77 27 44 27 55 27 0b c0  |......f'w'D'U' .|
00f0  66 27 77 27 02 c0 6f 5f  7f 4f 64 37 e4 e0 7e 07  |f'w'..o_.Od7..~.|
0100  d0 f3 4f 5f 5f 4f 40 17  51 07 90 f3 0c 94 8d 00  |[email protected]... ...|
0110  7a 93 6a 93 5a 93 4a 93  08 95 49 91 59 91 69 91  |z.j.Z.J...I.Y.i.|
0120  79 91 08 95 ff ff ff ff  ff ff ff ff ff ff ff ff  |y...............|
0130  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|

avrdude> write flash 0x200 0x100 0x55 ...
avrdude> flush
avrdude_pr1233 error: page erase not supported
avrdude> dump flash 0x200
0200  55 55 55 55 55 55 55 55  55 55 55 55 55 55 55 55  |UUUUUUUUUUUUUUUU|
0210  55 55 55 55 55 55 55 55  55 55 55 55 55 55 55 55  |UUUUUUUUUUUUUUUU|
0220  55 55 55 55 55 55 55 55  55 55 55 55 55 55 55 55  |UUUUUUUUUUUUUUUU|
0230  55 55 55 55 55 55 55 55  55 55 55 55 55 55 55 55  |UUUUUUUUUUUUUUUU|
0240  55 55 55 55 55 55 55 55  55 55 55 55 55 55 55 55  |UUUUUUUUUUUUUUUU|
0250  55 55 55 55 55 55 55 55  55 55 55 55 55 55 55 55  |UUUUUUUUUUUUUUUU|
0260  55 55 55 55 55 55 55 55  55 55 55 55 55 55 55 55  |UUUUUUUUUUUUUUUU|
0270  55 55 55 55 55 55 55 55  55 55 55 55 55 55 55 55  |UUUUUUUUUUUUUUUU|
0280  55 55 55 55 55 55 55 55  55 55 55 55 55 55 55 55  |UUUUUUUUUUUUUUUU|
0290  55 55 55 55 55 55 55 55  55 55 55 55 55 55 55 55  |UUUUUUUUUUUUUUUU|
02a0  55 55 55 55 55 55 55 55  55 55 55 55 55 55 55 55  |UUUUUUUUUUUUUUUU|
02b0  55 55 55 55 55 55 55 55  55 55 55 55 55 55 55 55  |UUUUUUUUUUUUUUUU|
02c0  55 55 55 55 55 55 55 55  55 55 55 55 55 55 55 55  |UUUUUUUUUUUUUUUU|
02d0  55 55 55 55 55 55 55 55  55 55 55 55 55 55 55 55  |UUUUUUUUUUUUUUUU|
02e0  55 55 55 55 55 55 55 55  55 55 55 55 55 55 55 55  |UUUUUUUUUUUUUUUU|
02f0  55 55 55 55 55 55 55 55  55 55 55 55 55 55 55 55  |UUUUUUUUUUUUUUUU|
0300  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0310  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0320  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0330  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|

avrdude> dump eeprom 0
0000  ff ff ff ff 4f 20 57 4f  52 4c 44 21 00 ff ff ff  |....O WORLD!....|
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 eeprom 0 "Hello World!"
avrdude> flush
avrdude_pr1233 error: page erase not supported
avrdude> dump eeprom 0 0x10
0000  48 65 6c 6c 6f 20 57 6f  72 6c 64 21 00 ff ff ff  |Hello World!....|

avrdude> write eeprom 0 0x100 0x55 ...
avrdude> flush
avrdude_pr1233 error: page erase not supported

avrdude> dump eeprom 0 0x100
0000  55 55 55 55 55 55 55 55  55 55 55 55 55 55 55 55  |UUUUUUUUUUUUUUUU|
0010  55 55 55 55 55 55 55 55  55 55 55 55 55 55 55 55  |UUUUUUUUUUUUUUUU|
0020  55 55 55 55 55 55 55 55  55 55 55 55 55 55 55 55  |UUUUUUUUUUUUUUUU|
0030  55 55 55 55 55 55 55 55  55 55 55 55 55 55 55 55  |UUUUUUUUUUUUUUUU|
0040  55 55 55 55 55 55 55 55  55 55 55 55 55 55 55 55  |UUUUUUUUUUUUUUUU|
0050  55 55 55 55 55 55 55 55  55 55 55 55 55 55 55 55  |UUUUUUUUUUUUUUUU|
0060  55 55 55 55 55 55 55 55  55 55 55 55 55 55 55 55  |UUUUUUUUUUUUUUUU|
0070  55 55 55 55 55 55 55 55  55 55 55 55 55 55 55 55  |UUUUUUUUUUUUUUUU|
0080  55 55 55 55 55 55 55 55  55 55 55 55 55 55 55 55  |UUUUUUUUUUUUUUUU|
0090  55 55 55 55 55 55 55 55  55 55 55 55 55 55 55 55  |UUUUUUUUUUUUUUUU|
00a0  55 55 55 55 55 55 55 55  55 55 55 55 55 55 55 55  |UUUUUUUUUUUUUUUU|
00b0  55 55 55 55 55 55 55 55  55 55 55 55 55 55 55 55  |UUUUUUUUUUUUUUUU|
00c0  55 55 55 55 55 55 55 55  55 55 55 55 55 55 55 55  |UUUUUUUUUUUUUUUU|
00d0  55 55 55 55 55 55 55 55  55 55 55 55 55 55 55 55  |UUUUUUUUUUUUUUUU|
00e0  55 55 55 55 55 55 55 55  55 55 55 55 55 55 55 55  |UUUUUUUUUUUUUUUU|
00f0  55 55 55 55 55 55 55 55  55 55 55 55 55 55 55 55  |UUUUUUUUUUUUUUUU|

avrdude> quit
avrdude>

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1233 -C .\avrdude_pr1233.conf -c pickit4 -p m32a
 -U .\m32a_blink.hex

                Vtarget                      : 5.00 V
                JTAG clock megaAVR/program   : 1000 kHz
                JTAG clock megaAVR/debug     : 100 kHz
                PDI/UPDI clock Xmega/megaAVR : 100 kHz
avrdude_pr1233: AVR device initialized and ready to accept instructions
avrdude_pr1233: device signature = 0x1e9502 (probably m32a)
avrdude_pr1233: Note: flash memory has been specified, an erase cycle will be performed.
                To disable this feature, specify the -D option.
avrdude_pr1233: erasing chip
avrdude_pr1233: reading input file .\m32a_blink.hex for flash
                with 526 bytes in 1 section within [0, 0x20d]
                using 5 pages and 114 pad bytes
avrdude_pr1233: writing 526 bytes flash ...

Writing | ################################################## | 100% 0.18 s

avrdude_pr1233: 526 bytes of flash written
avrdude_pr1233: verifying flash memory against .\m32a_blink.hex

Reading | ################################################## | 100% 0.11 s

avrdude_pr1233: 526 bytes of flash verified

avrdude_pr1233 done.  Thank you.

@mcuee
Copy link
Collaborator

mcuee commented Dec 21, 2022

@janegilruud

In the terminal mode, it is mentioned that page erase is not supported. Is this a limitation in JTAG mode? Or is it just that you have not implemented the function?

@xedbg
Copy link

xedbg commented Dec 21, 2022

These JTAG devices have only a "chip erase" function. Page erase can only be achieved by entering debug mode. So I think you could call this a limitation of the device.

@mcuee
Copy link
Collaborator

mcuee commented Dec 21, 2022

These JTAG devices have only a "chip erase" function. Page erase can only be achieved by entering debug mode. So I think you could call this a limitation of the device.

I see. In this case, this PR is good to go from my side.

@stefanrueger stefanrueger linked an issue Dec 21, 2022 that may be closed by this pull request
@stefanrueger stefanrueger merged commit 217adc5 into avrdudes:main Dec 21, 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.

megaAVR JTAG programming doesn't work for SNAP and PICkit4
5 participants