EmbeddedRelated.com
Forums
Memfault State of IoT Report

Partially Erasing Flash

Started by Leighton Rowe March 14, 2007
I'm currently reviewing the Embedded Flash Controller for the
AT91SAM7S256. I understand that there's only one Erase Command (Erase
All) that 'only' erases the entire flash and cancels if any of the
pages were locked.

Does anyone know any methods/workarounds for erasing individual
pages/sectors of the flash?

Many thanks,
Leighton
Hi Leighton,

Doesn't the Flash.c file available from Atmel provide 1024 "pages" of flash
memory?

//*
//* \fn AT91F_Flash_Unlock
//* \brief Clear the lock bit and set at 1 FSR bit=0
//* \input page number (0-1023)
//* \output Region
//*-
__ramfunc int AT91F_Flash_Unlock(unsigned int Flash_Lock_Page) {
//* Write the Clear Lock Bit command
AT91C_BASE_MC->MC_FCR = AT91C_MC_CORRECT_KEY | AT91C_MC_FCMD_UNLOCK |
(AT91C_MC_PAGEN & (Flash_Lock_Page << 8));

//* Wait the end of command
AT91F_Flash_Ready();

return (AT91F_Flash_Lock_Status());
}
Alan KM6VV
> Of Leighton Rowe
>
> I'm currently reviewing the Embedded Flash Controller for the
> AT91SAM7S256. I understand that there's only one Erase Command (Erase
> All) that 'only' erases the entire flash and cancels if any of the
> pages were locked.
>
> Does anyone know any methods/workarounds for erasing individual
> pages/sectors of the flash?
>
> Many thanks,
> Leighton
>
> Yahoo! Groups Links
>
Yes, but I think it depends on the processor you're using.

i.e.

AT91SAM7S512 has 2 banks of 1024 page flash memory (256 byte each)
AT91SAM7S256 has 1024 pages of flash
AT91SAM7S128 has only 512 pages...here you might run into problems if
u try to program 1024 pages

Leighton

--- In A..., "Alan KM6VV" wrote:
>
> Hi Leighton,
>
> Doesn't the Flash.c file available from Atmel provide 1024 "pages"
of flash
> memory?
>
> //*-----------------------------
-------
> //* \fn AT91F_Flash_Unlock
> //* \brief Clear the lock bit and set at 1 FSR bit=0
> //* \input page number (0-1023)
> //* \output Region
> //*-----------------------------
--------
> __ramfunc int AT91F_Flash_Unlock(unsigned int Flash_Lock_Page) {
> //* Write the Clear Lock Bit command
> AT91C_BASE_MC->MC_FCR = AT91C_MC_CORRECT_KEY |
AT91C_MC_FCMD_UNLOCK |
> (AT91C_MC_PAGEN & (Flash_Lock_Page << 8));
>
> //* Wait the end of command
> AT91F_Flash_Ready();
>
> return (AT91F_Flash_Lock_Status());
> }
> Alan KM6VV
> > Of Leighton Rowe
> >
> > I'm currently reviewing the Embedded Flash Controller for the
> > AT91SAM7S256. I understand that there's only one Erase Command
(Erase
> > All) that 'only' erases the entire flash and cancels if any of the
> > pages were locked.
> >
> > Does anyone know any methods/workarounds for erasing individual
> > pages/sectors of the flash?
> >
> > Many thanks,
> > Leighton
> >
> >
> >
> >
> >
> > Yahoo! Groups Links
> >
> >
>
Hi,

I have some trouble with AT91SAM7SE512..and i have been looking for
the flash.c file that you mentioned in your post..could you give me
the URL for this file or upload that file to the post..
best regards
jens

