I have code that erases Segment A, and then writes 8 bytes to it. This works like a charm every time.
However, soon as I comment out the erase segment code, it does not write to the flash. It runs through all the code to do the write, but I don't see the flash memory change at all, whereas I do with the previous method. I am running an MSP430F47183 at 16 MHz.
The following are my two functions. I call SegmentErase first, and then FlashWrite right after it.
void SegmentErase(InfoSegments whichSeg)
{
char* flashPtr = SEG_ADDR[whichSeg];// Set flash pointer to segment
WDTCTL = WDTPW | WDT_ARST_1000 | WDTCNTCL;
while(BUSY & FCTL3) // Wait for busy to clear before accessing flash registers, encapsulated in WDT timer.
;
WDTCTL = WDTPW | WDTHOLD;
FCTL2 = FWKEY | FSSEL_1 | 0x2F; // MCLK/48 for Flash Timing Generator = 333.3 kHz
FCTL3 = FWKEY | LOCKA; // Clear lock bit, must write a 1 to LOCKA to change state, writing 0 has no effect.
FCTL1 = FWKEY | ERASE | EEI; // Set Erase bit, allow interrupts
*flashPtr = 0; // Dummy write to erase Flash seg, this should do the erase.
WDTCTL = WDTPW | WDT_ARST_1000 | WDTCNTCL;
while(BUSY & FCTL3)
;
WDTCTL = WDTPW | WDTHOLD;
FCTL1 = FWKEY; // Clear erase bits
FCTL3 = FWKEY | LOCKA | LOCK; // Set locks on
}
int FlashWrite(char* addr, char* buffer, char len)
{
char* flashPtr = addr;
WDTCTL = WDTPW | WDT_ARST_1000 | WDTCNTCL;
while(BUSY & FCTL3)
;
WDTCTL = WDTPW | WDTHOLD;
FCTL2 = FWKEY | FSSEL_1 | 0x2F; // MCLK/48 for Flash Timing Generator = 333.3 kHz
FCTL3 = FWKEY | LOCKA; // Clear lock bit, must write a 1 to LOCKA to change state, writing 0 has no effect.
FCTL1 = FWKEY | WRT; // Set WRT
int i;
for (i = 0; i <= len; i++)
{
*flashPtr++ = *buffer++; // Write buffer
}
WDTCTL = WDTPW | WDT_ARST_1000 | WDTCNTCL;
while(BUSY & FCTL3)
;
WDTCTL = WDTPW | WDTHOLD;
FCTL1 = FWKEY; // Clear WRT bit
FCTL3 = FWKEY | LOCKA | LOCK; // Set locks on
return 1;
}