msp430f5438a Information Flash Seg Write -- Chapter

Today we are going to study how to write information flash by using msp430f5438a.

The size of the information is 512 bytes (1800-19FF).

when you start to write, you have to choose which segment you want to write into. That is to say, once you choose the segment, you can't write others until you select the other ones.

Here are some sample for how to write:

 1 //=================== 0x1800 - 0x187f =========================
 2 // Input = Remote & TireSensor. Holds values to write to seg D
 3 //=============================================================
 4 void write_segD(void)
 5 {
 6   int i;
 7   bool Re_Enable=false;
 8   u8 *pFlash = (u8 *)0x1800;
 9   
10   if(__get_interrupt_state()!=0)
11   {
12     __disable_interrupt();                  // 5xx Workaround: Disable global
13                                             // interrupt while erasing. Re-Enable
14                                             // GIE if needed
15     Re_Enable = true;
16   }
17   
18   TPMS_RCU_Group.tp.Marker[0]=99;
19   while(BUSY&FCTL3);                    //Test BUSY
20   FCTL3 = FWPW;                         //Clear LOCK
21   FCTL1 = FWPW+ERASE;                   //Enable segment erase
22   *(unsigned int *)pFlash = 0;          // Dummy write to erase Flash seg
23   while(BUSY&FCTL3);                    //Test BUSY
24   FCTL1 = FWPW+WRT;                     //Enable write
25   
26   pFlash = (u8 *)0x1800;
27   for(i=0;i< sizeof(PowerSupplyTypeDef)-1;i++)
28     *pFlash++ = BatCalValue.buf[i];
29   
30   for(i=0;i<16;i++)
31     *pFlash++ = TPMS_RCU_Group.table[i];
32   
33   FCTL1=FWKEY;                          // WRT = 0
34   FCTL3=FWKEY+LOCK;                     // LOCK = 1
35   if(Re_Enable==true)
36   {
37     __enable_interrupt();
38   }
39 }
 1 //=================== 0x1880 - 0x18ff ==========================
 2 //  Input = credential value. Holds values to write to seg C
 3 //==============================================================
 4 void write_segC(void)
 5 {
 6   int i;
 7   bool Re_Enable = false;
 8   char * Flash_ptr;                             // Initialize Flash pointer
 9   Flash_ptr = (char *) 0x1880;
10   
11   if(__get_interrupt_state()!=0)
12   {
13     __disable_interrupt();                  // 5xx Workaround: Disable global
14                                             // interrupt while erasing. Re-Enable
15                                             // GIE if needed
16     Re_Enable = true;
17   }
18   
19   FCTL3 = FWKEY;                                // Clear Lock bit
20   FCTL1 = FWKEY+ERASE;                          // Set Erase bit
21   *(unsigned int *)Flash_ptr = 0;               // Dummy write to erase Flash seg
22   FCTL1 = FWKEY+WRT;                            // Set WRT bit for write operation
23   
24   for (i = 0; i < CredentialsLenth; i++)
25   {
26     *Flash_ptr++ = XSocket.Credential.buf[i];   // Write value to flash
27   }
28   FCTL1 = FWKEY;                                // Clear WRT bit
29   FCTL3 = FWKEY+LOCK;                           // Set LOCK bit
30   
31   if(Re_Enable==true)
32   {
33     __enable_interrupt();
34   }
35 }

However, read is more easy. and simple.

 1 //=========================================
 2 //          Get segD value
 3 //=========================================
 4 void read_segD(void)
 5 {
 6   u8 *pFlash=(u8*)0x1800;
 7   
 8   for(int i=0;i< sizeof(PowerSupplyTypeDef)-1;i++)
 9     BatCalValue.buf[i] = *pFlash++;
10   
11   for(int i=0;i<16;i++)
12     TPMS_RCU_Group.table[i] = *pFlash++;
13 }
 1 //=========================================
 2 //          Get seg C value
 3 //=========================================
 4 void read_segC(void)
 5 {
 6   int i;
 7   char * pFlash;
 8   pFlash = (char *) 0x1880;
 9   for(i=0;i<CredentialsLenth;i++)
10   {
11     XSocket.Credential.buf[i]=*pFlash++;
12   }
13 }

 

 

OK, enjoy, thank you!

 

posted on 2017-11-21 16:32  Milo_lu  阅读(313)  评论(0)    收藏  举报

导航