Part Number: MSP430G2553
Tool/software: TI C/C++ Compiler
Hi Everybody,
I have a program that reads data from the the main memory, and loads it to parameters.
Afterwards, i set some configurations and stores them back into the main memory, but it get stuck in the following lines:
for(i = 0; i < BAUD_MENU_LEN; i++) WriteToFlash((char*)MEM_L_BDRT - i,(char)L_BaudRate[i]);// Legal BaudRate
i comment those lines and try again, i load data from main memory and write it back, and read it again bu now it stucks again from the start of the loadparameters function.
this is the load and save parameters code:
#define MEM_CNTBY 0xFC00// Count_By
#define MEM_NOD 0xFBFF// NOD
#define MEM_DP 0xFBFE// Decimal_Place
#define MEM_BDRT 0xFBFD// BAUD RATE
#define MEM_ZERO_H 0xFBF7// ZERO FIRST 16 BITS
#define MEM_ZERO_L 0xFBF5// ZERO LAST 16 BITS
#define MEM_MXWGT_H 0xFBFB// MAX WEIGHT FIRST 16 BITS
#define MEM_MXWGT_L 0xFBF9// MAX WEIGHT LAST 16 BITS
#define MEM_SLOPE_H 0xFBF3// SLOPE FIRST 16 BITS
#define MEM_SLOPE_L 0xFBF1// SLOPE LAST 16 BITS
#define MEM_AZT 0xFBEF// AUTO ZERO TRACKING
#define MEM_P2Z 0xFBEE// PUSH TO ZERO
#define MEM_UNIT 0xFBED// UNIT
#define MEM_TMEM 0xFBEC// TARE_MEMORY
#define MEM_ADMIN_PASS_LEN 0xFBEB// ManagerPasswordLen
#define MEM_ADMIN_PASS 0xFBEA// ManagerPassword
#define MEM_USER_PASS_LEN 0xFBE4// UserPasswordLen
#define MEM_USER_PASS 0xFBE3// UserPassword
#define MEM_IN_MOTION 0xFBDD// c_in_motion
#define MEM_TARE_H 0xFBDB// TARE FIRST 16 BITS
#define MEM_TARE_L 0xFBD9// TARE LAST 16 BITS
#define MEM_PWR_ON_H 0xFBD7
#define MEM_PWR_ON_L 0xFBD5
#define MEM_STD 0xFBD4
#define MEM_L_PP2Z 0xFBD3
#define MEM_L_BDRT 0xFBCC
void LoadParameters()
{
short m1,m2;
signed char c1;
long a;
ReadFromFlash((char*)MEM_CNTBY,(char*)&cnt_by_idx);// Count_By
if( cnt_by_idx < 0 || cnt_by_idx > COUNT_BY_ARR_LEN - 1)
cnt_by_idx = 3;
ReadFromFlash((char*)MEM_NOD,(char*)&n_o_d_idx);// Count_By
if( n_o_d_idx < 0 || n_o_d_idx > NOD_MENU_LEN - 1)
n_o_d_idx = 2;
ReadFromFlash((char*)MEM_DP,(char*)&idx_decimal_point);// idx_decimal_point
if(idx_decimal_point < 0 || idx_decimal_point > WEIGHT_ARR_LEN - 1)
idx_decimal_point = 2;
ReadFromFlash((char*)MEM_BDRT,(char*)&baud_rate_idx);// BAUD_RATE
if(baud_rate_idx < 0 || baud_rate_idx > BAUD_MENU_LEN - 1)
baud_rate_idx = 5;
ReadShortFromFlash((short*)MEM_MXWGT_H,(short*)&m1);// Max_Weight
ReadShortFromFlash((short*)MEM_MXWGT_L,(short*)&m2);// Max_Weight
max_weight = ((long)m2 << 16 | (long)m1 & 0xffff );
ReadShortFromFlash((short*)MEM_ZERO_H,(short*)&m1);// Zero_Weight
ReadShortFromFlash((short*)MEM_ZERO_L,(short*)&m2);// Zero_Weight
a = ((long)m2 << 16 | (long)m1 & 0xffff );
wt_zero = *((float*)&a);
ReadShortFromFlash((short*)MEM_SLOPE_H,(short*)&m1);// Slope
ReadShortFromFlash((short*)MEM_SLOPE_L,(short*)&m2);// Slope
a = ((long)m2 << 16 | (long)m1 & 0xffff );
wt_slope = *((float*)&a);
ReadFromFlash((char*)MEM_AZT,(char*)&az_tracking_idx);// AZT_Selection
if(az_tracking_idx < 0 || az_tracking_idx > AZT_MENU_LEN - 1)
az_tracking_idx = 0;
ReadFromFlash((char*)MEM_P2Z,(char*)&percent_p2z_idx);// AZT_Selection
if(percent_p2z_idx < 0 || percent_p2z_idx > P2Z_MENU_LEN - 1)
percent_p2z_idx = 1;
ReadFromFlash((char*)MEM_UNIT,(char*)&unit_idx);// Unit_Selection
if(unit_idx < 0 || unit_idx > UNIT_MENU_LEN - 1)
unit_idx = 0;
ReadFromFlash((char*)MEM_TMEM,(char*)&tara_memory_idx);// TMemory
if(tara_memory_idx < 0 || tara_memory_idx > TARA_M_MENU_LEN - 1)
tara_memory_idx = 1;
ReadFromFlash((char*)MEM_ADMIN_PASS_LEN,(char*)&M_PASS_LEN);// ManagerPasswordLen
char i = 0;
if(M_PASS_LEN > 0 && M_PASS_LEN <= MAX_PASS_LEN)
{
for(i = 0; i < MAX_PASS_LEN; i++)
ReadFromFlash((char*)MEM_ADMIN_PASS - i,(char*)&M_PASS_TO_SETUP[i]);// ManagerPassword
}
else
{
SetPassword("678" ,3 ,ManagerSetup);
}
ReadFromFlash((char*)MEM_USER_PASS_LEN,(char*)&PASS_LEN);// UserPasswordLen
if(PASS_LEN > 0 && PASS_LEN <= MAX_PASS_LEN)
{
for(i = 0; i < MAX_PASS_LEN; i++)
ReadFromFlash((char*)MEM_USER_PASS - i,(char*)&PASS_TO_SETUP[i]);// UserPasswor
}
else
{
SetPassword("867" ,3 ,Setup);
}
ReadFromFlash((char*)MEM_IN_MOTION,(char*)&c_in_motion);// c_in_motion
if(c_in_motion < 25 || c_in_motion > 100)
c_in_motion = 30;
if(tara_memory_idx == 0)
{
ReadShortFromFlash((short*)MEM_TARE_H,(short*)&m1);// Tare
ReadShortFromFlash((short*)MEM_TARE_L,(short*)&m2);// Tare
wt_tare = ((long)m2 << 16 | (long)m1 & 0xffff );
}
else
wt_tare = 0;
ReadShortFromFlash((short*)MEM_PWR_ON_H,(short*)&m1);// Tare
ReadShortFromFlash((short*)MEM_PWR_ON_L,(short*)&m2);// Tare
wt_pwron = ((long)m2 << 16 | (long)m1 & 0xffff );
ReadFromFlash((char*)MEM_STD,(char*)&standard_idx);// Unit_Selection
if(standard_idx < 0 || standard_idx > SMode_MENU_LEN - 1)
standard_idx = 0;
for(i = 0; i < P2Z_MENU_LEN; i++)
{
ReadFromFlash((char*)MEM_L_PP2Z - i,(char*)&c1);// Legal P2Z
if(c1 < 0 || c1 > 1)
L_PP2Z[i] = i%2;
else
L_PP2Z[i] = c1;
}
for(i = 0; i < BAUD_MENU_LEN; i++)
{
ReadFromFlash((char*)MEM_L_BDRT - i,(char*)&c1);// Legal BaudRate
if(c1 < 0 || c1 > 1)
L_BaudRate[i] = i%2;
else
L_BaudRate[i] = c1;
}
/*tempMaxWeight = (max_weight - wt_zero)/wt_slope;
tempPowerOn = (wt_pwron - wt_zero)/wt_slope;*/
/*ReadFromFlash();// M_Nod_Selection all seletions 0 or 1
ReadFromFlash();// M_Baud_Rate all seletions 0 or 1
ReadFromFlash();// M_P2Z_Selection all seletions 0 or 1
ReadFromFlash();// User_Selection
ReadFromFlash();// Password_Selection*/
}
void SaveParameters()
{
long a;
ClearFlash((char*)0xFC00);
ClearFlash((char*)0xFBFF);
ClearFlash((char*)0xFA00);
WriteToFlash((char*)MEM_CNTBY,(char)cnt_by_idx);// Count_By
WriteToFlash((char*)MEM_NOD,(char)n_o_d_idx);// Count_By
WriteToFlash((char*)MEM_DP,(char)idx_decimal_point);// idx_decimal_point
WriteToFlash((char*)MEM_BDRT,(char)baud_rate_idx);// idx_decimal_point
WriteShortToFlash((short*)MEM_MXWGT_H,(short)(max_weight & 0xFFFF));// Max_Weight
WriteShortToFlash((short*)MEM_MXWGT_L,(short)((max_weight >> 16) & 0xFFFF));// Max_Weight
a = *((long*)&wt_zero);
WriteShortToFlash((short*)MEM_ZERO_H,(short)(a & 0xFFFF));// Zero_Weight
WriteShortToFlash((short*)MEM_ZERO_L,(short)((a >> 16) & 0xFFFF));// Zero_Weight
a = *((long*)&wt_slope);
WriteShortToFlash((short*)MEM_SLOPE_H,(short)(a & 0xFFFF));// Slope
WriteShortToFlash((short*)MEM_SLOPE_L,(short)((a >> 16) & 0xFFFF));// Slope
WriteToFlash((char*)MEM_AZT,(char)az_tracking_idx);// AZT_Selection
WriteToFlash((char*)MEM_P2Z,(char)percent_p2z_idx);// P2Z_Selection
WriteToFlash((char*)MEM_UNIT,(char)unit_idx);// Unit_Selection
WriteToFlash((char*)MEM_TMEM,(char)tara_memory_idx);// TMemory
WriteToFlash((char*)MEM_ADMIN_PASS_LEN,(char)M_PASS_LEN);// ManagerPasswordLen
char i = 0;
for(i = 0; i < MAX_PASS_LEN; i++)
WriteToFlash((char*)MEM_ADMIN_PASS - i,(char)M_PASS_TO_SETUP[i]);// ManagerPassword
WriteToFlash((char*)MEM_USER_PASS_LEN,(char)PASS_LEN);// UserPasswordLen
for(i = 0; i < MAX_PASS_LEN; i++)
WriteToFlash((char*)MEM_USER_PASS - i,(char)PASS_TO_SETUP[i]);// UserPassword
WriteToFlash((char*)MEM_IN_MOTION,(char)c_in_motion);// c_in_motion
WriteShortToFlash((short*)MEM_TARE_H,(short)(wt_tare & 0xFFFF));// Tare
WriteShortToFlash((short*)MEM_TARE_L,(short)((wt_tare >> 16) & 0xFFFF));// Tare
WriteShortToFlash((short*)MEM_PWR_ON_H,(short)(wt_pwron & 0xFFFF));// Tare
WriteShortToFlash((short*)MEM_PWR_ON_L,(short)((wt_pwron >> 16) & 0xFFFF));// Tare
WriteToFlash((char*)MEM_STD,(char)standard_idx);// c_in_motion
for(i = 0; i < P2Z_MENU_LEN; i++)
WriteToFlash((char*)MEM_L_PP2Z - i,(char)L_PP2Z[i]);// Legal P2Z
/*for(i = 0; i < BAUD_MENU_LEN; i++)
WriteToFlash((char*)MEM_L_BDRT - i,(char)L_BaudRate[i]);// Legal BaudRate
/*WriteToFlash();// M_Nod_Selection all seletions 0 or 1
WriteToFlash();// M_Baud_Rate all seletions 0 or 1
WriteToFlash();// M_P2Z_Selection all seletions 0 or 1*/
}
This is the FlashHandling Code:
#include "FlashProcess.h"
void InitFlash()
{
FCTL2 = FWKEY + FSSEL0 + FN1;
}
void WriteLongToFlash(long* address, long val)
{
long* flash_ptr;
FCTL3 = FWKEY;
flash_ptr = address; // Initialize Flash pointer
while(FCTL3 & BUSY);
FCTL1 = FWKEY + WRT;
*flash_ptr = val;
while(FCTL3 & BUSY);
FCTL1 = FWKEY; // Clear WRT bit
FCTL3 = FWKEY + LOCK; // Set LOCK bit
}
void ReadLongFromFlash(long* address, long* val)
{
long* flash_ptr;
flash_ptr = address;
while(FCTL3 & BUSY);
*val = *flash_ptr;
}
void WriteShortToFlash(short* address, short val)
{
short* flash_ptr;
FCTL3 = FWKEY;
flash_ptr = address; // Initialize Flash pointer
while(FCTL3 & BUSY);
FCTL1 = FWKEY + WRT;
*flash_ptr = val;
while(FCTL3 & BUSY);
FCTL1 = FWKEY; // Clear WRT bit
FCTL3 = FWKEY + LOCK; // Set LOCK bit
}
void ReadShortFromFlash(short* address, short* val)
{
short* flash_ptr;
flash_ptr = address;
while(FCTL3 & BUSY);
*val = *flash_ptr;
}
void WriteToFlash(char* address, char val)
{
char* flash_ptr;
FCTL3 = FWKEY;
flash_ptr = address; // Initialize Flash pointer
while(FCTL3 & BUSY);
FCTL1 = FWKEY + WRT;
*flash_ptr = val;
while(FCTL3 & BUSY);
FCTL1 = FWKEY; // Clear WRT bit
FCTL3 = FWKEY + LOCK; // Set LOCK bit
}
void ReadFromFlash(char* address, char* val)
{
char* flash_ptr;
flash_ptr = address;
while(FCTL3 & BUSY);
*val = *flash_ptr;
}
void ClearFlash(char* address)
{
char *flash_ptr; // Segment D pointer
flash_ptr = address; // Initialize Flash segment D pointer
FCTL1 = FWKEY + ERASE; // Set Mass Erase bit
FCTL3 = FWKEY; // Clear Lock bit
*flash_ptr = 0; // Dummy write to erase Flash segment D
FCTL3 = FWKEY + LOCK;
}
void ClearAllFlash()
{
char *flash_ptr; // Segment D pointer
flash_ptr = (char *) 0x1000; // Initialize Flash segment D pointer
FCTL1 = FWKEY + MERAS; // Set Mass Erase bit
FCTL3 = FWKEY; // Clear Lock bit
*flash_ptr = 0; // Dummy write to erase Flash segment D
FCTL1 = FWKEY + WRT; // Set WRT bit for write operation
FCTL1 = FWKEY; // Clear WRT bit
FCTL3 = FWKEY + LOCK;
}