EmbeddedRelated.com
Forums
The 2024 Embedded Online Conference

ask about AT91SAM7S256 flash memory

Started by michael angelo February 28, 2008
Dear all,
About AT91SAM7S256, How do I use the internal flash memory as an EEPROM data storage ? Anybody know how to read and write the flash memory area ?

Thanks a lots for any attention

Regards
Fayes

---------------------------------
Never miss a thing. Make Yahoo your homepage.
Hi

reading:
flash is always mapped at base address 0x100000. You can read it using
standard load instructions.

writing:
see the datasheet
(http://www.atmel.com/dyn/resources/prod_documents/doc6175.pdf), chapter 20 -
embedded flash controller.

Pavel
Dne Thursday 28 of February 2008 23:50:31 michael angelo napsal(a):
> Dear all,
> About AT91SAM7S256, How do I use the internal flash memory as an EEPROM
> data storage ? Anybody know how to read and write the flash memory area ?
>
> Thanks a lots for any attention
>
> Regards
> Fayes
>
> ---------------------------------
> Never miss a thing. Make Yahoo your homepage.
I've done just this and it works quite well. Some things to watch for that I learned the hard
ware are:

(1) declare the area of storage as unsigned int storage[nnn] @ "segment" (IAR compiler)
and define 'segment' in your linker to a fixed location in flash that will always remain the
same

(2) reading - easy, just get &storage[0] as a pointer

(3) writing - need to have some ram functions (__ramfunc in IAR) to do the actual write as
you can't execute from flash and write flash at the same time!

(4) writing - per the data sheet. Here is some code that I wrote that should get you
started....

// ------------------
// ----- Setup flash wait states for programming flash.
// ------------------
__ramfunc void AT91FlashInit(void)
{
// set wait states and number of MCK cycles in 1.5 usecs
AT91C_BASE_MC->MC_FMR = (80 << 16) | AT91C_MC_FWS_3FWS;
}
// ------------------
// ----- Wait until the flash is ready.
// ------------------
__ramfunc unsigned int AT91FlashReady(void)
{
uint32 fr_status;
fr_status = 0;

// Wait for end of command
while((fr_status & AT91C_MC_FRDY) != AT91C_MC_FRDY)
{
fr_status = AT91C_BASE_MC->MC_FSR;
}

return fr_status;
}
//-------------------------------
------
// Write Flash pages starting at AT91C_IFLASH, size in bytes.
//
// Parameters: fw_flash_addr -> flash address to start writing to
// fw_size -> Number of bytes to write [must be multiple of 4]
// fw_buff -> Buffer to write from
//-------------------------------
------
__ramfunc int AT91FlashWrite(unsigned int fw_flash_addr, int fw_size, unsigned int
*fw_buff)
{
unsigned int fw_i;
unsigned int fw_page;
unsigned int fw_status;
unsigned int *fw_flash;

// init flash pointer (must be an integer boundary)
fw_flash = (unsigned int *) (fw_flash_addr & ~3);

AT91FlashInit();
DisableInterrupts();

while (fw_size > 0) {

// Get the Flash page number
fw_page = ((fw_flash_addr - (unsigned int)AT91C_IFLASH ) /
FLASH_PAGE_SIZE_BYTES);

// copy the new value
for (fw_i=0; (fw_i < (FLASH_PAGE_SIZE_BYTES/4)) & (fw_size > 0); fw_i++)
{
// copy the new flash contents to the write buffer
*fw_flash++=*fw_buff++;
fw_size -= 4;
}

// Send the 'write page' command
AT91C_BASE_MC->MC_FCR = AT91C_MC_CORRECT_KEY |
AT91C_MC_FCMD_START_PROG | (AT91C_MC_PAGEN & (fw_page << 8));

// Wait for end of command
fw_status = AT91FlashReady();

// having written one page, decrement size by 1 page and only
// exit WHILE after all pages have been written.
if (fw_size > FLASH_PAGE_SIZE_BYTES) {
// more pages still to write
fw_size -= FLASH_PAGE_SIZE_BYTES;
fw_flash_addr += FLASH_PAGE_SIZE_BYTES;
} else {
// finished!
fw_size = 0;
}

}

EnableInterrupts();
// Check the result (of last page)
if ((fw_status & (AT91C_MC_PROGE | AT91C_MC_LOCKE)) != 0)
return false;
else
return true;
}

-steve-

--- In A..., michael angelo wrote:
>
> Dear all,
> About AT91SAM7S256, How do I use the internal flash memory as an EEPROM data
storage ? Anybody know how to read and write the flash memory area ?
>
> Thanks a lots for any attention
>
> Regards
> Fayes

The 2024 Embedded Online Conference