MSP430G2553 flash operation example

Publisher:skyshoucangLatest update time:2018-07-11 Source: eefocusKeywords:MSP430G2553 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

/* Actually applicable to MSP430x2xx series MCU, including the following 10 commonly used functions: 

(1) Initialization. Flash_Init (unsigned char Div, unsigned char Seg): Set the Flash timing according to the SMCLK frequency.

The frequency division coefficient of the clock is determined by the Seg segment number to determine the starting address of the segment for the planned operation. 

(2) Erase the entire segment. Flash_Erase(): segment erase function. 

(3) Read byte. Flash_ReadChar(unsigned int Addr): reads 1 byte of data at offset address Addr. 

(4) Read word. Flash_ReadWord(unsigned int Addr): reads the data of 1 word at the offset address Addr. 

(5) Read a string of bytes into the RAM array. Flash_ReadSeg(unsigned int Addr, unsigned int SegSize, char * 

Array): Reads data with the starting offset address Addr and length SegSize bytes to the Array array in RAM. 

(6) Directly write 1 byte. Flash_Direct_WriteChar(unsigned int Addr): Directly write 1 byte at the offset address Addr

The data. 

(7) Directly write 1 word. Flash_Direct_WriteWord(unsigned int Addr): Directly write the value of 1 word at the offset address Addr.

according to. 

(8) Write 1 byte after backup. Flash_Bak_WriteChar(unsigned int Addr): Back up other data in the segment first, then write to the offset.

Write 1 byte of data to the address Addr, and then restore the other data in the segment. (Only for the information flash segment, use RAM backup) 

(9) Write 1 word after backup. Flash_Bak_WriteWord(unsigned int Addr): Back up other data in the segment first, then write it to the offset.

Write 1 word of data to the address Addr, and then restore the other data in the segment. (Only for the information flash segment, use RAM backup). 

(10) Read SegA dedicated function. Flash_SegA_ReadChar(unsigned int Addr): read SegA segment offset address Addr position 1

Bytes of data. 

Note: 1. The block write function needs to call the function pointer in RAM to use, which is not involved in this library function. 

        2. Other long-byte data types need to use structures for reading and writing, which are not covered by this library function. 

        3. All functions are for unsigned integer data. If you need to use signed integer, you need to modify the function. 

        4. The InfoA segment is processed separately, with only the byte read function Flash_SegA_ReadChar(), and no erase function is provided. 

        5. The segment operation first address SegAddr of other functions is "limited" by the Flash_Init() function, so it is not easy to be written by mistake*/ 


#include "MSP430G2553.h" 

unsigned int SegAddr=0; //global variable 

unsigned int SegPre=0; //global variable current information segment 

/****************************************************** *************************************************** ***  

* Name: Flash_Init() 

* Function: Initialize the Flash clock 

* Entry parameters: Div: The required frequency division value calculated based on the SMCLK frequency, which can be set to 1-64 

* Selection principle: After SMCLK frequency division, it falls between 257kHz and 476kHz

* Seg: segment number, can be set to "0"-"31" or "A", "B", "C", "D". 

* Export parameters: 1: Configuration successful 

* 0: Configuration failed 

* Note: Before operating other Flash functions, you need to call this initialization function to set the clock division and the first address of the segment to be operated. 

* Absolute addresses do not appear in other functions to prevent misoperation. 

* Example: Flash_Init(3,'B' ) 3-division, operation on Info B segment 

*************************************************** *************************************************** **/ 

