Writing an int to Flash

Started by dasGnu July 11, 2008
Hello,

I'm trying to store an int in Flash.... and reading it back after switching the device (a MSP430F2274) on.

"int command_time;" - is a global declared integer
"Flash_Settings" is specified in lnk_msp430f2274.cmd as "Flash_Settings = 0x10C0;"
and within the module as "extern volatile char Flash_Settings[2];"

The function to write is:

void settings_to_flash(){

_disable_interrupts();

BCSCTL1 = CALBC1_1MHZ; // Set DCO to 1MHz
DCOCTL = CALDCO_1MHZ;
FCTL2 = FWKEY + FSSEL0 + FN1; // MCLK/3 for Flash Timing Generator
FCTL3 = FWKEY + LOCKA; // Clear LOCK & LOCKA bits
FCTL1 = FWKEY + WRT; // Set WRT bit for write operation
// write int to Flash
Flash_Settings[0] = (command_time>>8) & 0xFF;
Flash_Settings[1] = command_time & 0xFF;
FCTL1 = FWKEY; // Clear WRT bit
FCTL3 = FWKEY + LOCKA + LOCK; // Set LOCK & LOCKA bit

init_dco(); // Set DCO back to working value

_enable_interrupts();
}

when reading back the int I never get the right values....?

Reading is done as:
command_time = Flash_Settings[0];
command_time |= Flash_Settings[1] >> 8;

What am I doing wrong? First I thought my shifting is wrong, but while debugging the values read from Flash are totally incorrect.
Maybe someone can help me out. Thanks,

Mitch
__________________________________________________________
Gesendet von Yahoo! Mail.
Dem pfiffigeren Posteingang.
http://de.overview.mail.yahoo.com


One thing I see offhand, that you are setting the LOCKA bit where you are commenting that you are clearing it...if 0x10C0 is in INFO A on the device in question, that's at least part of the problem.

This is code for a 169, that works. Note lack of a LOCKA bit.

void WriteToFlash(char *addr, char *block,int len)
{
_DINT();
FCTL1 = FWKEY + WRT;
FCTL3 = FWKEY; // Clear Lock bit
memcpy(addr,block,len);
FCTL1 = FWKEY; // Clear Erase bit
FCTL3 = FWKEY + LOCK; // Reset LOCK bit
_EINT();
}
Hello,
>
>I'm trying to store an int in Flash.... and reading it back after switching the device (a MSP430F2274) on.
>
>"int command_time;" - is a global declared integer
>"Flash_Settings" is specified in lnk_msp430f2274.cmd as "Flash_Settings = 0x10C0;"
>and within the module as "extern volatile char Flash_Settings[2];"
>
>The function to write is:
>
>void settings_to_flash(){
>
> _disable_interrupts();
>
> BCSCTL1 = CALBC1_1MHZ; // Set DCO to 1MHz
> DCOCTL = CALDCO_1MHZ;
> FCTL2 = FWKEY + FSSEL0 + FN1; // MCLK/3 for Flash Timing Generator
> FCTL3 = FWKEY + LOCKA; // Clear LOCK & LOCKA bits
> FCTL1 = FWKEY + WRT; // Set WRT bit for write operation
> // write int to Flash
> Flash_Settings[0] = (command_time> > 8) & 0xFF;
> Flash_Settings[1] = command_time & 0xFF;
> FCTL1 = FWKEY; // Clear WRT bit
> FCTL3 = FWKEY + LOCKA + LOCK; // Set LOCK & LOCKA bit
>
> init_dco(); // Set DCO back to working value
>
> _enable_interrupts();
>}
>
>when reading back the int I never get the right values....?
>
>Reading is done as:
>command_time = Flash_Settings[0];
>command_time |= Flash_Settings[1] > > 8;
>
>What am I doing wrong? First I thought my shifting is wrong, but while debugging the values read from Flash are totally incorrect.
>Maybe someone can help me out. Thanks,
>
>Mitch
> __________________________________________________________
>Gesendet von Yahoo! Mail.
>Dem pfiffigeren Posteingang.
>http://de.overview.mail.yahoo.com
>
>
>

(1) LOCKA for F2xx is tricky. To unlock, use:
if (FCTL3 & LOCKA) FCTL3 |= (FXKEY | LOCK);
To lock, use:
if (~(FCTL3 & LOCKA)) FCTL3 |= (FXKEY | LOCK);

(2) In your F169 code, did you set FCTL2?

(3) If "addr" is odd and "len">1, does it work?

(4) If "addr" is even and "len">2, does it work?

