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 #40108] [PATCH] Fix avrftdi verification error after flash write #303

Closed
avrs-admin opened this issue Dec 10, 2021 · 12 comments
Closed
Labels
invalid This doesn't seem right

Comments

@avrs-admin
Copy link

Nemui
Thu 26 Sep 2013 03:22:18 AM UTC

When write flash below sector size,usually polling byte cannot found.
In this case,verify flash after flash write always fail.

Tested with ATmega1284p(has 256byte sector size).
202byte(<256byte) binary fail,but over 256byte binaries was Ok.

Attached patch fixes this problem to re-initialize avrftdi when polling byte cannot be found.

file #29220: success_patched_dude.log
file #29219: failure_orig_dude.log
file #29218: avrdude_avrftdi_fix.patch
file #29223: avrftdi_sync.patch
file #29858: avrftdi_testing_report.7z

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

@avrs-admin
Copy link
Author

Hannes Weisbach
Thu 26 Sep 2013 10:28:56 AM UTC

Thank you for reporting this.

Can you supply a log with -vvvvv as parameters to avrdude as well as the binary you are programming?
I am not accepting this patch as-is because (more or less) randomly resetting programmers is usually not the solution to the problem. In fact, the code is commented with a TODO, which already states what has to be done: /* TODO sync write */.
This means the flush part of write_flush() has to be called.

I've attached a patch (against r1248) implementing this TODO. Could you please try it and report back if they work (apply with -p1)?

(file #29223)

@avrs-admin
Copy link
Author

Nemui Trinomius
Wed 11 Dec 2013 04:49:07 AM UTC

Dear hweisbach,
Sorry for my late reply.

I tested your patch and got work well.
Please see attached report for detail.
(file #29858)

@mcuee
Copy link
Collaborator

mcuee commented Jun 5, 2022

@mcuee
Copy link
Collaborator

mcuee commented Jun 8, 2022

I have both read and write issues using FT2232H based JTAGkey-2 with ATmega2560.

I will try the patch mentioned in this thread. https://savannah.nongnu.org/support/download.php?file_id=29223
(Edit: in the end I did not try this as it is a different issue)

From 7cdba58072b2672691a7ad41f3e997d271fa9e50 Mon Sep 17 00:00:00 2001
From: Hannes Weisbach <[email protected]>
Date: Thu, 26 Sep 2013 12:12:43 +0200
Subject: [PATCH 1/2] Adds avrftdi_sync() from write_flush().

The synchronziation part is moved in a separate function to facilitate
re-use
---
 avrdude/avrftdi.c | 47 ++++++++++++++++++++++++++++++-----------------
 1 file changed, 30 insertions(+), 17 deletions(-)

diff --git avrdude/avrftdi.c avrdude/avrftdi.c
index e49ce26..17f3cfd 100644
--- avrdude/avrftdi.c
+++ avrdude/avrftdi.c
@@ -472,6 +472,35 @@ static inline int avrftdi_transmit(PROGRAMMER * pgm, unsigned char mode, const u
 		return avrftdi_transmit_mpsse(pdata, mode, buf, data, buf_size);
 }
 
+static int avrftdi_sync(avrftdi_t* pdata)
+{
+	unsigned char buf[1];
+	const unsigned char cmd[] = { GET_BITS_LOW, SEND_IMMEDIATE };
+	E(ftdi_write_data(pdata->ftdic, cmd, sizeof(cmd)) != sizeof(cmd), pdata->ftdic);
+
+	int num = 0;
+	do
+	{
+		int n = ftdi_read_data(pdata->ftdic, buf, sizeof(buf));
+		if(n > 0)
+			num += n;
+		E(n < 0, pdata->ftdic);
+	} while(num < 1);
+
+	/*
+	 * TODO: error handling when more bytes are read than anticipated. This would
+	 * indicate we lost synchronization with the FTDI device. Not sure what the
+	 * proper way of handling this would be. Probably restart programming from
+	 * scratch or abort. If avrdude would be capable we could re-init FTDI and
+	 * start re-programming from the last page (omitting chip erase, but erasing
+	 * the current page, because it might contain garbage).
+	 */
+	if(num > 1)
+		log_warn("Read %d extra bytes\n", num-1);
+
+	return 0;
+}
+
 static int write_flush(avrftdi_t* pdata)
 {
 	unsigned char buf[6];
@@ -502,23 +531,7 @@ static int write_flush(avrftdi_t* pdata)
 	 */
 	//E(ftdi_usb_purge_buffers(pdata->ftdic), pdata->ftdic);
 
-	unsigned char cmd[] = { GET_BITS_LOW, SEND_IMMEDIATE };
-	E(ftdi_write_data(pdata->ftdic, cmd, sizeof(cmd)) != sizeof(cmd), pdata->ftdic);
-	
-	int num = 0;
-	do
-	{
-		int n = ftdi_read_data(pdata->ftdic, buf, sizeof(buf));
-		if(n > 0)
-			num += n;
-		E(n < 0, pdata->ftdic);
-	} while(num < 1);
-	
-	if(num > 1)
-		log_warn("Read %d extra bytes\n", num-1);
-
-	return 0;
-
+	return avrftdi_sync(pdata);
 }
 
 static int avrftdi_check_pins_bb(PROGRAMMER * pgm, bool output)
-- 
1.8.4


From 81e0f9b6ecddf43d482c9ebb9e9a6f60065395ff Mon Sep 17 00:00:00 2001
From: Hannes Weisbach <[email protected]>
Date: Thu, 26 Sep 2013 12:14:07 +0200
Subject: [PATCH 2/2] Call avrftdi_sync() before sleeping in page programming
 mode

This ensures that the preceding command was fully transmitted to the AVR
and that the subsequent sleep (programming time) actually happens at the
same time the AVR programs its memory.
---
 avrdude/avrftdi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git avrdude/avrftdi.c avrdude/avrftdi.c
index 17f3cfd..5f2bd58 100644
--- avrdude/avrftdi.c
+++ avrdude/avrftdi.c
@@ -1091,7 +1091,7 @@ static int avrftdi_flash_write(PROGRAMMER * pgm, AVRPART * p, AVRMEM * m,
 		log_warn("No suitable byte (!=0xff) for polling found.\n");
 		log_warn("Trying to sleep instead, but programming errors may occur.\n");
 		log_warn("Be sure to verify programmed memory (no -V option)\n");
-		/* TODO sync write */
+		avrftdi_sync(to_pdata(pgm));
 		/* sleep */
 		usleep((m->max_write_delay));
 	}
-- 
1.8.4

@mcuee
Copy link
Collaborator

mcuee commented Jun 8, 2022

My issue with ATmega2560 is not related to this one. The patch in #474 fixed my issues with reading/writing of ATmega2560 using FT2232H based JTAGKey-2.

@mcuee mcuee added the unconfirmed Maybe a bug, needs to be reproduced by someone else label Jun 20, 2022
@mcuee
Copy link
Collaborator

mcuee commented Jun 25, 2022

Most likely this issue has been fixed and no longer valid.

@mcuee mcuee added invalid This doesn't seem right and removed unconfirmed Maybe a bug, needs to be reproduced by someone else labels Jul 10, 2022
@mcuee
Copy link
Collaborator

mcuee commented Jul 10, 2022

I will close this as invalid for now. Please re-open when the issue occurs.

@mcuee
Copy link
Collaborator

mcuee commented Aug 20, 2022

file #29218: avrdude_avrftdi_fix.patch

diff -urN b/avrftdi.c a/avrftdi.c
--- avrftdi.c	2013-09-20 09:25:29 +0900
+++ avrftdi.c	2013-09-20 09:48:28 +0900
@@ -49,6 +49,15 @@
 #define MIN(a,b) ((a)<(b)?(a):(b))
 #endif
 
+/*
+ * Verify error occurs after write flash with cannot find a poll byte situation!
+ * This situation caused by writing below first sector size.
+ * (e.g. Write below 256byte binary to ATMEGA1284p(has 256b SectorSize).)
+ * To re-initializing ftdi interface,verify action works well.
+ */
+static int reset_if =0;
+
+
 #ifdef DO_NOT_BUILD_AVRFTDI
 
 static int avrftdi_noftdi_open (struct programmer_t *pgm, char * name)
@@ -1081,6 +1090,8 @@
 		/* TODO sync write */
 		/* sleep */
 		usleep((m->max_write_delay));
+		/* Rize reset_if flag */
+		reset_if = 1;
 	}
 
 	return len;
@@ -1142,6 +1153,13 @@
 		buf_dump(o_buf, sizeof(o_buf), "o_buf", 0, 32);
 	}
 
