#include "BoardConfig.h"
void Write_A(uchar value);
void Copy_A2B(void);
void main( void )
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
BoardConfig(0xb8);
FCTL2 = FWKEY + FSSEL0 + FN0; //Select source
uchar value = 0;
for(;;)
{
Write_A(value++); //Write data to segment A
Copy_A2B(); //Copy data from segment A to segment B
_NOP();
}
}
void Write_A(uchar value)
{
uchar i;
uchar *Flash_ptr;
Flash_ptr = (uchar *)0x1080;
FCTL1 = FWKEY + ERASE; //Set ERASE mode
FCTL3 = FWKEY; //Clear LOCK
*Flash_ptr = 0; //Dummy write
FCTL1 = FWKEY + WRT;
for(i = 0;i < 128;i++)
{
*Flash_ptr++ = value; //Write value
}
FCTL1 = FWKEY; //Clear WRT
FCTL3 = FWKEY + LOCK; //Set LOCK
}
//Copy data from B to A
void Copy_A2B(void)
{
uchar *Flash_ptrA;
uchar *Flash_ptrB;
uint i;
Flash_ptrA = (uchar *)0X1080;
Flash_ptrB = (uchar *)0x1000;
FCTL1 = FWKEY + ERASE;
FCTL3 = FWKEY;
*Flash_ptrB = 0;
FCTL1 = FWKEY + WRT;
for(i = 0;i < 128;i++)
{
*Flash_ptrB++ = *Flash_ptrA++;
}
FCTL1 = FWKEY;
FCTL3 = FWKEY + LOCK;
}
再来个块写入的(TI例程)
//****************************************************************************
// MSP430F14x Demo - Flash In-System Programming, BlockWrite
//
// Description: This program first copies the FlashWrite routine to RAM, then
// erases flash seg A, then it increments all values in seg A using the 64
// byte block write mode.
//
// Assumed default MCLK = DCO ~800 kHz.
// Minimum RAM requirement = 512 bytes
//
// ** Set Breakpoint on NOP in the Mainloop to avoid Stressing Flash **
//
// MSP430F149
// -----------------
// /|| XIN|-
// | | |
// --|RST XOUT|-
// | |
//
// H. Grewal / L. Westlund
// Texas Instruments Inc.
// Jun 2006
// Built with IAR Embedded Workbench Version: 3.30A
//******************************************************************************
#include
// Global variables
char value = 0; // 8-bit value to write to segment A
char* Flash_ptr; // Flash pointer
char* RAM_ptr; // RAM pointer
char* END_ptr; // End of FlashWrite routine
// Function prototypes
void FlashWrite();
void CopyRoutine();
void End_of_FlashWrite();
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
_DINT(); // Diable Interrupts
CopyRoutine(); // Copy FlashWrite routine to RAM
_EINT(); // Enable Interrupts
while(1) // Repeat forever
{
Flash_ptr = (char *) 0x1000; // Initialize Flash pointer
FCTL2 = FWKEY + FSSEL1 + FN0; // MCLK/2 for Flash Timing Generator
FCTL1 = FWKEY + ERASE; // Set Erase bit
FCTL3 = FWKEY; // Clear Lock bit
*Flash_ptr = 0; // Dummy write to erase Flash segment
while(!(FCTL3 & WAIT)); // WAIT until Flash is ready
asm("CALL #300h"); // Execute FlashWrite from RAM
// Inline Assembly
value++; // Increment value
_NOP(); // SET BREAKPOINT HERE
}
}
void CopyRoutine()
{
Flash_ptr = (char*)FlashWrite; // Set pointer to FlashWrite routine
RAM_ptr = (char*)0x0300; // Set pointer to RAM