機(jī)電之家資源網(wǎng)
單片機(jī)首頁(yè)|單片機(jī)基礎(chǔ)|單片機(jī)應(yīng)用|單片機(jī)開(kāi)發(fā)|單片機(jī)文案|軟件資料下載|音響制作|電路圖下載 |嵌入式開(kāi)發(fā)
培訓(xùn)信息
贊助商
MSP430 Flash編程程序
MSP430 Flash編程程序
 更新時(shí)間:2009-12-3 16:29:41  點(diǎn)擊數(shù):0
【字體: 字體顏色
//flash.c文件
#define  __FLASH__
#define  __HW_v_2_1__
#include "flash.h"
/*************************************************************************************************
*     This section contains all FLASH memory relevant functions:                                 *
*       writeByte                                                                                *
*       writeWord                                                                                *
*       eraseFLASH                                                                               *
*       saveInfoFlash                                                                            *
*       changeInfo                                                                               *
*       updateInfo                                                                               *
*                                                                                                *
*************************************************************************************************/
/*************************************************************************************************
Function :        flash_writeFLASH
Parameter :       *dst  :  address within the FLASH page
                  value :  BYTE that has to be written to FLASH
Date :            08.09.2001 / 17.11.2002 / 22.11.2002
Description :     this function writes a byte to an address in FLASH memory
                  warning: in FLASH only zeros can be written. if a bit value needs to be set to
                  one from zero, the whole page has to be erased, thus setting all bits to one.
                  then the value can be written correctly. this function does not perform the
                  necessary FLASH page erase.
*************************************************************************************************/
void flash_writeByte(BYTE *dst, BYTE value)
{
  FCTL2 = FWKEY | FSSEL0 | 20;        //clock source is MCLK, divisor is 20
  do
  {
    _NOP();
  } while(FCTL3 & 0x0001);            // wait for BUSY to reset
  FCTL3 = FWKEY;                      // reset the LOCK bit to enable program/erase
  FCTL1 = FWKEY | WRT;                // set WRT for single acces

*dst = value;                       // do the write as a byte

return;
}

/*************************************************************************************************
Function :        flash_writeWord
Parameter :       *dst  :  address within the FLASH page
                  value :  BYTE that has to be written to FLASH
Date :            22.11.2002
Description :     this function writes a word to an address in FLASH memory
                  warning: in FLASH only zeros can be written. if a bit value needs to be set to
                  one from zero, the whole page has to be erased, thus setting all bits to one.
                  then the value can be written correctly. this function does not perform the
                  necessary FLASH page erase.
*************************************************************************************************/
void flash_writeWord(WORD *dst, WORD value)
{
  FCTL2 = FWKEY | FSSEL0 | 20;        //clock source is MCLK, divisor is 20
  do
  {
    _NOP();
  } while(FCTL3 & 0x0001);            // wait for BUSY to reset
  FCTL3 = FWKEY;                      // reset the LOCK bit to enable program/erase
  FCTL1 = FWKEY | WRT;                // set WRT for single acces

*dst = value;                       // do the write as a word

return;
}

/*************************************************************************************************
Function :        flash_eraseFLASH
Parameter :       *seg  :  any address within the FLASH page that is to be erased
Date :            08.09.2001 / 19.11.2002
Description :     this function erases a FLASH page
*************************************************************************************************/
void flash_eraseFLASH(BYTE *seg)
{
  FCTL2 = FWKEY | FSSEL0 | 20;        //clock source is MCLK, divisor is 20
  do
  {
    _NOP();
  } while(FCTL3 & 0x0001);            // wait for BUSY to reset
  FCTL3 = FWKEY;                      // reset the LOCK bit to enable program/erase
  FCTL1 = FWKEY | ERASE;              // set single segment erase function

*seg = 0xFF;                        // do a dummy write to start erase

FCTL3 = FWKEY | LOCK;               // lock the flash again

return;
}

/*************************************************************************************************
Function :        flash_saveInfoFlash
Parameter :       *container : pointer to updated copy of data in RAM
                  *flashPageBase : pointer to start of destination flash segment
Date :            11.09.2002 / 26.11.2002
Description :     saves info flash page 0 to RAM (!!! works only for INFO flash access !!!)
*************************************************************************************************/
void flash_saveInfoFlash(BYTE *container, BYTE *flashPageBase)
{
  BYTE i;

flashPageBase = (BYTE*)((WORD)(flashPageBase) & 0xFF80);   // wrap around for flash page base address

for(i=0;i<128;i++)
  {
    container = *flashPage++;
  }
}

/*************************************************************************************************
Function :        flash_changeInfo
Parameter :       *container : pointer to updated copy of data in RAM
                  *pointer   : original pointer to variable that has to be updated
                  value      : new value for variable
Date :            11.09.2002 / 26.11.2002
Description :     chages a word in the info flash 0 data (in RAM)
*************************************************************************************************/
void flash_changeInfo(BYTE* containerBase, BYTE *pointer, BYTE value)
{
  // pointer points into flash page to variable that is supposed to be changed
  // subtract flash page offset (0x1000) and add scratch pad offset
  pointer = (BYTE*)(((WORD)(pointer) & 0x7F) + (WORD)(containerBase));
  *pointer = value;
}

/*************************************************************************************************
Function :        flash_update
Parameter :       *container : pointer to updated copy of data in RAM
                  *flashPageBase : pointer to start of destination flash segment
Date :            11.09.2002 / 26.11.2002
Description :     erases the flash page and writes the values from the RAM save area to flash
                  (!!! works only in INFO flash !!!)
*************************************************************************************************/
void flash_updateInfo(BYTE *container, BYTE *flashPageBase)
{
  // assumes prior saving and changing of flash data
  BYTE i;

flashPageBase = (BYTE*)((WORD)(flashPageBase) & 0xFF80);   // wrap around for flash page base address

_DINT();
  flash_eraseFLASH(flashPageBase);

for(i=0;i<128;i++)
  {
    flash_writeByte(flashPageBase++,*(container++));
  }
  _EINT();
}