+	/* Detect reset_if flag */
+	if(reset_if == 1){
+		/* Re-Initialize */
+		avrftdi_initialize(pgm,p);
+		reset_if =0;
+	}
+
 	if (0 > avrftdi_transmit(pgm, MPSSE_DO_READ | MPSSE_DO_WRITE, o_buf, i_buf, len * 4))
 		return -1;
 ``

@mcuee
Copy link
Collaborator

mcuee commented Aug 20, 2022

Error log.
file #29219: failure_orig_dude.log

C:/Devz/AVR/avrdude/avrdude -v -p atmega1284p -P usb -c jtagkey    -B6 -U flash:w:main.hex 

avrdude: Version 6.1-svn-20130917, compiled on Sep 20 2013 at 09:27:17
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2009 Joerg Wunsch

         System wide configuration file is "C:\Devz\AVR\avrdude\avrdude.conf"

         Using Port                    : usb
         Using Programmer              : jtagkey
         Setting bit clk period        : 6.0
         AVR Part                      : ATmega1284P
         Chip Erase delay              : 55000 us
         PAGEL                         : PD7
         BS2                           : PA0
         RESET disposition             : dedicated
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom        65    10   128    0 no       4096    8      0  9000  9000 0xff 0xff
           flash         65    10   256    0 yes    131072  256    512  4500  4500 0xff 0xff
           lock           0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           lfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           hfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           efuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00
           calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00

         Programmer Type : avrftdi
         Description     : Amontec JTAGKey, JTAGKey-Tiny and JTAGKey2

avrdude: AVR device initialized and ready to accept instructions

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

avrdude: Device signature = 0x1e9705
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 "main.hex"
avrdude: input file main.hex auto detected as Intel Hex
avrdude: writing flash (202 bytes):

Writing | W avrftdi_flash_write(1078): No suitable byte (!=0xff) for polling found.
W avrftdi_flash_write(1079): Trying to sleep instead, but programming errors may occur.
W avrftdi_flash_write(1080): Be sure to verify programmed memory (no -V option)
################################################## | 100% 0.02s

avrdude: 202 bytes of flash written
avrdude: verifying flash memory against main.hex:
avrdude: load data flash data from input file main.hex:
avrdude: input file main.hex auto detected as Intel Hex
avrdude: input file main.hex contains 202 bytes
avrdude: reading on-chip flash data:

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

avrdude: verifying ...
avrdude: verification error, first mismatch at byte 0x0000
         0xff != 0x0c
avrdude: verification error; content mismatch

avrdude done.  Thank you.

@mcuee
Copy link
Collaborator

mcuee commented Aug 20, 2022

file #29220: success_patched_dude.log

Good run log after patching avrdude.

C:/Devz/AVR/avrdude/avrdude -v -p atmega1284p -P usb -c jtagkey    -B6 -U flash:w:main.hex 

avrdude: Version 6.1-svn-20130917, compiled on Sep 20 2013 at 10:02:57
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2009 Joerg Wunsch

         System wide configuration file is "C:\Devz\AVR\avrdude\avrdude.conf"

         Using Port                    : usb
         Using Programmer              : jtagkey
         Setting bit clk period        : 6.0
         AVR Part                      : ATmega1284P
         Chip Erase delay              : 55000 us
         PAGEL                         : PD7
         BS2                           : PA0
         RESET disposition             : dedicated
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom        65    10   128    0 no       4096    8      0  9000  9000 0xff 0xff
           flash         65    10   256    0 yes    131072  256    512  4500  4500 0xff 0xff
           lock           0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           lfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           hfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           efuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00
           calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00

         Programmer Type : avrftdi
         Description     : Amontec JTAGKey, JTAGKey-Tiny and JTAGKey2

