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

Replace internal knowledge in jtag3.c by a public API #996

Merged
merged 2 commits into from
Jun 28, 2022

Conversation

dl8dtl
Copy link
Contributor

@dl8dtl dl8dtl commented Jun 17, 2022

In certain situations (CRC failure, device locked), that JTAG3
read functions need to return an indication to the caller that
it is OK to proceed, and allow erasing the device anyway.

Historically, the JTAG3 code passed the respective protocol
errors directly (and unexplained) up to the caller, leaving
the decision to the caller how to handle the situation.

Replace that by a more common return value API. New code should
prefer this API instead of any hardcoded return values.

This is an alternative implementation proposal meant to supersede
PR #982

In certain situations (CRC failure, device locked), that JTAG3
read functions need to return an indication to the caller that
it is OK to proceed, and allow erasing the device anyway.

Historically, the JTAG3 code passed the respective protocol
errors directly (and unexplained) up to the caller, leaving
the decision to the caller how to handle the situation.

Replace that by a more common return value API. New code should
prefer this API instead of any hardcoded return values.
@mcuee mcuee added the enhancement New feature or request label Jun 20, 2022
@umbynos
Copy link
Contributor

umbynos commented Jun 21, 2022

I just tried with avrdude built at this commit on macos but it does not seem to supeseed #982
I tried to upload to an Arduino Uno WiFi Rev 2(that board has an ATmega 4809 and an mEDBG programmer based on Atmega32U4) on macOS, but this is the output I got:

avrdude: Version 7.0
         Copyright (c) Brian Dean, http://www.bdmicro.com/
         Copyright (c) Joerg Wunsch

         System wide configuration file is "/Users/ubidefeo/Library/Arduino15/packages/arduino/tools/avrdude/6.3.0-arduino17/etc/avrdude.conf"
         User configuration file is "/Users/ubidefeo/.avrduderc"
avrdude: warning at /Users/ubidefeo/.avrduderc:2: programmer arduinoisp overwrites previous definition /Users/ubidefeo/Library/Arduino15/packages/arduino/tools/avrdude/6.3.0-arduino17/etc/avrdude.conf:922.

         Using Port                    : usb
         Using Programmer              : xplainedmini_updi
         Overriding Baud Rate          : 115200
avrdude: Found CMSIS-DAP compliant device, using EDBG protocol
         AVR Part                      : ATmega4809
         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
           ----------- -------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           signature               0     0     0    0 no          3    1      0     0     0 0x00 0x00
           prodsig                 0     0     0    0 no         61   61      0     0     0 0x00 0x00
           sernum                  0     0     0    0 no         10    1      0     0     0 0x00 0x00
           osccal16                0     0     0    0 no          2    1      0     0     0 0x00 0x00
           osccal20                0     0     0    0 no          2    1      0     0     0 0x00 0x00
           tempsense               0     0     0    0 no          2    1      0     0     0 0x00 0x00
           osc16err                0     0     0    0 no          2    1      0     0     0 0x00 0x00
           osc20err                0     0     0    0 no          2    1      0     0     0 0x00 0x00
           fuses                   0     0     0    0 no          9   10      0     0     0 0x00 0x00
           fuse0       wdtcfg      0     0     0    0 no          1    1      0     0     0 0x00 0x00
           fuse1       bodcfg      0     0     0    0 no          1    1      0     0     0 0x00 0x00
           fuse2       osccfg      0     0     0    0 no          1    1      0     0     0 0x00 0x00
           fuse4       tcd0cfg     0     0     0    0 no          1    1      0     0     0 0x00 0x00
           fuse5       syscfg0     0     0     0    0 no          1    1      0     0     0 0x00 0x00
           fuse6       syscfg1     0     0     0    0 no          1    1      0     0     0 0x00 0x00
           fuse7       append      0     0     0    0 no          1    1      0     0     0 0x00 0x00
           fuse8       bootend     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
           data                    0     0     0    0 no          0    1      0     0     0 0x00 0x00
           userrow     usersig     0     0     0    0 no         64   64      0     0     0 0x00 0x00
           flash                   0     0     0    0 no      49152  128      0     0     0 0x00 0x00
           eeprom                  0     0     0    0 no        256   64      0     0     0 0x00 0x00

         Programmer Type : JTAGICE3_UPDI
         Description     : Atmel AVR XplainedMini in UPDI mode
avrdude: jtag3_getparm(): bad response to get parameter (scope 0x01, section 0, parm 0)

avrdude: jtag3_getparm(): bad response to get parameter (scope 0x01, section 0, parm 1)
avrdude: initialization failed, rc=-1
         Double check connections and try again, or use -F to override
         this check.


avrdude done.  Thank you.

Whereas with #982:

avrdude: Version 7.0
         Copyright (c) Brian Dean, http://www.bdmicro.com/
         Copyright (c) Joerg Wunsch

         System wide configuration file is "/Users/ubidefeo/Library/Arduino15/packages/arduino/tools/avrdude/6.3.0-arduino17/etc/avrdude.conf"
         User configuration file is "/Users/ubidefeo/.avrduderc"
avrdude: warning at /Users/ubidefeo/.avrduderc:2: programmer arduinoisp overwrites previous definition /Users/ubidefeo/Library/Arduino15/packages/arduino/tools/avrdude/6.3.0-arduino17/etc/avrdude.conf:922.

         Using Port                    : usb
         Using Programmer              : xplainedmini_updi
         Overriding Baud Rate          : 115200
avrdude: Found CMSIS-DAP compliant device, using EDBG protocol
         AVR Part                      : ATmega4809
         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
           ----------- -------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           signature               0     0     0    0 no          3    0      0     0     0 0x00 0x00
           prodsig                 0     0     0    0 no         61   61      0     0     0 0x00 0x00
           sernum                  0     0     0    0 no         10    0      0     0     0 0x00 0x00
           osccal16                0     0     0    0 no          2    0      0     0     0 0x00 0x00
           osccal20                0     0     0    0 no          2    0      0     0     0 0x00 0x00
           tempsense               0     0     0    0 no          2    0      0     0     0 0x00 0x00
           osc16err                0     0     0    0 no          2    0      0     0     0 0x00 0x00
           osc20err                0     0     0    0 no          2    0      0     0     0 0x00 0x00
           fuses                   0     0     0    0 no          9   10      0     0     0 0x00 0x00
           fuse0       wdtcfg      0     0     0    0 no          1    0      0     0     0 0x00 0x00
           fuse1       bodcfg      0     0     0    0 no          1    0      0     0     0 0x00 0x00
           fuse2       osccfg      0     0     0    0 no          1    0      0     0     0 0x00 0x00
           fuse4       tcd0cfg     0     0     0    0 no          1    0      0     0     0 0x00 0x00
           fuse5       syscfg0     0     0     0    0 no          1    0      0     0     0 0x00 0x00
           fuse6       syscfg1     0     0     0    0 no          1    0      0     0     0 0x00 0x00
           fuse7       append      0     0     0    0 no          1    0      0     0     0 0x00 0x00
           fuse8       bootend     0     0     0    0 no          1    0      0     0     0 0x00 0x00
           lock                    0     0     0    0 no          1    0      0     0     0 0x00 0x00
           data                    0     0     0    0 no          0    0      0     0     0 0x00 0x00
           userrow     usersig     0     0     0    0 no         64   64      0     0     0 0x00 0x00
           flash                   0     0     0    0 no      49152  128      0     0     0 0x00 0x00
           eeprom                  0     0     0    0 no        256   64      0     0     0 0x00 0x00

         Programmer Type : JTAGICE3_UPDI
         Description     : Atmel AVR XplainedMini in UPDI mode
         ICE HW version  : 0
         ICE FW version  : 1.19 (rel. 57)
         Serial number   : B7B965EBBB11BB6C3C32
         Vtarget         : 5.00 V
avrdude: Partial Family_ID returned: "mega"
avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.07s

avrdude: Device signature = 0x1e9651 (probably m4809)
avrdude: erasing chip
avrdude: reading input file "/private/var/folders/l_/cjj77tdd0g73w_yr33wytwxr0000gn/T/arduino-sketch-AF8B62A1684AEEEB964F5664BDD80C85/BlinkDemo.ino.hex"
avrdude: writing flash (2022 bytes):

Writing | ################################################## | 100% 0.33s

avrdude: 2022 bytes of flash written
avrdude: reading input file "0x01"
avrdude: writing fuse2/osccfg (1 bytes):

Writing | ################################################## | 100% 0.01s

avrdude: 1 bytes of fuse2/osccfg written
avrdude: reading input file "0xC9"
avrdude: writing fuse5/syscfg0 (1 bytes):

Writing | ################################################## | 100% 0.02s

avrdude: 1 bytes of fuse5/syscfg0 written
avrdude: reading input file "0x02"
avrdude: writing fuse8/bootend (1 bytes):

Writing | ################################################## | 100% 0.02s

avrdude: 1 bytes of fuse8/bootend written
avrdude: reading input file "/Users/ubidefeo/Library/Arduino15/packages/arduino/hardware/megaavr/1.8.7/bootloaders/atmega4809_uart_bl.hex"
avrdude: writing flash (280 bytes):

Writing | ################################################## | 100% 0.12s

avrdude: 280 bytes of flash written

avrdude done.  Thank you.

@dl8dtl
Copy link
Contributor Author

dl8dtl commented Jun 21, 2022

Is there any way I could reproduce the issue with another board? I've got an ATmega4809 Curiosity Nano board (I believe).

@MCUdude
Copy link
Collaborator

MCUdude commented Jun 21, 2022

@dl8dtl I believe you need an ATmega32u4 based programmer.
You can easily build your own: https://github.com/MCUdude/microUPDI

@dl8dtl
Copy link
Contributor Author

dl8dtl commented Jun 21, 2022

I think I do have an ATmega32U2 based devboard around, but not an ATmega32U4 one.

@dl8dtl
Copy link
Contributor Author

dl8dtl commented Jun 21, 2022

I believe you need an ATmega32u4 based programmer.

OK, it's not a Curiosity Nano which I've got but an Arduino Nano Every board. It doesn't have the ATmega32U4 but a SAMD11.

Is it somehow possible to trigger that wrong CRC fuse setting explicitly to make it return that error?

@dl8dtl
Copy link
Contributor Author

dl8dtl commented Jun 21, 2022

It doesn't have the ATmega32U4 but a SAMD11.

But it doesn't use the EDBG protocol …

@dl8dtl
Copy link
Contributor Author

dl8dtl commented Jun 21, 2022

Hmm, the AVR128Dx Curiosity Nano boards use EDBG (though with a SAMD chip), and the AVR-Dx controllers have the same syscfg0 layout.
When I program syscfg0 from 0xc0 to 0x00, however, I can still access the device.

@MCUdude
Copy link
Collaborator

MCUdude commented Jun 21, 2022

I can confirm that this PR does not resolve #982. I have an Arduino UNO Wifi Rev2 to test with.

With #982 applied:

$ ./avrdude -cxplainedmini_updi -patmega4809 -v

avrdude: Version 7.0-20220508
         Copyright (c) Brian Dean, http://www.bdmicro.com/
         Copyright (c) Joerg Wunsch

         System wide configuration file is "/Users/hans/Downloads/avrdude-joerg/src/avrdude.conf"
         User configuration file is "/Users/hans/.avrduderc"
         User configuration file does not exist or is not a regular file, skipping

         Using Port                    : usb
         Using Programmer              : xplainedmini_updi
avrdude: Found CMSIS-DAP compliant device, using EDBG protocol
         AVR Part                      : ATmega4809
         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
           ----------- -------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           signature               0     0     0    0 no          3    1      0     0     0 0x00 0x00
           prodsig                 0     0     0    0 no         61   61      0     0     0 0x00 0x00
           sernum                  0     0     0    0 no         10    1      0     0     0 0x00 0x00
           osccal16                0     0     0    0 no          2    1      0     0     0 0x00 0x00
           osccal20                0     0     0    0 no          2    1      0     0     0 0x00 0x00
           tempsense               0     0     0    0 no          2    1      0     0     0 0x00 0x00
           osc16err                0     0     0    0 no          2    1      0     0     0 0x00 0x00
           osc20err                0     0     0    0 no          2    1      0     0     0 0x00 0x00
           fuses                   0     0     0    0 no          9   10      0     0     0 0x00 0x00
           fuse0       wdtcfg      0     0     0    0 no          1    1      0     0     0 0x00 0x00
           fuse1       bodcfg      0     0     0    0 no          1    1      0     0     0 0x00 0x00
           fuse2       osccfg      0     0     0    0 no          1    1      0     0     0 0x00 0x00
           fuse4       tcd0cfg     0     0     0    0 no          1    1      0     0     0 0x00 0x00
           fuse5       syscfg0     0     0     0    0 no          1    1      0     0     0 0x00 0x00
           fuse6       syscfg1     0     0     0    0 no          1    1      0     0     0 0x00 0x00
           fuse7       append      0     0     0    0 no          1    1      0     0     0 0x00 0x00
           fuse8       bootend     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
           data                    0     0     0    0 no          0    1      0     0     0 0x00 0x00
           userrow     usersig     0     0     0    0 no         64   64      0     0     0 0x00 0x00
           flash                   0     0     0    0 no      49152  128      0     0     0 0x00 0x00
           eeprom                  0     0     0    0 no        256   64      0     0     0 0x00 0x00

         Programmer Type : JTAGICE3_UPDI
         Description     : Atmel AVR XplainedMini in UPDI mode
         ICE HW version  : 0
         ICE FW version  : 1.19 (rel. 57)
         Serial number   : 11B050488B1C13A05D65
         Vtarget         : 5.00 V
avrdude: Partial Family_ID returned: "mega"
avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.07s

avrdude: Device signature = 0x1e9651 (probably m4809)

avrdude done.  Thank you.

With this (#996) applied:

$ ./avrdude -cxplainedmini_updi -patmega4809 -v

avrdude: Version 7.0-20220508
         Copyright (c) Brian Dean, http://www.bdmicro.com/
         Copyright (c) Joerg Wunsch

         System wide configuration file is "/Users/hans/Downloads/avrdude-joerg/src/avrdude.conf"
         User configuration file is "/Users/hans/.avrduderc"
         User configuration file does not exist or is not a regular file, skipping

         Using Port                    : usb
         Using Programmer              : xplainedmini_updi
avrdude: Found CMSIS-DAP compliant device, using EDBG protocol
         AVR Part                      : ATmega4809
         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
           ----------- -------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           signature               0     0     0    0 no          3    1      0     0     0 0x00 0x00
           prodsig                 0     0     0    0 no         61   61      0     0     0 0x00 0x00
           sernum                  0     0     0    0 no         10    1      0     0     0 0x00 0x00
           osccal16                0     0     0    0 no          2    1      0     0     0 0x00 0x00
           osccal20                0     0     0    0 no          2    1      0     0     0 0x00 0x00
           tempsense               0     0     0    0 no          2    1      0     0     0 0x00 0x00
           osc16err                0     0     0    0 no          2    1      0     0     0 0x00 0x00
           osc20err                0     0     0    0 no          2    1      0     0     0 0x00 0x00
           fuses                   0     0     0    0 no          9   10      0     0     0 0x00 0x00
           fuse0       wdtcfg      0     0     0    0 no          1    1      0     0     0 0x00 0x00
           fuse1       bodcfg      0     0     0    0 no          1    1      0     0     0 0x00 0x00
           fuse2       osccfg      0     0     0    0 no          1    1      0     0     0 0x00 0x00
           fuse4       tcd0cfg     0     0     0    0 no          1    1      0     0     0 0x00 0x00
           fuse5       syscfg0     0     0     0    0 no          1    1      0     0     0 0x00 0x00
           fuse6       syscfg1     0     0     0    0 no          1    1      0     0     0 0x00 0x00
           fuse7       append      0     0     0    0 no          1    1      0     0     0 0x00 0x00
           fuse8       bootend     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
           data                    0     0     0    0 no          0    1      0     0     0 0x00 0x00
           userrow     usersig     0     0     0    0 no         64   64      0     0     0 0x00 0x00
           flash                   0     0     0    0 no      49152  128      0     0     0 0x00 0x00
           eeprom                  0     0     0    0 no        256   64      0     0     0 0x00 0x00

         Programmer Type : JTAGICE3_UPDI
         Description     : Atmel AVR XplainedMini in UPDI mode
avrdude: jtag3_getparm(): bad response to get parameter (scope 0x01, section 0, parm 0)

avrdude: jtag3_getparm(): bad response to get parameter (scope 0x01, section 0, parm 1)
avrdude: initialization failed, rc=-1
         Double check connections and try again, or use -F to override
         this check.


avrdude done.  Thank you.

@dl8dtl
Copy link
Contributor Author

dl8dtl commented Jun 21, 2022

Please run it with -vvvv, and attach the resulting file (don't post it inline).

@MCUdude
Copy link
Collaborator

MCUdude commented Jun 21, 2022

Please run it with -vvvv, and attach the resulting file (don't post it inline).

Sure!

avrdude7_jtag3_retcode.txt

@mcuee
Copy link
Collaborator

mcuee commented Jun 21, 2022

From here:

First of all, the problem is probably due to a bug in avrdude, which incorrectly writes the CRC bits (fuse SYSCFG0 - 0x05).
Once CRC is wrong, the only way to unlock the chip is through an unlock/chip erase procedure which differs from the normal chip erase.
This procedure was triggered only in case the return value of avr_signature was -68.
Due to other changes, mEDBG reports -67 as errors, so it doesn't trigger the right erase procedure.

The above analysis assumes the issue is with avrdude, but on the other hand, SAMD21 based EDBG has no issues, can we assume the issue is more likely with the ATmega32U4 FW implementation of EDBG from Arduino side? The issues reported are only for the two Arduino Wifi boards (Uno Wifi and Mega2560 Wifi).

@mcuee
Copy link
Collaborator

mcuee commented Jun 22, 2022

Hmm, the AVR128Dx Curiosity Nano boards use EDBG (though with a SAMD chip), and the AVR-Dx controllers have the same syscfg0 layout. When I program syscfg0 from 0xc0 to 0x00, however, I can still access the device.

FYI, I have AVR128DB48 Curiosity Nano (SAMD21 based) and ATmega328PB-xmini (ATmega32U4 based but not using UPDI mode but rather ISP mode). Both do not seem to have issues. So it seems to be specific to the ATmega32U4 based FW implementation of UPDI.

It will be good to see if others Xplained Mini board using ATmega32U4 based UPDI interface have the same issues or not.

For example, ATtiny817 Xplained Mini.
https://www.microchip.com/en-us/development-tool/attiny817-xmini

@dl8dtl
Copy link
Contributor Author

dl8dtl commented Jun 22, 2022

ATmega328PB-xmini

I do have that one as well. Maybe it's possible to use that EDBG chip also to attach it to some external MCU.

I'll give that a try.

For example, ATtiny817 Xplained Mini.

I've got that one as well. You're right, the ATtiny817 does have the same SYSCFG fuses, so I can try it directly.

@dl8dtl
Copy link
Contributor Author

dl8dtl commented Jun 22, 2022

I've got that one as well. You're right, the ATtiny817 does have the same SYSCFG fuses, so I can try it directly.

Yep, that experiences the same behaviour.

So, I've got something to test at tonight.

@MCUdude
Copy link
Collaborator

MCUdude commented Jun 22, 2022

I can confirm that this PR (#996) doesn't work with the ATtiny817 Xplained Mini board either (ATtiny817 replaced with an ATtiny3217).

RSP3_FAIL_CRC_FAILURE is 0x43 rather than 0x47.

jtag3_errcode() must only be applied to a reason code, not
to any general status code.
@dl8dtl
Copy link
Contributor Author

dl8dtl commented Jun 22, 2022

I found a number of mistakes and confusions, and fixed them.
Now, I could revive my ATtiny817 Xplained-Mini from the bad CRC fuse setting.
The key point though is, you have to invoke avrdude with -e to trigger a chip erase, and within the same session (e.g. in terminal mode), turn of the CRC check in syscfg0 fuse.

@MCUdude
Copy link
Collaborator

MCUdude commented Jun 23, 2022

Thanks for looking into it @dl8dtl! I can confirm that your latest commit fix the issue, and I'm now able to initialize my ATtiny817 Xplained Mini and my Arduino UNO Wifi Rev2 board.

@dl8dtl
Copy link
Contributor Author

dl8dtl commented Jun 23, 2022

Thanks, so it would be interesting to get @umbynos to also verify it, then we can merge it.

@dl8dtl
Copy link
Contributor Author

dl8dtl commented Jun 23, 2022

And any new code is encouraged to use the new symbolic return codes:


#define LIBAVRDUDE_SUCCESS 0
#define LIBAVRDUDE_GENERAL_FAILURE (-1)
#define LIBAVRDUDE_NOTSUPPORTED (-2)
#define LIBAVRDUDE_SOFTFAIL (-3)

@umbynos
Copy link
Contributor

umbynos commented Jun 23, 2022

Monday I'll try on my Uno WiFi Rev2. Thanks 😄

@umbynos
Copy link
Contributor

umbynos commented Jun 27, 2022

Ok I tested this on linux with an Arduino Uno WiFi Rev2 and it seems to work. With a normal upload from the IDE and the Arduino CLI. But I did not tested this with bad CRC fuse settings. Could you help me on this? How could I write this fuse settings to my board?

@MCUdude
Copy link
Collaborator

MCUdude commented Jun 27, 2022

@umbynos I haven't really used the CRC functionality but according the the ATmega4809 datasheet, the default checksum is 0xFFFF, and the correct checksum has to be calculated and placed in a known location in flash.

just write 0x09 to the SSYSCFG0 fuse instead of 0xC9 for the UNO Wifi Rev2 and you should be able to test the CRC fuctionality with any program as long as you don't touch the default CRC checksum of 0xFFFF.

avrdude -Cavrdude.conf -v -cxplainedmini_updi -patmega4809 -Usyscfg0:w:0x09:m

From the datasheet:

The CRC reads in byte-by-byte of the content of the section(s) it is set up to check, starting with byte 0, and generates a new checksum per byte. The byte is sent through a shift register as depicted below, starting with the most significant bit. If the last bytes in the section contain the correct checksum, the CRC will pass. See 25.3.2.1 Checksum for how to place the checksum. The initial value of the checksum register is 0xFFFF.

23.3.2.1 Checksum
megaAVR® 0-series Cyclic Redundancy Check Memory Scan (CRCSCAN...
The pre-calculated checksum must be present in the last location of the section to be checked. If the BOOT section should be checked, the checksum must be saved in the last bytes of the BOOT section, and similarly for APPLICATION and entire Flash. Table 25-1 shows explicitly how the checksum should be stored for the different sections. Also, see the CRCSCAN.CTRLB register description for how to configure which section to check and the device fuse description for how to configure the BOOTEND and APPEND fuses.
Table 25-1. Placement the Pre-Calculated Checksum in Flash

Section to Check CHECKSUM[15:8] CHECKSUM[7:0]
BOOT FUSE_BOOTEND*256-2 FUSE_BOOTEND*256-1
BOOT and APPLICATION FUSE_APPEND*256-2 FUSE_APPEND*256-1
Full Flash FLASHEND-1 FLASHEND

@dl8dtl
Copy link
Contributor Author

dl8dtl commented Jun 27, 2022

How could I write this fuse settings to my board?

Write anything but the default of 11 into the topmost two bits of syscfg0 fuse.

E.g. in terminal mode:

% ./build_freebsd/src/avrdude -c xplainedmini_updi -p t817 -t

         Vtarget                      : 5.00 V
avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.10s

avrdude: Device signature = 0x1e9320 (probably t817)
avrdude> d syscfg0
>>> d syscfg0 

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

0000  f4                                                |.               |

avrdude> w syscfg0 0 0x34
>>> w syscfg0 0 0x34 

Writing | ################################################## | 100% 0.03s

avrdude> q
>>> q 

avrdude done.  Thank you.

As soon as you finish this session, you cannot connect anymore:

% ./build_freebsd/src/avrdude -c xplainedmini_updi -p t817 -t

         Vtarget                      : 5.00 V
avrdude: AVR device initialized and ready to accept instructions

Reading |                                                    | 0% 0.00savrdude: Device is locked! Chip erase required to unlock.
avr_read(): error reading address 0x0000
    read operation failed for memory "signature"
avrdude: error reading signature data for part "ATtiny817", rc=-3
avrdude: Received FamilyID: "tinyAVR"
avrdude: error reading signature data, rc=-3

avrdude done.  Thank you.

(As an improvement, we could avoid even trying to read the signature in that situation.)

@umbynos
Copy link
Contributor

umbynos commented Jun 28, 2022

Ok, after running avrdude -Cavrdude.conf -v -cxplainedmini_updi -patmega4809 -Usyscfg0:w:0x09:m the board:

avrdude: Version 7.0
         Copyright (c) Brian Dean, http://www.bdmicro.com/
         Copyright (c) Joerg Wunsch

         System wide configuration file is "etc/avrdude.conf"
         User configuration file is "/home/umberto/.avrduderc"
         User configuration file does not exist or is not a regular file, skipping

         Using Port                    : usb
         Using Programmer              : xplainedmini_updi
avrdude: Found CMSIS-DAP compliant device, using EDBG protocol
         AVR Part                      : ATmega4809
         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
           ----------- -------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           signature               0     0     0    0 no          3    0      0     0     0 0x00 0x00
           prodsig                 0     0     0    0 no         61   61      0     0     0 0x00 0x00
           sernum                  0     0     0    0 no         10    0      0     0     0 0x00 0x00
           osccal16                0     0     0    0 no          2    0      0     0     0 0x00 0x00
           osccal20                0     0     0    0 no          2    0      0     0     0 0x00 0x00
           tempsense               0     0     0    0 no          2    0      0     0     0 0x00 0x00
           osc16err                0     0     0    0 no          2    0      0     0     0 0x00 0x00
           osc20err                0     0     0    0 no          2    0      0     0     0 0x00 0x00
           fuses                   0     0     0    0 no          9   10      0     0     0 0x00 0x00
           fuse0       wdtcfg      0     0     0    0 no          1    0      0     0     0 0x00 0x00
           fuse1       bodcfg      0     0     0    0 no          1    0      0     0     0 0x00 0x00
           fuse2       osccfg      0     0     0    0 no          1    0      0     0     0 0x00 0x00
           fuse4       tcd0cfg     0     0     0    0 no          1    0      0     0     0 0x00 0x00
           fuse5       syscfg0     0     0     0    0 no          1    0      0     0     0 0x00 0x00
           fuse6       syscfg1     0     0     0    0 no          1    0      0     0     0 0x00 0x00
           fuse7       append      0     0     0    0 no          1    0      0     0     0 0x00 0x00
           fuse8       bootend     0     0     0    0 no          1    0      0     0     0 0x00 0x00
           lock                    0     0     0    0 no          1    0      0     0     0 0x00 0x00
           data                    0     0     0    0 no          0    0      0     0     0 0x00 0x00
           userrow     usersig     0     0     0    0 no         64   64      0     0     0 0x00 0x00
           flash                   0     0     0    0 no      49152  128      0     0     0 0x00 0x00
           eeprom                  0     0     0    0 no        256   64      0     0     0 0x00 0x00

         Programmer Type : JTAGICE3_UPDI
         Description     : Atmel AVR XplainedMini in UPDI mode
         ICE HW version  : 0
         ICE FW version  : 1.19 (rel. 57)
         Serial number   : 62C1BCEC743BA03919A4
         Vtarget         : 5.00 V
avrdude: Partial Family_ID returned: "mega"
avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.07s

avrdude: Device signature = 0x1e9651 (probably m4809)
avrdude: reading input file "0x09"
avrdude: writing fuse5/syscfg0 (1 bytes):

Writing | ################################################## | 100% 0.02s

avrdude: 1 bytes of fuse5/syscfg0 written
avrdude: verifying fuse5/syscfg0 memory against 0x09:

Reading | ################################################## | 100% 0.02s

avrdude: verification error, first mismatch at byte 0x0000
         0x0d != 0x09
avrdude: verification error; content mismatch

avrdude done.  Thank you.

But when running the same command again:

Reading |                                                    | 0% 0.00savrdude: Device is locked! Chip erase required to unlock.
avr_read(): error reading address 0x0000
    read operation failed for memory "signature"
avrdude: error reading signature data for part "ATmega4809", rc=-3
avrdude: System Information Block: "megaAVR P:0D:1-3"
avrdude: Received FamilyID: "megaAVR"
avrdude: error reading signature data, rc=-3

avrdude done.  Thank you.

So I would say the CRC functionality works, since the board successfully recovers when flashed again from the Arduino IDE or Arduino CLI.

Write anything but the default of 11 into the topmost two bits of syscfg0 fuse.

I can confirm that this causes:

Reading |                                                    | 0% 0.00savrdude: Device is locked! Chip erase required to unlock.
avr_read(): error reading address 0x0000
    read operation failed for memory "signature"
avrdude: error reading signature data for part "ATmega4809", rc=-3
avrdude: Received FamilyID: "megaAVR"
avrdude: error reading signature data, rc=-3

I would say this functionality works too, since the board successfully recovers when flashed again from the Arduino IDE or Arduino CLI. 🎉

@umbynos
Copy link
Contributor

umbynos commented Jun 28, 2022

I also tried to revive the uno wifi rev2 from the bad CRC fuse setting without #982 and #996 and:

Reading |                                                    | 0% 0.00savrdude: bad response to enter progmode command: 0xa0
avr_read(): error reading address 0x0000
    read operation failed for memory "signature"
avrdude: error reading signature data for part "ATmega4809", rc=-67
avrdude: error reading signature data, rc=-67

avrdude done.  Thank you.

So I would say this supersedes #982 and fixes the issue. Thanks a lot!

umbynos added a commit to arduino/avrdude-packing that referenced this pull request Jun 28, 2022
@dl8dtl
Copy link
Contributor Author

dl8dtl commented Jun 28, 2022

Thanks for the detailed review, much appreciated!

@dl8dtl dl8dtl merged commit 7ec51a5 into avrdudes:main Jun 28, 2022
umbynos added a commit to arduino/avrdude-packing that referenced this pull request Nov 2, 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.

4 participants