/*flash.h文件
+-------------------------------------------------------------------------------+
:    copyright (c) Jean Randhahn      :
+-------------------------------------------------------------------------------+
: Project         : CANeye - Uni Rostock                                        :
: Compiler        : IAR workbench GUI 2.31E / target descriptor v1.26A/WIN      :
+-------------------------------------------------------------------------------+
:                                                                               :
: class    : flash                                                       :
: File name       : flash.h                                                     :
: Target hardware : MSP430F148/9                                                :
:                                                                               :
: File Editor     : J. Randhahn                                                 :
: Created         : 06.08.2002                                                  :
:                                                                               :
: Description     :                                                             :
:                                                                           :
:                                                                             :
:                                                                  :
:                                                                               :
+-------------------------------------------------------------------------------+
:                                                                               :
: Modification history:                                                         :
:                                                                               :
:                                                                               :
+-------------------------------------------------------------------------------+
*/
#ifndef __FLASH_H__
#define __FLASH_H__

#ifdef  __FLASH__
/*::::::::::::::::::::::::: START OF LOCAL PART ::::::::::::::::::::::::::::::::*/
/*----------------------- LOCAL INCLUDES ---------------------------------------*/
#include <msp430x14x.h>
#include "type.h"

/*----------------------- LOCAL DEFINITIONS ------------------------------------*/

/*----------------------- LOCAL FUNCTION DECLARATIONS --------------------------*/
       void          flash_eraseFLASH(BYTE *seg);
       void          flash_writeWord(WORD *dst, WORD value);
       void          flash_writeByte(BYTE *dst, BYTE value);
       void          flash_saveInfoFlash(BYTE *container, BYTE *flashPage);
       void          flash_changeInfo(BYTE* containerBase, BYTE *pointer, BYTE value);
       void          flash_updateInfo(BYTE *container, BYTE *flashPageBase);

/*----------------------- LOCAL CONSTANTS AND VARIABLES ------------------------*/

/*----------------------- PUBLIC VARIABLES WITH INITIALISATION -----------------*/


/*::::::::::::::::::::::::: END OF LOCAL PART ::::::::::::::::::::::::::::::::::*/

#else
/*----------------------- PUBLIC VARIBALES INITIALIZED LOCALLY -----------------*/
#endif

/*-------------------- PUBLIC DEFINITIONS --------------------------------------*/

/*-------------------- PUBLIC CONSTANTS AND VARIABLES --------------------------*/

/*-------------------- PUBLIC FUNCTION DECLARATIONS ----------------------------*/
extern void          flash_eraseFLASH(BYTE *seg);
extern void          flash_writeWord(WORD *dst, WORD value);
extern void          flash_writeByte(BYTE *dst, BYTE value);
extern void          flash_saveInfoFlash(BYTE *container, BYTE *flashPage);
extern void          flash_changeInfo(BYTE* containerBase, BYTE *pointer, BYTE value);
extern void          flash_updateInfo(BYTE *container, BYTE *flashPageBase);
#endif


//type.h文件
/* START type definitions for convinience with microcontrollers ****************************/
typedef unsigned char    BYTE;            /* 8 bits */
typedef unsigned short   WORD;           /* 16 bits */
typedef unsigned long    LONGWORD;       /* 32 bits */

/* for dividing a WORD into two BYTEs */
typedef  union   _WORD_BYTE
          { WORD   w;
            BYTE   b[2];
          } WORD_BYTE;
  • 上一篇: MSP430單片機(jī)常見(jiàn)加密總結(jié)
  • 下一篇: 沒(méi)有了
  • 發(fā)表評(píng)論   告訴好友   打印此文  收藏此頁(yè)  關(guān)閉窗口  返回頂部
    熱點(diǎn)文章
     
    推薦文章
     
    相關(guān)文章
    網(wǎng)友評(píng)論:(只顯示最新5條。)
    關(guān)于我們 | 聯(lián)系我們 | 廣告合作 | 付款方式 | 使用幫助 | 機(jī)電之家 | 會(huì)員助手 | 免費(fèi)鏈接

    點(diǎn)擊這里給我發(fā)消息66821730(技術(shù)支持)點(diǎn)擊這里給我發(fā)消息66821730(廣告投放) 點(diǎn)擊這里給我發(fā)消息41031197(編輯) 點(diǎn)擊這里給我發(fā)消息58733127(審核)
    本站提供的機(jī)電設(shè)備,機(jī)電供求等信息由機(jī)電企業(yè)自行提供,該企業(yè)負(fù)責(zé)信息內(nèi)容的真實(shí)性、準(zhǔn)確性和合法性。
    機(jī)電之家對(duì)此不承擔(dān)任何保證責(zé)任,有侵犯您利益的地方請(qǐng)聯(lián)系機(jī)電之家,機(jī)電之家將及時(shí)作出處理。
    Copyright 2007 機(jī)電之家 Inc All Rights Reserved.機(jī)電之家-由機(jī)電一體化網(wǎng)更名-聲明
    電話(huà):0571-87774297 傳真:0571-87774298
    杭州濱興科技有限公司提供技術(shù)支持

    主辦:杭州市高新區(qū)(濱江)機(jī)電一體化學(xué)會(huì)
    中國(guó)行業(yè)電子商務(wù)100強(qiáng)網(wǎng)站

    網(wǎng)站經(jīng)營(yíng)許可證:浙B2-20080178-1