--- In A..., "Alan KM6VV" wrote:
>
> Hi Leighton,
>
> Doesn't the Flash.c file available from Atmel provide 1024 "pages"
of flash
> memory?
>
> //*-----------------------------
-------
> //* \fn AT91F_Flash_Unlock
> //* \brief Clear the lock bit and set at 1 FSR bit=0
> //* \input page number (0-1023)
> //* \output Region
> //*-----------------------------
--------
> __ramfunc int AT91F_Flash_Unlock(unsigned int Flash_Lock_Page) {
> //* Write the Clear Lock Bit command
> AT91C_BASE_MC->MC_FCR = AT91C_MC_CORRECT_KEY |
AT91C_MC_FCMD_UNLOCK |
> (AT91C_MC_PAGEN & (Flash_Lock_Page << 8));
>
> //* Wait the end of command
> AT91F_Flash_Ready();
>
> return (AT91F_Flash_Lock_Status());
> }
> Alan KM6VV
> > Of Leighton Rowe
> >
> > I'm currently reviewing the Embedded Flash Controller for the
> > AT91SAM7S256. I understand that there's only one Erase Command
(Erase
> > All) that 'only' erases the entire flash and cancels if any of the
> > pages were locked.
> >
> > Does anyone know any methods/workarounds for erasing individual
> > pages/sectors of the flash?
> >
> > Many thanks,
> > Leighton
> >
> >
> >
> >
> >
> > Yahoo! Groups Links
> >
> >
>
//*----
//* ATMEL Microcontroller Software Support - ROUSSET -
//*----
//* The software is delivered "AS IS" without warranty or condition of any
//* kind, either express, implied or statutory. This includes without
//* limitation any warranty or condition with respect to merchantability or
//* fitness for any particular purpose, or against the infringements of
//* intellectual property rights of others.
//*----
//* File Name : Flash.h
//* Object : Flash constan description
//* Creation : JPP 30/Jun/2004
//*
//*----
#ifndef Flash_h
#define Flash_h

/*-------------------------------*/
/* Flash Status Field Definition */
/*-------------------------------*/

#define AT91C_MC_FSR_MVM ((unsigned int) 0xFF << 8) // (MC) Status Register GPNVMx: General-purpose NVM Bit Status
#define AT91C_MC_FSR_LOCK ((unsigned int) 0xFFFF << 16) // (MC) Status Register LOCKSx: Lock Region x Lock Status
#define ERASE_VALUE 0xFFFFFFFF

/*-----------------------*/
/* Flash size Definition */
/*-----------------------*/
/* 64 Kbytes of Internal High-speed Flash, Organized in 512 Pages of 128 Bytes */

#define FLASH_PAGE_SIZE_BYTE 256
#define FLASH_PAGE_SIZE_LONG 32

#define FLASH_LOCK_BITS_SECTOR 16
#define FLASH_SECTOR_PAGE 32
#define FLASH_LOCK_BITS 16 /* 16 lock bits, each protecting 16 sectors of 32 pages*/
#define FLASH_BASE_ADDRESS 0x00100000
/*------------------------------*/
/* External function Definition */
/*------------------------------*/

/* Flash function */
extern void AT91F_Flash_Init(void);
extern int AT91F_Flash_Check_Erase(unsigned int * start, unsigned int size);
extern int AT91F_Flash_Erase_All(void);
extern int AT91F_Flash_Write( unsigned int Flash_Address ,int size ,unsigned int * buff);
extern int AT91F_Flash_Write_all( unsigned int Flash_Address ,int size ,unsigned int * buff);

/* Lock Bits functions */
extern int AT91F_Flash_Lock_Status(void);
extern int AT91F_Flash_Lock (unsigned int Flash_Lock);
extern int AT91F_Flash_Unlock(unsigned int Flash_Lock);

/* NVM bits functions */
extern int AT91F_NVM_Status(void);
extern int AT91F_NVM_Set (unsigned char NVM_Number);
extern int AT91F_NVM_Clear(unsigned char NVM_Number);

/* Security bit function */
extern int AT91F_SET_Security_Status (void);
extern int AT91F_SET_Security (void);

#endif /* Flash_h */
Thanks Alan,

Thank you for the files...now let me test this if it
works fine with 512kbyte version of at91sam

best regards
jens
--- Alan KM6VV wrote:

