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.

MSP430I2040: how to write large chunk of data into flash?

Part Number: MSP430I2040


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.

  • The problem is tracked down to the firmware downloading software we use.  This block of code to save data into flash will run immediately upon first system boot. But the software immediately resets MCU right after downloading firmware, while last bytes of data are still being written into flash.

**Attention** This is a public forum