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.

Copy value from one segment to other

Other Parts Discussed in Thread: MSP430F123

Sir,

I am new to Microcontroller.I have a doubt in the below program.It is a silly doubt but i am not able to get it. Here why we are incrementing the value of variable called value we should store the same value 0 in segmentA and then copy that to SegmentB.Please help me.

// 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.
// //* Set Breakpoint on NOP in the Mainloop to avoid Stressing Flash *//
//
// MSP430F123(2)
// -----------------
// /|\| XIN|-
// | | |
// --|RST XOUT|-
// | |
//
// M. Mitchell
// Texas Instruments Inc.
// Feb 2005
// Built with CCE Version: 3.2.0 and IAR Embedded Workbench Version: 3.21A
//******************************************************************************

#include <msp430x12x2.h>

char value; // 8-bit value to write to segment A

// Function prototypes
void write_SegA (char value);
void copy_A2B (void);

void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
FCTL2 = FWKEY + FSSEL0 + FN0; // MCLK/2 for Flash Timing Generator
value = 0; // initialize value

while(1) // Repeat forever
{
write_SegA(value++); // Write segment A, increment value
copy_A2B(); // Copy segment A to B
_NOP(); // SET BREAKPOINT HERE
}
}

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

Flash_ptr = (char *) 0x1080; // Initialize Flash pointer
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

for (i=0; i<128; i++)
{
*Flash_ptr++ = value; // Write value to flash
}

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


void copy_A2B (void)
{
char *Flash_ptrA; // Segment A pointer
char *Flash_ptrB; // Segment B pointer
unsigned int i;

Flash_ptrA = (char *) 0x1080; // Initialize Flash segment A pointer
Flash_ptrB = (char *) 0x1000; // Initialize Flash segment B pointer
FCTL1 = FWKEY + ERASE; // Set Erase bit
FCTL3 = FWKEY; // Clear Lock bit
*Flash_ptrB = 0; // Dummy write to erase Flash segment B
FCTL1 = FWKEY + WRT; // Set WRT bit for write operation

for (i=0; i<128; i++)
{
*Flash_ptrB++ = *Flash_ptrA++; // copy value segment A to segment B
}

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

  • saloni singhal said:
    Here why we are incrementing the value of variable called value we should store the same value 0 in segmentA and then copy that to SegmentB


    The reason is that it is a loop.

    First, 0 is written to A and then copied to B. Then a breakpoint is hit and you can see that both segments have '0' content.
    When you continue, '1' is written to A and copied to B and the breakpoint is hit. And you can see that A and B both contain '1'. and so on.
    Since flash cannot write '1' to a bit that was already written '0'. it means that all both, erasing flash and writing the new value, was successful, and also the copy function.

    Writing only one static value wouldn't show you any change after running the program for the first time. But with the value++, each loop writes a different value.

    And this has not much to do with microcontollers. Besides the required programming of the flash controller and the dummy write to erase the segment, this is plain C and works on any system.

**Attention** This is a public forum