> Hi Jens,
>
> Doesn't look like I can upload files!
>
> I've attached them, and copied you. This is for
> AT91SAM7S256.
>
> I didn't put it in our project, so I don't have a
> URL to the original.
>
> Alan KM6VV
> > On Behalf Of Jenish
> >
> > Hi,
> >
> > I have some trouble with AT91SAM7SE512..and i have
> been looking for
> > the flash.c file that you mentioned in your
> post..could you give me
> > the URL for this file or upload that file to the
> post..
> > best regards
> > jens
> >
> >
> >
//*----
> //* ATMEL Microcontroller Software Support
> - ROUSSET -
>
//*----
> //* The software is delivered "AS IS" without
> warranty or condition of any
> //* kind, either express, implied or statutory. This
> includes without
> //* limitation any warranty or condition with
> respect to merchantability or
> //* fitness for any particular purpose, or against
> the infringements of
> //* intellectual property rights of others.
>
//*----
> //* File Name : Flash.c
> //* Object : Flash routine
> //* Creation : JPP 30/Jun/2004
> //* Modif : JPM 16/Nov/2004 Flash
> write status
>
//*----
>
> // Include Standard files
> #include
> #include #define false 0
> #define true 1
>
//*----
> //* \fn AT91F_Flash_Init
> //* \brief Flash init
>
//*----
> void AT91F_Flash_Init (void)
> {
> //* Set number of Flash Waite sate
> // SAM7S64 features Single Cycle Access at Up
> to 30 MHz
> // if MCK = 47923200, 50 Cycles for 1 seconde
> ( field MC_FMR->FMCN)
> AT91C_BASE_MC->MC_FMR = ((AT91C_MC_FMCN)&(50
> <<16)) | AT91C_MC_FWS_1FWS ;
> }
//*----
> //* \fn AT91F_Flash_Ready
> //* \brief Wait the flash ready
>
//*----
> __ramfunc int AT91F_Flash_Ready (void)
> {
> unsigned int status;
> status = 0;
>
> //* Wait the end of command
> while ((status & AT91C_MC_FRDY) !> AT91C_MC_FRDY )
> {
> status = AT91C_BASE_MC->MC_FSR;
> }
> return status;
> }
//*----
> //* \fn AT91F_Flash_Lock_Status
> //* \brief Get the Lock bits field status
>
//*----
> __ramfunc int AT91F_Flash_Lock_Status(void)
> {
> return (AT91C_BASE_MC->MC_FSR &
> AT91C_MC_FSR_LOCK);
> }
>
//*----
> //* \fn AT91F_Flash_Lock
> //* \brief Write the lock bit and set at 0 FSR Bit > 1
> //* \input page number (0-1023)
> //* \output Region
>
//*----
> __ramfunc int AT91F_Flash_Lock (unsigned int
> Flash_Lock_Page)
> {
> //* set the Flash controller base address
> AT91PS_MC ptMC = AT91C_BASE_MC;
>
> //* write the flash
> //* Write the Set Lock Bit command
> ptMC->MC_FCR = AT91C_MC_CORRECT_KEY |
> AT91C_MC_FCMD_LOCK | (AT91C_MC_PAGEN &
> (Flash_Lock_Page << 8) ) ;
>
> //* Wait the end of command
> AT91F_Flash_Ready();
>
> return (AT91F_Flash_Lock_Status());
> }
>
//*----
> //* \fn AT91F_Flash_Unlock
> //* \brief Clear the lock bit and set at 1 FSR bit=0
> //* \input page number (0-1023)
> //* \output Region
>
//*----
> __ramfunc int AT91F_Flash_Unlock(unsigned int
> Flash_Lock_Page)
> {
> //* Write the Clear Lock Bit command
> AT91C_BASE_MC->MC_FCR = AT91C_MC_CORRECT_KEY
> | AT91C_MC_FCMD_UNLOCK | (AT91C_MC_PAGEN &
> (Flash_Lock_Page << 8) ) ;
>
> //* Wait the end of command
> AT91F_Flash_Ready();
>
> return (AT91F_Flash_Lock_Status());
> }
//*----
> //* \fn AT91F_Flash_Check_Erase
> //* \brief Check the memory at 0xFF in 32 bits
> access
>
//*----
> __ramfunc int AT91F_Flash_Check_Erase (unsigned int
> * start, unsigned int size)
> {
> unsigned int i;
> //* Check if flash is erased
> for (i=0; i < (size/4) ; i++ )
> {
> if ( start[i] != ERASE_VALUE ) return
> false;
> }
> return true ;
> }
//*----
> //* \fn AT91F_Flash_Erase_All
> //* \brief Send command erase all flash
>
//*----
> __ramfunc int AT91F_Flash_Erase_All(void)
> {
> //* set the Flash controller base address
> AT91PS_MC ptMC = AT91C_BASE_MC;
> //* Write the Erase All command
> ptMC->MC_FCR = AT91C_MC_CORRECT_KEY |
> AT91C_MC_FCMD_ERASE_ALL ;
> //* Wait the end of command
> AT91F_Flash_Ready();
> //* Check the result
> return ( (ptMC->MC_FSR & ( AT91C_MC_PROGE |
> AT91C_MC_LOCKE ))==0) ;
> }
//*----
> //* \fn AT91F_Flash_Write
> //* \brief Write in one Flash page located in
> AT91C_IFLASH, size in 32 bits
> //* \input Flash_Address: start at 0x0010 0000 size:
> in byte
>
//*----
> __ramfunc int AT91F_Flash_Write( unsigned int
> Flash_Address ,int size ,unsigned int * buff)
> {
> //* set the Flash controller base address
> AT91PS_MC ptMC = AT91C_BASE_MC;
> unsigned int i, page, status;
> unsigned int * Flash;
> //* init flash pointer
> Flash = (unsigned int *) Flash_Address;
> //* Get the Flash page number
> page = ((Flash_Address - (unsigned
> int)AT91C_IFLASH ) /FLASH_PAGE_SIZE_BYTE);
> //* copy the new value
> for (i=0; (i < FLASH_PAGE_SIZE_BYTE) & (size >
> 0) ;i++, Flash++,buff++,size-=4 ){
> //* copy the flash to the write buffer ensuring
> code generation
> *Flash=*buff;
> }
> //* Write the write page command
> ptMC->MC_FCR = AT91C_MC_CORRECT_KEY |
> AT91C_MC_FCMD_START_PROG | (AT91C_MC_PAGEN & (page
> <<8)) ;
> //* Wait the end of command
> status = AT91F_Flash_Ready();
> //* Check the result
> if ( (status & ( AT91C_MC_PROGE | AT91C_MC_LOCKE
> ))!=0) return false;
> return true;
> }
//*----
> //* \fn AT91F_Flash_Write
> //* \brief Write in one Flash page located in
> AT91C_IFLASH, size in byte
> //* \input Start address (base=AT91C_IFLASH) size
> (in byte ) and buff address
>
//*----
> __ramfunc int AT91F_Flash_Write_all( unsigned int
> Flash_Address ,int size ,unsigned int * buff)
> {
>
> int next, status;
>
=== message truncated ===>
//*----
> //* ATMEL Microcontroller Software Support
> - ROUSSET -
>
//*----
> //* The software is delivered "AS IS" without
> warranty or condition of any
> //* kind, either express, implied or statutory. This
> includes without
> //* limitation any warranty or condition with
> respect to merchantability or
> //* fitness for any particular purpose, or against
> the infringements of
> //* intellectual property rights of others.
>
//*----
> //* File Name : Flash.h
> //* Object : Flash constan description
> //* Creation : JPP 30/Jun/2004
> //*
>
//*----
> #ifndef Flash_h
> #define Flash_h
>
> /*-------------------------------*/
> /* Flash Status Field Definition */
> /*-------------------------------*/
>
> #define AT91C_MC_FSR_MVM ((unsigned int)
> 0xFF << 8) // (MC) Status Register GPNVMx:
> General-purpose NVM Bit Status
> #define AT91C_MC_FSR_LOCK ((unsigned int)
> 0xFFFF << 16) // (MC) Status Register LOCKSx: Lock
> Region x Lock Status
> #define ERASE_VALUE 0xFFFFFFFF
>
> /*-----------------------*/
> /* Flash size Definition */
> /*-----------------------*/
> /* 64 Kbytes of Internal High-speed Flash, Organized
> in 512 Pages of 128 Bytes */
>
> #define FLASH_PAGE_SIZE_BYTE 256
> #define FLASH_PAGE_SIZE_LONG 32
>
> #define FLASH_LOCK_BITS_SECTOR 16
> #define FLASH_SECTOR_PAGE 32
> #define FLASH_LOCK_BITS 16 /* 16 lock
> bits, each protecting 16 sectors of 32 pages*/
> #define FLASH_BASE_ADDRESS 0x00100000
> /*------------------------------*/
> /* External function Definition */
> /*------------------------------*/
>
> /* Flash function */
> extern void AT91F_Flash_Init(void);
> extern int AT91F_Flash_Check_Erase(unsigned int *
> start, unsigned int size);
> extern int AT91F_Flash_Erase_All(void);
> extern int AT91F_Flash_Write( unsigned int
> Flash_Address ,int size ,unsigned int * buff);
> extern int AT91F_Flash_Write_all( unsigned int
> Flash_Address ,int size ,unsigned int * buff);
>
> /* Lock Bits functions */
> extern int AT91F_Flash_Lock_Status(void);
> extern int AT91F_Flash_Lock (unsigned int
> Flash_Lock);
> extern int AT91F_Flash_Unlock(unsigned int
> Flash_Lock);
>
> /* NVM bits functions */
> extern int AT91F_NVM_Status(void);
> extern int AT91F_NVM_Set (unsigned char NVM_Number);
> extern int AT91F_NVM_Clear(unsigned char
> NVM_Number);
>
> /* Security bit function */
> extern int AT91F_SET_Security_Status (void);
> extern int AT91F_SET_Security (void);
>
> #endif /* Flash_h */
>

____________________________________________________________________________________
Don't pick lemons.
See all the new 2007 cars at Yahoo! Autos.
http://autos.yahoo.com/new_cars.html

Memfault State of IoT Report