--- In m..., sburck@... wrote:
>
> One thing I see offhand, that you are setting the LOCKA bit where
you are commenting that you are clearing it...if 0x10C0 is in INFO A
on the device in question, that's at least part of the problem.
>
> This is code for a 169, that works. Note lack of a LOCKA bit.
>
> void WriteToFlash(char *addr, char *block,int len)
> {
> _DINT();
> FCTL1 = FWKEY + WRT;
> FCTL3 = FWKEY; // Clear Lock bit
> memcpy(addr,block,len);
> FCTL1 = FWKEY; // Clear Erase bit
> FCTL3 = FWKEY + LOCK; // Reset LOCK bit
> _EINT();
> }
> Hello,
> >
> >I'm trying to store an int in Flash.... and reading it back after
switching the device (a MSP430F2274) on.
> >
> >"int command_time;" - is a global declared integer
> >"Flash_Settings" is specified in lnk_msp430f2274.cmd as
"Flash_Settings = 0x10C0;"
> >and within the module as "extern volatile char Flash_Settings[2];"
> >
> >The function to write is:
> >
> >void settings_to_flash(){
> >
> > _disable_interrupts();
> >
> > BCSCTL1 = CALBC1_1MHZ; // Set DCO to 1MHz
> > DCOCTL = CALDCO_1MHZ;
> > FCTL2 = FWKEY + FSSEL0 + FN1; // MCLK/3 for Flash
Timing Generator
> > FCTL3 = FWKEY + LOCKA; // Clear LOCK &
LOCKA bits
> > FCTL1 = FWKEY + WRT; // Set WRT bit for
write operation
> > // write int to Flash
> > Flash_Settings[0] = (command_time> > 8) & 0xFF;
> > Flash_Settings[1] = command_time & 0xFF;
> > FCTL1 = FWKEY; // Clear WRT bit
> > FCTL3 = FWKEY + LOCKA + LOCK; // Set LOCK & LOCKA bit
> >
> > init_dco(); //
Set DCO back to working value
> >
> > _enable_interrupts();
> >}
> >
> >when reading back the int I never get the right values....?
> >
> >Reading is done as:
> >command_time = Flash_Settings[0];
> >command_time |= Flash_Settings[1] > > 8;
> >
> >What am I doing wrong? First I thought my shifting is wrong, but
while debugging the values read from Flash are totally incorrect.
> >Maybe someone can help me out. Thanks,
> >
> >Mitch
> > __________________________________________________________
> >Gesendet von Yahoo! Mail.
> >Dem pfiffigeren Posteingang.
> >http://de.overview.mail.yahoo.com
> >
> >
> >
> >
>
Old cow yellow -

I do set the FCTL2 in my code, but the value there is determined by the speed of his MCLK, so I left it out of the example.

The code works on a 169 in both of the cases you asked about.

Hello,
>
>I'm trying to store an int in Flash.... and reading it back after switching the device (a MSP430F2274) on.
>
>"int command_time;" - is a global declared integer
>"Flash_Settings" is specified in lnk_msp430f2274.cmd as "Flash_Settings = 0x10C0;"
>and within the module as "extern volatile char Flash_Settings[2];"
>
>The function to write is:
>
>void settings_to_flash(){
>
> _disable_interrupts();
>
> BCSCTL1 = CALBC1_1MHZ; // Set DCO to 1MHz
> DCOCTL = CALDCO_1MHZ;
> FCTL2 = FWKEY + FSSEL0 + FN1; // MCLK/3 for Flash Timing Generator
> FCTL3 = FWKEY + LOCKA; // Clear LOCK & LOCKA bits
> FCTL1 = FWKEY + WRT; // Set WRT bit for write operation
> // write int to Flash
> Flash_Settings[0] = (command_time> > 8) & 0xFF;
> Flash_Settings[1] = command_time & 0xFF;
> FCTL1 = FWKEY; // Clear WRT bit
> FCTL3 = FWKEY + LOCKA + LOCK; // Set LOCK & LOCKA bit
>
> init_dco(); // Set DCO back to working value
>
> _enable_interrupts();
>}
>
>when reading back the int I never get the right values....?
>
>Reading is done as:
>command_time = Flash_Settings[0];
>command_time |= Flash_Settings[1] > > 8;
>
>What am I doing wrong? First I thought my shifting is wrong, but while debugging the values read from Flash are totally incorrect.
>Maybe someone can help me out. Thanks,
>
>Mitch
> __________________________________________________________
>Gesendet von Yahoo! Mail.
>Dem pfiffigeren Posteingang.
>http://de.overview.mail.yahoo.com
>
>
>

Old cow yellow -

I do set the FCTL2 in my code, but the value there is determined by the speed of his MCLK, so I left it out of the example.

The code works on a 169 in both of the cases you asked about.

Hello,
>
>I'm trying to store an int in Flash.... and reading it back after switching the device (a MSP430F2274) on.
>
>"int command_time;" - is a global declared integer
>"Flash_Settings" is specified in lnk_msp430f2274.cmd as "Flash_Settings = 0x10C0;"
>and within the module as "extern volatile char Flash_Settings[2];"
>
>The function to write is:
>
>void settings_to_flash(){
>
> _disable_interrupts();
>
> BCSCTL1 = CALBC1_1MHZ; // Set DCO to 1MHz
> DCOCTL = CALDCO_1MHZ;
> FCTL2 = FWKEY + FSSEL0 + FN1; // MCLK/3 for Flash Timing Generator
> FCTL3 = FWKEY + LOCKA; // Clear LOCK & LOCKA bits
> FCTL1 = FWKEY + WRT; // Set WRT bit for write operation
> // write int to Flash
> Flash_Settings[0] = (command_time> > 8) & 0xFF;
> Flash_Settings[1] = command_time & 0xFF;
> FCTL1 = FWKEY; // Clear WRT bit
> FCTL3 = FWKEY + LOCKA + LOCK; // Set LOCK & LOCKA bit
>
> init_dco(); // Set DCO back to working value
>
> _enable_interrupts();
>}
>
>when reading back the int I never get the right values....?
>
>Reading is done as:
>command_time = Flash_Settings[0];
>command_time |= Flash_Settings[1] > > 8;
>
>What am I doing wrong? First I thought my shifting is wrong, but while debugging the values read from Flash are totally incorrect.
>Maybe someone can help me out. Thanks,
>
>Mitch
> __________________________________________________________
>Gesendet von Yahoo! Mail.
>Dem pfiffigeren Posteingang.
>http://de.overview.mail.yahoo.com
>
>
>

(1) LOCKA for F2xx is tricky. To unlock, use:
if (FCTL3 & LOCKA) FCTL3 |= (FXKEY | LOCK);
To lock, use:
if (~(FCTL3 & LOCKA)) FCTL3 |= (FXKEY | LOCK);

(2) In your F169 code, did you set FCTL2?

(3) If "addr" is odd and "len">1, does it work?

(4) If "addr" is even and "len">2, does it work?

--- In m..., sburck@... wrote:
>
> One thing I see offhand, that you are setting the LOCKA bit where
you are commenting that you are clearing it...if 0x10C0 is in INFO A
on the device in question, that's at least part of the problem.
>
> This is code for a 169, that works. Note lack of a LOCKA bit.
>
> void WriteToFlash(char *addr, char *block,int len)
> {
> _DINT();
> FCTL1 = FWKEY + WRT;
> FCTL3 = FWKEY; // Clear Lock bit
> memcpy(addr,block,len);
> FCTL1 = FWKEY; // Clear Erase bit
> FCTL3 = FWKEY + LOCK; // Reset LOCK bit
> _EINT();
> }
> Hello,
> >
> >I'm trying to store an int in Flash.... and reading it back after
switching the device (a MSP430F2274) on.
> >
> >"int command_time;" - is a global declared integer
> >"Flash_Settings" is specified in lnk_msp430f2274.cmd as
"Flash_Settings = 0x10C0;"
> >and within the module as "extern volatile char Flash_Settings[2];"
> >
> >The function to write is:
> >
> >void settings_to_flash(){
> >
> > _disable_interrupts();
> >
> > BCSCTL1 = CALBC1_1MHZ; // Set DCO to 1MHz
> > DCOCTL = CALDCO_1MHZ;
> > FCTL2 = FWKEY + FSSEL0 + FN1; // MCLK/3 for Flash
Timing Generator
> > FCTL3 = FWKEY + LOCKA; // Clear LOCK &
LOCKA bits
> > FCTL1 = FWKEY + WRT; // Set WRT bit for
write operation
> > // write int to Flash
> > Flash_Settings[0] = (command_time> > 8) & 0xFF;
> > Flash_Settings[1] = command_time & 0xFF;
> > FCTL1 = FWKEY; // Clear WRT bit
> > FCTL3 = FWKEY + LOCKA + LOCK; // Set LOCK & LOCKA bit
> >
> > init_dco(); //
Set DCO back to working value
> >
> > _enable_interrupts();
> >}
> >
> >when reading back the int I never get the right values....?
> >
> >Reading is done as:
> >command_time = Flash_Settings[0];
> >command_time |= Flash_Settings[1] > > 8;
> >
> >What am I doing wrong? First I thought my shifting is wrong, but
while debugging the values read from Flash are totally incorrect.
> >Maybe someone can help me out. Thanks,
> >
> >Mitch
> > __________________________________________________________
> >Gesendet von Yahoo! Mail.
> >Dem pfiffigeren Posteingang.
> >http://de.overview.mail.yahoo.com
> >
> >
> >
> >
>
One thing I see offhand, that you are setting the LOCKA bit where you are commenting that you are clearing it...if 0x10C0 is in INFO A on the device in question, that's at least part of the problem.

