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.

FCTL1 = FRKEY?

Other Parts Discussed in Thread: MSP430F2112

I have a question on reading data from flash.

Should I write

FCTL1 = FRKEY;

before I read data from flash?

 

The below is my program. This works without problem.

When I write data to flash, I set FCTL1 = FWKEY;

Then, I read data from flash. But when I set FCTL1 = FRKEY;

the program stops.

Is the FCTL1 automatically cleared for reading?

 

I use msp430F2112.

========================

#include "msp430x21x2.h"

union uFlashData {
    float fval;
    char cval[9];
} g_flashVal;


// Function prototypes
void read_SegC();
void write_SegC();

void main(void)
{
  WDTCTL = WDTPW + WDTHOLD;                 // Stop watchdog timer
  if (CALBC1_1MHZ ==0xFF || CALDCO_1MHZ == 0xFF)                                    
  { 
    while(1);                               // If calibration constants erased
                                            // do not load, trap CPU!!
  }  

  // Init for Flash Writing
  BCSCTL1 = CALBC1_1MHZ;                    // Set DCO to 1MHz
  DCOCTL = CALDCO_1MHZ;
  FCTL2 = FWKEY + FSSEL0 + FN1;             // MCLK/3 for Flash Timing Generator

  TA0CCR0 = 50000;
  TA0CTL = TASSEL_2 + MC_1;                  // SMCLK, upmode
  __enable_interrupt();                     // enable interrupts

  g_flashVal.fval = 3.14159265;
  write_SegC();
 
  g_flashVal.fval = 0.0;
  read_SegC();                           // Write segment C, increment value
  __no_operation();                       // SET BREAKPOINT HERE

}


void write_SegC()
{
  char *Flash_ptr;                          // Flash pointer
  unsigned int i;

  Flash_ptr = (char *)0x1040;               // Initialize Flash pointer
  FCTL3 = FWKEY;                            // Clear Lock bit
  FCTL1 = FWKEY + ERASE + EEI;              // Set Erase bit, allow interrupts
  *Flash_ptr = 0;                           // Dummy write to erase Flash seg

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

  for (i = 0; i < 4; i++)
  {
    *Flash_ptr++ = g_flashVal.cval[i];      // Write value to flash
  }

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

void read_SegC()
{
  char *Flash_ptr;                          // Flash pointer
  unsigned int i;

  Flash_ptr = (char *)0x1040;               // Initialize Flash pointer

  for (i = 0; i < 4; i++)
  {
    g_flashVal.cval[i] = *Flash_ptr++;        // Read value from flash
    _NOP();
  }
 
}

 

 

**Attention** This is a public forum