EmbeddedRelated.com
Forums

Little help on non volatile variables / memory segments

Started by gleitaoeec December 11, 2006
Hi all.

I'm trying to store 3 char variables on the flash memory of an
MSP430F123. These 3 variables must remain unchanged on power failure,
so I can update a counter at power up.

I'm using the C TI example fet120_flash_write.c changed for my needs,
i.e., I don't clear the memory at startup, I point de Flash pointer
to 0x1080 and write the char values on this address and the next 2.
The reading process is just accessing the memory positions and
building the number.

When I reset the MSP it always loads the same values, for the 3
variables, and of course, not the ones that were written.

Am I writing wrong, reading wrong, the memory segment selected is not
appropriate, or I just can't store a variable on the flash memory
expecting for it to be there on power-up, because it just doesn't
work that way? How do I declare a variable as non-volatile?
The writing and reading methods code is below.

Thanks for your time.

Best regards.

Gustavo Leit
void writeMemory()
{
char digitH = count / 100;
char digitD = (count / 10) % 10;
char digitU = count % 10;

Flash_ptr = (char *) 0x1080;

FCTL1 = FWKEY + WRT;
FCTL3 = FWKEY + !LOCK;

*Flash_ptr++ = digitH;
*Flash_ptr++ = digitD;
*Flash_ptr = digitU;

FCTL1 = FWKEY;
FCTL3 = FWKEY + LOCK;
}

void readMemory()
{
Flash_ptr = (char *) 0x1080;

count = (*Flash_ptr) * 100 + (*(Flash_ptr+1)) * 10 + (*
(Flash_ptr+2));

}

Beginning Microcontrollers with the MSP430

At a glance, I see 2 issues so far :

Inverting the bits uses the ~ operator =>
Use ~LOCK instead of !LOCK
The ! operator is for true/false Boolean.

To operate on flash, it's best to qualify as "const" :
Flash_ptr = (const char *) 0x1080;

There might be other issues, but the !LOCK instead of ~LOCK will not unlock the Flash !
Also, where do you wait for the flash operation to complete ?

Best Regards,
Kris

-----Original Message-----
From: m... [mailto:m...] On Behalf Of gleitaoeec
Sent: Monday, 11 December 2006 9:26 PM
To: m...
Subject: [msp430] Little help on non volatile variables / memory segments

Hi all.

I'm trying to store 3 char variables on the flash memory of an
MSP430F123. These 3 variables must remain unchanged on power failure,
so I can update a counter at power up.

I'm using the C TI example fet120_flash_write.c changed for my needs,
i.e., I don't clear the memory at startup, I point de Flash pointer
to 0x1080 and write the char values on this address and the next 2.
The reading process is just accessing the memory positions and
building the number.

When I reset the MSP it always loads the same values, for the 3
variables, and of course, not the ones that were written.

Am I writing wrong, reading wrong, the memory segment selected is not
appropriate, or I just can't store a variable on the flash memory
expecting for it to be there on power-up, because it just doesn't
work that way? How do I declare a variable as non-volatile?
The writing and reading methods code is below.

Thanks for your time.

Best regards.

Gustavo Leit
void writeMemory()
{
char digitH = count / 100;
char digitD = (count / 10) % 10;
char digitU = count % 10;

Flash_ptr = (char *) 0x1080;

FCTL1 = FWKEY + WRT;
FCTL3 = FWKEY + !LOCK;

*Flash_ptr++ = digitH;
*Flash_ptr++ = digitD;
*Flash_ptr = digitU;

FCTL1 = FWKEY;
FCTL3 = FWKEY + LOCK;
}

void readMemory()
{
Flash_ptr = (char *) 0x1080;

count = (*Flash_ptr) * 100 + (*(Flash_ptr+1)) * 10 + (*
(Flash_ptr+2));

}

Yahoo! Groups Links
Hi Kris.

> Also, where do you wait for the flash operation to complete ?

Forgot to check the busy bit. And disabling the interrupts, by the
way. :)

Thank you for your reply.

