STM32x Flash programming issues - ARM-USB-OSD, STM32x10xxB

Hi,

Quick-checked through the code and found that flash fill* does not work correctly when count>1024. Below is a diff to fix this:

Index: flash.c
===================================================================
--- flash.c	(revision 1396)
+++ flash.c	(working copy)
@@ -727,6 +727,7 @@
 	u32 count;
 	u8 chunk[1024];
 	u32 wrote = 0;
+	u32 cur_size = 0;
 	int chunk_count;
 	char *duration_text;
 	duration_t duration;
@@ -786,9 +787,9 @@
 
 	duration_start_measure(&duration);
 
-	for (wrote=0; wrote<(count*wordsize); wrote+=sizeof(chunk))
+	for (wrote=0; wrote<(count*wordsize); wrote += cur_size)
 	{
-		int cur_size = MIN( (count*wordsize - wrote) , 1024 );
+		cur_size = MIN( (count*wordsize - wrote), sizeof(chunk) );
 		flash_bank_t *bank;
 		bank = get_flash_bank_by_addr(target, address);
 		if(bank == NULL)
@@ -798,7 +799,6 @@
 		err = flash_driver_write(bank, chunk, address - bank->base + wrote, cur_size);
 		if (err!=ERROR_OK)
 			return err;
-		wrote += cur_size;
 	}
 
 	if ((retval = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)

However, this does not resolve the corrupt download from IAR problem.

Also the erratic flash writing behaviour persists - is seems that any FLASH writing operations (flash fill* in particular) succeeds every once out of several attempts, even if reset halt is executed immediately before flash write operations. In most cases, it spits out this:

> reset halt
JTAG tap: stm32.cpu tap/device found: 0x3ba00477 (Manufacturer: 0x23b, Part: 0xba00, Version: 0x3)
JTAG Tap/device matched
JTAG tap: stm32.bs tap/device found: 0x16410041 (Manufacturer: 0x020, Part: 0x6410, Version: 0x1)
JTAG Tap/device matched
target state: halted
target halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0xfffffffe

> flash fillw 0x08000040 0x33445566 11
flash writing failed with error code: 0xfffffc7a
error writing to flash at address 0x08000000 at offset 0x00000040 (-902)

called at file "command.c", line 456
called at file "embedded:startup.tcl", line 89
called at file "embedded:startup.tcl", line 93

>_

How can I help troubleshooting this?