SAM3S4C flash read

Discussion around product based on ARM Cortex M3 core.

Moderators: nferre, ncollot

entik
Posts: 17
Joined: Tue Mar 13, 2012 5:19 pm

SAM3S4C flash read

Fri Apr 24, 2015 4:00 pm

I have bootloader and program configuration, bootloader code is in flash at position 0x400000, max size 0x4000, main program at 0x00404000 with max size 0x3C000 (values taken from scatter files).
The last flash page of the bootloader (address 0x00404000-256) is used to store some information about the device. Both bootloader and main program should have RW access. Bootloader works fine, but main program is not able to access the page even for read. Following code causes program to stuck.

Code: Select all


#define AT91C_IFLASH_PROG  	(0x00404000)
#define IFLASH_PAGE_SIZE              	(256)

	uint32_t* info = (uint32_t*)(AT91C_IFLASH_PROG-IFLASH_PAGE_SIZE);	
	uint8_t i;
	
	for ( i = 0; i < IFLASH_PAGE_SIZE/4; i++ )
	{
		mFWInfoPage[i] = info[i];
	}
I am not sure what is actually going on in the code, as this happens only when I use bootloader to write main program and then call it. When I load only main program to the chip and try to debug it, it works fine.

I'll be grateful for any ideas.

Thanks
Petr
entik
Posts: 17
Joined: Tue Mar 13, 2012 5:19 pm

Re: SAM3S4C flash read

Thu Apr 30, 2015 11:39 am

I have been playing a bit with the configuration and I found out some interesting details, which does not make sense to me, but I hope somebody would be able to figure out what is going on. I'll start from scratch, I have SAM3S4C uP, with bootloader/main program setup.
Bootloader is uploaded using JTAG, and resides in first 63 pages (0-62) of the memory. Scatter file created from the Keil memory dialog:

Code: Select all

LR_IROM1 0x00400000 0x00003F00  {    ; load region size_region
  ER_IROM1 0x00400000 0x00003F00  {  ; load address = execution address
   *.o (RESET, +First)
   *(InRoot$$Sections)
   .ANY (+RO)
  }
  RW_IRAM1 0x20000000 0x0000C000  {  ; RW data
   .ANY (+RW +ZI)
  }
}
Page 63 is used to store some information about the device - serial number, device ID, some user setup. It should be accessible by both bootloader and main program for read/write.

Pages 64 and on are reserved for main program, which is written into this section by bootloader. Scatter file:

Code: Select all

LR_IROM1 0x00404000 0x0003C000  {    ; load region size_region
  ER_IROM1 0x00404000 0x0003C000  {  ; load address = execution address
   *.o (RESET, +First)
   *(InRoot$$Sections)
   .ANY (+RO)
  }
  RW_IRAM1 0x20000000 0x0000C000  {  ; RW data
   .ANY (+RW +ZI)
  }
}
At the startup, both bootloader and main program read the page 63 from the flash to the mFWInfoPage variable, using following code (common for both):

Code: Select all

#define IFLASH_PAGE_SIZE (256) 
#define AT91C_IFLASH (0x400000)
#define FLASH_FWINFO_PAGE 63

//important constant - explained later
#define IPAGE_ADDR_DEV_ADDR 20 

uint32_t mFWInfoPage[IFLASH_PAGE_SIZE/4];	

uint32_t* info = (uint32_t*)(AT91C_IFLASH+FLASH_FWINFO_PAGE*IFLASH_PAGE_SIZE);	
uint8_t i,j;
	
for ( i = 0; i < IFLASH_PAGE_SIZE/4; i++ )
{
	mFWInfoPage[i] = info[i];
}

This code runs fine for the bootloader, but it does not finish for the main program. Some interesting things I noticed/tried:
-it does not help if I move the read part of the code to the RAM
-the code got stuck always on the same index, defined by this directive #define IPAGE_ADDR_DEV_ADDR in the header. If I skip this index, it works

Code: Select all

for ( i = 0; i < IFLASH_PAGE_SIZE/4; i++ )
{
	if ( i != IPAGE_ADDR_DEV_ADDR )
	{
		mFWInfoPage[i] = info[i];
	}
}
-I have to write first to this index and save the mFWInfoPage to the flash; as long as I just read it from flash, it works fine
-I can read/write any other index of the same flash page and it works fine
-the very same code works for another device, the only difference is that the other device is not using this particular index (IPAGE_ADDR_DEV_ADDR) anywhere else in the code

So, the chain of actions leading to this issue is upload bootloader - upload main program - use the device at your will, turn it off, reupload the main program, write values to other indexes and save the whole page to flash or whatever else - write to this particular index and save the page to the flash - got stuck on the startup readout forever. 

I'll be grateful for any hint, as this issue is driving me crazy.
Petr

Return to “SAM3 Cortex-M3 MCU”

Who is online

Users browsing this forum: No registered users and 1 guest