MSP430F149的存儲器結(jié)構(gòu)及FLASH讀寫
1.1 FLASH特點
本文引用地址:http://m.butianyuan.cn/article/201611/322341.htm寫操作只能將1改寫為0,不能將0改寫成1。FLASH擦除后所有單元變?yōu)?,擦除操作只能針對整個段。FLASH在擦除前不能被改寫。
1.2 MSP430F149存儲器編址方式
MSP430F149的ROM為60K+256B的FLASH,RAM為2K。MSP430存儲器采用馮諾依曼結(jié)構(gòu),RAM和ROM合在一起編址。MSP430F149內(nèi)部集成有FLASH控制器,可以簡化對FLASH的操作。
64K的尋址空間分為RAM、FLASH。
RAM分兩塊:
1、寄存器(0000H-01FFH),存放特殊寄存器、設(shè)備寄存器、變量與堆棧。
2、數(shù)據(jù)RAM(01FFH-),存放各種變量、中間結(jié)果、堆棧。
FLASH分兩塊:
1、主FLASH 一般用于存放程序代碼。
2、信息FLASH(InfoFlash)用作掉電后保存少量數(shù)據(jù)。分為InfoA(0X1080-0X10FF)和InfoB(0X1000-0X1080),每段各128B。
1.3 操作
三種操作:讀取、擦除(只能針對段擦除)、寫入(可以寫入單個字節(jié))。
2使用方法
2.1 程序架構(gòu)
讀取FLASH方法和讀取RAM方法相同。寫和擦除FLASH要進(jìn)行如下配置:
配置寄存器
制定指針地址
寫數(shù)據(jù)/復(fù)制數(shù)據(jù)
配置寄存器
2.2 參數(shù)配置
主要配置三個寄存器FCTL1,F(xiàn)CTL2,F(xiàn)CTL3。
1、配置FLASH控制器時鐘。時鐘要求控制在250-470Khz之間。
FCTL2 = FWKEY + FSSEL0 + FN0; //2分頻
2、用指針指向地址Unsigned char *ptr= (unsigned char *) 0x1080;
3、進(jìn)入寫模式或擦除模式FCTL1=FWKEY+WRT;或FCTL1=FWKEY+ERASE
4、清除鎖定位 FCTL3=FWKEY;
5、寫數(shù)據(jù) *ptr=0x30;或擦除*ptr=0;
6、退出寫狀態(tài),恢復(fù)鎖存
FCTL1=FWKEY;
FCTL3=FWKEY+LOCK;
2.3 說明
上電FLASH默認(rèn)狀態(tài)是讀。為防止錯誤操作,F(xiàn)LASH的三個寄存器FCTLX都采用了密碼核對機制FWKEY。
上電時FLASH的InfoFLASH區(qū)域值均為1,可直接進(jìn)行寫操作,但此后的寫操作必須先擦除才能正確寫入。
MSP430系列單片機FLASH擦寫典型次數(shù)是10萬次,最低保證1萬次。產(chǎn)品設(shè)計初期要規(guī)劃好FLASH的壽命,盡量不讓某些需要頻繁改動的數(shù)據(jù)保存在FLASH中,這些數(shù)據(jù)可保存在RAM中,在斷電前才存入FLASH中。
3相關(guān)寄存器
1、FCTL2
FWKEY Bits
15-8
FCTLx password. Always read as 096h. Must be written as 0A5h or a PUC
will be generated.
FSSELx Bits
7−6
Flash controller clock source select
00 ACLK
01 MCLK
10 SMCLK
11 SMCLK
FNx Bits
5-0
Flash controller clock divider. These six bits select the divider for the flash
controller clock. The divisor value is FNx + 1. For example, when FNx=00h,
the divisor is 1. When FNx=03Fh the divisor is 64.
FCTL2 = FWKEY + FSSEL0 + FN0;
2、FCTL1
FWKEY
Bits
15-8
FCTLx password. Always read as 096h. Must be written as 0A5h or a PUC
will be generated.
MERAS
ERASE
Bit 2
Bit 1
Mass erase and erase. These bits are used together to select the erase mode.
MERAS and ERASE are automatically reset when EMEX is set.
MERAS ERASE Erase Cycle
0 0 No erase
0 1 Erase individual segment only
1 0 Erase all main memory segments
1 1 Erase all main and information memory segments
WRT Bit 6 Write. This bit is used to select any write mode. WRT is automatically reset
when EMEX is set.
0 Write mode is off
1 Write mode is on
3、FCTL3
FWKEYx Bits
15-8
FCTLx password. Always read as 096h. Must be written as 0A5h or a PUC
will be generated.
LOCK Bit 4 Lock. This bit unlocks the flash memory for writing or erasing. The LOCK bit
can be set anytime during a byte/word write or erase operation and the
operation will complete normally. In the block write mode if the LOCK bit is set
while BLKWRT=WAIT=1, then BLKWRT and WAIT are reset and the mode
ends normally.
0 Unlocked
1 Locked
4 實例
4.1 上電后向InfoFLASH某單元寫入一個字節(jié)數(shù)據(jù)
1、配置FLASH控制器時鐘。時鐘要求控制在250-470Khz之間。
FCTL2 = FWKEY + FSSEL0 + FN0; //2分頻
2、用指針指向地址Unsigned char *ptr= (unsigned char *) 0x1080;
3、進(jìn)入寫模式FCTL1=FWKEY+WRT;
4、清除鎖定位 FCTL3=FWKEY;
5、寫數(shù)據(jù) *ptr=0x30;
6、退出寫狀態(tài),恢復(fù)鎖存
注意FLASH操作期間不允許中斷,所以寫FLASH時要對全局中斷允許進(jìn)行操作。
代碼如下:
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
FCTL2 = FWKEY + FSSEL0 + FN0; // MCLK/2 for Flash Timing Generator
unsigned char *ptr=(unsigned char *)0x1080;
FCTL1=FWKEY+WRT;
FCTL3=FWKEY;
_DINT();
*ptr=0x30;
while(FCTL3&BUSY);
_EINT();
FCTL1=FWKEY;
FCTL3=FWKEY+LOCK;
4.2 向FLASH的InfoA中連續(xù)寫入128B數(shù)據(jù)
說明:通過FCTL1將FLASH設(shè)為段擦除模式后,只要向某個段內(nèi)任何存儲單元寫入0即可進(jìn)行擦除操作。
void write_SegA (char value)
{
char *Flash_ptr; // Flash pointer
unsigned int i;
Flash_ptr = (char *) 0x1080; // Initialize Flash pointer
FCTL1 = FWKEY + ERASE; // Set Erase bit
FCTL3 = FWKEY; // Clear Lock bit
*Flash_ptr = 0; // Dummy write to erase Flash segment
FCTL1 = FWKEY + WRT; // Set WRT bit for write operation
for (i=0; i<128; i++)
{
*Flash_ptr++ = value; // Write value to flash
}
FCTL1 = FWKEY; // Clear WRT bit
FCTL3 = FWKEY + LOCK; // Set LOCK bit
}
評論