關(guān)于MSP430-Flash超過64K的讀寫操作方法
關(guān)于MSP430-Flash超過64K的讀寫操作方法
#include "msp430xG46x.h"
void WriteFlashErrorNum(void);
//----------------------------------------------------------------------
//功能:從flash中讀出數(shù)據(jù),只需給出首地址和取出數(shù)據(jù)的數(shù)量
//輸入:waddr為flash的地址,length為數(shù)據(jù)長度
//輸出:無
//----------------------------------------------------------------------
void ReadFlash(unsigned long int waddr,unsigned int length)
{
unsigned char i=0;
if(FlashMangleFlag==0)
{
while(FCTL3 & BUSY);
if(length>64) length=64;
while(length--)
{
Internal_flashdata[i++]=__data20_read_char(waddr++);
}
}
}
//----------------------------------------------------------------------
//功能:段擦除;
//日期:2006.12.13
//輸入:waddr為flash的地址,
//輸出:無
//----------------------------------------------------------------------
//void EraseFlashOneBlock(unsigned long int waddr)
void EraseFlash(unsigned long int waddr)
{
if(FlashMangleFlag==0)
{
if((waddr>=0x1f400) && (waddr<0x20000))
{
while(FCTL3 & BUSY);
_DINT();
FCTL2 = FWKEY + FSSEL1 + FN4;
FCTL3 = FWKEY; // Clear Lock bit
FCTL1 = FWKEY + ERASE; // Set Erase bit
__data20_write_char(waddr,0); // Dummy write to erase Flash segment
while(FCTL3 & BUSY);
FCTL3 = FWKEY + LOCK; // Reset LOCK bit
_EINT();
}
}
}
//----------------------------------------------------------------------
//功能:將數(shù)據(jù)寫入flash;
//日期:2006.12.13
//輸入:waddr為flash的地址,length為數(shù)據(jù)長度,*buf為指向數(shù)據(jù)的指針
//輸出:無
//----------------------------------------------------------------------
unsigned char WriteFlash(unsigned long int waddr,unsigned char *buf,unsigned int length)
{
unsigned int i;
unsigned char temp,ErrorFlag = 0;
if(FlashMangleFlag==0)
{
if((waddr>=0x1f400) && (waddr<0x20000))
{
while(FCTL3 & BUSY);
_DINT();
FCTL2 = FWKEY + FSSEL1 + FN4;
FCTL3 = FWKEY; // Clear Lock bit
FCTL1 = FWKEY + WRT; // Set WRT bit for write operation
for(i=0;i { __data20_write_char(waddr,*buf); // Write value to flash temp = __data20_read_char(waddr); // Read one byte if(temp!=(*buf)) // 判斷讀出的數(shù)據(jù)和寫入的數(shù)據(jù)是否相當(dāng) { ErrorFlag = 1; //寫入flash操作錯誤標(biāo)志 ErrorFlashNum ++ ; } waddr++;buf++; } while(FCTL3 & BUSY); FCTL1 = FWKEY; FCTL3 = FWKEY + LOCK; // Reset LOCK bit _EINT(); if(ErrorFlag == 1) WriteFlashErrorNum(); } } return ErrorFlag; }