Gustavo Leit
--- In m..., "Microbit" wrote:
>
> At a glance, I see 2 issues so far :
>
> Inverting the bits uses the ~ operator =>
> Use ~LOCK instead of !LOCK
> The ! operator is for true/false Boolean.
>
> To operate on flash, it's best to qualify as "const" :
> Flash_ptr = (const char *) 0x1080;
>
> There might be other issues, but the !LOCK instead of ~LOCK will
not unlock the Flash !
> Also, where do you wait for the flash operation to complete ?
>
> Best Regards,
> Kris
>
> -----Original Message-----
> From: m... [mailto:m...] On
Behalf Of gleitaoeec
> Sent: Monday, 11 December 2006 9:26 PM
> To: m...
> Subject: [msp430] Little help on non volatile variables / memory
segments
>
> Hi all.
>
> I'm trying to store 3 char variables on the flash memory of an
> MSP430F123. These 3 variables must remain unchanged on power
failure,
> so I can update a counter at power up.
>
> I'm using the C TI example fet120_flash_write.c changed for my
needs,
> i.e., I don't clear the memory at startup, I point de Flash pointer
> to 0x1080 and write the char values on this address and the next 2.
> The reading process is just accessing the memory positions and
> building the number.
>
> When I reset the MSP it always loads the same values, for the 3
> variables, and of course, not the ones that were written.
>
> Am I writing wrong, reading wrong, the memory segment selected is
not
> appropriate, or I just can't store a variable on the flash memory
> expecting for it to be there on power-up, because it just doesn't
> work that way? How do I declare a variable as non-volatile?
> The writing and reading methods code is below.
>
> Thanks for your time.
>
> Best regards.
>
> Gustavo Leit
> void writeMemory()
> {
> char digitH = count / 100;
> char digitD = (count / 10) % 10;
> char digitU = count % 10;
>
> Flash_ptr = (char *) 0x1080;
>
> FCTL1 = FWKEY + WRT;
> FCTL3 = FWKEY + !LOCK;
>
> *Flash_ptr++ = digitH;
> *Flash_ptr++ = digitD;
> *Flash_ptr = digitU;
>
> FCTL1 = FWKEY;
> FCTL3 = FWKEY + LOCK;
> }
>
> void readMemory()
> {
> Flash_ptr = (char *) 0x1080;
>
> count = (*Flash_ptr) * 100 + (*(Flash_ptr+1)) * 10 + (*
> (Flash_ptr+2));
>
> }
>
>
> Yahoo! Groups Links
>
If your code itself is in Flash, there is no need to check the Flash
busy bit. The CPU will fetch a fake instruction "jump to this same
location" until busy bit is cleared.

--- In m..., "gleitaoeec" wrote:
>
> Hi Kris.
>
> > Also, where do you wait for the flash operation to complete ?
>
> Forgot to check the busy bit. And disabling the interrupts, by the
> way. :)
>
> Thank you for your reply.
>
> Gustavo Leit
> --- In m..., "Microbit" wrote:
> >
> > At a glance, I see 2 issues so far :
> >
> > Inverting the bits uses the ~ operator =>
> > Use ~LOCK instead of !LOCK
> > The ! operator is for true/false Boolean.
> >
> > To operate on flash, it's best to qualify as "const" :
> > Flash_ptr = (const char *) 0x1080;
> >
> > There might be other issues, but the !LOCK instead of ~LOCK will
> not unlock the Flash !
> > Also, where do you wait for the flash operation to complete ?
> >
> > Best Regards,
> > Kris
> >
> > -----Original Message-----
> > From: m... [mailto:m...] On
> Behalf Of gleitaoeec
> > Sent: Monday, 11 December 2006 9:26 PM
> > To: m...
> > Subject: [msp430] Little help on non volatile variables / memory
> segments
> >
> > Hi all.
> >
> > I'm trying to store 3 char variables on the flash memory of an
> > MSP430F123. These 3 variables must remain unchanged on power
> failure,
> > so I can update a counter at power up.
> >
> > I'm using the C TI example fet120_flash_write.c changed for my
> needs,
> > i.e., I don't clear the memory at startup, I point de Flash
pointer
> > to 0x1080 and write the char values on this address and the next
2.
> > The reading process is just accessing the memory positions and
> > building the number.
> >
> > When I reset the MSP it always loads the same values, for the 3
> > variables, and of course, not the ones that were written.
> >
> > Am I writing wrong, reading wrong, the memory segment selected is
> not
> > appropriate, or I just can't store a variable on the flash memory
> > expecting for it to be there on power-up, because it just doesn't
> > work that way? How do I declare a variable as non-volatile?
> >
> >
> > The writing and reading methods code is below.
> >
> > Thanks for your time.
> >
> > Best regards.
> >
> > Gustavo Leit
> >
> >
> > void writeMemory()
> > {
> > char digitH = count / 100;
> > char digitD = (count / 10) % 10;
> > char digitU = count % 10;
> >
> > Flash_ptr = (char *) 0x1080;
> >
> > FCTL1 = FWKEY + WRT;
> > FCTL3 = FWKEY + !LOCK;
> >
> > *Flash_ptr++ = digitH;
> > *Flash_ptr++ = digitD;
> > *Flash_ptr = digitU;
> >
> > FCTL1 = FWKEY;
> > FCTL3 = FWKEY + LOCK;
> > }
> >
> > void readMemory()
> > {
> > Flash_ptr = (char *) 0x1080;
> >
> > count = (*Flash_ptr) * 100 + (*(Flash_ptr+1)) * 10 + (*
> > (Flash_ptr+2));
> >
> > }
> >
> >
> >
> >
> >
> >
> > Yahoo! Groups Links
>
Ah, actually true. I forgot about that.
Haven't used self-programming in a while !
Ironic, considering MSP430's flash programming model is so user friendly compared to say
AVR's "self programming" painful procedure.