unsigned char Flash_Init(unsigned char Div,unsigned char Seg) 

  //-----Set the Flash clock and frequency division, the frequency division is just the lowest bit, just use Div-1----- 

  if(Div<1) Div=1; 

  if(Div>64) Div=64; 

  FCTL2 = FWKEY + FSSEL_2 + Div-1; // SMCLK is used by default, and the frequency division coefficient parameter is passed in 

  //-----When the operation object is the main Flash segment, the segment start address can be set by multiples of 512----- 

  SegPre = Seg; //Get the current segment 

  if (Seg <= 31) //Judge whether it is in the main Flash segment 

    { 

    SegAddr=0xFFFF-(Seg+1)*512+1; //Calculate the segment start address 

         return(1); //After the assignment is successful, you can exit and return the success flag "1" 

    } 

  //-----When the operation object is the information Flash segment, just exhaustively enumerate----- 

  switch(Seg) //Judge whether it is in the information Flash segment 

  { 

  case 'A': case 'a': SegAddr=0x10C0; break; 

  case 'B': case 'b': SegAddr=0x1080; break; 

  case 'C': case 'c': SegAddr=0x1040; break; 

  case 'D': case 'd': SegAddr=0x1000; break; 

  default: SegAddr=0x20FF; return(0); //0x20FF address is a blank area to protect Flash 

  } 

  return(1); 

/****************************************************** *************************************************** ***  

* Name: Flash_Erase() 

* Function: Erase a data block of Flash. The erase segment is determined by the SegAddr variable of the initialization function Flash_Init(). 

* Entry parameters: None 

* Export parameters: None 

* Note: The function gives the operation code for erasing the InfoFlashA segment (commented out), but it is not recommended for beginners. 

* Example: None 

*************************************************** *************************************************** **/ 

void Flash_Erase() 

   unsigned char *Ptr_SegAddr; //Segment pointer 

   Ptr_SegAddr = (unsigned char *)SegAddr; //Initialize Flash pointer 

   FCTL1 = FWKEY + ERASE; //Segment erase mode 

   FCTL3 = FWKEY; //Unlock 

   //FCTL3 = FWKEY+LOCKA; //Unlock InfoFlashA as well 

   _DINT(); 

   *Ptr_SegAddr = 0; // Erase the segment to be operated 

   while(FCTL3&BUSY); //Busy 

   _EINT(); 

   FCTL1 = FWKEY; //Cancel erase mode 

   FCTL3 = FWKEY+LOCK; //Lock 

// FCTL3 = FWKEY+LOCK+LOCKA; //Lock InfoFlashA as well 

/****************************************************** *************************************************** ***  

* Name: Flash_ReadChar() 

* Function: Read a byte from Flash 

* Entry parameter: Addr: offset address where data is stored 

* Output parameter: Data: the data read back; returns 0 when the offset overflows 

* Description: None 

* Example: None 

*************************************************** *************************************************** **/ 

unsigned char Flash_ReadChar (unsigned int Addr) 

   unsigned char Data=0; 

   unsigned int *Ptr_SegAddr,temp=0; //Segment pointer 

   //----- Segment range limitation. For memory management security, only operations in this segment are allowed----- 

   if((SegPre<=31&&Addr>=512) ||(SegPre>31&&Addr>=64) ) 

     return 0; 

   temp =SegAddr+Addr; 

   Ptr_SegAddr =(void*)temp; //initialize Flash pointer 

   Data = * (Ptr_SegAddr); 

   return(Data); 

/****************************************************** *************************************************** ***  

* Name: Flash_ReadWord() 

* Function: Read back an integer variable from FlashROM, the address should be an even number 

* Input parameter: Addr: the offset address where the data is stored, still calculated in bytes, must be an even number 

* Output parameter: Data: integer variable value read back; returns 0 when offset overflows 

* Description: None 

* Example: None 

*************************************************** *************************************************** **/ 

unsigned int Flash_ReadWord (unsigned int Addr) 

   unsigned int *Ptr_SegAddr; 

   unsigned int temp=0,Data=0; //Segment pointer 

   //----- Segment range limitation. For memory management security, only operations in this segment are allowed----- 

   if((SegPre<=31&&Addr>=512) ||(SegPre>31&&Addr>=64) ) 

     return 0; 

   temp = SegAddr+Addr; 

     Ptr_SegAddr = (void *)temp; //Initialize Flash pointer 

   Data = * (Ptr_SegAddr); 

   return(Data); 

