EmbeddedRelated.com
Forums

Writing in flash in run time

Started by Ananth kumar August 18, 2005
Hello All,


How to write in flash memory when code is in running mode?
(not using the JTAG)

Can anyone send me the application notes for "In-application 
programming with MSP430"?


Thanks in advance,
Ananth Kumar






Beginning Microcontrollers with the MSP430

>From: Ananth kumar <s_ananth_kumar@s_an...>
>Subject: [msp430] Writing in flash in run time
>
>How to write in flash memory when code is in running mode?
>(not using the JTAG)
>
>Can anyone send me the application notes for "In-application 
>programming with MSP430"?
>

look at www.ti.com/

navigate to the MSP430 section and look for the Design Resources - Code
Examples.

under IAR examples there's the MSP-FET430P120 selection. in it,
there's the file: FET120_flash_write.c

Its header includes:
//MSP-FET430P120 Demo - Flash In-System Programming, Copy SegA to SegB
//
//  Description: This program first erases flash seg A, then it increments all
//  values in seg A, then it erases seg B, then  copies seg A to seg B.
//  Assumed MCLK 550kHz - 900kHz.

This might be what you want.

Try reading the manual, too. There's a whole chapter about the FLASH
system.

Rachel Adamec
Norristown, PA


Below you'll find the code I use to
* Erase entire infomemory
* Read/Write single bytes from/to infomemory
* Read/Write Doubles from/to infomemory

I hope you find this useful.  You will likely have to check out all of 
the setup parameters (especially flash clock) in order to make it work 
well on your system.

-Adam

const unsigned char * INFOMEM = (unsigned char *) 0x1000;

// Erase entire information memory
int erase(void)
{
    unsigned char * segment;
    segment = (unsigned char *) INFOMEM;
   
    FCTL2 = FWKEY + FSSEL_1 + FN0 + FN1; // Select MCLK/3 (1.04MHz / 3)

    FCTL3 = FWKEY; // Unlock flash memory
    FCTL1 = FWKEY + ERASE; // Set erase segment mode   
    *segment = 0; // Dummy write to segment we are erasing
    while((FCTL3 & WAIT) != WAIT);
 
    segment += 128; // Point to next segment
     FCTL3 = FWKEY; // Unlock flash memory
    FCTL1 = FWKEY + ERASE; // Set erase segment mode   
    *segment = 0; // Dummy write to segment we are erasing
    while((FCTL3 & WAIT) != WAIT); // Wait for erase to complete

    FCTL3 = FWKEY + LOCK; // Lock flash memory
    return 1;
}

// Save a byte to the information memory
int save(char data, unsigned char address)
{
    char * segment;
    segment = (char *) INFOMEM;
   
    FCTL2 = FWKEY + FSSEL_1 + FN0 + FN1; // Select MCLK/3 (1.04MHz / 3)

    FCTL3 = FWKEY; // Unlock flash memory
    FCTL1 = FWKEY + WRT; // Set write byte mode   
    segment[address] = data; // Write byte
    while((FCTL3 & WAIT) != WAIT); // Wait for write to complete
    FCTL3 = FWKEY + LOCK; // Lock flash memory
    return address;
}

double readdouble(int offset)
{// Read a double from infomem
    double data;
    int i;
    char * charptr;
    charptr = (char *) &data;
   
    for(i=0;i<4;i++)
    {
        charptr[i] = read(i + (offset * 4));
    }           
    return data;   
}

int savedouble(double data, int offset)
{// Save a double into infomem
    char * charptr;
    int i;
    charptr = (char *) &data;
   
    for(i=0;i<4;i++)
    {
        save(charptr[i], i + (offset * 4));
    }           
    return 0;   
}

char read(unsigned char address)
{
    char * segment;
    segment = (char *) INFOMEM;
    return segment[address];
}

Ananth kumar wrote:

