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

[bug #48261] STK500V2 ISP problem with ATmega8/8515/8535, ATmega16 and ATmega32 #425

Closed
avrs-admin opened this issue Dec 10, 2021 · 91 comments · Fixed by #1273
Closed

[bug #48261] STK500V2 ISP problem with ATmega8/8515/8535, ATmega16 and ATmega32 #425

avrs-admin opened this issue Dec 10, 2021 · 91 comments · Fixed by #1273
Assignees
Labels
bug Something isn't working
Milestone

Comments

@avrs-admin
Copy link

Johnny Quest [email protected]
Sat 18 Jun 2016 04:51:15 AM UTC

Hello:
First off, I have done my homework on this matter and would not have posted seeking assistance unless it was a last resort.  smiley

This message was posted on AVR FREAKS. http://www.avrfreaks.net/forum/atmega32a-avr-dragon-programming-flaky-ness

Background:  Ex-disti-FAE for ATMEL as one of my support lines.  Very familiar with ATmega48/88/168/328, ATtiny25/45/85, ATmega32U4, AT90USB1286 (and somewhat on the ATmega2560).

Development environment:  Using Linux for development running AVR STUDIO in a "Virtual WINDOWS" environment.   AVRDUDE V6.3 for Linux.  ATmega32A, 16MHz external crystal, VCC = 5V (PwrSup is 1A capable).  Fuses set for external high-freq crystal, bootloader at 0x7E00, bootloader reset enabled (lfuse = 0x8F   hfuse = 0x96).

Quandary: developing code for ATmega32A; when using AVRDUDE to program FLASH using ISP with "DRAGON_ISP" programmer, AVRDUDE fails with:

avrdude: verification error, first mismatch at byte 0x7e00
0xff != 0x0f


Command line invokation is:

avrdude -u -p m32 -c dragon_isp -B 1MHz -U flash:w:AVR_Specific_Builds/m32/ATTOBASICV234_m32-16MHZ-uart_btldr.hex


Partial dump from original HEX file around address 0x7E00:
:107D00000000000000000000000000000000000073
:107D10000000000000000000000000000000000063
:107D20000000000000000000000000000000000053
:107D30000000000000000000000000000000000043
:107D40000000000000000000000000000000000033
:107D50000000000000000000000000000000000023
:107D60000000000000000000000000000000000013
:107D70000000000000000000000000000000000003
:107D8000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF03
:107D9000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF3
:107DA000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE3
:107DB000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFD3
:107DC000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC3
:107DD000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFB3
:107DE000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA3
:107DF000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF93
:107E00000F92CDB7DEB711248FE598E09EBF8DBFEE
:107E100084B714BE81FFD6D085E08EBD82E08BB9D9
:107E200088E18AB986E880BD80E189B98EE0B5D065
:107E3000BD9A26E080E39CEF54E040E29DBD8CBDFE
:107E400058BF08B602FEFDCF38B3342738BBA8951B
:107E50002150A1F788249924CC24C394F5E0DF2E87
:107E6000E1E1EE2E73E0F72E91D0813469F48ED0EB
:107E7000898398D08981823811F1813811F485E0A5
:107E800001C083E07FD07BC0823411F484E103C061
:107E9000853419F485E08ED072C0853561F476D0D2
:107EA000082F10E073D090E0982E8824802A912A21
:107EB000880C991C63C0863521F484E07BD080E077
:107EC000E1CF843609F03FC061D060D0B82E5ED0DB
:107ED00080E0881680E7980618F4F401F7BEE8956C
:107EE00000E610E053D0F80181938F01BA94D1F7E6
:107EF000F0E08F16F0E79F0618F0F401F7BEE89562

Addresses 0x7D00 to 0x7DFF is waveform data for DDS function.


After failure, partial dump of reback of FLASH memory around address 0x7E00.  Even the data starting at 0x7D00 is incorrect.
:107D0000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF83
:107D1000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF73
:107D2000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF63
:107D3000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF53
:107D4000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF43
:107D5000FFFF000000000000000000000000000025
:107D60000000000000000000000000000000000013
:107D70000000000000000000000000000000000003
:107D8000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF03
:107D9000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF3
:107DA000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE3
:107DB000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFD3
:107DC000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC3
:107DD000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFB3
:107DE000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA3
:107DF000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF93
:107E0000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF82
:107E1000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF72
:107E2000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF62
:107E3000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF52
:107E4000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF42
:107E5000FFFFA1F788249924CC24C394F5E0DF2EFA
:107E6000E1E1EE2E73E0F72E91D0813469F48ED0EB
:107E7000898398D08981823811F1813811F485E0A5
:107E800001C083E07FD07BC0823411F484E103C061
:107E9000853419F485E08ED072C0853561F476D0D2
:107EA000082F10E073D090E0982E8824802A912A21
:107EB000880C991C63C0863521F484E07BD080E077
:107EC000E1CF843609F03FC061D060D0B82E5ED0DB
:107ED00080E0881680E7980618F4F401F7BEE8956C
:107EE00000E610E053D0F80181938F01BA94D1F7E6
:107EF000F0E08F16F0E79F0618F0F401F7BEE89562


All other ATmega devices program properly using ISP and JTAG, without incident, so I do not believe it is the DRAGON.  The ATmega32A programs properly using JTAG with AVRDUDE and DRAGON, only ISP fails using AVRDUDE.  Using DRAGON (1MHz sck) under AVR STUDIO, ISP programming is successful.

I have tried ISP with USBASP and USBtinyISP as well and they function correctly.

I have tried with V6.3, V6.2 V6.0 of AVRDUDE.  V5.x versions fail to communicate with DRAGON due to "parallel port connection error" (?????DRAGON is USB!!)  I have looked at the avrdude.conf for other "compatible devices" and found ATmega328p and ATmega329 are accurate.  Using AVRDUDE "-F" switch produces the same verification failure with these two part definitions.

I have tried various "-B" settings for AVRDUDE to no avail.
I have tried using external 8MHz crystal to no avail.
I have tried using internal 8MHz oscillator to no avail.

I have four (4) NEW ATmega32A's, none are blown as I can successfully program all of them using JTAG, USBtinyISP and USBasp but using DRAGON_ISP, all fail in the same manner.

Chinese fakes?  I do not see how as these parts do not look like it. They have ATMEL logo and date code of "1508".

Is this an AVRDUDE issue, an ATmega32A issue or combination of both?

Does anyone have any answers, ideas or clues to offer?

Thank you in advance.

Peace and blessings,
Scott

file #37526: ATTOBASICV234_m32-16MHZ-uart_btldr.hex
file #37550: ATTOBASICV234_m16-16MHZ-uart_btldr.hex

This issue was migrated from https://savannah.nongnu.org/bugs/?48261

@avrs-admin
Copy link
Author

Scott Vitale
Wed 22 Jun 2016 06:53:34 PM UTC

Hello:
As a further test, I recently ordered and received two (2) ATmega16A's.  Using the same test conditions, but uploading a 16KB image to FLASH, I receive the same "verification error" as I did with the ATmega32A's.  Again, the failure is complained about at the bootloader's selected address.  The clock speed is 16MHz, external crystal.  Here is the command line invocation and subsequent error message.  0x3e00 is the selected start address of the bootloader via the fuse settings.

avrdude -u -p m16 -c dragon_isp -B 500kHz -U flash:w:ATTOBASICV234_m16-16MHZ-uart_btldr.hex

"avrdude: verifying ...
avrdude: verification error, first mismatch at byte 0x3e00
0xff != 0x0f
avrdude: verification error; content mismatch
"

I will upload the original file for this one as well.

Peace and blessings,
Johnny Quest

@avrs-admin
Copy link
Author

Scott Vitale
Sat 25 Jun 2016 04:35:40 PM UTC

Thought this might be helpful.

My AVR DRAGON: Hardware Revision: 0x107, Firmware Version: 0x610610

BTW:  The original HEX file was generated by AVRASM.EXE (from AS 4.19).

I also recently generated and tested with with two 32KB files. one filled with all 0x00's and the other with random values.  Both programmed properly on my setup.

Thinking that missing data between unused address ranges was a contributing factor,  I used srec_cat to fill in the missing address ranges of my original test file with 0x00's and again with 0xFF's to generate two separate full 32KB address files.  Once again, avrdude + DRAGON_ISP failed.

Discussion is here: http://www.avrfreaks.net/comment/1916896#comment-1916896

Also, This error was confirmed by some one else for me.  The specifics for his setup are AVRDUDE 6.1 with ATmega32 (non "A").

Discussion is here: http://www.avrfreaks.net/comment/1917241#comment-1917241

@mcuee
Copy link
Collaborator

mcuee commented May 30, 2022

Not so sure if this is still relevant or not. I have the AVR Dragon but not the M32 or M32A for testing. Interestingly the avrfreaks thread also says Dragon is flaky.

@MCUdude
Copy link
Collaborator

MCUdude commented May 30, 2022

I'll give it a try later today with an ATmega32.

The dragon is indeed very flakey. Some information here:
http://www.aplomb.nl/TechStuff/Dragon/Dragon.html

Three main reasons:

  1. The Dragon is USB-powered, and has a step-up converter on board to generate 12V for the HV-programming.
    However, when touching the area where this circuit located (down-left corener on the picture), causes the voltage to go up, and that's fatal. Dragon DEAD.
  2. Powering the Dragon from a USB-port that is not "prepared" to deliver the 850 mA rush-in current, causes the step-up coverter to die, because of the sudden drop of the USB-supply voltage.
  3. If the Dragon is connected to a faulty board (sh*t happens ...), it's interface isn't rugged enough to withstand that abuse.

Since we know this, .... time for some action.

  1. Solution is simple: don't touch the Dragon in that area. And how to accomplish that ? Give it a proper housing.
  2. Not all PC-USB-ports can deliver the rush-in current, and failing to do so means ..... ah, we know that now.
    Solution: Power the Dragon from an USB-hub with an external, beefy power-supply.
  3. Add a buffer that can withstand that abuse.

@MCUdude
Copy link
Collaborator

MCUdude commented May 30, 2022

I can confirm that there's something going on with the dragon.

Here are the ATmega32's fuse settings:

$ ./avrdude -p atmega32 -c usbasp -Ulfuse:r:-:h -Uhfuse:r:-:h

avrdude: AVR device initialized and ready to accept instructions

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

avrdude: Device signature = 0x1e9502 (probably m32)
avrdude: reading lfuse memory:

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

avrdude: writing output file "<stdout>"
0x8f
avrdude: reading hfuse memory:

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

avrdude: writing output file "<stdout>"
0x96

avrdude done.  Thank you.

Here is OPs hex file written with an AVR dragon (fails):

Dragon non-verbose output:

$ ./avrdude -p atmega32 -c dragon_isp -Uflash:w:/Users/hans/Downloads/ATTOBASICV234_m32-16MHZ-uart_btldr.hex

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.15s

avrdude: Device signature = 0x1e9502 (probably m32)
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 "/Users/hans/Downloads/ATTOBASICV234_m32-16MHZ-uart_btldr.hex"
avrdude: input file /Users/hans/Downloads/ATTOBASICV234_m32-16MHZ-uart_btldr.hex auto detected as Intel Hex
avrdude: writing flash (32768 bytes):

Writing | ################################################## | 100% 12.89s

avrdude: 32768 bytes of flash written
avrdude: verifying flash memory against /Users/hans/Downloads/ATTOBASICV234_m32-16MHZ-uart_btldr.hex:
avrdude: input file /Users/hans/Downloads/ATTOBASICV234_m32-16MHZ-uart_btldr.hex auto detected as Intel Hex

Reading | ################################################## | 100% 12.40s

avrdude: verification error, first mismatch at byte 0x7e00
         0xff != 0x0f
avrdude: verification error; content mismatch

avrdude done.  Thank you.

Dragon verbose output:

It turned out that the output was so long that it exceeded the maximum amount of character a single post can have.
You can find the complete output here: https://gist.github.com/MCUdude/bfb62a7c16316bb88778b92313749824


Here's OPs hex file written with a USBasp (succeeds):

$ ./avrdude -p atmega32 -c usbasp -Uflash:w:/Users/hans/Downloads/ATTOBASICV234_m32-16MHZ-uart_btldr.hex

avrdude: AVR device initialized and ready to accept instructions

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

avrdude: Device signature = 0x1e9502 (probably m32)
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 "/Users/hans/Downloads/ATTOBASICV234_m32-16MHZ-uart_btldr.hex"
avrdude: input file /Users/hans/Downloads/ATTOBASICV234_m32-16MHZ-uart_btldr.hex auto detected as Intel Hex
avrdude: writing flash (32768 bytes):

Writing | ################################################## | 100% 4.08s

avrdude: 32768 bytes of flash written
avrdude: verifying flash memory against /Users/hans/Downloads/ATTOBASICV234_m32-16MHZ-uart_btldr.hex:
avrdude: input file /Users/hans/Downloads/ATTOBASICV234_m32-16MHZ-uart_btldr.hex auto detected as Intel Hex

Reading | ################################################## | 100% 2.51s

avrdude: 32768 bytes of flash verified

avrdude done.  Thank you.

To my surprise, the AVRISPmkII also fails:

$ ./avrdude -p atmega32 -c avrispmkii -Uflash:w:/Users/hans/Downloads/ATTOBASICV234_m32-16MHZ-uart_btldr.hex

avrdude: AVR device initialized and ready to accept instructions

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

avrdude: Device signature = 0x1e9502 (probably m32)
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 "/Users/hans/Downloads/ATTOBASICV234_m32-16MHZ-uart_btldr.hex"
avrdude: input file /Users/hans/Downloads/ATTOBASICV234_m32-16MHZ-uart_btldr.hex auto detected as Intel Hex
avrdude: writing flash (32768 bytes):

Writing | ################################################## | 100% 18.16s

avrdude: 32768 bytes of flash written
avrdude: verifying flash memory against /Users/hans/Downloads/ATTOBASICV234_m32-16MHZ-uart_btldr.hex:
avrdude: input file /Users/hans/Downloads/ATTOBASICV234_m32-16MHZ-uart_btldr.hex auto detected as Intel Hex

Reading | ################################################## | 100% 17.64s

avrdude: verification error, first mismatch at byte 0x7e00
         0xff != 0x0f
avrdude: verification error; content mismatch

avrdude done.  Thank you.

@dl8dtl do you have any idea why the Dragon and the AVRISPmkII fail?

@MCUdude
Copy link
Collaborator

MCUdude commented May 30, 2022

Interesting!

If I remove the following part of the hex file, located between the user program and the bootloader section, it works with both the Dragon and the AVRISPmkII...

 :10385000666F756E642120000000FBFD61206269C7
 :103860006E617279206E756D62657200FB496E76CD
 :10387000616C6964206E756D626572206F6620618F
 :103880007267756D656E74732E000000FB204576BF
 :10389000656E2061646472657373206F6E6C79214C
 :0438A0002000189557
 :107D00000000000000000000000000000000000073
 :107D10000000000000000000000000000000000063
 :107D20000000000000000000000000000000000053
 :107D30000000000000000000000000000000000043
 :107D40000000000000000000000000000000000033
 :107D50000000000000000000000000000000000023
 :107D60000000000000000000000000000000000013
 :107D70000000000000000000000000000000000003
-:107D8000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF03
-:107D9000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF3
-:107DA000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE3
-:107DB000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFD3
-:107DC000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC3
-:107DD000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFB3
-:107DE000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA3
-:107DF000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF93
 :107E00000F92CDB7DEB711248FE598E09EBF8DBFEE
 :107E100084B714BE81FFD6D085E08EBD82E08BB9D9
 :107E200088E18AB986E880BD80E189B98EE0B5D065
 :107E3000BD9A26E080E39CEF54E040E29DBD8CBDFE
 :107E400058BF08B602FEFDCF38B3342738BBA8951B

@mcuee
Copy link
Collaborator

mcuee commented Jun 3, 2022

Interesting I got verfication error with my newly bough Arduino Mega2560 clone (with CH340) and initially I also got verification error with an hex file (exported from Arduino blinky example). And Arduino itself also has the verification error message after followed by stk500v2 timeout error.

But then I tried to program the official bootloader again and then try again with avrdude and Arduino, both are now okay. Maybe the clone board is flashed with bad bootloader which somehow interferes the operation of avrdude. It does not seem to have 0xFF in between the bootloader and the application though. SO the issue may be different.

Sorry I did not read out the original bootloader written so I can not upload that faulty bootloader.

Edit: my issue is not relevant. Sorry for the noise.

@MCUdude
Copy link
Collaborator

MCUdude commented Jun 8, 2022

@dl8dtl you're probably the one that knows the source code the best. How can one type of programmer (USBasp) work with OPs hex files, while others (AVRISPmkII, Dragon ISP) don't? Is this related to the programmer hardware/firmware, or does Avrdude treat these programmers differently in terms of what's "legal" to write?

@dl8dtl
Copy link
Contributor

dl8dtl commented Jun 8, 2022

Sounds to me like a bug in the stk500v2 code (which is used for Dragon, STK500, AVRISPmkII and so on).

@dl8dtl dl8dtl added the bug Something isn't working label Jun 8, 2022
@dl8dtl dl8dtl added this to the AVRDUDE 7.1 milestone Jun 8, 2022
@mcuee
Copy link
Collaborator

mcuee commented Jun 15, 2022

Not so sure if it is to do with the boot_start handling in stk500v2.c, but it is supposedly to be used only for xmega.

@mcuee
Copy link
Collaborator

mcuee commented Jun 16, 2022

I do not have the ATmega32A, not so sure if I can test with ATmega328P or not.

@mcuee
Copy link
Collaborator

mcuee commented Jul 11, 2022

I just got an ATmega32A board running MightyCore.

Tested with an AVRISP mkii clone, I can reproduce the issue.

PS C:\work\avr\avrdude_test\avrdude-7.0_bin64> .\avrdude -p atmega32a -c stk500v2 -P COM6 
-Ulfuse:r:-:h -Uhfuse:r:-:h

avrdude.exe: AVR device initialized and ready to accept instructions

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

avrdude.exe: Device signature = 0x1e9502 (probably m32a)
avrdude.exe: reading lfuse memory:

Reading | ################################################## | 100% 0.03s

avrdude.exe: writing output file "<stdout>"
0x8f
avrdude.exe: reading hfuse memory:

Reading | ################################################## | 100% 0.03s

avrdude.exe: writing output file "<stdout>"
0x96

avrdude.exe done.  Thank you.

PS C:\work\avr\avrdude_test\avrdude-7.0_bin64> .\avrdude -p atmega32a -c avrispmkii 
-U flash:w:ATTOBASICV234_m32-16MHZ-uart_btldr.hex:i

avrdude.exe: AVR device initialized and ready to accept instructions

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

avrdude.exe: Device signature = 0x1e9502 (probably m32a)
avrdude.exe: NOTE: "flash" memory has been specified, an erase cycle will be performed
             To disable this feature, specify the -D option.
avrdude.exe: erasing chip
avrdude.exe: reading input file "ATTOBASICV234_m32-16MHZ-uart_btldr.hex"
avrdude.exe: writing flash (32768 bytes):

Writing | ################################################## | 100% 3.45s

avrdude.exe: 32768 bytes of flash written
avrdude.exe: verifying flash memory against ATTOBASICV234_m32-16MHZ-uart_btldr.hex:

Reading | ################################################## | 100% 2.84s

avrdude.exe: verification error, first mismatch at byte 0x7e00
             0xff != 0x0f
avrdude.exe: verification error; content mismatch

avrdude.exe done.  Thank you.

However, I can not reproduce the issue with an STK500v2 clone. That is strange.

PS C:\work\avr\avrdude_test\avrdude-7.0_bin64> .\avrdude -p atmega32a -c stk500v2 -P COM6 
-U flash:w:ATTOBASICV234_m32-16MHZ-uart_btldr.hex:i

avrdude.exe: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.08s

avrdude.exe: Device signature = 0x1e9502 (probably m32a)
avrdude.exe: NOTE: "flash" memory has been specified, an erase cycle will be performed
             To disable this feature, specify the -D option.
avrdude.exe: erasing chip
avrdude.exe: reading input file "ATTOBASICV234_m32-16MHZ-uart_btldr.hex"
avrdude.exe: writing flash (32768 bytes):

Writing | ################################################## | 100% 4.66s

avrdude.exe: 32768 bytes of flash written
avrdude.exe: verifying flash memory against ATTOBASICV234_m32-16MHZ-uart_btldr.hex:

Reading | ################################################## | 100% 3.94s

avrdude.exe: 32768 bytes of flash verified

avrdude.exe done.  Thank you.

@mcuee
Copy link
Collaborator

mcuee commented Jul 11, 2022

Interesting!

If I remove the following part of the hex file, located between the user program and the bootloader section, it works with both the Dragon and the AVRISPmkII...

@MCUdude
And yes I can reproduce the same thing with my AVRISP mkii clone (this looks like a good clone which works under Atmel Studio 7).

PS C:\work\avr\avrdude_test\avrdude-7.0_bin64> .\avrdude -p atmega32a -c avrispmkii 
-U flash:w:ATTOBASICV234_m32-16MHZ-uart_btldr_ok.hex:i

avrdude.exe: AVR device initialized and ready to accept instructions

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

avrdude.exe: Device signature = 0x1e9502 (probably m32a)
avrdude.exe: NOTE: "flash" memory has been specified, an erase cycle will be performed
             To disable this feature, specify the -D option.
avrdude.exe: erasing chip
avrdude.exe: reading input file "ATTOBASICV234_m32-16MHZ-uart_btldr_ok.hex"
avrdude.exe: writing flash (32768 bytes):

Writing | ################################################## | 100% 3.41s

avrdude.exe: 32768 bytes of flash written
avrdude.exe: verifying flash memory against ATTOBASICV234_m32-16MHZ-uart_btldr_ok.hex:

Reading | ################################################## | 100% 2.84s

avrdude.exe: 32768 bytes of flash verified

avrdude.exe done.  Thank you.

@mcuee
Copy link
Collaborator

mcuee commented Jul 11, 2022

I can reproduce the issue with a more exact clone of STK500v2 (which uses ATmega8535 and support high voltage programming). It uses an old USB to serial chip and does not work well under Windows so I tested this under Linux (Ubuntu 20.04).

mcuee@UbuntuSwift3:~/build/avr$ avrdude -c stk500v2 -P /dev/ttyUSB1 -p atmega32a  
-Ulfuse:r:-:h -Uhfuse:r:-:h

avrdude: AVR device initialized and ready to accept instructions

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

avrdude: Device signature = 0x1e9502 (probably m32a)
avrdude: reading lfuse memory:

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

avrdude: writing output file "<stdout>"
0x8f
avrdude: reading hfuse memory:

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

avrdude: writing output file "<stdout>"
0x96

avrdude done.  Thank you.
        
mcuee@UbuntuSwift3:~/build/avr$ avrdude -c stk500v2 -P /dev/ttyUSB1 -p atmega32a  
-U flash:w:./ATTOBASICV234_m32-16MHZ-uart_btldr.hex:i

avrdude: AVR device initialized and ready to accept instructions

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

avrdude: Device signature = 0x1e9502 (probably m32a)
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 "./ATTOBASICV234_m32-16MHZ-uart_btldr.hex"
avrdude: writing flash (32768 bytes):

Writing | ################################################## | 100% 3.64s

avrdude: 32768 bytes of flash written
avrdude: verifying flash memory against ./ATTOBASICV234_m32-16MHZ-uart_btldr.hex:

Reading | ################################################## | 100% 1.78s

avrdude: verification error, first mismatch at byte 0x7e00
         0xff != 0x0f
avrdude: verification error; content mismatch

avrdude done.  Thank you.


@mcuee
Copy link
Collaborator

mcuee commented Jul 11, 2022

Then I have another STK500v2 clone using a 8051 CPU (STC STC15W1K16S) which also claims to support HVSP/HVPP, and it does not have this issue. Looks like this one and the previous STK500v2 clone may not follow exactly the official STK500 FW.

And then the AVeRISP mkii clone and the STK500v2 clone using ATmega8535 (which also claims to support HVSP/HVPP) seem to follow the official FW.

mcuee@UbuntuSwift3:~/build/avr$ avrdude -c stk500v2 -P /dev/ttyUSB1 -B 1 -p atmega32a  
-U flash:w:./ATTOBASICV234_m32-16MHZ-uart_btldr.hex:i

avrdude: AVR device initialized and ready to accept instructions

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

avrdude: Device signature = 0x1e9502 (probably m32a)
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 "./ATTOBASICV234_m32-16MHZ-uart_btldr.hex"
avrdude: writing flash (32768 bytes):

Writing | ################################################## | 100% 3.66s

avrdude: 32768 bytes of flash written
avrdude: verifying flash memory against ./ATTOBASICV234_m32-16MHZ-uart_btldr.hex:

Reading | ################################################## | 100% 2.90s

avrdude: 32768 bytes of flash verified

avrdude done.  Thank you.

@mcuee
Copy link
Collaborator

mcuee commented Jul 11, 2022

BTW, I can reproduce the issue with the default demo FW on my board.
atmega32a_demo_muzi.hex.txt

@mcuee
Copy link
Collaborator

mcuee commented Jul 28, 2022

@dl8dtl and @MCUdude
Since this is marked for AVRDUDE 7.1 milestone, how do we further diagnose the issue and then try to find a fix?

@mcuee
Copy link
Collaborator

mcuee commented Jul 31, 2022

Then I have another STK500v2 clone using a 8051 CPU (STC STC15W1K16S) which also claims to support HVSP/HVPP, and it does not have this issue. Looks like this one and the previous STK500v2 clone may not follow exactly the official STK500 FW.

And then the AVeRISP mkii clone and the STK500v2 clone using ATmega8535 (which also claims to support HVSP/HVPP) seem to follow the official FW.

So this somehow points for FW issues? Or there needs to be a workaround in avrdude?

@MCUdude
Copy link
Collaborator

MCUdude commented Aug 3, 2022

Maybe @stefanrueger is able to figure out what's wrong here?

@mcuee I beleve there is an issue with the Avrdude implementation, and not necessary the official Atmel programmers. I can confirm that the ATTOBASICV234_m32-16MHZ-uart_btldr.hex file can sucessfully be written to an ATmega32 using an Atmel AVRISPmkII programmer though Microchip Studio 7 or using their CLI tool, atprogram.exe.

@stefanrueger
Copy link
Collaborator

stefanrueger commented Aug 4, 2022

It's a bootloader section write issue, so I wonder whether it's worthwhile playing with the lock bytes and the fuses: change the fuses for a larger, say 1024 bytes, boot section and see whether the verification error happens at 31 KiB rather than 31.5 KiB; that requires a non-0xff input test file up to the brim (maybe a test file with 16 K nop; or for variety, maybe some different effective nops such as mov rx,rx or even mov rx,ry to generate non-trivial bit patterns; just avoid a random file that risks port I/O). I know the BL lock bits normally are about allowing LPM/SPM in the selected bootloader area, but who knows whether that's the same for the ATmega32A? So, I'd ensure lock bits are all-permissive (though this could just be unnecessary superstition).

I also had a quick look at the write delays:

$ avrdude -p m32a/w
.wd_chip_erase 9.000 ms ATmega32A
.wd_eeprom 9.000 ms ATmega32A
.wd_flash 4.500 ms ATmega32A
.wd_lfuse 2.000 ms ATmega32A
.wd_hfuse 2.000 ms ATmega32A
.wd_lock 2.000 ms ATmega32A

That kinda looks suspicious. Normally, wd for fuses is the same as for flash.

Are the lock/fuse r/w commands in order? Perhaps check with avrdude -p m32a/s against data sheet. If that fails, I'd look at the write lock/fuse routines in stk500v2 (they may well not use the avrdude.conf values, so a careful check of the code might be in order).

If stk500v2.c reasons about where the bootloader sits that might well be a good place to check (I think @mcuee mentioned that already, that's a great shout).

I realise I am a bit vague here, but I neither have the programmers in question nor the part.

@MCUdude
Copy link
Collaborator

MCUdude commented Aug 4, 2022

It's a bootloader section write issue, so I wonder whether it's worthwhile playing with the lock bytes and the fuses: change the fuses for a larger, say 1024 bytes, boot section and see whether the verification error happens at 31 KiB rather than 31.5 KiB

For reference, here is the same file written but the bootloader section size is set to 1024 bytes instead of 512:

$ ./avrdude -patmega32 -cavrispmkii -Ulfuse:w:0xbf:m -Uhfuse:w:0xc4:m -Uflash:w:/Users/hans/Downloads/ATTOBASICV234_m32-16MHZ-uart_btldr.hex

avrdude: AVR device initialized and ready to accept instructions

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

avrdude: Device signature = 0x1e9502 (probably m32)
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 0xbf for lfuse
avrdude: writing 1 byte lfuse ...

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

avrdude: 1 byte of lfuse written
avrdude: verifying lfuse memory against 0xbf

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

avrdude: 1 byte of lfuse verified
avrdude: reading input file 0xc4 for hfuse
avrdude: writing 1 byte hfuse ...

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

avrdude: 1 byte of hfuse written
avrdude: verifying hfuse memory against 0xc4

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

avrdude: 1 byte of hfuse verified
avrdude: input file /Users/hans/Downloads/ATTOBASICV234_m32-16MHZ-uart_btldr.hex auto detected as Intel Hex
avrdude: reading input file /Users/hans/Downloads/ATTOBASICV234_m32-16MHZ-uart_btldr.hex for flash
         with 15206 bytes in 4 sections within [0, 0x7fff]
         using 120 pages and 154 pad bytes
avrdude: writing 15206 bytes flash ...

Writing | ################################################## | 100% 1.31s

avrdude: 15206 bytes of flash written
avrdude: verifying flash memory against /Users/hans/Downloads/ATTOBASICV234_m32-16MHZ-uart_btldr.hex

Reading | ################################################## | 100% 0.82s

avrdude: verification error, first mismatch at byte 0x7e00
         0xff != 0x0f
avrdude: verification error; content mismatch

avrdude done.  Thank you.

I also generated a 16kiB intel hex file with all zeros, and a 32kiB as well. Both of these files can be verified just fine...

$ srec_cat -generate 0x0000 0x3FFF -repeat_data 0x00 -o zeros16kib.hex -Intel
$ srec_cat -generate 0x0000 0x7FFF -repeat_data 0x00 -o zeros32kib.hex -Intel
$ ./avrdude -patmega32 -cavrispmkii -Ulfuse:w:0xbf:m -Uhfuse:w:0xc6:m

avrdude: AVR device initialized and ready to accept instructions

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

avrdude: Device signature = 0x1e9502 (probably m32)
avrdude: reading input file 0xbf for lfuse
avrdude: writing 1 byte lfuse ...

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

avrdude: 1 byte of lfuse written
avrdude: verifying lfuse memory against 0xbf

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

avrdude: 1 byte of lfuse verified
avrdude: reading input file 0xc6 for hfuse
avrdude: writing 1 byte hfuse ...

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

avrdude: 1 byte of hfuse written
avrdude: verifying hfuse memory against 0xc6

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

avrdude: 1 byte of hfuse verified

avrdude done.  Thank you.



$ ./avrdude -patmega32 -cavrispmkii -Uflash:w:zeros16kib.hex 

avrdude: AVR device initialized and ready to accept instructions

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

avrdude: Device signature = 0x1e9502 (probably m32)
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: input file zeros16kib.hex auto detected as Intel Hex
avrdude: reading input file zeros16kib.hex for flash
avrdude: writing 16383 bytes flash ...

Writing | ################################################## | 100% 1.49s

avrdude: 16383 bytes of flash written
avrdude: verifying flash memory against zeros16kib.hex

Reading | ################################################## | 100% 0.94s

avrdude: 16383 bytes of flash verified

avrdude done.  Thank you.



$ ./avrdude -patmega32 -cavrispmkii -Uflash:w:zeros32kib.hex 

avrdude: AVR device initialized and ready to accept instructions

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

avrdude: Device signature = 0x1e9502 (probably m32)
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: input file zeros32kib.hex auto detected as Intel Hex
avrdude: reading input file zeros32kib.hex for flash
avrdude: writing 32767 bytes flash ...

Writing | ################################################## | 100% 2.93s

avrdude: 32767 bytes of flash written
avrdude: verifying flash memory against zeros32kib.hex

Reading | ################################################## | 100% 1.85s

avrdude: 32767 bytes of flash verified

avrdude done.  Thank you.

That kinda looks suspicious. Normally, wd for fuses is the same as for flash.

This is the table from the ATmega32 datasheet. Chaning the timing in avrdude.conf to match the values specified in the datasheet didn't make any difference.
image

@stefanrueger
Copy link
Collaborator

Looks like a really hard nut. @MCUdude's test put paid to my theory about the boot section. Certain input files can be written certain input files cannot be written. The input file that does not work has 4 sections. Then @mcuee has found an input file with one section that also does not work. The OP seems to mention m328p can be written, the m32a cannot.

There were clear-cut errors of the m32 entry in avrdude.conf, but correcting these made no difference. I checked stk500v2.c and indeed these wd values are not used (only wd_chip_erase). Instead there are a couple of usleep(10000) with comments indicating the author thought that a hack (so hacking these up any further might make a change but that's not a satisfying solution even if it worked). Could it be that other vital m32 avrdude.conf entries used in stk500v2.c are wrong? I have no idea where to look up the correct values needed, and which ones are actually used, so I am out of my depth here. But I think giving the avrdude.conf entry a long hard check will at least have the side-effect of getting that correct and removing this as potential error source.

Also, what is the flash readout with :I of the correctly written flash and what's the one of the incorrectly written flash? The OP seemed to say errors started earlier at 0x7d00 (which might be in a "hole" that's not part of the verification), and could be where the write attempt for 0x7e00 has diffracted to. A diff of the two read-backs might give some insight?

@mcuee
Copy link
Collaborator

mcuee commented Aug 5, 2022

Indeed @MCUdude's testing results make this issue more difficult.

@stefanrueger
Two guess from my side -- maybe not relevant at all but just my guess from going through all the issues in recent months.

  1. Can it be similar to the strange issue of [bug #51320] linuxgpio and linuxspi problem: 0x00 written as 0xFF when byte count is less than 0x10 in hex file lines #455? Apparent the proposed fix does not seem to be right there but it did fix the issue.

  2. Could it be related to -U writes sometimes more bytes to flash/memory than input file has #1050?

@mcuee
Copy link
Collaborator

mcuee commented Aug 5, 2022

I can repeat the problem as well. Writing has the problem, reading is good. Interestingly only the location from 0x7E00 has the issues (26 bytes).

C:\work\avr\avrdude_test\avrdude_bin> .\avrdude -qqp m32a -c avrispmkii 
-U lfuse:w:0xbf:m -U hfuse:w:0xc6:m && echo OK
OK

C:\work\avr\avrdude_test\avrdude_bin> .\avrdude -qqp m32a -c avrispmkii 
-U .\atmega32a_demo_muzi.hex && echo OK
avrdude.exe: verification error, first mismatch at byte 0x7e00
             0xff != 0x0f
avrdude.exe: verification error; content mismatch

C:\work\avr\avrdude_test\avrdude_bin> .\avrdude -qqp m32a -c avrispmkii 
-U flash:r:m32a_muzi_avrisp2_readback.hex:i && echo OK
OK

C:\work\avr\avrdude_test\avrdude_bin> .\avrdude -qqp m32a -c usbasp 
-U flash:v:m32a_muzi_avrisp2_readback.hex:i && echo OK
OK

Original hex file.

:207D0000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF83
:207D2000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF63
:207D4000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF43
:207D6000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF23
:207D8000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF03
:207DA000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE3
:207DC000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC3
:207DE000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA3
:207E00000F92CDB7DEB711248FE598E09EBF8DBF84B714BE982F9D7009F0D2D085E08EBDB2
:207E200082E08BB988E18AB986E880BD80E189B98EE0B2D0B89A24E080E39CEF54E041E019
:207E40009DBD8CBD58BF08B602FEFDCF38B3342738BBA8952150A1F700E010E053E0F52E39
:207E6000EE24E39465E0D62E71E1C72E8ED0813471F48BD0898394D08981823809F477C0AE
:207E8000813811F486E001C083E07BD077C0823411F484E103C0853419F485E089D06EC083
:207EA000853569F472D0A82EBB246FD0082F10E0102F00270A291B29000F111F5EC0863559
:207EC00021F484E075D080E0E0CF843609F039C05CD05BD0A82E59D0982EBA2C20E6622E91
:207EE000712C53D0F30181933F01BA94D1F758D0F5E49F1609F4FFCFF801F7BEE89507B6FB
:207F000000FCFDCFF8018A2DA0E6B0E04C9150E011962C91119730E0322F2227242B352B51
:207F200012960901E7BEE89511243296825071F7F801D7BEE89507B600FCFDCFC7BEE895A4
:207F40001DC0843769F421D020D0B82E1ED028D03801F30185913F0114D0BA94D1F70EC034
:207F6000853739F41DD08EE10CD085E90AD082E08CCF813511F488E00FD012D080E101D0C5
:207F800075CF5D9BFECF8CB908955F9BFECF5C9901C0A8958CB1089598E191BD81BD0895C0
:207FA000F4DF803219F088E0F7DFFFCF84E1E9CFCF93C82FEADFC150E9F7F2DFCF91089529
:207FC000282E80E0E9DFE0E0FF270994FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFB4
:207FE000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF020697
:00000001FF

What has been written:

:207D0000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF83
:207D2000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF63
:207D4000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF43
:207D6000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF23
:207D8000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF03
:207DA000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE3
:207DC000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC3
:207DE000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA3
:207E0000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFD2D085E08EBD2A
:207E200082E08BB988E18AB986E880BD80E189B98EE0B2D0B89A24E080E39CEF54E041E019
:207E40009DBD8CBD58BF08B602FEFDCF38B3342738BBA8952150A1F700E010E053E0F52E39
:207E6000EE24E39465E0D62E71E1C72E8ED0813471F48BD0898394D08981823809F477C0AE
:207E8000813811F486E001C083E07BD077C0823411F484E103C0853419F485E089D06EC083
:207EA000853569F472D0A82EBB246FD0082F10E0102F00270A291B29000F111F5EC0863559
:207EC00021F484E075D080E0E0CF843609F039C05CD05BD0A82E59D0982EBA2C20E6622E91
:207EE000712C53D0F30181933F01BA94D1F758D0F5E49F1609F4FFCFF801F7BEE89507B6FB
:207F000000FCFDCFF8018A2DA0E6B0E04C9150E011962C91119730E0322F2227242B352B51
:207F200012960901E7BEE89511243296825071F7F801D7BEE89507B600FCFDCFC7BEE895A4
:207F40001DC0843769F421D020D0B82E1ED028D03801F30185913F0114D0BA94D1F70EC034
:207F6000853739F41DD08EE10CD085E90AD082E08CCF813511F488E00FD012D080E101D0C5
:207F800075CF5D9BFECF8CB908955F9BFECF5C9901C0A8958CB1089598E191BD81BD0895C0
:207FA000F4DF803219F088E0F7DFFFCF84E1E9CFCF93C82FEADFC150E9F7F2DFCF91089529
:207FC000282E80E0E9DFE0E0FF270994FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFB4
:207FE000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF020697
:00000001FF

@mcuee
Copy link
Collaborator

mcuee commented Aug 5, 2022

And as per @MCUdude, I need to remove all the empty lines before it will work. Even single empty lines will cause problems.

C:\work\avr\avrdude_test\avrdude_bin> .\avrdude -qqp m32a -c avrispmkii 
-U .\atmega32a_demo_muzi_mod.hex && echo OK
avrdude.exe: verification error, first mismatch at byte 0x7e00
             0xff != 0x0f
avrdude.exe: verification error; content mismatch

C:\work\avr\avrdude_test\avrdude_bin> .\avrdude -qqp m32a -c avrispmkii 
-U .\atmega32a_demo_muzi_mod1.hex && echo OK
OK

$ diff atmega32a_demo_muzi_mod.hex atmega32a_demo_muzi_mod1.hex
37d36
< :20048000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7C

atmega32a_demo_muzi_mod.hex.txt
atmega32a_demo_muzi_mod1.hex.txt

@mcuee
Copy link
Collaborator

mcuee commented Aug 5, 2022

Actually I can reproduce the issue with very simple hex file like the following. 26bytes will be written wrongly as 0xFF.

:020000040000FA
:20000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00
:20002000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE0
:20008000000000000000000000000000000000000000000000000000000000000000000060
:2000A000000000000000000000000000000000000000000000000000000000000000000040
:2000C000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF40
:2000E000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF20
:20010000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
:00000001FF

C:\work\avr\avrdude_test\avrdude_bin> .\avrdude -qqp m32a -c avrispmkii 
-U .\simpletest1.hex && echo OK
avrdude.exe: verification error, first mismatch at byte 0x0080
             0xff != 0x00
avrdude.exe: verification error; content mismatch

C:\work\avr\avrdude_test\avrdude_bin> .\avrdude -qqp m32a -c avrispmkii -t
avrdude> read flash 0x80 0x40
>>> read flash 0x80 0x40
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 00 00 00 00 00 00  |................|
00a0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00b0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|

@MCUdude
Copy link
Collaborator

MCUdude commented Aug 5, 2022

Actually I can reproduce the issue with very simple hex file like the following. 26bytes will be written wrongly as 0xFF.

The hex file you fail to be written to an ATmega8, ATmega16, or an ATmega32, but I can confirm that I can write it to an ATmega324P, ATmega328P, ATmega64, ATmega128, and ATmega1281.

ATmega32:

$ cat test-error.hex 
:020000040000FA
:20000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00
:20002000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE0
:20008000000000000000000000000000000000000000000000000000000000000000000060
:2000A000000000000000000000000000000000000000000000000000000000000000000040
:2000C000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF40
:2000E000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF20
:20010000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
:00000001FF
$ ./avrdude -patmega32 -cavrispmkii -Uflash:w:test-error.hex:i

avrdude: AVR device initialized and ready to accept instructions

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

avrdude: Device signature = 0x1e9502 (probably m32)
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 test-error.hex for flash
         with 128 bytes in 2 sections within [0, 0x11f]
         using 2 pages and 128 pad bytes, cutting off 96 trailing 0xff bytes
avrdude: writing 128 bytes flash ...

Writing | #########################                          | 50% 0.00savrdude: stk500v2_command(): warning: Command timed out
avrdude: stk500v2_paged_write: write command failed
Writing | ################################################## | 100% 0.02s

avrdude: 128 bytes of flash written
avrdude: verifying flash memory against test-error.hex

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

avrdude: 224 bytes of flash verified

avrdude done.  Thank you.


$ echo "read flash 0 0x100" | ./avrdude -patmega32 -cavrispmkii -t

avrdude: AVR device initialized and ready to accept instructions

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

avrdude: Device signature = 0x1e9502 (probably m32)
>>> read flash 0 0x100 

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

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 done.  Thank you.

ATmega8:

$ ./avrdude -patmega8 -cavrispmkii -Uflash:w:test-error.hex:i

avrdude: AVR device initialized and ready to accept instructions

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

avrdude: Device signature = 0x1e9307 (probably m8)
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 test-error.hex for flash
         with 128 bytes in 2 sections within [0, 0x11f]
         using 2 pages and 0 pad bytes, cutting off 96 trailing 0xff bytes
avrdude: writing 128 bytes flash ...

Writing | ################################################## | 100% 0.00s

avrdude: stk500v2_command(): warning: Command timed out
avrdude: stk500v2_paged_write: write command failed
avrdude: 128 bytes of flash written
avrdude: verifying flash memory against test-error.hex

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

avrdude: 224 bytes of flash verified

avrdude done.  Thank you.

ATmega16:

$ ./avrdude -patmega16 -cavrispmkii -Uflash:w:test-error.hex:i

avrdude: AVR device initialized and ready to accept instructions

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

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 test-error.hex for flash
         with 128 bytes in 2 sections within [0, 0x11f]
         using 2 pages and 128 pad bytes, cutting off 96 trailing 0xff bytes
avrdude: writing 128 bytes flash ...

Writing | #########################                          | 50% 0.00savrdude: stk500v2_command(): warning: Command timed out
avrdude: stk500v2_paged_write: write command failed
Writing | ################################################## | 100% 0.02s

avrdude: 128 bytes of flash written
avrdude: verifying flash memory against test-error.hex

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

avrdude: 224 bytes of flash verified

avrdude done.  Thank you.

ATmega64:

$ ./avrdude -patmega64 -cavrispmkii -Uflash:w:test-error.hex:i

avrdude: AVR device initialized and ready to accept instructions

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

avrdude: Device signature = 0x1e9602 (probably m64)
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 test-error.hex for flash
         with 128 bytes in 2 sections within [0, 0x11f]
         using 1 page and 128 pad bytes, cutting off 96 trailing 0xff bytes
avrdude: writing 128 bytes flash ...

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

avrdude: 128 bytes of flash written
avrdude: verifying flash memory against test-error.hex

Reading | ################################################## | 100% 0.03s

avrdude: 224 bytes of flash verified

avrdude done.  Thank you.

ATmega128:

$ ./avrdude -patmega128 -cavrispmkii -Uflash:w:test-error.hex:i

avrdude: AVR device initialized and ready to accept instructions

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

avrdude: Device signature = 0x1e9702 (probably m128)
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 test-error.hex for flash
         with 128 bytes in 2 sections within [0, 0x11f]
         using 1 page and 128 pad bytes, cutting off 96 trailing 0xff bytes
avrdude: writing 128 bytes flash ...

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

avrdude: 128 bytes of flash written
avrdude: verifying flash memory against test-error.hex

Reading | ################################################## | 100% 0.03s

avrdude: 224 bytes of flash verified

avrdude done.  Thank you.

ATmega1281:

$ ./avrdude -patmega1281 -cavrispmkii -Uflash:w:test-error.hex:i

avrdude: AVR device initialized and ready to accept instructions

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

avrdude: Device signature = 0x1e9704 (probably m1281)
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 test-error.hex for flash
         with 128 bytes in 2 sections within [0, 0x11f]
         using 1 page and 128 pad bytes, cutting off 96 trailing 0xff bytes
avrdude: writing 128 bytes flash ...

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

avrdude: 128 bytes of flash written
avrdude: verifying flash memory against test-error.hex

Reading | ################################################## | 100% 0.03s

avrdude: 224 bytes of flash verified

avrdude done.  Thank you.

For reference, I'm getting the same error when writing the ATTOBASICV234_m16-16MHZ-uart_btldr.hex file regardless of what the fuses are set to when writing to an ATmega32.

I'm not getting the error when writing the ATTOBASICV234_m16-16MHZ-uart_btldr.hex file to an ATmega324P or an ATmega328P. However, I do get an error when trying to write the /Users/hans/Downloads/ATTOBASICV234_m16-16MHZ-uart_btldr.hex file to an ATmega16, regardless of the fuse settings:

$ ./avrdude -patmega16 -cavrispmkii -Uflash:w:/Users/hans/Downloads/ATTOBASICV234_m16-16MHZ-uart_btldr.hex

avrdude: AVR device initialized and ready to accept instructions

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

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: input file /Users/hans/Downloads/ATTOBASICV234_m16-16MHZ-uart_btldr.hex auto detected as Intel Hex
avrdude: reading input file /Users/hans/Downloads/ATTOBASICV234_m16-16MHZ-uart_btldr.hex for flash
         with 15204 bytes in 4 sections within [0, 0x3fff]
         using 120 pages and 156 pad bytes
avrdude: writing 15204 bytes flash ...

Writing | ################################################## | 100% 1.28s

avrdude: 15204 bytes of flash written
avrdude: verifying flash memory against /Users/hans/Downloads/ATTOBASICV234_m16-16MHZ-uart_btldr.hex

Reading | ################################################## | 100% 0.82s

avrdude: verification error, first mismatch at byte 0x3e00
         0xff != 0x0f
avrdude: verification error; content mismatch

avrdude done.  Thank you.

@MCUdude
Copy link
Collaborator

MCUdude commented Aug 5, 2022

Just to complete the test:

ATmega8535 (fails):

$ ./avrdude -patmega8535 -cavrispmkii -Uflash:w:test-error.hex:i

avrdude: AVR device initialized and ready to accept instructions

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

avrdude: Device signature = 0x1e9308 (probably m8535)
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 test-error.hex for flash
         with 128 bytes in 2 sections within [0, 0x11f]
         using 2 pages and 0 pad bytes, cutting off 96 trailing 0xff bytes
avrdude: writing 128 bytes flash ...

Writing | ################################################## | 100% 0.00s

avrdude: stk500v2_command(): warning: Command timed out
avrdude: stk500v2_paged_write: write command failed
avrdude: 128 bytes of flash written
avrdude: verifying flash memory against test-error.hex

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

avrdude: 224 bytes of flash verified

avrdude done.  Thank you.

ATmega8515 (fails):

$ ./avrdude -patmega8515 -cavrispmkii -Uflash:w:test-error.hex:i

avrdude: AVR device initialized and ready to accept instructions

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

avrdude: Device signature = 0x1e9306 (probably m8515)
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 test-error.hex for flash
         with 128 bytes in 2 sections within [0, 0x11f]
         using 2 pages and 0 pad bytes, cutting off 96 trailing 0xff bytes
avrdude: writing 128 bytes flash ...

Writing | ################################################## | 100% 0.00s

avrdude: stk500v2_command(): warning: Command timed out
avrdude: stk500v2_paged_write: write command failed
avrdude: 128 bytes of flash written
avrdude: verifying flash memory against test-error.hex

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

avrdude: 224 bytes of flash verified

avrdude done.  Thank you.

ATmega162 (succeeds):

$ ./avrdude -patmega162 -cavrispmkii -Uflash:w:test-error.hex:i

avrdude: AVR device initialized and ready to accept instructions

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

avrdude: Device signature = 0x1e9404 (probably m162)
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 test-error.hex for flash
         with 128 bytes in 2 sections within [0, 0x11f]
         using 2 pages and 128 pad bytes, cutting off 96 trailing 0xff bytes
avrdude: writing 128 bytes flash ...

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

avrdude: 128 bytes of flash written
avrdude: verifying flash memory against test-error.hex

Reading | ################################################## | 100% 0.03s

avrdude: 224 bytes of flash verified

avrdude done.  Thank you.

@mcuee
Copy link
Collaborator

mcuee commented Nov 14, 2022

@MCUdude
BTW, I found the srec_cat hex file you generated has one byte less.

srec_cat -generate 0x0000 0x7FFF -repeat_data 0x00 -o zeros32kib.hex -Intel

should be changed to

srec_cat -generate 0x0000 0x8000 -repeat_data 0x00 -o zeros32kB.hex -Intel

@mcuee mcuee changed the title [bug #48261] ATmega32A, AVR DRAGON ISP programming, verification error [bug #48261] STK500V2 ISP problem with ATmega8/8515/8535, ATmega16 and ATmega32 Nov 28, 2022
@mcuee
Copy link
Collaborator

mcuee commented Nov 28, 2022

@dl8dtl and @MCUdude

Looks like this one is tough to tackle. Shall we remove it from the 7.1 milestone? It is not really that bad since the hex files generated by tools will usually not trigger this bug.

@MCUdude
Copy link
Collaborator

MCUdude commented Nov 28, 2022

Before we drop it from the 7.1 release, I'd like to play around with the stk500v2.c file. After all, the mega commit (d742827) didn't change that much in stk500v2.c anyways. I can't promise anything, but it's worth a shot.

@mcuee, can you test if this applies to STK500 HVPP programming as well? STK500V2 uses a different writing routine than the regular ISP. I'm currently at work, so I can't test this at the moment.

Another thing. can you check if verification works as it should? In stk500v2.c, both stk500v2_paged_write() and stk500v2_paged_load() could be affected by this bug, but I'm not sure if's writing the hex file or reading/verifying that fails.
This can be tested by writing a "difficult" hex file using a different programmer (USBasp, USBtiny, etc) and verifying it against the hex file using an stk500v2 compatible programmer like the Dragon or AVRISPmkII.

@mcuee
Copy link
Collaborator

mcuee commented Nov 28, 2022

@MCUdude

I upgraded my STK500 ATmega8535 Clone with V2 FW from V1 FW using usbasp.

It seems to me -c stk500pp has no issues.

PS C:\work\avr\avrdude_test\avrdude_bin> cat .\simpletest1.hex
:020000040000FA
:20000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00
:20002000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE0
:20008000000000000000000000000000000000000000000000000000000000000000000060
:2000A000000000000000000000000000000000000000000000000000000000000000000040
:2000C000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF40
:2000E000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF20
:20010000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
:00000001FF
PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude -c stk500pp -P COM3 -p m32a -U .\simpletest1.hex

avrdude: AVR device initialized and ready to accept instructions
avrdude: device signature = 0x1e9502 (probably m32a)
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 .\simpletest1.hex for flash
         with 128 bytes in 2 sections within [0, 0x11f]
         using 2 pages and 128 pad bytes, cutting off 96 trailing 0xff bytes
avrdude: writing 128 bytes flash ...

Writing | ################################################## | 100% 0.06 s

avrdude: 128 bytes of flash written
avrdude: verifying flash memory against .\simpletest1.hex

Reading | ################################################## | 100% 0.05 s

avrdude: 224 bytes of flash verified

avrdude done.  Thank you.

-c stk500v2 has the issue.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude -c stk500v2 -P COM3 -p m32a -U .\simpletest1.hex

avrdude: AVR device initialized and ready to accept instructions
avrdude: device signature = 0x1e9502 (probably m32a)
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 .\simpletest1.hex for flash
         with 128 bytes in 2 sections within [0, 0x11f]
         using 2 pages and 128 pad bytes, cutting off 96 trailing 0xff bytes
avrdude: writing 128 bytes flash ...

Writing | ################################################## | 100% 0.29 s

avrdude: 128 bytes of flash written
avrdude: verifying flash memory against .\simpletest1.hex

Reading | ################################################## | 100% 0.51 s

avrdude warning: verification mismatch
        device 0xff != input 0x00 at addr 0x0080 (error)
avrdude error: verification mismatch

avrdude done.  Thank you.

I also tested the orginal reported hex file and the conclusion is the same. -c stk500pp is okay but not -c stk500v2. BTW, HVPP is really very fast.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude -c stk500v2 -P COM3 -p m32a
 -U .\hex2\ATTOBASICV234_m32-16MHZ-uart_btldr.hex

avrdude: AVR device initialized and ready to accept instructions
avrdude: device signature = 0x1e9502 (probably m32a)
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 .\hex2\ATTOBASICV234_m32-16MHZ-uart_btldr.hex for flash
         with 15206 bytes in 4 sections within [0, 0x7fff]
         using 120 pages and 154 pad bytes
avrdude: writing 15206 bytes flash ...

Writing | ################################################## | 100% 21.73 s

avrdude: 15206 bytes of flash written
avrdude: verifying flash memory against .\hex2\ATTOBASICV234_m32-16MHZ-uart_btldr.hex

Reading | ################################################## | 100% 19.35 s

avrdude warning: verification mismatch
        device 0xff != input 0x0f at addr 0x7e00 (error)
avrdude error: verification mismatch

avrdude done.  Thank you.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude -c stk500pp -P COM3 -p m32a 
-U .\hex2\ATTOBASICV234_m32-16MHZ-uart_btldr.hex

avrdude: AVR device initialized and ready to accept instructions
avrdude: device signature = 0x1e9502 (probably m32a)
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 .\hex2\ATTOBASICV234_m32-16MHZ-uart_btldr.hex for flash
         with 15206 bytes in 4 sections within [0, 0x7fff]
         using 120 pages and 154 pad bytes
avrdude: writing 15206 bytes flash ...

Writing | ################################################## | 100% 2.99 s

avrdude: 15206 bytes of flash written
avrdude: verifying flash memory against .\hex2\ATTOBASICV234_m32-16MHZ-uart_btldr.hex

Reading | ################################################## | 100% 1.97 s

avrdude: 15206 bytes of flash verified

avrdude done.  Thank you.

@mcuee
Copy link
Collaborator

mcuee commented Nov 28, 2022

Another thing. can you check if verification works as it should? In stk500v2.c, both stk500v2_paged_write() and stk500v2_paged_load() could be affected by this bug, but I'm not sure if's writing the hex file or reading/verifying that fails.
This can be tested by writing a "difficult" hex file using a different programmer (USBasp, USBtiny, etc) and verifying it against the hex file using an stk500v2 compatible programmer like the Dragon or AVRISPmkII.

I believe writing is the issue and not reading, based on my previous testing.

I just created a test hex file (big size but similar to my simple test hex file) and write the flashing using usbasp. -c stk500v2 is okay to verify the hex file.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude -c usbasp -p m32a -U .\m32a_test_issue425.hex

avrdude: AVR device initialized and ready to accept instructions
avrdude: device signature = 0x1e9502 (probably m32a)
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 .\m32a_test_issue425.hex for flash
         with 32256 bytes in 1 section within [0, 0x7dff]
         using 252 pages and 0 pad bytes
avrdude: writing 32256 bytes flash ...

Writing | ################################################## | 100% 8.08 s

avrdude: 32256 bytes of flash written
avrdude: verifying flash memory against .\m32a_test_issue425.hex

Reading | ################################################## | 100% 5.88 s

avrdude: 32256 bytes of flash verified

avrdude done.  Thank you.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude -c stk500v2 -P COM3 -p m32a -U flash:v:.\m32a_test_issue425.hex:i

avrdude: AVR device initialized and ready to accept instructions
avrdude: device signature = 0x1e9502 (probably m32a)
avrdude: verifying flash memory against .\m32a_test_issue425.hex

Reading | ################################################## | 100% 41.96 s

avrdude: 32256 bytes of flash verified

avrdude done.  Thank you.

m32a_test_issue425.zip

@mcuee
Copy link
Collaborator

mcuee commented Nov 28, 2022

Before we drop it from the 7.1 release, I'd like to play around with the stk500v2.c file. After all, the mega commit (d742827) didn't change that much in stk500v2.c anyways. I can't promise anything, but it's worth a shot.

@MCUdude and @dl8dtl

Hopefully the above test results give you some clues to fix the issue.

  1. -c stk500pp is okay but not -c stk500v2
  2. reading/varification are okay and only writing is the issue.

@stefanrueger
Copy link
Collaborator

The commit in question introduced memory tags to describe holes input data. Could it be that the physical programmer FW somehow autoincrements for those parts in a different way than stk500v2? So any hole in the data (a page_size worth of 0xFF) could throw a spanner in the works.

This hole hypothesis can be tested as follows: modify stk500v2.c to always (unconditionally) use stk500v2_loadaddr() for every page written/read irrespective of whether autoincrement might render that unnecessary.

If that fixes the problem, then the question remains why for those parts only (as m328p works?). Maybe it is one of the myriad STK500v2 parameters that are different from m328p to m32

avrdude -pm32/S >/tmp/m32; avrdude -pm328p/S >/tmp/m328p; diff /tmp/m32{,8p}

Always good to double-check avrdude.conf entries.

BTW, when I started my career as avrdude contributor I fixed the load extended address code for vvv large parts (think m2560) for a number of -c programmers. Glancing over stk500v2.c that looks wrong, too! It has the same tell-tale code that won't work with files with holes

avrdude/src/stk500v2.c

Lines 2456 to 2462 in 2f2a6c0

// Ensure a new "load extended address" will be issued
// when crossing a 64 KB boundary in flash.
if (hiaddr != (addr & ~0xFFFF)) {
hiaddr = addr & ~0xFFFF;
if (stk500v2_loadaddr(pgm, use_ext_addr | (addr >> addrshift)) < 0)
return -1;
}

This is an independent problem, but still a problem. No doubt, @mcuee can confirm that using a wiring bootloader on a m2560 won't be able to handle those fiendish files with holes crossing over the 64k word boundary.

@stefanrueger
Copy link
Collaborator

BTW, not so sure why pgerase does not work here.

@mcuee I think we have been here before: the terminal assumes the programmer has a working page erase if the function pointer pgm->page_erase exists. I have no idea why stk500v2.c would have and install a page erase function that simply prints "this function must never be called" and returns -1. Instead the -c programmer should set pgm->page_erase = NULL; (Page erase is an optional function: the caller must check its existence before use).

@stefanrueger
Copy link
Collaborator

The root cause of the issue is the mega commit d742827 on 15 Sept 2011.

It might actually be that this commit works as intended (it's been in place for over 10 years), but that it's uncovered a weakness of the stk500v2.c code.

@mcuee
Copy link
Collaborator

mcuee commented Nov 29, 2022

The commit in question introduced memory tags to describe holes input data. Could it be that the physical programmer FW somehow autoincrements for those parts in a different way than stk500v2? So any hole in the data (a page_size worth of 0xFF) could throw a spanner in the works.

@stefanrueger
This rings a bell. I've tested with a few STK500v2 clones which do not use official FW (using ATmega8535) but rather their own implementation using 8051 MCU. They do not have this issue.

This hole hypothesis can be tested as follows: modify stk500v2.c to always (unconditionally) use stk500v2_loadaddr() for every page written/read irrespective of whether autoincrement might render that unnecessary.

Sorry just wonder how to do that.

@mcuee
Copy link
Collaborator

mcuee commented Nov 29, 2022

The root cause of the issue is the mega commit d742827 on 15 Sept 2011.

It might actually be that this commit works as intended (it's been in place for over 10 years), but that it's uncovered a weakness of the stk500v2.c code.

@stefanrueger
Apparently that commit did bring quite some issues but later fixed for other programmers.

Please refer to my earlier comments.

  1. usbasp
    [bug #48261] STK500V2 ISP problem with ATmega8/8515/8535, ATmega16 and ATmega32 #425 (comment)
  2. usbtinyisp
    [bug #48261] STK500V2 ISP problem with ATmega8/8515/8535, ATmega16 and ATmega32 #425 (comment)
  3. JTAG ICE 1
    [bug #48261] STK500V2 ISP problem with ATmega8/8515/8535, ATmega16 and ATmega32 #425 (comment)
  4. avrftdi
    [bug #48261] STK500V2 ISP problem with ATmega8/8515/8535, ATmega16 and ATmega32 #425 (comment)

Summary: The root cause of the issue is the mega commit d742827 on 15 Sept 2011.

The mega commit affects multiple programmers but some of them (like jtagmki, avrftdi, usbasp and usbtiny) have since fixed the issue. It does not seem to affect avr109/avr910 and STK500v1.

Confirmed and need to be fixed: stk500v2, jtagmkii (AVR Dragon and AVR JTAGICE mkii) in ISP mode

I can reproduce the issue with pickit4_isp. If that is the case, probably all modern Microchip tools are affected when using ISP (stk500v2.c, jtagmkII.c and jtag3.c based tool).

Based on the test results, ATmega8/8515/8535, ATmega16/32/64/128 are affected. ATmega328P are not affected.

@mcuee
Copy link
Collaborator

mcuee commented Nov 29, 2022

Maybe it is one of the myriad STK500v2 parameters that are different from m328p to m32

avrdude -pm32/S >/tmp/m32; avrdude -pm328p/S >/tmp/m328p; diff /tmp/m32{,8p}

Always good to double-check avrdude.conf entries.

Here is the output.

$ ./avrdude -pm32/S >/tmp/m32; ./avrdude -pm328p/S >/tmp/m328p; diff /tmp/m32{,8p}
2c2
< # ATmega32
---
> # ATmega328P
5,10c5,10
< part
<     desc                   = "ATmega32";
<     id                     = "m32";
<     prog_modes             = PM_SPM | PM_ISP | PM_HVPP | PM_JTAG | PM_JTAGmkI;
<     mcuid                  = 58;
<     n_interrupts           = 21;
---
> part parent "m328"
>     desc                   = "ATmega328P";
>     id                     = "m328p";
>     prog_modes             = PM_SPM | PM_ISP | PM_HVPP | PM_debugWIRE;
>     mcuid                  = 119;
>     n_interrupts           = 26;
13,14c13
<     stk500_devcode         = 0x91;
<     avr910_devcode         = 0x72;
---
>     stk500_devcode         = 0x86;
17,18c16,17
<     bs2                    = 0xa0;
<     signature              = 0x1e 0x95 0x02;
---
>     bs2                    = 0xc2;
>     signature              = 0x1e 0x95 0x0f;
20d18
<     allowfullpagebitstream = yes;
28a27
>     pollmethod             = 1;
33a33,37
>     flash_instr            =  0xb6, 0x01, 0x11;
>     eeprom_instr           =
>         0xbd, 0xf2, 0xbd, 0xe1, 0xbb, 0xcf, 0xb4, 0x00,
>         0xbe, 0x01, 0xb6, 0x01, 0xbc, 0x00, 0xbb, 0xbf,
>         0x99, 0xf9, 0xbb, 0xaf;
35c39,42
<     latchcycles            = 6;
---
>     latchcycles            = 5;
>     togglevtg              = 1;
>     poweroffdelay          = 15;
>     resetdelayms           = 1;
36a44
>     resetdelay             = 15;
40d47
<     idr                    = 0x31;
42,43c49,51
<     ocdrev                 = 2;
<     chip_erase             = "1010.1100--1000.0000--xxxx.xxxx--xxxx.xxxx";
---
>     eecr                   = 0x3f;
>     ocdrev                 = 1;
>     chip_erase             = "1010.1100--100x.xxxx--xxxx.xxxx--xxxx.xxxx";
49,50c57,58
<         min_write_delay    = 9000;
<         max_write_delay    = 9000;
---
>         min_write_delay    = 3600;
>         max_write_delay    = 3600;
52,54c60,62
<         mode               = 4;
<         delay              = 10;
<         blocksize          = 64;
---
>         mode               = 65;
>         delay              = 20;
>         blocksize          = 4;
56,57c64,65
<         read               = "1010.0000--00xx.xxaa--aaaa.aaaa--oooo.oooo";
<         write              = "1100.0000--00xx.xxaa--aaaa.aaaa--iiii.iiii";
---
>         read               = "1010.0000--000x.xxaa--aaaa.aaaa--oooo.oooo";
>         write              = "1100.0000--000x.xxaa--aaaa.aaaa--iiii.iiii";
70c78
<         mode               = 33;
---
>         mode               = 65;
72c80
<         blocksize          = 64;
---
>         blocksize          = 128;
76,77c84,85
<         loadpage_lo        = "0100.0000--00xx.xxxx--xxaa.aaaa--iiii.iiii";
<         loadpage_hi        = "0100.1000--00xx.xxxx--xxaa.aaaa--iiii.iiii";
---
>         loadpage_lo        = "0100.0000--000x.xxxx--xxaa.aaaa--iiii.iiii";
>         loadpage_hi        = "0100.1000--000x.xxxx--xxaa.aaaa--iiii.iiii";
83,84c91,92
<         min_write_delay    = 2000;
<         max_write_delay    = 2000;
---
>         min_write_delay    = 4500;
>         max_write_delay    = 4500;
91,92c99,100
<         min_write_delay    = 2000;
<         max_write_delay    = 2000;
---
>         min_write_delay    = 4500;
>         max_write_delay    = 4500;
96a105,112
>     memory "efuse"
>         size               = 1;
>         min_write_delay    = 4500;
>         max_write_delay    = 4500;
>         read               = "0101.0000--0000.1000--xxxx.xxxx--oooo.oooo";
>         write              = "1010.1100--1010.0100--xxxx.xxxx--xxxx.xiii";
>     ;
>
99,100c115,116
<         min_write_delay    = 2000;
<         max_write_delay    = 2000;
---
>         min_write_delay    = 4500;
>         max_write_delay    = 4500;
107c123
<         read               = "0011.0000--xxxx.xxxx--xxxx.xxaa--oooo.oooo";
---
>         read               = "0011.0000--000x.xxxx--xxxx.xxaa--oooo.oooo";
111,112c127,128
<         size               = 4;
<         read               = "0011.1000--00xx.xxxx--0000.00aa--oooo.oooo";
---
>         size               = 1;
>         read               = "0011.1000--000x.xxxx--0000.0000--oooo.oooo";

@mcuee
Copy link
Collaborator

mcuee commented Nov 29, 2022

BTW, when I started my career as avrdude contributor I fixed the load extended address code for vvv large parts (think m2560) for a number of -c programmers. Glancing over stk500v2.c that looks wrong, too! It has the same tell-tale code that won't work with files with holes

avrdude/src/stk500v2.c

Lines 2456 to 2462 in 2f2a6c0

// Ensure a new "load extended address" will be issued
// when crossing a 64 KB boundary in flash.
if (hiaddr != (addr & ~0xFFFF)) {
hiaddr = addr & ~0xFFFF;
if (stk500v2_loadaddr(pgm, use_ext_addr | (addr >> addrshift)) < 0)
return -1;
}

This is an independent problem, but still a problem. No doubt, @mcuee can confirm that using a wiring bootloader on a m2560 won't be able to handle those fiendish files with holes crossing over the 64k word boundary.

Haha, I remember those testing experiences.

On the other hand, it is interesting that wiring bootloader works in this case with your good hext file. Same for my STK500v2 with official FW (ATmega8535 based).

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude -c wiring -P COM5 -p m2560 -D
 -U .\hex\blink-mega2560_lext-test.hex

avrdude: AVR device initialized and ready to accept instructions
avrdude: device signature = 0x1e9801 (probably m2560)
avrdude: reading input file .\hex\blink-mega2560_lext-test.hex for flash
         with 1346 bytes in 4 sections within [0, 0x3106d]
         using 7 pages and 446 pad bytes
avrdude: writing 1346 bytes flash ...

Writing | ################################################## | 100% 0.31 s

avrdude: 1346 bytes of flash written
avrdude: verifying flash memory against .\hex\blink-mega2560_lext-test.hex

Reading | ################################################## | 100% 0.15 s

avrdude: 1346 bytes of flash verified

avrdude done.  Thank you.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude -c stk500v2 -P COM11 -p m2560
 -B1 -U .\hex\blink-mega2560_lext-test.hex

avrdude: AVR device initialized and ready to accept instructions
avrdude: device signature = 0x1e9801 (probably m2560)
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 .\hex\blink-mega2560_lext-test.hex for flash
         with 1346 bytes in 4 sections within [0, 0x3106d]
         using 7 pages and 446 pad bytes
avrdude: writing 1346 bytes flash ...

Writing | ################################################## | 100% 0.36 s

avrdude: 1346 bytes of flash written
avrdude: verifying flash memory against .\hex\blink-mega2560_lext-test.hex

Reading | ################################################## | 100% 0.14 s

avrdude: 1346 bytes of flash verified

avrdude done.  Thank you.

PS C:\work\avr\avrdude_test\avrdude_bin> cat .\hex\blink-mega2560_lext-test.hex
:020000040000FA
:2000000076C0000086C0000084C0000082C0000080C000007EC000007CC000007AC00000EA
:2000200078C0000076C0000074C0000072C0000070C000006EC000006CC000006AC0000038
:2000400068C0000066C0000064C0000062C000009DC000005EC000005CC000005AC000005B
:2000600058C00000F1C000001EC1000052C0000050C000004EC000004CC000004AC0000092
:2000800048C0000046C0000044C0000042C0000040C000003EC000003CC000003AC0000058
:2000A00038C0000036C0000034C0000032C0000030C000002EC000002CC000002AC00000B8
:2000C00028C0000026C0000024C0000022C0000020C000001EC000001CC000001AC0000018
:2000E00018C00000626C696E6B2E2E2E0A0011241FBECFEFD1E2DEBFCDBF00E00CBF22E02B
:20010000A0E0B2E001C01D92A436B207E1F778D047C176CF10928000C2E0C0938100279A04
:20012000C093C0001092C50082E28093C40086E08093C200E6D080E291E090930B02809303
:200140000A0210920D0210920C02C093050210920F0210920E0282E092E090936102809306
:20016000600264EE70E080E0992701D147D06C197D098E099F09653F714081059105B0F314
:200180003DD06B017C011F9AF1CF08950F930FB70F930AB50F5F0ABD70F00BB50F4F0BBD0F
:2001A00050F0009100020F4F00930002009101020F4F009301020F910FBF0F9118952FB750
:2001C000F894609184006091850036B37AB58BB5909100022FBF2091010230FF06C06F3FE8
:2001E00021F07F5F8F4F9F4F2F4F33E0269597958795779567953A95C9F70895E0CF089530
:20020000789493E094BD95BD81E080936F0024E02093B1008093B000909391008093900057
:200220009093A1008093A000909321018093200187E880937A001092C1006CDFA6DFFECFD2
:2002400073D080E090E008951F920F920FB60F9211240BB60F922F938F939F93EF93FF9375
:200260008091C0009091C6008C71E0911302EF5FE77020911202E21739F0E0931302F0E05F
:20028000EA5EFD4F908301C082E080931102FF91EF919F918F912F910F900BBE0F900FBE7A
:2002A0000F901F9018951F920F920FB60F9211240BB60F928F93EF93FF93E0911402EF5FE9
:2002C000EF73E0931402809115028E1305C08091C1008F7D8093C1008091C000806480932B
:2002E000C000F0E0E25EFD4F80818093C600FF91EF918F910F900BBE0F900FBE0F901F90B6
:2003000018951092150210921402109213021092120210921002E1ECF0E08081886980830C
:2003200080818F7D80830895CF93C82F8A3019F48DE0FADF03C08F3708F0CFE3909115023F
:200340009F5F9F73809114029817E1F3E92FF0E0E25EFD4FC0839093150281E0809310026C
:200360008091C10080628093C100CF910895CF92DF92EF92FF926B017C012FEFC21AD20A55
:20038000E20AF20A8BBFFB018791882321F0CCDFC701B601F0CFFF90EF90DF90CF900895F9
:0803A000F89400C0F894FFCFAF
:020000040001F9
:20100000426F6E6A6F7572206C65206D6F6E64650A426F6E6A6F7572206C65206D6F6E6425
:20102000650A426F6E6A6F7572206C65206D6F6E64650A426F6E6A6F7572206C65206D6F68
:201040006E64650A426F6E6A6F7572206C65206D6F6E64650A426F6E6A6F7572206C652052
:201060006D6F6E64650A426F6E6A6F7572206C65206D6F6E64650A426F6E6A6F7572206CDB
:2010800065206D6F6E64650A426F6E6A6F7572206C65206D6F6E64650A426F6E6A6F7572C2
:0A10A000206C65206D6F6E64650A18
:020000040002F8
:2010000048656C6C6F2C20776F726C640A48656C6C6F2C20776F726C640A48656C6C6F2CCC
:2010200020776F726C640A48656C6C6F2C20776F726C640A48656C6C6F2C20776F726C6484
:201040000A48656C6C6F2C20776F726C640A48656C6C6F2C20776F726C640A48656C6C6FAE
:201060002C20776F726C640A48656C6C6F2C20776F726C640A48656C6C6F2C20776F726C7C
:02108000640A00
:020000040003F7
:201000004865692056657264656E0A4865692056657264656E0A4865692056657264656EEE
:201020000A4865692056657264656E0A4865692056657264656E0A48656920566572646532
:201040006E0A4865692056657264656E0A4865692056657264656E0A486569205665726409
:0E106000656E0A4865692056657264656E0A01
:00000001FF

@mcuee mcuee modified the milestones: AVRDUDE 7.1, AVRDUDE 8.0 Jan 2, 2023
@mcuee
Copy link
Collaborator

mcuee commented Jan 6, 2023

PR #1265 fixed a long standing issue $455 with linuxgpio and linuxspi. It may be somewhat related to this issue.

On the other hand, this issue may be different and likely due to firmware issue and we may have to use reverse engineering from Microchip Studio to see if there is any difference from Microchip Studio.

@mcuee
Copy link
Collaborator

mcuee commented Jan 8, 2023

There is no issue using Microchip Studio with the following hex file.

PS C:\work\avr\avrdude_test\avrdude_bin> cat .\simpletest_issue425_m8a.hex
:020000040000FA
:20000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00
:20002000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE0
:20008000000000000000000000000000000000000000000000000000000000000000000060
:2000A000000000000000000000000000000000000000000000000000000000000000000040
:00000001FF

Microchip Studio is okay.

Screenshot 2023-01-08 205040

avrdude git main run log:

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude -c stk500v2 -P COM4 -p m8a -U .\simpletest_issue425_m8a.hex

avrdude: AVR device initialized and ready to accept instructions
avrdude: device signature = 0x1e9307 (probably m8a)
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 .\simpletest_issue425_m8a.hex for flash
         with 128 bytes in 2 sections within [0, 0xbf]
         using 2 pages and 0 pad bytes
avrdude: writing 128 bytes flash ...

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

avrdude: 128 bytes of flash written
avrdude: verifying flash memory against .\simpletest_issue425_m8a.hex

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

avrdude warning: verification mismatch
        device 0xff != input 0x00 at addr 0x0080 (error)
avrdude error: verification mismatch

avrdude done.  Thank you.

@mcuee
Copy link
Collaborator

mcuee commented Jan 8, 2023

The following is with HHD Free Device Monitoring Studio, capturing the Serial Port (COM4) data when using Mircochip Studio 7.0 to program the ATmega8A.

  1. Packet View
    STK500V2_m8a_monitor.txt

  2. Request View
    STK500V2_m8a_monitor_requestview.txt

@stefanrueger
Copy link
Collaborator

stefanrueger commented Jan 8, 2023

Ah, yes, I remember we had narrowed it down to input files with a hole that covers at least one page. In your simple test example, the input file has two 64-byte blocks (64 bytes = 1 page): one with all 0xff at address 0 and the other one with all 0x00 at address 0x80 leaving a 64-byte hole at address 0x40.

I have read through the stk500v2_paged_write() routine. It uses mem->mode, mem->delay and mem->readback[] parameters from avrdude.conf.in to parameterise the call. Now, the thing is that the mode is 0x21 for ATmega128 ATmega128A ATmega16 ATmega16A ATmega32 ATmega32A ATmega64 ATmega64A ATmega8 ATmega8515 ATmega8535 ATmega8A ATtiny26, which means paged write (0x01) and determine when it's finished by polling using the poll values in the command (0x20). As far as I can tell, the poll value is (and should be) in this case 0xff. Now, one hypothesis is that if you write a page full of 0xff, the STK500 programmer firmware might not do the right thing: Endless loop? No wait? Wrong wait? I checked, and the delay and readback parameters are correct in avrdude.conf for the ATmega8A (10 ms). They are not correct for other parts, including the ATmega16, but I suspect that is not the issue here. I'll create a PR I have created PR #1272 for this, as this was easy to correct

[edit: question was answered above]Here is what we could ask: Is it that we have a hole that is the problem or the fact that we try to program a page full of 0xff? Create another hex file with two pages: one page with 64 bytes all 0xff at address 0 followed by a page all with 0.

  • If that programs correctly then it is the hole that is the problem (which would chime with the observation that the commit that introduced TAG_ALLOCATED where the problem starts)
  • If that does not program correctly then it is morelikely related to the fact that the programming mode uses polling to determine the end of the page write. As it would be (flash) pages full of 0xff, we could usleep(20*1000) if mem->mode & 0x20 rings true and the page is full of 0xff. Or we could not program a page with only 0xff seeing as it might be a NOP (but would need to make sure that flash looks like NOR-memory for all programmers served by stk500v2, including bootloaders)

@stefanrueger
Copy link
Collaborator

OK, reading through the previous posts, it looks like this has nothing to do with holes and everything to do with value polling on a page full of 0xff for these parts. Here is patch that delays when we ask STK500 to write a page using value polling when the page is full of poll bytes:

diff --git a/src/stk500v2.c b/src/stk500v2.c
index 49a331f..e92cf8d 100644
--- a/src/stk500v2.c
+++ b/src/stk500v2.c
@@ -2162,6 +2162,13 @@ static int stk500isp_write_byte(const PROGRAMMER *pgm, const AVRPART *p, const A
   return 0;
 }
 
+
+// Are all n bytes pointed to by p the same byte chr?
+static int _is_all_chr(const unsigned char *p, size_t n, unsigned char chr) {
+  return n <= 0 || (*p == chr && memcmp(p, p+1, n-1) == 0);
+}
+
+
 static int stk500v2_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *m,
                                 unsigned int page_size,
                                 unsigned int addr, unsigned int n_bytes)
@@ -2280,6 +2287,10 @@ static int stk500v2_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const A
     memcpy(buf+10,m->buf+addr, block_size);
 
     result = stk500v2_command(pgm,buf,block_size+10, sizeof(buf));
+    if(commandbuf[3] & 0x20)    // Value polling: might fail when page only has poll bytes
+      if(_is_all_chr(m->buf+addr, block_size, commandbuf[8]))
+        if(10+m->delay > 0)
+          usleep((10+m->delay)*1000);
     if (result < 0) {
       pmsg_error("write command failed\n");
       return -1;

@stefanrueger
Copy link
Collaborator

Here another idea: Don't program with value polling when the page only has poll bytes:

diff --git a/src/stk500v2.c b/src/stk500v2.c
index 49a331f..650a4be 100644
--- a/src/stk500v2.c
+++ b/src/stk500v2.c
@@ -2162,6 +2162,13 @@ static int stk500isp_write_byte(const PROGRAMMER *pgm, const AVRPART *p, const A
   return 0;
 }
 
+
+// Are all n bytes pointed to by p the same byte chr?
+static int _is_all_chr(const unsigned char *p, size_t n, unsigned char chr) {
+  return n <= 0 || (*p == chr && memcmp(p, p+1, n-1) == 0);
+}
+
+
 static int stk500v2_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *m,
                                 unsigned int page_size,
                                 unsigned int addr, unsigned int n_bytes)
@@ -2279,7 +2286,12 @@ static int stk500v2_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const A
 
     memcpy(buf+10,m->buf+addr, block_size);
 
-    result = stk500v2_command(pgm,buf,block_size+10, sizeof(buf));
+    int do_write = 1;
+    if(commandbuf[3] & 0x20)    // Value polling: might fail when page only has poll bytes
+      if(_is_all_chr(m->buf+addr, block_size, commandbuf[8]))
+        do_write = 0;
+
+    result = do_write? stk500v2_command(pgm,buf,block_size+10, sizeof(buf)): 0;
     if (result < 0) {
       pmsg_error("write command failed\n");
       return -1;

@mcuee
Copy link
Collaborator

mcuee commented Jan 9, 2023

Here another idea: Don't program with value polling when the page only has poll bytes:

This seems to be in line with the Microchip Studio serial sniffer output. I will give this patch a try.

@mcuee
Copy link
Collaborator

mcuee commented Jan 9, 2023

Here another idea: Don't program with value polling when the page only has poll bytes:

This seems to be in line with the Microchip Studio serial sniffer output. I will give this patch a try.

@stefanrueger

Yes this fixed the issue. Great work!

diff --git a/src/stk500v2.c b/src/stk500v2.c
index 49a331f..6145206 100644
--- a/src/stk500v2.c
+++ b/src/stk500v2.c
@@ -2162,6 +2162,11 @@ static int stk500isp_write_byte(const PROGRAMMER *pgm, const AVRPART *p, const A
   return 0;
 }

+// Are all n bytes pointed to by p the same byte chr?
+static int _is_all_chr(const unsigned char *p, size_t n, unsigned char chr) {
+  return n <= 0 || (*p == chr && memcmp(p, p+1, n-1) == 0);
+}
+
 static int stk500v2_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *m,
                                 unsigned int page_size,
                                 unsigned int addr, unsigned int n_bytes)
@@ -2279,7 +2284,12 @@ static int stk500v2_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const A

     memcpy(buf+10,m->buf+addr, block_size);

-    result = stk500v2_command(pgm,buf,block_size+10, sizeof(buf));
+    int do_write = 1;
+    if(commandbuf[3] & 0x20)    // Value polling: might fail when page only has poll bytes
+      if(_is_all_chr(m->buf+addr, block_size, commandbuf[8]))
+        do_write = 0;
+
+    result = do_write? stk500v2_command(pgm,buf,block_size+10, sizeof(buf)): 0;
     if (result < 0) {
       pmsg_error("write command failed\n");
       return -1;

C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_issue455_sr2 -c stk500v2 -P COM4 -p m8a 
-U .\simpletest_issue425_m8a.hex
avrdude_issue455_sr2: AVR device initialized and ready to accept instructions
avrdude_issue455_sr2: device signature = 0x1e9307 (probably m8a)
avrdude_issue455_sr2: Note: flash memory has been specified, an erase cycle will be performed.
                      To disable this feature, specify the -D option.
avrdude_issue455_sr2: erasing chip
avrdude_issue455_sr2: reading input file .\simpletest_issue425_m8a.hex for flash
                      with 128 bytes in 2 sections within [0, 0xbf]
                      using 2 pages and 0 pad bytes
avrdude_issue455_sr2: writing 128 bytes flash ...

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

avrdude_issue455_sr2: 128 bytes of flash written
avrdude_issue455_sr2: verifying flash memory against .\simpletest_issue425_m8a.hex

Reading | ################################################## | 100% 0.03 s

avrdude_issue455_sr2: 128 bytes of flash verified

avrdude_issue455_sr2 done.  Thank you.

@mcuee
Copy link
Collaborator

mcuee commented Jan 9, 2023

OK, reading through the previous posts, it looks like this has nothing to do with holes and everything to do with value polling on a page full of 0xff for these parts. Here is patch that delays when we ask STK500 to write a page using value polling when the page is full of poll bytes:

Just for understanding, I test this patch as well. It does not work.

diff --git a/src/stk500v2.c b/src/stk500v2.c
index 49a331f..3ef29ec 100644
--- a/src/stk500v2.c
+++ b/src/stk500v2.c
@@ -2162,6 +2162,11 @@ static int stk500isp_write_byte(const PROGRAMMER *pgm, const AVRPART *p, const A
   return 0;
 }

+// Are all n bytes pointed to by p the same byte chr?
+static int _is_all_chr(const unsigned char *p, size_t n, unsigned char chr) {
+  return n <= 0 || (*p == chr && memcmp(p, p+1, n-1) == 0);
+}
+
 static int stk500v2_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *m,
                                 unsigned int page_size,
                                 unsigned int addr, unsigned int n_bytes)
@@ -2280,6 +2285,10 @@ static int stk500v2_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const A
     memcpy(buf+10,m->buf+addr, block_size);

     result = stk500v2_command(pgm,buf,block_size+10, sizeof(buf));
+    if(commandbuf[3] & 0x20)    // Value polling: might fail when page only has poll bytes
+      if(_is_all_chr(m->buf+addr, block_size, commandbuf[8]))
+        if(10+m->delay > 0)
+          usleep((10+m->delay)*1000);
     if (result < 0) {
       pmsg_error("write command failed\n");
       return -1;

C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_issue455_sr1 -c stk500v2 -P COM4 -p m8a
 -U .\simpletest_issue425_m8a.hex

avrdude_issue455_sr1: AVR device initialized and ready to accept instructions
avrdude_issue455_sr1: device signature = 0x1e9307 (probably m8a)
avrdude_issue455_sr1: Note: flash memory has been specified, an erase cycle will be performed.
                      To disable this feature, specify the -D option.
avrdude_issue455_sr1: erasing chip
avrdude_issue455_sr1: reading input file .\simpletest_issue425_m8a.hex for flash
                      with 128 bytes in 2 sections within [0, 0xbf]
                      using 2 pages and 0 pad bytes
avrdude_issue455_sr1: writing 128 bytes flash ...

Writing | ################################################## | 100% 0.06 s

avrdude_issue455_sr1: 128 bytes of flash written
avrdude_issue455_sr1: verifying flash memory against .\simpletest_issue425_m8a.hex

Reading | ################################################## | 100% 0.03 s

avrdude_issue455_sr1 warning: verification mismatch
                     device 0xff != input 0x00 at addr 0x0080 (error)
avrdude_issue455_sr1 error: verification mismatch

avrdude_issue455_sr1 done.  Thank you.

@mcuee
Copy link
Collaborator

mcuee commented Jan 9, 2023

Here another idea: Don't program with value polling when the page only has poll bytes:

This seems to be in line with the Microchip Studio serial sniffer output. I will give this patch a try.

@stefanrueger

Yes this fixed the issue. Great work!

Tested with AVRISP mkii as well.

C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_issue455_sr2.exe  -c avrisp2 -p m8a -U .\simpletest_issue425_m8a.hex

avrdude_issue455_sr2: AVR device initialized and ready to accept instructions
avrdude_issue455_sr2: device signature = 0x1e9307 (probably m8a)
avrdude_issue455_sr2: Note: flash memory has been specified, an erase cycle will be performed.
                      To disable this feature, specify the -D option.
avrdude_issue455_sr2: erasing chip
avrdude_issue455_sr2: reading input file .\simpletest_issue425_m8a.hex for flash
                      with 128 bytes in 2 sections within [0, 0xbf]
                      using 2 pages and 0 pad bytes
avrdude_issue455_sr2: writing 128 bytes flash ...

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

avrdude_issue455_sr2: 128 bytes of flash written
avrdude_issue455_sr2: verifying flash memory against .\simpletest_issue425_m8a.hex

Reading | ################################################## | 100% 0.03 s

avrdude_issue455_sr2: 128 bytes of flash verified

avrdude_issue455_sr2 done.  Thank you.

@mcuee
Copy link
Collaborator

mcuee commented Jan 20, 2023

One more test with PICKit 4 and current git main is good.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude -c pickit4_isp -p m8a -U .\hex3\simpletest_issue425_m8a.hex

avrdude: AVR device initialized and ready to accept instructions
avrdude: device signature = 0x1e9307 (probably m8a)
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 .\hex3\simpletest_issue425_m8a.hex for flash
         with 128 bytes in 2 sections within [0, 0xbf]
         using 2 pages and 0 pad bytes
avrdude: writing 128 bytes flash ...

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

avrdude: 128 bytes of flash written
avrdude: verifying flash memory against .\hex3\simpletest_issue425_m8a.hex

Reading | ################################################## | 100% 0.04 s

avrdude: 128 bytes of flash verified

avrdude done.  Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants