EmbeddedRelated.com
Forums

Flash Programming

Started by rbdi...@gmail.com March 17, 2009
Hi I am very new to microcontrollers. Now my assignment is to write a program to store data on the flash memory of the controller. I am using AT91SAM7s128. Can any one pls send me the code.
On Tue, Mar 17, 2009 at 7:59 AM, wrote:
> Hi I am very new to microcontrollers. Now my assignment is to write a
> program to store data on the flash memory of the controller. I am using
> AT91SAM7s128. Can any one pls send me the code.
>
Hi r...@gmail.com,
why don't you look on the Atmel site for some info?
The "AT91SAM7S-EK Software Package for IAR 5.2, Keil and GNU" that you
can find on the AT91SAM7S-EK page
(http://www.atmel.com/dyn/products/tools_card.asp?tool_id784)
include some cute sample, like the one named:
"basic-internalflash-project-at91sam7s-ek"

Which:

/// !!!Description
///
/// The program performs the following set of commands:
/// - First, it unlocks the whole %flash
/// - It writes a pattern in the last page of the %flash, and checks it back
/// - It locks the last region of the %flash, and verifies that the status is
/// updated accordingly
/// - It unlocks the last region and verifies that it the status is updated
/// accordingly
/// - Lastly, GPNVM 1 is cleared and set, the status being checked for update
/// between the operations (the original state of the GPNVM is restored
/// after this step)
Have a nice day and keep up with your homework!

Regards
Pietro
r...@gmail.com wrote:
> Hi I am very new to microcontrollers. Now my assignment is to write
> a program to store data on the flash memory of the controller. I am
> using AT91SAM7s128. Can any one pls send me the code.

Here is a link that might help. I put it on this
newsgroup once a year in the spring and it's
surprisingly useful:



Ralph

Hi ,
i am stating at the cost of being misunderstood !!

Kindly use this forum to clear your doubts and learn as much as possible...

it is my earnest request not to use this forum to get univ assignments(or
something similiar ) done !!

Go ahead get your hands dirty ... ppl are here to help you out in case you
need any !!

Hope you got my point ...

regards,
som

On Tue, Mar 17, 2009 at 12:29 PM, wrote:

> Hi I am very new to microcontrollers. Now my assignment is to write a
> program to store data on the flash memory of the controller. I am using
> AT91SAM7s128. Can any one pls send me the code.
>
>
Hi,

I have similar problem. I posted it on at91 forum but still no answer.

http://www.at91.com/samphpbb/viewtopic.php?f&tU42

I have a problem when trying to write to flash on a AT91SAM7X256. I'm
running code from RAM and all interrupts are disabled. My oscillator frequency is 18.432MHz and MCK is 47923200 Hz. I have read all posts on forum concerning flash read/write but I'm still can't get flash working.

I'm trying to write 0x12345678 at address 0x0013FF00 (last page of FLASH). It seems like my flash read is working. So far, whatever value I write, I get 0xFFFFFFFF (default value after FLASH erase). I always get "Programming Error" message so it looks like I didn't configure FCR register on the right way.

I entered the write protection key 0x5A so this part should be OK. If I try to start page programming in the same line where I entered write protection key and page number in FCR register, program stops.

Every help will be appreciated.
void flashInit(void) {
AT91C_BASE_MC->MC_FMR = ((AT91C_MC_FMCN) & (72 << 16)) | AT91C_MC_FWS_1FWS ; // Number Of MCK Cylcles in 1.5 us; 1.5 * 48 = 72
}

void flashWrite(void) {
unsigned long flashaddr = 0x0; // beginning of write buffer
unsigned long *wrptr = flashaddr;
unsigned long status, temp;

*wrptr = 0x123456789;

AT91C_BASE_MC->MC_FMR &= ~AT91C_MC_NEBP; // No Erase Before Programming

AT91C_BASE_MC->MC_FCR = ((0x5A << 24) & AT91C_MC_KEY) | ((1023 << 8) & AT91C_MC_PAGEN);

AT91C_BASE_MC->MC_FCR = AT91C_MC_FCMD_START_PROG;

status = AT91C_BASE_MC->MC_FSR;

if(status & (0x1 << 2))
putc("Lock Error\n");

if(status & (0x1 << 3))
putc("Programming Error\n");

while(!(AT91C_BASE_MC->MC_FSR & AT91C_MC_FRDY));

}

void flashRead() {
int i;
unsigned long *flashaddr = 0x0013FF00;
nprintf(*flashaddr); putchar('\n');

Call from main:

flashInit();
flashWritePage();
flashRead();

You're not properly initialising your pointer in the Write function.
Your code currently prepares your pointer to write to 0...

Try :

void flashWrite(void) {
unsigned long flashaddr = 0x0; // beginning of write buffer
unsigned long *wrptr = &flashaddr;

Don't forget your "address-of" operator when you init a pointer this way !!!

HTH
Best regards,
Kris

On Wed, 2009-03-18 at 08:08 +0000, tpuda wrote:
> Hi,
>
> I have similar problem. I posted it on at91 forum but still no answer.
>
> http://www.at91.com/samphpbb/viewtopic.php?f&tU42
>
> I have a problem when trying to write to flash on a AT91SAM7X256. I'm
> running code from RAM and all interrupts are disabled. My oscillator frequency is 18.432MHz and MCK is 47923200 Hz. I have read all posts on forum concerning flash read/write but I'm still can't get flash working.
>
> I'm trying to write 0x12345678 at address 0x0013FF00 (last page of FLASH). It seems like my flash read is working. So far, whatever value I write, I get 0xFFFFFFFF (default value after FLASH erase). I always get "Programming Error" message so it looks like I didn't configure FCR register on the right way.
>
> I entered the write protection key 0x5A so this part should be OK. If I try to start page programming in the same line where I entered write protection key and page number in FCR register, program stops.
>
> Every help will be appreciated.
> void flashInit(void) {
> AT91C_BASE_MC->MC_FMR = ((AT91C_MC_FMCN) & (72 << 16)) | AT91C_MC_FWS_1FWS ; // Number Of MCK Cylcles in 1.5 us; 1.5 * 48 = 72
> }
>
> void flashWrite(void) {
> unsigned long flashaddr = 0x0; // beginning of write buffer
> unsigned long *wrptr = flashaddr;
> unsigned long status, temp;
>
> *wrptr = 0x123456789;
>
> AT91C_BASE_MC->MC_FMR &= ~AT91C_MC_NEBP; // No Erase Before Programming
>
> AT91C_BASE_MC->MC_FCR = ((0x5A << 24) & AT91C_MC_KEY) | ((1023 << 8) & AT91C_MC_PAGEN);
>
>
> AT91C_BASE_MC->MC_FCR = AT91C_MC_FCMD_START_PROG;
>
> status = AT91C_BASE_MC->MC_FSR;
>
> if(status & (0x1 << 2))
> putc("Lock Error\n");
>
> if(status & (0x1 << 3))
> putc("Programming Error\n");
>
>
> while(!(AT91C_BASE_MC->MC_FSR & AT91C_MC_FRDY));
>
> }
>
> void flashRead() {
> int i;
> unsigned long *flashaddr = 0x0013FF00;
> nprintf(*flashaddr); putchar('\n');
>
> Call from main:
>
> flashInit();
> flashWritePage();
> flashRead();
>
>
I have only worked with the internal flash of the at91sam7se but taking a look from the AT91SAM7X256 datasheet, they seem to be similar. So comparing my code to yours, here are my observations/suggestions:

1.) You need checking of status bits after issuing flash commands and not executing another command if the flash controller is still busy processing the old command.
2.) You need to properly configure the FMCN field.
3.) AT91C_MC_FCMD_ START_PROG should include the proper page number and KEY

If you want to know more of my code, register to www.sharingbrain.com and post your problem there. It's a very new forum and no members yet, I created it as a way for people to share their knowledge to others. I'm also inviting other members to join the forum there and post/share their knowledge.
--- On Wed, 3/18/09, tpuda wrote:
From: tpuda
Subject: [AT91SAM] Re: Flash Programming
To: A...
Date: Wednesday, March 18, 2009, 4:08 PM

Hi,

I have similar problem. I posted it on at91 forum but still no answer.

http://www.at91. com/samphpbb/ viewtopic. php?f& tU42

I have a problem when trying to write to flash on a AT91SAM7X256. I'm

running code from RAM and all interrupts are disabled. My oscillator frequency is 18.432MHz and MCK is 47923200 Hz. I have read all posts on forum concerning flash read/write but I'm still can't get flash working.

I'm trying to write 0x12345678 at address 0x0013FF00 (last page of FLASH). It seems like my flash read is working. So far, whatever value I write, I get 0xFFFFFFFF (default value after FLASH erase). I always get "Programming Error" message so it looks like I didn't configure FCR register on the right way.

I entered the write protection key 0x5A so this part should be OK. If I try to start page programming in the same line where I entered write protection key and page number in FCR register, program stops.

Every help will be appreciated.

void flashInit(void) {

AT91C_BASE_MC- >MC_FMR = ((AT91C_MC_FMCN) & (72 << 16)) | AT91C_MC_FWS_ 1FWS ; // Number Of MCK Cylcles in 1.5 us; 1.5 * 48 = 72

}

void flashWrite(void) {

unsigned long flashaddr = 0x0; // beginning of write buffer

unsigned long *wrptr = flashaddr;

unsigned long status, temp;

*wrptr = 0x123456789;

AT91C_BASE_MC- >MC_FMR &= ~AT91C_MC_NEBP; // No Erase Before Programming

AT91C_BASE_MC- >MC_FCR = ((0x5A << 24) & AT91C_MC_KEY) | ((1023 << 8) & AT91C_MC_PAGEN) ;

AT91C_BASE_MC- >MC_FCR = AT91C_MC_FCMD_ START_PROG;

status = AT91C_BASE_MC- >MC_FSR;

if(status & (0x1 << 2))

putc("Lock Error\n");

if(status & (0x1 << 3))

putc("Programming Error\n");

while(!(AT91C_ BASE_MC-> MC_FSR & AT91C_MC_FRDY) );

}

void flashRead() {

int i;

unsigned long *flashaddr = 0x0013FF00;

nprintf(*flashaddr) ; putchar('\n' );

Call from main:

flashInit();

flashWritePage( );

flashRead();
Thanks, but still no progress. I have modified my flash write function. Now im pretty that I'm writing on right address. My program stops after "start programming" command.

void flashWrite(void) {
unsigned long flashaddr = 0x0013FF00;
unsigned long *wrptr;
unsigned long status;

wrptr = flashaddr;
*wrptr = 0x12345678;

AT91C_BASE_MC->MC_FMR &= ~AT91C_MC_NEBP; // No Erase Before Programming

while(!(AT91C_BASE_MC->MC_FSR & AT91C_MC_FRDY)); // check if FRDY flag is set
putc("FRDY flag set; executing next instruction\n");

AT91C_BASE_MC->MC_FCR = ((0x5A << 24) & AT91C_MC_KEY) | ((1023 << 8) & AT91C_MC_PAGEN) | AT91C_MC_FCMD_START_PROG;

status = AT91C_BASE_MC->MC_FSR;

if(status & (0x1 << 2))
putc("Lock Error\n");

if(status & (0x1 << 3))
putc("Programming Error\n");

while(!(AT91C_BASE_MC->MC_FSR & AT91C_MC_FRDY));
}
--- In A..., Microbit_Ubuntu wrote:
>
> You're not properly initialising your pointer in the Write function.
> Your code currently prepares your pointer to write to 0...
>
> Try :
>
> void flashWrite(void) {
> unsigned long flashaddr = 0x0; // beginning of write buffer
> unsigned long *wrptr = &flashaddr;
>
> Don't forget your "address-of" operator when you init a pointer this way !!!
>
> HTH
> Best regards,
> Kris
>

I updated my code on AT91 forum. Please check it out.

http://www.at91.com/samphpbb/viewtopic.php?f&tU42&p341#p16341

Thanks louie but I have already change my code and posted it again. I posted it on SharingBrain.com. Here is an update.
I have a problem when trying to write on internal flash of AT91SAM7X256. All interrupts are disabled and I'm only using USART to check results. My oscillator frequency is 18.432MHz and MCK is 47923200 Hz. I have read all posts on forum concerning flash read/write but I'm still can't get flash working.

I'm trying to write 0x12345678 at address 0x0013FF00 (last page of FLASH). It seems like my flash read is working. So far, whatever value I try to write my program stops after "start programming" command. It is the same for other flash address locations. If I set the wrong write protection key, I get Programming Error message (PROGE flag) and 0xFFFFFFFF after flashRead is executed. If i split "start programming" command in two parts, I get same result. I also get the same result when I remove "No Erase Before Programming" part of code.

Part of code that executes program but Programming Error occurs:





Call from main:

--- In A..., louie ouano wrote:
>
> I have only worked with the internal flash of the at91sam7se but taking a look from the AT91SAM7X256 datasheet, they seem to be similar. So comparing my code to yours, here are my observations/suggestions:
>
> 1.) You need checking of status bits after issuing flash commands and not executing another command if the flash controller is still busy processing the old command.
> 2.) You need to properly configure the FMCN field.
> 3.) AT91C_MC_FCMD_ START_PROG should include the proper page number and KEY
>
> If you want to know more of my code, register to www.sharingbrain.com and post your problem there. It's a very new forum and no members yet, I created it as a way for people to share their knowledge to others. I'm also inviting other members to join the forum there and post/share their knowledge.