cancel
Showing results for 
Search instead for 
Did you mean: 

Flash programming error

AKim.15
Associate II

Hello,

I am verifying my flash write program.

The first writing is ok, but the error occurred from the second writing.

The following is my flash write program.

int cmd_flash_write(void) 
{
	uint32_t s_addr;
	uint32_t data[512];
	uint32_t len, i, status;
 
	s_addr = atoi(argv[1]);
	len = atoi(argv[2]);
 
	/* Random data */
	for(i = 0; i < len; i++) {
		data[i] = rand() % 0xffffffff;
	}
 
	FLASH_Unlock(); 
	status = FLASH_If_Write(&s_addr, (U32*)&data[0] ,len);
	FLASH_Lock();
 
	Uart_Printf("status : %d\n", status);
	
	return TRUE;
}
U32 FLASH_If_Write(__IO U32* FlashAddress, U32* Data ,U16 DataLength){
	U32 i = 0;
	U32 a, b;
 
	//dbg_printf("FlashAddress : 0x%.8x 0x%.8x %d\n", *FlashAddress, *Data, DataLength);
	for (i = 0; (i < DataLength) && (*FlashAddress <= (USER_FLASH_END_ADDRESS-4)); i++){
		if((*FlashAddress>=BOOTLOADER_START_ADDRESS) && (*FlashAddress<=BOOTLOADER_END_ADDRESS)) { //protect the boot area
			dbg_printf("ADDRESS : 0x%.8x Error\n", *FlashAddress);
			return (1);
		}
		
		/* Device voltage range supposed to be [2.7V to 3.6V], the operation will
		be done by word */ 
		if (FLASH_ProgramWord(*FlashAddress, *(U32*)(Data+i)) == FLASH_COMPLETE){
			a = *(__IO U32*)*FlashAddress;
			b = *(U32*)(Data+i);
 
			/* Check the written value */
			if (a != b){
				/* Flash content doesn't match SRAM content */
				return(2);
			}
 
			/* Increment FLASH destination address */
			*FlashAddress += 4;
		}
		else{
			/* Error occurred while writing data in Flash memory */
			return (1);
		}
	}
 
	return (0);
}

The return value of the FLASH_If_Write() function is '2'.

It means that the written data is different from the read data.

Why does this difference occur from the second time?

Thanks

This discussion is locked. Please start a new topic to ask your question.
2 REPLIES 2

>>Why does this difference occur from the second time?

Where do you set a value for the flash address? Value in s_addr never set.

You only get to write flash once, then you need to erase it.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
AKim.15
Associate II

Thanks for your answer, Clive