avrdude: AVR device initialized and ready to accept instructions

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

avrdude: Device signature = 0x1e9705
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 "main.hex"
avrdude: input file main.hex auto detected as Intel Hex
avrdude: writing flash (202 bytes):

Writing | W avrftdi_flash_write(1087): No suitable byte (!=0xff) for polling found.
W avrftdi_flash_write(1088): Trying to sleep instead, but programming errors may occur.
W avrftdi_flash_write(1089): Be sure to verify programmed memory (no -V option)
################################################## | 100% 0.02s

avrdude: 202 bytes of flash written
avrdude: verifying flash memory against main.hex:
avrdude: load data flash data from input file main.hex:
avrdude: input file main.hex auto detected as Intel Hex
avrdude: input file main.hex contains 202 bytes
avrdude: reading on-chip flash data:

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

avrdude: verifying ...
avrdude: 202 bytes of flash verified

avrdude done.  Thank you.

@mcuee
Copy link
Collaborator

mcuee commented Aug 20, 2022

Testing report:
file #29858: avrftdi_testing_report.7z

Summary:

AVRDUDE Testing Report
20131211 Nemui Trinomius

Report URL:
 http://savannah.nongnu.org/bugs/?40108
Bug Summary:
 Verify error occurs after write with below sector size binary (using avrftdi).
 

Testing:
 Compare output logs pached/no-patched situation.
 See also test/environment.txt for this testing environment.

 *case1-1:
   Using no-patched avrdude v6.1 with -v-v-v option
   Write 204byte Binary(testprogram build with -Os option,below atmega1284p's flash sectorsize(256byte))
    ->Verify fail.(see test/case1-1.txt)
 *case1-2:
   Using no-patched avrdude v6.1 with -v-v-v option
   Write 1076byte Binary(testprogram build with -O0 option,above atmega1284p's flash sectorsize(256byte))
    ->Verify Succeed.(see test/case1-2.txt)
 *case1-3:
   Using no-patched avrdude v6.1 with -v-v-v-v-v option
   Write 204byte Binary(testprogram build with -Os option,below atmega1284p's flash sectorsize(256byte))
    ->Verify Succeed.(see test/case1-3.txt)
 *case1-4:
   Using no-patched avrdude v6.1 with -v-v-v-v-v option
   Write 1076byte Binary(testprogram build with -O0 option,above atmega1284p's flash sectorsize(256byte))
    ->Verify Succeed.(see test/case1-4.txt)

 *case2-1:
   Using patched(Hannes Weisbach's patch) avrdude v6.1 with -v-v-v option
   Write 204byte Binary(testprogram build with -Os option,below atmega1284p's flash sectorsize(256byte))
    ->Verify Succeed.(see test/case2-1.txt)
 *case2-2:
   Using patched(Hannes Weisbach's patch) avrdude v6.1 with -v-v-v option
   Write 1076byte Binary(testprogram build with -O0 option,above atmega1284p's flash sectorsize(256byte))
    ->Verify Succeed.(see test/case2-2.txt)
 *case2-3:
   Using patched(Hannes Weisbach's patch) avrdude v6.1 with -v-v-v-v-v option
   Write 204byte Binary(testprogram build with -Os option,below atmega1284p's flash sectorsize(256byte))
    ->Verify Succeed.(see test/case2-3.txt)
 *case2-4:
   Using patched(Hannes Weisbach's patch) avrdude v6.1 with -v-v-v-v-v option
   Write 1076byte Binary(testprogram build with -O0 option,above atmega1284p's flash sectorsize(256byte))
    ->Verify Succeed.(see test/case2-4.txt)


In "case1-3" must duplicate verify error,but verify succeeded....
Adding -v-v-v-v-v option cause syncwrite action due to buffer dump?


Conclusion:
 Avrftdi sync-write action seems work fine by Hannes Weisbach's patch.

@mcuee
Copy link
Collaborator

mcuee commented Aug 20, 2022

New discussion in #1073. The patch mentioned in this thread is too old and no longer valid.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests

2 participants