>Hello All,
>
>
>How to write in flash memory when code is in running mode?
>(not using the JTAG)
>
>Can anyone send me the application notes for "In-application 
>programming with MSP430"?
>
>
>Thanks in advance,
>Ananth Kumar
>
>
>
>
>
>
>
>
>.
>
> 
>Yahoo! Groups Links
>
>
>
> 
>
>
>
>
>
>  
>

Hi,
can someone explain to me what the purpose of 
;i < 4; and (offset * 4) in the code block

     for(i=0;i<4;i++)
     {
         save(charptr[i], i + (offset * 4));
     }
found in readdouble and savedouble functions?

would it be any different if i want to write an int?

thanks in advance

John



--- In msp430@msp4..., "M. Adam Davis" <adavis@...> wrote:
>
> Below you'll find the code I use to
> * Erase entire infomemory
> * Read/Write single bytes from/to infomemory
> * Read/Write Doubles from/to infomemory
> 
> I hope you find this useful.  You will likely have to check out 
all of 
> the setup parameters (especially flash clock) in
order to make it 
work 
> well on your system.
> 
> -Adam
> 
> const unsigned char * INFOMEM = (unsigned char *) 0x1000;
> 
> // Erase entire information memory
> int erase(void)
> {
>     unsigned char * segment;
>     segment = (unsigned char *) INFOMEM;
>    
>     FCTL2 = FWKEY + FSSEL_1 + FN0 + FN1; // Select MCLK/3 
(1.04MHz / 3)
> 
>     FCTL3 = FWKEY; // Unlock flash memory
>     FCTL1 = FWKEY + ERASE; // Set erase segment mode   
>     *segment = 0; // Dummy write to segment we are erasing
>     while((FCTL3 & WAIT) != WAIT);
>  
>     segment += 128; // Point to next segment
>      FCTL3 = FWKEY; // Unlock flash memory
>     FCTL1 = FWKEY + ERASE; // Set erase segment mode   
>     *segment = 0; // Dummy write to segment we are erasing
>     while((FCTL3 & WAIT) != WAIT); // Wait for erase to complete
> 
>     FCTL3 = FWKEY + LOCK; // Lock flash memory
>     return 1;
> }
> 
> // Save a byte to the information memory
> int save(char data, unsigned char address)
> {
>     char * segment;
>     segment = (char *) INFOMEM;
>    
>     FCTL2 = FWKEY + FSSEL_1 + FN0 + FN1; // Select MCLK/3 
(1.04MHz / 3)
> 
>     FCTL3 = FWKEY; // Unlock flash memory
>     FCTL1 = FWKEY + WRT; // Set write byte mode   
>     segment[address] = data; // Write byte
>     while((FCTL3 & WAIT) != WAIT); // Wait for write to complete
>     FCTL3 = FWKEY + LOCK; // Lock flash memory
>     return address;
> }
> 
> double readdouble(int offset)
> {// Read a double from infomem
>     double data;
>     int i;
>     char * charptr;
>     charptr = (char *) &data;
>    
>     for(i=0;i<4;i++)
>     {
>         charptr[i] = read(i + (offset * 4));
>     }           
>     return data;   
> }
> 
> int savedouble(double data, int offset)
> {// Save a double into infomem
>     char * charptr;
>     int i;
>     charptr = (char *) &data;
>    
>     for(i=0;i<4;i++)
>     {
>         save(charptr[i], i + (offset * 4));
>     }           
>     return 0;   
> }
> 
> char read(unsigned char address)
> {
>     char * segment;
>     segment = (char *) INFOMEM;
>     return segment[address];
> }
> 
> Ananth kumar wrote:
> 
> >Hello All,
> >
> >
> >How to write in flash memory when code is in running mode?
> >(not using the JTAG)
> >
> >Can anyone send me the application notes for "In-application 
> >programming with MSP430"?
> >
> >
> >Thanks in advance,
> >Ananth Kumar
> >
> >
> >
> >
> >
> >
> >
> >
> >.
> >
> > 
> >Yahoo! Groups Links
> >
> >
> >
> > 
> >
> >
> >
> >
> >
> >  
> >
>