I am working on MSP430F5438, I am facing some problems in reading from flash memory location ,I have to write data continousouly on flash and after that read it from flash memory ,If i Read -Write 4 byte at one location of Flash it is Working Properly , Pls go through code given Below and Solve my Problems ,Where I mistaken ,Thankyou .
unsigned long BUF[ ] = {0X10000,0X20000};
unsigned long *Flash_ptr = BUF;
unsigned long Flash_ptr_start = 0X10000;
/**** write value into flash Memory****/
void flash_write_val(){ char temp[5];
while (FCTL3 & BUSY); // Check if Flash being used FCTL3 = FWKEY; FCTL1 = FWKEY+BLKWRT; // Enable long-word write (*(unsigned long *)Flash_ptr) = rec_long; // Write to Flash check = (*(unsigned long *)Flash_ptr);
temp[0] = check ; //LSB temp[1] = (check >> 8) ; temp[2] = (check >> 16); temp[3] = (check >> 24); temp[4] = '\0';
halLcdPrintLine("valwrt2flash..",4,OVERWRITE_TEXT); // Here we get Value which is Written into Flash Mem Location halLcdPrintLine(temp,5,OVERWRITE_TEXT);
Flash_ptr++; // Increment Flash mem location to read val frm next location while (FCTL3 & BUSY ); // Check if Flash being used FCTL1 = FWKEY; // Clear WRT bit FCTL3 = FWKEY+LOCK; // Set LOCK bit}
/****Read value from flash Memory****/
void flash_read_val(){ char temp[5];
// unsigned long *Flash_ptr_start = BUF; // if i set flash_ptr_start value equal to directly buffer it will not go into the loop why ?? unsigned long Flash_end = 0X10001; while( Flash_ptr_start < 0X10001 ) { P1OUT |= LED2; Flash_data = 0; Flash_data = *(unsigned long *)Flash_ptr_start; P1OUT &= ~LED2; temp[0] = Flash_data ; //LSB temp[1] = (Flash_data >> 8) ; temp[2] = (Flash_data >> 16); temp[3] = (Flash_data >> 24); temp[4] = '\0'; halLcdPrintLine("valrd frm flash..",6,OVERWRITE_TEXT); // while reading from same memory location i get result zero why ???? halLcdPrintLine(temp,7,OVERWRITE_TEXT); Flash_ptr_start++;
delay(500); }}
/*******************************************UART*************************************/
#pragma vector=USCI_A1_VECTOR__interrupt void USCI_A1_ISR (void){
UartReceiveBuffer[bufferSize++] = UCA1RXBUF;
}
void main()
{
flashEraseBank(BUF[0]);
if( bufferSize > 3 ) { send_XOFF(); //P1OUT |= LED2; // LED1 ON rec_long = UartReceiveBuffer [ 0 ]; rec_long = (rec_long << 8) | UartReceiveBuffer[ 1 ]; rec_long = (rec_long << 8) | UartReceiveBuffer[ 2 ]; rec_long = (rec_long << 8) | UartReceiveBuffer[ 3 ];
bufferSize = 0;
send_XON ();
Priyanka Dhomneunsigned long Flash_ptr_start = 0X10000;
Priyanka Dhomne // unsigned long *Flash_ptr_start = BUF;
You have two different definitions of Flash_ptr_start.
The global one is a long int variable, the one define din the function is a pointer variable that points to a long int value.
If you use the pointer definition inside your funciton, it will shadow the global long int variable (don't ou get a compiler warning about this?).
You're going through some implicit typecasting here.
_____________________________________Before posting bug reports or ask for help, do at least quick scan over this article. It applies to any kind of problem reporting. On any forum. And/or look here.If you cannot discuss your problem in the public, feel free to start a private conversation: click on my name and then 'start conversation'. But please do so only if you really cannot do it in a public thread, as I usually read all threads. And I prefer to answer where others can profit from it (or contribute to it) too.
unsigned long *Flash_ptr_start = BUF;
This assignment is commented in code...........i had not used in this code.only global one is used.
Priyanka DhomneThis assignment is commented in code...........i had not used in this code.only global one is used.
If you give an either/or description of the problem ("If I..." implies that it works if you don't), you should also give the two different code versions. I assumed that this comment was the difference.
k.......... i tell you what whole problem is .
In code when we intialize unsigned long *Flash_ptr = BUF; (it will point to first mem location 0x10000 ), Writing into Flash is Proper ,while Reading from Flash it will not into loop itself ?
while (FCTL3 & BUSY); // Check if Flash being usedFCTL3 = FWKEY; FCTL1 = FWKEY+BLKWRT; // Enable long-word write(*(unsigned long *)Flash_ptr) = rec_long; // Write to Flashcheck = (*(unsigned long *)Flash_ptr);
temp[0] = check ; //LSBtemp[1] = (check >> 8) ;temp[2] = (check >> 16);temp[3] = (check >> 24);temp[4] = '\0';
halLcdPrintLine("valwrt2flash..",4,OVERWRITE_TEXT); // Here we get Value which is Written into Flash Mem LocationhalLcdPrintLine(temp,5,OVERWRITE_TEXT);
Flash_ptr++; // Increment Flash mem location to read val frm next locationwhile (FCTL3 & BUSY ); // Check if Flash being usedFCTL1 = FWKEY; // Clear WRT bitFCTL3 = FWKEY+LOCK; // Set LOCK bit}
This code is not worked thats why i tried and initalize unsigned long *Flash_ptr = 0X10000(Directly pointing to memory location); In this Case it will go into the loop but give output zero.
Is problem in this code wrt pointer increment ,while incrementing it is not pointing to another location(0X10001),I Hope you Understand my Problem ,Thank You
Okay, I see some oddities...
1) the loop is controlled by Flash_ptr_start, so it makes no difference where Flash_ptr points to.
2) I'm not sure whether your constants are right. Ic you write 0x10000, this is an int, not a long int, and migh tbe truncated to 0x0000 by the parser before it is assigned. Try using 0x10000L instead. I'm not sure what the C standard and teh compiler docu tells about long integer constants, but IIRC, some past problems in this forum were solved by adding the long modifier to the constant.If this is the case, then 0x10000 truncates to 0x0000, which is smaller than (0x10001 truncated to) 0x0001 in the if. However, BUF is > 0x0001 and won't enter the loop.
3) (usigned long *)Flash_ptr_start may result in a 16 bit pointer if you don't use large data model. In small data model (the default), data pointers are 16 bit only, so the cast truncates the value of Flash_ptr_start and you're reading from address 0x00000 instead of 0x10000.
2) I'm not sure whether your constants are right. Ic you write 0x10000, this is an int, not a long int, and migh tbe truncated to 0x0000 by the parser before it is assigned. Try using 0x10000L instead. I'm not sure what the C standard and teh compiler docu tells about long integer constants, but IIRC, some past problems in this forum were solved by adding the long modifier to the constant.If this is the case, then 0x10000 truncates to 0x0000, which is smaller than (0x10001 truncated to) 0x0001 in the if. However, BUF is > 0x0001 and won't enter the loop
I Tried to run my Code using 0x10000L ,Still not going into the Loop.I understand that what you are trying to tell me that pointer is points not pointing to 0x10000 ,but it will point to 0x0000 .
Do you have any code Examples Regarding Flash Read Write,Which would help me for reading and writing large amount of data from flash.
Priyanka DhomneDo you have any code Examples Regarding Flash Read Write,Which would help me for reading and writing large amount of data from flash.
Sorry, my flash writing code is hand-crafted assembly for MSPGCC.
However, I suggest starting writing to the info memory area below 64k first. If this works, you can go and extend the code for >64k memory.