Before I go and screw something up. Could anyone look at the code below to see if I am doing this correctly.
I need to:
1. Read two integers from the MSP430 Information Flash and store the values in global variables.
2. Erase flash information segment B,
3. Write two integers to MSP430 flash information segment B.
#include "msp430g2231.h".....
#define info_seg_B 0x1080
unsigned int storedVlvOffPosition;
unsigned int storedVlvOnPosition;
// Declare Flash Subroutines
void read_segment(int address);
void erase_segment(int address);
void write_segment(int address, int valveOff, int valveOn);
void main(void)
{
WDTCTL = WDTPW + WDTHOLD;
.....
// Call the flash functions....
read_segment(info_seg_B); // Read the previously stored valve positions from flash
erase_segment(info_seg_B); // Erase the segment to prepare write new values
write_segment(info_seg_B, vlvOffPosition, vlvOnPosition); // Write the new valve positions to flash
} // End of main()
//*************************************************************************
// Read stored valve positions from the Information flash segment B
//*************************************************************************
void read_segment(int address)
{
int *Flash_ptr; // Flash pointer
Flash_ptr = (int *)address; // Initialize flash pointer
storedVlvOffPosition = *Flash_ptr;
Flash_ptr = Flash_ptr + 2; // Increment the flash pointer to next integer
storedVlvOnPosition = *Flash_ptr;
}
//*************************************************************************
// Erase Information Flash segment B
//*************************************************************************
void erase_segment(int address)
{
int *Flash_ptr; // Flash pointer
Flash_ptr = (int *)address; // Initialize Flash pointer to 0x1080
FCTL1 = FWKEY + ERASE; // Set Erase bit
FCTL3 = FWKEY; // Clear Lock bit
*Flash_ptr = 0; // Dummy write to erase Flash segment
FCTL3 = FWKEY + LOCK; // Set LOCK bit
}
//************************************************************************
// Write valve on/off positions to the Information flash Segment B
//************************************************************************
void write_segment(int address, int valveOff, int valveOn)
{
int *Flash_ptr; // Flash pointer
Flash_ptr = (int *)address; // Initialize Flash pointer to 0x1080
FCTL3 = FWKEY; // Clear Lock bit
FCTL1 = FWKEY + WRT; // Set WRT bit for write operation
*Flash_ptr = valveOff; // Write value to flash
Flash_ptr = Flash_ptr + 2; // Increment the flash pointer to next even address
*Flash_ptr = valveOn; // Write value to flash
FCTL1 = FWKEY; // Clear WRT bit
FCTL3 = FWKEY + LOCK; // Set LOCK bit
}