Best Regards,
Kris

-----Original Message-----
From: m... [mailto:m...] On Behalf Of old_cow_yellow
Sent: Saturday, 16 December 2006 5:41 AM
To: m...
Subject: [msp430] Re: Little help on non volatile variables / memory segments

If your code itself is in Flash, there is no need to check the Flash
busy bit. The CPU will fetch a fake instruction "jump to this same
location" until busy bit is cleared.

--- In m..., "gleitaoeec" wrote:
>
> Hi Kris.
>
> > Also, where do you wait for the flash operation to complete ?
>
> Forgot to check the busy bit. And disabling the interrupts, by the
> way. :)
>
> Thank you for your reply.
>
> Gustavo Leit
> --- In m..., "Microbit" wrote:
> >
> > At a glance, I see 2 issues so far :
> >
> > Inverting the bits uses the ~ operator =>
> > Use ~LOCK instead of !LOCK
> > The ! operator is for true/false Boolean.
> >
> > To operate on flash, it's best to qualify as "const" :
> > Flash_ptr = (const char *) 0x1080;
> >
> > There might be other issues, but the !LOCK instead of ~LOCK will
> not unlock the Flash !
> > Also, where do you wait for the flash operation to complete ?
> >
> > Best Regards,
> > Kris
> >
> > -----Original Message-----
> > From: m... [mailto:m...] On
> Behalf Of gleitaoeec
> > Sent: Monday, 11 December 2006 9:26 PM
> > To: m...
> > Subject: [msp430] Little help on non volatile variables / memory
> segments
> >
> > Hi all.
> >
> > I'm trying to store 3 char variables on the flash memory of an
> > MSP430F123. These 3 variables must remain unchanged on power
> failure,
> > so I can update a counter at power up.
> >
> > I'm using the C TI example fet120_flash_write.c changed for my
> needs,
> > i.e., I don't clear the memory at startup, I point de Flash
pointer
> > to 0x1080 and write the char values on this address and the next
2.
> > The reading process is just accessing the memory positions and
> > building the number.
> >
> > When I reset the MSP it always loads the same values, for the 3
> > variables, and of course, not the ones that were written.
> >
> > Am I writing wrong, reading wrong, the memory segment selected is
> not
> > appropriate, or I just can't store a variable on the flash memory
> > expecting for it to be there on power-up, because it just doesn't
> > work that way? How do I declare a variable as non-volatile?
> >
> >
> > The writing and reading methods code is below.
> >
> > Thanks for your time.
> >
> > Best regards.
> >
> > Gustavo Leit
> >
> >
> > void writeMemory()
> > {
> > char digitH = count / 100;
> > char digitD = (count / 10) % 10;
> > char digitU = count % 10;
> >
> > Flash_ptr = (char *) 0x1080;
> >
> > FCTL1 = FWKEY + WRT;
> > FCTL3 = FWKEY + !LOCK;
> >
> > *Flash_ptr++ = digitH;
> > *Flash_ptr++ = digitD;
> > *Flash_ptr = digitU;
> >
> > FCTL1 = FWKEY;
> > FCTL3 = FWKEY + LOCK;
> > }
> >
> > void readMemory()
> > {
> > Flash_ptr = (char *) 0x1080;
> >
> > count = (*Flash_ptr) * 100 + (*(Flash_ptr+1)) * 10 + (*
> > (Flash_ptr+2));
> >
> > }
> >
> >
> >
> >
> >
> >
> > Yahoo! Groups Links
>

Yahoo! Groups Links