Dos anybody have any sample code showing how to first erase and then write a large amount of data values to the internal flash on a MSP430F66?
And can do it 10 times in a row?
I have code that can do it 1-3 times, but before 10 tries the firmware “jumps” to something different.
Below is my code that works 1-3 times, but never 10 times.
Suddenly the code pointer jumps to something else.
#define LogbookData 0x40000
typedef void (*FARPTR)();
//*****************************************************************************
void x_write_short(FARPTR addr, unsigned short data)
{
__data20_write_short((unsigned long)addr, data);
__data20_write_short((unsigned long)addr, data);
}
//******************************************************************************
void Erase_Flash_Segment(unsigned long Adr)
{
__bic_SR_register(GIE); // disable Global Interrupt
FCTL3 = FWKEY; // Clear Lock bit
FCTL1 = FWKEY + ERASE; // Set Erase bit
x_write_short((FARPTR)Adr, 0xFFFF); // Dummy write to erase Flash To_segment
FCTL3 = FWKEY + LOCK; // Reset LOCK bit
__bis_SR_register(GIE); // enable Global Interrupt
}
//******************************************************************************
void Erase_Logbook_Segments(void)
{
unsigned long Segment;
for (Segment=0;Segment<20;Segment++)
{
Erase_Flash_Segment(LogbookData + (Segment*0x200));
}
}
//******************************************************************************
void Flash_Write_Logbook(void)
{
unsigned int i;
unsigned long Flash_Pointer;
Erase_Logbook_Segments();
Flash_Pointer = LogbookData; // Initialize Logbook Flash pointer
FCTL3 = FWKEY; // Clear Lock bit
FCTL1 = FWKEY + WRT; // Set WRT bit for write operation
Logbook_Ptr = &Logbook.Device; // Pointer to first byte in logbook struct
x_write_short((FARPTR)Flash_Pointer, 0xFFFF); // DUMMY write
while (FCTL3 & BUSY); // wait until BUSY=0
FCTL1 = FWKEY + WRT; // set write (byte) mode
for (i=0;i<sizeof(Logbook)/sizeof(int);i++)
{
x_write_short((FARPTR)Flash_Pointer, Logbook_Ptr[i]);
Flash_Pointer += 2;
while (FCTL3 & BUSY); // wait until BUSY=0
}
FCTL1 = FWKEY; // Clear WRT bit
FCTL3 = FWKEY + LOCK; // Reset LOCK bit
while ( ! (FCTL3 & WAIT) );
}
//******************************************************************************
void Test_Flash_Logbook(void)
{
Stop_Watchdog_Timer();
for (i=1;i<10;i++)
{
Flash_Write_Logbook();
}
}