/****************************************************** *************************************************** ***  

* Name: Flash_ReadSeg() 

* Function: Copy a string of data in the Flash segment to the Array array in RAM 

* Entry parameters: Addr: starting offset address 

* SegSize: number of data 

* *Array: Head pointer of the array in RAM 

* Exit parameter: return error information 0: offset overflow; 1: normal operation 

* Description: None 

* Example: None 

*************************************************** *************************************************** **/ 

char Flash_ReadSeg(unsigned int Addr, unsigned int SegSize, unsigned char * Array) 

  unsigned int i=0,temp=0; 

  unsigned char *Ptr_SegAddr; //Segment pointer 

     //----- Segment range limitation. For memory management security, only operations in this segment are allowed----- 

  if((SegPre<=31&&(Addr+SegSize)>512) ||(SegPre>31&&(Addr+SegSize)>64) ) 

     return 0; 

  for(i=0;i

  { 

    temp=SegAddr+Addr+i; //Prevent compiler from processing pointer offset errors 

    Ptr_SegAddr = (unsigned char *)temp; //Initialize Flash pointer 

    Array[i]=*Ptr_SegAddr; // pointer shift method assignment 

  } 

     return 1; 

/****************************************************** *************************************************** ***  

* Name: Flash_Direct_WriteChar() 

* Function: Force write a byte (Char type variable) into Flash, regardless of whether it is empty 

* Entry parameter: Addr: offset address where data is stored 

            Data: data to be written 

* Exit parameter: return error information 0: offset overflow; 1: normal operation 

* Example: Flash_Direct_WriteChar(0,123); write the constant 123 to unit 0 

             Flash_Direct_WriteChar(1,a);Write integer variable a into unit 1 

*************************************************** *************************************************** **/ 

char Flash_Direct_WriteChar (unsigned int Addr, unsigned char Data) 

  unsigned int temp=0; 

  unsigned char *Ptr_SegAddr; //Segment pointer 

     //----- Segment range limitation. For memory management security, only operations in this segment are allowed----- 

  if((SegPre<=31&&Addr>=512) ||(SegPre>31&&Addr>=64) ) 

     return 0; 

  temp = SegAddr+Addr; 

  Ptr_SegAddr = (unsigned char *)temp; //Initialize Flash pointer 

  FCTL1=FWKEY+WRT; //Normal write status 

  FCTL3=FWKEY; //Unlock 

// FCTL3=FWKEY+LOCKA; //Unlock (including segment A) 

  _DINT(); //Disable total interrupt 

  *Ptr_SegAddr=Data; //Specify address, write 1 byte 

  while(FCTL3&BUSY); //Wait for the operation to complete 

  _EINT(); //Open the general interrupt 

  FCTL1=FWKEY; //Exit write state 

  FCTL3=FWKEY+LOCK; //Restore lock to protect data 

// FCTL3=FWKEY+LOCK+LOCKA; //Restore lock and protect data (including segment A) 

    return 1; 

/****************************************************** *************************************************** *** 

* Name: Flash_Direct_WriteWord() 

* Function: Force a font variable to be written into Flash, regardless of whether the storage location is erased beforehand 

* Input parameter: Addr: the offset address where the data is stored, still calculated in bytes, must be an even number 

              Data: data to be written 

* Exit parameter: return error information 0: offset overflow; 1: normal operation 

* Example: Flash_Direct_WriteWord(0,123); write the constant 123 to unit 0 

             Flash_Direct_WriteWord(2,a);Write integer variable a into unit 2 

*************************************************** *************************************************** **/ 

char Flash_Direct_WriteWord (unsigned int Addr, unsigned int Data) 

  unsigned int temp=0; 

  unsigned int *Ptr_SegAddr; //Segment pointer 

     //----- Segment range limitation. For memory management security, only operations in this segment are allowed----- 

  if((SegPre<=31&&Addr>=512) ||(SegPre>31&&Addr>=64) ) 

     return 0; 

  temp=SegAddr+Addr; 

  Ptr_SegAddr = (unsigned int *)temp; //Initialize Flash pointer 

  FCTL1=FWKEY+WRT; //Normal write status 

  FCTL3=FWKEY; //Unlock 

// FCTL3=FWKEY+LOCKA; //Unlock (including segment A) 

  _DINT(); //Disable total interrupt 

  *Ptr_SegAddr=Data; //Write 16-bit word 

  while(FCTL3&BUSY); //Wait for the operation to complete 

  _EINT(); //Open the general interrupt 

  FCTL1=FWKEY; //Exit write state 

  FCTL3=FWKEY+LOCK; //Restore lock to protect data 

// FCTL3=FWKEY+LOCK+LOCKA; //Restore lock and protect data (including segment A) 

    return 1; 

/****************************************************** ********************************************** ****** *** 

* Name: Flash_Bak_WriteChar() 

* Function: Write a byte (Char type variable) to Flash without destroying other data in the segment 

* Entry parameter: Addr: the address where the data is stored 

             Data: data to be written 

* Exit parameter: return error information 0: offset overflow; 1: normal operation 

* Example: Flash_Bak_WriteChar(0,123); write the constant 123 to cell 0 

              Flash_Bak_WriteChar(1,a); write variable a to unit 1 

*************************************************** *************************************************** **/  

char Flash_Bak_WriteChar (unsigned char Addr, unsigned char Data) 

  unsigned int temp=0; 

  unsigned char *Ptr_SegAddr; //Segment pointer 

  unsigned char BackupArray[64]; //Open up 64 bytes of temporary RAM backup Seg 

  unsigned char i = 0; 

     //----- Segment range limitation. For memory management security, only operations in this segment are allowed----- 

  if((SegPre<=31&&Addr>=512) || (SegPre>31&&Addr>64) ) 

     return 0; 

  for(i=0;i<64;i++) 

  { 

     temp=SegAddr+i; 

     Ptr_SegAddr = (unsigned char *)temp; //Initialize Flash pointer 

     BackupArray[i]=*Ptr_SegAddr; // pointer shift method assignment 

  } 

  Flash_Erase(); //Erase the segment to be operated 

  FCTL1 = FWKEY + WRT; //Normal write (non-block write) 

  FCTL3 = FWKEY; //Unlock 

// FCTL3 = FWKEY; // Unlock (including segment A) 

  for (i=0; i<64; i++) 

  { 

  _DINT(); //Disable total interrupt 

  if(i==Addr) 

  { 

     temp=SegAddr+Addr; 

     Ptr_SegAddr = (unsigned char *)temp; //Initialize Flash pointer 

    *Ptr_SegAddr =Data; //Write data 

    while(FCTL3&BUSY); //Wait for the write operation to complete 

    } 

  else 

  { 

     temp=SegAddr+i; 

     Ptr_SegAddr = (unsigned char *)temp; //Initialize Flash pointer 

    *Ptr_SegAddr = BackupArray[i]; //Restore other data in Flash 

    while(FCTL3&BUSY); //Wait for the write operation to complete 

  } 

  _EINT(); //Open the general interrupt 

  } 

  FCTL1 = FWKEY; // Clear write 

  FCTL3 = FWKEY + LOCK; //Lock 

// FCTL3 = FWKEY + LOCK; // Lock (including segment A) 

    return 1; 

/****************************************************** *************************************************** ***  

* Name: Flash_Bak_WriteWord() 

* Function: Write a word (int type variable) to Flash without destroying other data in the segment 

* Input parameters: Addr: the address where the data is stored, which is still the offset address in bytes and must be an even number 

             Data: data to be written 

* Exit parameter: return error information 0: offset overflow; 1: normal operation 

* Description: MSP430 microcontroller can directly operate on 16-bit data, so in order to speed up 

* All functions operate directly on word. 

* Example: Flash_Bak_WriteWord(0,123); write the constant 123 to unit 0 

             Flash_Bak_WriteWord(1,a);Write variable a into unit 1 

*************************************************** *************************************************** **/ 

char Flash_Bak_WriteWord(unsigned int Addr, unsigned int Data) 

  unsigned int *Ptr_SegAddr; //Segment pointer 

  Ptr_SegAddr = (unsigned int *)SegAddr; //Initialize Flash pointer 

  //-----Note: The following operands are all word16-bit data types----- 

  unsigned int BackupArray[32]; //Open up a 32-word (64-byte) temporary RAM backup Seg 

  unsigned int i = 0; 

    //----- Segment range limitation. For memory management security, only operations in this segment are allowed----- 

  if((SegPre<=31&&Addr>=512) ||(SegPre>31&&Addr>64) ) 

  return 0; 

  for(i=0;i<32;i++) //word type occupies two bytes 

  { 

     BackupArray[i]= *(Ptr_SegAddr+i); //Pointer shift method to assign values ​​to words 

  } 

  Flash_Erase(); //Erase the segment to be operated 

  FCTL1 = FWKEY + WRT; //Normal write (non-block write) 

  FCTL3 = FWKEY; //Unlock 

// FCTL3 = FWKEY+LOCKA; // Unlock (including segment A) 

  for (i=0; i<32; i++) //word type occupies two bytes, odd addresses need to be skipped 

  { 

  _DINT(); //Disable total interrupt 

  if(i==Addr) 

  { 

    *(Ptr_SegAddr+Addr) =Data; //Write font data 

    while(FCTL3&BUSY); //Wait for the write operation to complete 

    } 

  else 

  { 

    *(Ptr_SegAddr+i)= BackupArray[i]; //Restore other data in Flash, restore by word 

    while(FCTL3&BUSY); //Wait for the write operation to complete 

  } 

  _EINT(); //Open the general interrupt 

  } 

  FCTL1 = FWKEY; // Clear write 

  FCTL3 = FWKEY + LOCK; //Lock 

// FCTL3 = FWKEY + LOCK+LOCKA; //Lock (including LOCKA) 

    return 1; 

/****************************************************** *************************************************** ***  

* Name: Flash_SegA_ReadChar() 

* Function: Read a byte from InfoA 

* Entry parameter: Addr: offset address where data is stored 

* Export parameter: Data: the data read back; when the offset overflows, it returns 0 

* Description: None 

* Example: None 

*************************************************** *************************************************** **/ 

unsigned char Flash_SegA_ReadChar (unsigned int Addr) 

  unsigned int temp=0; 

  unsigned char Data=0; 

    //----- Segment range limitation. For memory management security, only operations in this segment are allowed----- 

  if(Addr>=64) 

    return 0; 

  unsigned char *Ptr_SegAddr; //Segment pointer 

  temp = 0x10c0+Addr; 

  Ptr_SegAddr = (unsigned char *)temp; //Initialize Flash pointer 

  Data=*Ptr_SegAddr; //Directly the first address of InfoA, without using the global variable SegAddr 

  return(Data); 


unsigned char gTemp=0;

unsigned char gA;

void main()

{



  WDTCTL = WDTPW + WDTHOLD; //Turn off the dog   


  DCOCTL = CALDCO_8MHZ; // Set DCO to 8MHz first 

  BCSCTL1 = CALBC1_8MHZ;                        

  BCSCTL2 |= DIVM_1+DIVS_2; // After dividing by 2, the MCLK is 4MHz, and after dividing by 4, the SMCLK is 2MHz


  Flash_Init(6,'B'); //6-division frequency is 333.3kHz, operate B information segment


  gTemp=Flash_ReadChar(0); //Read


  Flash_Bak_WriteChar (0,0x23); //Write


  gTemp=Flash_ReadChar(0); //Read again


  gA=gTemp;


}


Keywords:MSP430G2553 Reference address:MSP430G2553 flash operation example

Previous article:MSP430 My Low Power System Programming Template
Next article:MSP430G2553 WDT NMI interrupt example

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号