This is code for a 169, that works. Note lack of a LOCKA bit.

void WriteToFlash(char *addr, char *block,int len)
{
_DINT();
FCTL1 = FWKEY + WRT;
FCTL3 = FWKEY; // Clear Lock bit
memcpy(addr,block,len);
FCTL1 = FWKEY; // Clear Erase bit
FCTL3 = FWKEY + LOCK; // Reset LOCK bit
_EINT();
}
Hello,
>
>I'm trying to store an int in Flash.... and reading it back after switching the device (a MSP430F2274) on.
>
>"int command_time;" - is a global declared integer
>"Flash_Settings" is specified in lnk_msp430f2274.cmd as "Flash_Settings = 0x10C0;"
>and within the module as "extern volatile char Flash_Settings[2];"
>
>The function to write is:
>
>void settings_to_flash(){
>
> _disable_interrupts();
>
> BCSCTL1 = CALBC1_1MHZ; // Set DCO to 1MHz
> DCOCTL = CALDCO_1MHZ;
> FCTL2 = FWKEY + FSSEL0 + FN1; // MCLK/3 for Flash Timing Generator
> FCTL3 = FWKEY + LOCKA; // Clear LOCK & LOCKA bits
> FCTL1 = FWKEY + WRT; // Set WRT bit for write operation
> // write int to Flash
> Flash_Settings[0] = (command_time> > 8) & 0xFF;
> Flash_Settings[1] = command_time & 0xFF;
> FCTL1 = FWKEY; // Clear WRT bit
> FCTL3 = FWKEY + LOCKA + LOCK; // Set LOCK & LOCKA bit
>
> init_dco(); // Set DCO back to working value
>
> _enable_interrupts();
>}
>
>when reading back the int I never get the right values....?
>
>Reading is done as:
>command_time = Flash_Settings[0];
>command_time |= Flash_Settings[1] > > 8;
>
>What am I doing wrong? First I thought my shifting is wrong, but while debugging the values read from Flash are totally incorrect.
>Maybe someone can help me out. Thanks,
>
>Mitch
> __________________________________________________________
>Gesendet von Yahoo! Mail.
>Dem pfiffigeren Posteingang.
>http://de.overview.mail.yahoo.com
>
>
>

Hello,

I'm trying to store an int in Flash.... and reading it back after switching the device (a MSP430F2274) on.

"int command_time;" - is a global declared integer
"Flash_Settings" is specified in lnk_msp430f2274.cmd as "Flash_Settings = 0x10C0;"
and within the module as "extern volatile char Flash_Settings[2];"

The function to write is:

void settings_to_flash(){

_disable_interrupts();

BCSCTL1 = CALBC1_1MHZ; // Set DCO to 1MHz
DCOCTL = CALDCO_1MHZ;
FCTL2 = FWKEY + FSSEL0 + FN1; // MCLK/3 for Flash Timing Generator
FCTL3 = FWKEY + LOCKA; // Clear LOCK & LOCKA bits
FCTL1 = FWKEY + WRT; // Set WRT bit for write operation
// write int to Flash
Flash_Settings[0] = (command_time>>8) & 0xFF;
Flash_Settings[1] = command_time & 0xFF;
FCTL1 = FWKEY; // Clear WRT bit
FCTL3 = FWKEY + LOCKA + LOCK; // Set LOCK & LOCKA bit

init_dco(); // Set DCO back to working value

_enable_interrupts();
}

when reading back the int I never get the right values....?

Reading is done as:
command_time = Flash_Settings[0];
command_time |= Flash_Settings[1] >> 8;

What am I doing wrong? First I thought my shifting is wrong, but while debugging the values read from Flash are totally incorrect.
Maybe someone can help me out. Thanks,

Mitch
__________________________________________________________
Gesendet von Yahoo! Mail.
Dem pfiffigeren Posteingang.
http://de.overview.mail.yahoo.com