Hi,
I am trying to save a large chunk of data, a struct for some user generated settings, into flash to persistent them. The size if 112B. Somehow sometimes only the first 3x ~ 10x bytes are actually saved when read back the entire flash, the remaining bytes will be 0xFF as a result of the entire section of flash being erased before writing.
uint8_t tlv[64];
uint8_t *Flash_ptr;
// Read TLV calibration data before erasing information memory
memcpy((void *)&tlv[0], (void*)TLV_START, sizeof(tlv));
// Save settings struct into flash or other persistent medium
Flash_ptr = (uint8_t *)SETTINGS_ADDR;
// Erase information memory
WDTCTL = WDTPW | WDTHOLD;
FCTL2 = FWKEY | FSSEL_1 | FN1 | FN3 | FN5; // MCLK/42 for Flash Timing Generator
while (FCTL3 & BUSY); // Make sure flash controller is not busy
FCTL3 = FWKEY; // Clear Lock bit
if(FCTL3 & LOCKSEG) { // If Info Seg is still locked
FCTL3 = FWKEY | LOCKSEG; // Clear LOCKSEG bit
}
FCTL1 = FWKEY | ERASE; // Set Erase bit
*Flash_ptr = 0; // Dummy write to erase info segment
while (FCTL3 & BUSY); // Make sure flash controller is not busy
FCTL3 = FWKEY;
if(FCTL3 & LOCKSEG) { // If Info Seg is still locked
FCTL3 = FWKEY | LOCKSEG; // Clear LOCKSEG bit
}
FCTL1 = FWKEY | WRT;
memcpy((void *)SETTINGS_ADDR, &settings, sizeof(Settings));
// Save TLV calibration data back into flash
memcpy((void *)TLV_START, &tlv[0], sizeof(tlv));
FCTL1 = FWKEY; // Clear WRT bit
FCTL3 = FWKEY | LOCKSEG; // Set LOCK bit
At first I thought this only happens to information flash, but later I found it also happens when SETTINGS_ADDR is pointed to main flash such as 0xF800. Datasheet mentions total accumulative programming time shouldn't exceed 8 ms. But my calculation indicates 112B takes less than 3 ms. with MCLK/42 as clock.
Could someone advise what I am missing here?
Thanks.