CODE
void flash_replace32(int32_t *ptr, int32_t word)
{
int *read_ptr;
int *write_ptr;
int w;
//Optimise the case where the new and old values are the same
if (*ptr == word)
return;
flash_clr( (int *) FSEG_A);
_DINT();
//Set to write mode to prepare for copy
FCTL3 = FWKEY; /* Lock = 0 */
FCTL1 = FWKEY | WRT;
//Copy block B to A
read_ptr = (int *) FSEG_B;
write_ptr = (int *) FSEG_A;
for (w = 0; w < 64; w++)
*write_ptr++ = *read_ptr++;
flash_clr((int *) FSEG_B);
//Set to write mode to prepare for copy
FCTL3 = FWKEY; /* Lock = 0 */
FCTL1 = FWKEY | WRT;
//Copy block A to B, slipping in the new value at the right location
read_ptr = (int *) FSEG_A;
write_ptr = (int *) FSEG_B;
for (w = 0; w < 64; w++, read_ptr++, write_ptr++)
{
if (write_ptr == (int *) ptr)
{
*ptr = word;
//Bump the counter to allow for just writing a double
//word.
w++;
read_ptr++;
write_ptr++;
}
else
{
*write_ptr = *read_ptr;
}
}
flash_secure();
_EINT();
}
{
int *read_ptr;
int *write_ptr;
int w;
//Optimise the case where the new and old values are the same
if (*ptr == word)
return;
flash_clr( (int *) FSEG_A);
_DINT();
//Set to write mode to prepare for copy
FCTL3 = FWKEY; /* Lock = 0 */
FCTL1 = FWKEY | WRT;
//Copy block B to A
read_ptr = (int *) FSEG_B;
write_ptr = (int *) FSEG_A;
for (w = 0; w < 64; w++)
*write_ptr++ = *read_ptr++;
flash_clr((int *) FSEG_B);
//Set to write mode to prepare for copy
FCTL3 = FWKEY; /* Lock = 0 */
FCTL1 = FWKEY | WRT;
//Copy block A to B, slipping in the new value at the right location
read_ptr = (int *) FSEG_A;
write_ptr = (int *) FSEG_B;
for (w = 0; w < 64; w++, read_ptr++, write_ptr++)
{
if (write_ptr == (int *) ptr)
{
*ptr = word;
//Bump the counter to allow for just writing a double
//word.
w++;
read_ptr++;
write_ptr++;
}
else
{
*write_ptr = *read_ptr;
}
}
flash_secure();
_EINT();
}