TI MSP430 Microcontroller Writing to FLASH

Question: "How can I store a value, let's say 32, to address 0x1040 in the FLASH memory?"

Answer 1: from old_cow_yellow

ORG 0x1040
DB 0x32
END

Answer 2: from Ajesh GUPTA

Flash_ptr = (int *)0x1040;
FCTL3 = FWKEY; // Clear Lock bit
FCTL1 = FWKEY + ERASE; // Set Erase bit
*Flash_ptr = 0; // Dummy write to erase Flash seg
FCTL1 = FWKEY; // Clear WRT bit
FCTL3 = FWKEY + LOCK; // Set LOCK bit

FCTL1 = FWKEY + WRT; // Set WRT bit for write operation
*Flash_ptr = value;
FCTL1 = FWKEY; // Clear WRT bit
FCTL3 = FWKEY + LOCK; // Set LOCK bit

Answer 3: from Darren Logan

first turn OFF interrupts

/* PROCEDURE: write to flash memory ----------------------------------------------------------*/
void FlashWrite(unsigned int faddress, unsigned int word)
{

/* Note this procedure does NOT erase the flash segment before writing the new value.
Writing a new value over an old value does not work as the flash cells will only accept
a written 0 and not a written 1. You must erase the whole segment before writing a new value. */


int *Flash_ptr; /* Flash pointer */

Flash_ptr = (int *) 0x1000 + faddress; /* Initialize Flash pointer (segment B - start of info. memory) */

FCTL1 = FWKEY + ERASE; /* Set Erase bit */
FCTL3 = FWKEY; /* Clear LOCK bit */
FCTL1 = FWKEY + WRT; /* Set WRT bit for write operation */
*Flash_ptr=word; /* Write the word to flash */
FCTL1 = FWKEY; /* Clear WRT bit */
FCTL3 = FWKEY + LOCK; /* Reset LOCK bit */

} /* FlashWrite */

Verifying Flash

To avoid Flash25 bug, do not use @Rn or @Rn+ to read the FLASH during marginal read mode. Use X(Rn) as the source address is ok. (But be aware that most assemblers will change 0(Rn) to @Rn behind your back.

See:

Questions: