⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 flash_routines.c

📁 基于MSPF449的三相电压表功率的开发程序
💻 C
字号:
/***********************************************************************/
/*  flashD_tst.c                                            2000-05-30 */
/*                                                                     */
/*                          Flash test                                 */
/*                                                                     */
/* This software demonstrates the functionality and use of the on-chip */
/* advanced flash self-programming feature of MSP430 devices.          */
/* After starting a flash write or erase cycle, the program will wait  */
/* until the flash is read-accessable again, so no program must be     */
/* copied into RAM anymore.                                            */
/* To demonstrate this feature the following program erases and writes */
/* to the information memory starting at address 1000h, which is part  */
/* of every MSP430 flash-based device.                                 */
/*                                                                     */ 
/* Note 1: project must include assembly file "flash_var.s43", because */
/* in this file some variables at absolute address are defined, which  */
/* are used in this software.                                          */
/*                                                                     */
/* Note 2: Since all interrupt vectors are unavailable during flash    */
/* programming, all interrupts must be disabled.                       */
/*                                                                     */
/* Texas Instruments Incorporated                                      */
/***********************************************************************/
#include "emeter_3phase.h"
#include  "flashd.h"        /* function prototypes */


/***********************************************************************/
/*                         Flash_wb                                    */
/* programs 1 byte (8 bit) into the flash memory                       */
/***********************************************************************/
void Flash_wb( char *Data_ptr, char byte )
{
  _DINT();
  FCTL3 = 0x0A500;          /* Lock = 0 */
  FCTL1 = 0x0A540;          /* WRT = 1 */
  *Data_ptr=byte;           /* program Flash word */
}

/***********************************************************************/
/*                         Flash_ww                                    */
/* programs 1 word (16 bits) into the flash memory                     */
/***********************************************************************/
void Flash_ww( int *Data_ptr, int word )
{
  FCTL3 = 0x0A500;          /* Lock = 0 */
  FCTL1 = 0x0A540;          /* WRT = 1 */
  *Data_ptr=word;           /* program Flash word */
}
/***********************************************************************/
/*                         Flash_ww                                    */
/* programs 1 long word (32 bits) into the flash memory                     */
/***********************************************************************/
void Flash_wl( long *Data_ptr, long word )
{
  _DINT();
  FCTL3 = 0x0A500;          /* Lock = 0 */
  FCTL1 = 0x0A540;          /* WRT = 1 */
  *Data_ptr=word;           /* program Flash word */
}
/***********************************************************************/
/*                         Flash_clr                                   */
/* erases 1 Segment of flash memory                                    */
/***********************************************************************/
void Flash_clr( int *Data_ptr )
{
  _DINT();
  FCTL1 = 0x0A502;          /* ERASE = 1 */
  FCTL3 = 0x0A500;          /* Lock = 0 */
  *Data_ptr=0;              /* erase Flash segment */
}

/***********************************************************************/
/*                                                                     */
/* secure all flash                                                    */
/***********************************************************************/
void Flash_secure(void)
{
  _DINT();
  FCTL1 = 0x0A500;          /* ERASE,WRITE = 0 */
  FCTL3 = 0x0A510;          /* Lock = 1 */
}
//***********************************************************************/
//  This routine makes the flash looks like EEPROM. It will erase and replace
//    just one word
//  This routine copies will erase SEGA and then image SEGB to SEGA
//  It will then erase SEGB and copy from SEGA back to SEGB all 128 bytes
//    except the one to be replaced.
//***********************************************************************/
void replace_flash_word(int *Data_ptr, int word )
{
int   *read_ptr,*write_ptr;
int   w;
                          //clear SEG A
Flash_clr( (int *) FSEG_A);
                          //set to write mode to prepare for copy
_DINT();
FCTL3 = 0x0A500;          /* Lock = 0 */
FCTL1 = 0x0A540;          /* WRT = 1 */

                          //Set up read and write pointers
read_ptr = (int *) FSEG_B;
write_ptr = (int *) FSEG_A;
                          //copy from B to A
for( w=0; w<64; w++ )
 {
 *write_ptr++=*read_ptr++;
 }
                          //clear B
Flash_clr( (int *) FSEG_B);

                          //set to write mode to prepare for copy
_DINT();
FCTL3 = 0x0A500;          /* Lock = 0 */
FCTL1 = 0x0A540;          /* WRT = 1 */

                          //Set up read and write pointers
read_ptr = (int *) FSEG_A;
write_ptr = (int *) FSEG_B;
                          //copy back the result
                          //except the word to be changed.
for( w=0; w<64; w++,read_ptr++,write_ptr++ )
 {
 if(write_ptr==Data_ptr)
  *write_ptr=word;
 else
  *write_ptr=*read_ptr;
 }
Flash_secure();
}
//***********************************************************************/
//*  This routine makes the flash looks like EEPROM. It will erase and replace
//*    just one long word
//*  This routine copies will erase SEGA and then image SEGB to SEGA
//*  It will then erase SEGB and copy from SEGA back to SEGB all 128 bytes
//*    except the one to be replaced.
//***********************************************************************/
void replace_flash_long(long *Data_ptr, long word )
{
int  *read_ptr,*write_ptr;
int w;
                          //clear SEG A
Flash_clr( (int *) FSEG_A);
                          //set to write mode to prepare for copy
_DINT();
FCTL3 = 0x0A500;          /* Lock = 0 */
FCTL1 = 0x0A540;          /* WRT = 1 */

                          //Set up read and write pointers
read_ptr = (int *) FSEG_B;
write_ptr = (int *) FSEG_A;
                          //copy from B to A
for( w=0; w<64; w++ )
 {
 *write_ptr++=*read_ptr++;
 }
                          //clear B
Flash_clr( (int *) FSEG_B);

                          //set to write mode to prepare for copy
_DINT();
FCTL3 = 0x0A500;          /* Lock = 0 */
FCTL1 = 0x0A540;          /* WRT = 1 */

                          //Set up read and write pointers
read_ptr = (int *) FSEG_A;
write_ptr = (int *) FSEG_B;
                          //copy back the result
                          //except the word to be changed                           
for( w=0; w<64; w++,read_ptr++,write_ptr++ )
 {
 if(write_ptr==(int*) Data_ptr)
  {
  *Data_ptr=word;                     //this is a double word write so need to advance counter
  w++;
  read_ptr++;
  write_ptr++;
  }
 else
  *write_ptr=*read_ptr;
 }
Flash_secure();
}
// === end of function ==================================================

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -