This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CCS/MSP430F5419A: accessing flash memory

Expert 1165 points
Part Number: MSP430F5419A


Tool/software: Code Composer Studio

Hello, I'm trying to save switch staus in flash memory. So that the next time I turn on the device, I will have previous switch setting. following is the code I'm unable to so, please help me.

#include <msp430.h>
volatile int Rdata;
volatile int Sw1=0;
volatile int Sw2=0;

// Function prototypes
void read(void);
void write(void);
void switching(unsigned int);

void main(void)
{
WDTCTL = WDTPW + WDTHOLD; //Disable the Watchdog timer for convenience.
P1DIR |= BIT0; //Set pin 4.1 to the output direction.
P1DIR |= BIT1;
// P4SEL |= BIT1; //Select pin 4.1 as our PWM output

//configuring the switch
P2REN |= (BIT7 + BIT6);
P2OUT |= (BIT7 + BIT6);
P2IE |= (BIT7 + BIT6);
P2IES |= (BIT7 + BIT6);
P2IFG &= ~(BIT7 + BIT6);

__enable_interrupt();

while (1)
{


read();


if (Sw1 == 0)
{
switching(Sw2); //turn the led according to the value of counter SW2
}
else if (Sw1 == 1)
{

switching(Rdata); // turn the led according to value of counter SW2 written on flash memory when SW1=1;
write();

}
}
}

void switching(unsigned int a)
{
if (a == 0)
{
P1OUT = BIT0;
}
else if (a == 1)
{
P1OUT = BIT1;
}
}

void write(void)
{
__disable_interrupt(); // Disable interrupts

int *Flash_ptr = (int *) 0x0E000;
while (BUSY & FCTL3)
; // Check if Flash being used
FCTL1 = FWKEY + ERASE; // Set Erase bit
FCTL3 = FWKEY; // Clear Lock bit
*Flash_ptr = 0; // Dummy write to erase Flash segment
while (BUSY & FCTL3)
; // Check if Flash being used
FCTL1 = FWKEY; // Clear WRT bit
FCTL3 = FWKEY + LOCK; // Set LOCK bit

FCTL1 = FWKEY + WRT; // Set WRT bit for write operation

while ((FCTL3 & BUSY) != 0)
;
*Flash_ptr= Sw2; // Write value to flash

FCTL1 = FWKEY; // Clear WRT bit
FCTL3 = FWKEY + LOCK; // Set LOCK bit
__enable_interrupt();
}

void read(void)
{

int *Flash_ptr = (int *) 0x0E000;


while ((FCTL3 & BUSY) != 0)
;
Rdata = *Flash_ptr;

}
#pragma vector=PORT2_VECTOR
__interrupt
void Port_2(void)
{

switch (__even_in_range(P2IV, 0x10))
{
case 0x00:
break; //None
case 0x02:
break; //Pin0
case 0x04:
break; //Pin1
case 0x06:
break; //pin2
case 0x08:
break; //pin3
case 0x0A:
break; //pin4
case 0x0C:
break; //pin5
case 0x0E:
{
if (Sw1 < 1)
{
Sw1++;
}
else
{
Sw1 = 0;
}

P2IFG &= ~BIT6;
break;
}

case 0x10:
{
//pin7

if (Sw2 < 1)
{
Sw2++;

}
else
{
Sw2 = 0;
}

P2IFG &= ~BIT7;

break;

}
}

}

  • Hi Kary,

    It looks like you have some operations out of order, namely, you are locking the flash before writing to it, which would prevent the write. Also, you are trying to erase before you unlock the flash, which would prevent the erase as well. We have example code showing how to write to flash, I would recommend following it for the proper procedure. 

  • I figured it out this morning, but still the flash pointer points -1, why?

    #include <msp430.h>
    volatile int Rdata;
    volatile int Sw1=0;
    volatile int Sw2=0;

    // Function prototypes
    void read(void);
    void write(void);
    void switching(unsigned int);

    void main(void)
    {
    WDTCTL = WDTPW + WDTHOLD; //Disable the Watchdog timer for convenience.
    P1DIR |= BIT0; //Set pin 4.1 to the output direction.
    P1DIR |= BIT1;
    // P4SEL |= BIT1; //Select pin 4.1 as our PWM output

    //configuring the switch
    P2REN |= (BIT7 + BIT6);
    P2OUT |= (BIT7 + BIT6);
    P2IE |= (BIT7 + BIT6);
    P2IES |= (BIT7 + BIT6);
    P2IFG &= ~(BIT7 + BIT6);

    __enable_interrupt();

    while (1)
    {



    if (Sw1 == 0)
    {
    switching(Sw2); //turn the led according to the value of counter SW2
    }
    else if (Sw1 == 1)
    {
    read();
    switching(Rdata); // turn the led according to value of counter SW2 written on flash memory when SW1=1;
    write();

    }
    }
    }

    void switching(unsigned int a)
    {
    if (a == 0)
    {
    P1OUT = BIT0;
    }
    else if (a == 1)
    {
    P1OUT = BIT1;
    }
    }

    void write(void)
    {
    __disable_interrupt(); // Disable interrupts

    int *Flash_ptr = (int *) 0x0E000;
    while (BUSY & FCTL3)
    ; // Check if Flash being used
    FCTL1 = FWKEY + ERASE; // Set Erase bit
    FCTL3 = FWKEY; // Clear Lock bit
    *Flash_ptr = 0; // Dummy write to erase Flash segment

    FCTL1 = FWKEY + WRT; // Set WRT bit for write operation

    while ((FCTL3 & BUSY) != 0)
    ;
    *Flash_ptr= Sw2; // Write value to flash

    FCTL1 = FWKEY; // Clear WRT bit
    FCTL3 = FWKEY + LOCK; // Set LOCK bit
    __enable_interrupt();
    }

    void read(void)
    {

    int *Flash_ptr = (int *)0x0E000;


    while ((FCTL3 & BUSY) != 0)
    ;
    Rdata = *Flash_ptr;

    }
    #pragma vector=PORT2_VECTOR
    __interrupt
    void Port_2(void)
    {

    switch (__even_in_range(P2IV, 0x10))
    {
    case 0x00:
    break; //None
    case 0x02:
    break; //Pin0
    case 0x04:
    break; //Pin1
    case 0x06:
    break; //pin2
    case 0x08:
    break; //pin3
    case 0x0A:
    break; //pin4
    case 0x0C:
    break; //pin5
    case 0x0E:
    {
    if (Sw1 < 1)
    {
    Sw1++;
    }
    else
    {
    Sw1 = 0;
    }

    P2IFG &= ~BIT6;
    break;
    }

    case 0x10:
    {
    //pin7

    if (Sw2 < 1)
    {
    Sw2++;

    }
    else
    {
    Sw2 = 0;
    }

    P2IFG &= ~BIT7;

    break;

    }
    }

    }
  • Do you mean the flash contents are -1 or the pointer address is -1?
    Are you observing the flash contents in the memory viewer?
  • yes the flash pointer contents are -1, but the switch satus in flash pointer i'm is saving is either 0 or 1.
  • I see that your erase operation is still taking place before you unlock the flash controller, so the erase wont actually take place.
    Try fixing this and seeing what happens. I would take another look at the example code I pointed you to and make sure you are following it.

    Also there is an appnote that may be helpful: http://www.ti.com/lit/slaa729

    Lastly, if you are writing to flash alot (which it appears you are), you are likely going to wear out the flash. I recommend that you take a look at our FRAM offerings for this sort of application.

**Attention** This is a public forum