MSP430Flash读写操作总结Word文档格式.docx
《MSP430Flash读写操作总结Word文档格式.docx》由会员分享,可在线阅读,更多相关《MSP430Flash读写操作总结Word文档格式.docx(12页珍藏版)》请在冰豆网上搜索。
从Flash中擦除的过程中所有的定时都会被Flash控制,CPU被挂起。
擦除完成后需要一个假写入CPU才能复位。
从Flash擦除时有可能把后面CPU需要执行的代码擦除。
如果发生这样的情况,在擦除后CPU的执行状况将不可预测。
Flash中擦除流程图
从RAM中擦除
从RAM中擦除时CPU不会被挂起,可以继续执行代码。
必须检测BUSY位以判断擦除是否结束,如果在擦除的过程中(即BUSY=1时)访问Flash,这是一个违规的访问,ACCVIFG会置位,而擦除的结果也将不可预测。
RAM中擦除流程图
FlashWrite
MSP430X14X有两种写入模式,分为段写入(byte/wordwrite),和块写入(BlockWrite),块写入要快得多,但是操作麻烦,在擦除的过程中不能有一个Flashword(low+highbyte),则会发生损坏。
CPU不能在BUSY=1时访问Flash,否则ACCFIG将置位写入将不可预测。
1.Byte/Wordwrite
Byte/Word写入可以从Flash或者RAM初始化,当从Flash中初始化时,所有的定时都会被Flash控制,CPU被挂起。
写完后CPU将继续执行后面的代码。
当从RAM中初始化时,BUSY必须在CPU访问Flash前置0.否则ACCFIG将被置位,写入的结果将不可预测。
在Byte/Word写模式下写入总时间不能超过4ms,如果超过了,当再想这块任何地址写入数据时必须先擦除。
Byte/Word写入流程图
从RAM中执行Byte/Word写入
块写入
块写入时没一小块不能超过t_cpt=4ms,块写入只能从RAM中进行,在块写入的过程中WAIT位要置0,当想Flash中写入数据时,需要先检查WAIT位是否为1.当前块写完后BLKWRT要清0.
流程图
在擦除或者写入的过程中访问Flash,见下表
Flash的寄存器
FCTL1,选择擦除和写入模式的寄存器
FRKEY/FWKEY高八位为密码读的密码为96h,写的密码为A5h。
BLKWRT块写入模式选择位,可以自动被EMEX置位
WRT字写入模式选择位,可任意自动被EMEX置位
MERASE和ERASE,擦除模式选择位
FCTL2时钟选择寄存器
FWKEYx密码位
FSSELx时钟选择位
FNx分频比分频值等于FN+1
两个例子
#include<
msp430x14x.h>
#include"
BoardConfig.h"
voidWrite_A(ucharvalue);
voidCopy_A2B(void);
voidmain(void)
{
//Stopwatchdogtimertopreventtimeoutreset
WDTCTL=WDTPW+WDTHOLD;
BoardConfig(0xb8);
FCTL2=FWKEY+FSSEL0+FN0;
//Selectsource
ucharvalue=0;
for(;
;
)
Write_A(value++);
//WritedatatosegmentA
Copy_A2B();
//CopydatafromsegmentAtosegmentB
_NOP();
}
voidWrite_A(ucharvalue)
uchari;
uchar*Flash_ptr;
Flash_ptr=(uchar*)0x1080;
FCTL1=FWKEY+ERASE;
//SetERASEmode
FCTL3=FWKEY;
//ClearLOCK
*Flash_ptr=0;
//Dummywrite
FCTL1=FWKEY+WRT;
for(i=0;
i<
128;
i++)
*Flash_ptr++=value;
//Writevalue
FCTL1=FWKEY;
//ClearWRT
FCTL3=FWKEY+LOCK;
//SetLOCK
//CopydatafromBtoA
voidCopy_A2B(void)
uchar*Flash_ptrA;
uchar*Flash_ptrB;
uinti;
Flash_ptrA=(uchar*)0X1080;
Flash_ptrB=(uchar*)0x1000;
*Flash_ptrB=0;
*Flash_ptrB++=*Flash_ptrA++;
再来个块写入的(TI例程)
//****************************************************************************
//MSP430F14xDemo-FlashIn-SystemProgramming,BlockWrite
//
//Description:
ThisprogramfirstcopiestheFlashWriteroutinetoRAM,then
//erasesflashsegA,thenitincrementsallvaluesinsegAusingthe64
//byteblockwritemode.
//AssumeddefaultMCLK=DCO~800kHz.
//MinimumRAMrequirement=512bytes
//**SetBreakpointonNOPintheMainlooptoavoidStressingFlash**
//MSP430F149
//-----------------
///|\|XIN|-
//|||
//--|RSTXOUT|-
//||
//H.Grewal/L.Westlund
//TexasInstrumentsInc.
//Jun2006
//BuiltwithIAREmbeddedWorkbenchVersion:
3.30A
//******************************************************************************
msp430x16x.h>
//Globalvariables
charvalue=0;
//8-bitvaluetowritetosegmentA
char*Flash_ptr;
//Flashpointer
char*RAM_ptr;
//RAMpointer
char*END_ptr;
//EndofFlashWriteroutine
//Functionprototypes
voidFlashWrite();
voidCopyRoutine();
voidEnd_of_FlashWrite();
voidmain(void)
//Stopwatchdogtimer
_DINT();
//DiableInterrupts
CopyRoutine();
//CopyFlashWriteroutinetoRAM
_EINT();
//EnableInterrupts
while
(1)//Repeatforever
Flash_ptr=(char*)0x1000;
//InitializeFlashpointer
FCTL2=FWKEY+FSSEL1+FN0;
//MCLK/2forFlashTimingGenerator
//SetErasebit
//ClearLockbit
//DummywritetoeraseFlashsegment
while(!
(FCTL3&
WAIT));
//WAITuntilFlashisready
asm("
CALL#300h"
);
//ExecuteFlashWritefromRAM
//InlineAssembly
value++;
//Incrementvalue
//SETBREAKPOINTHERE
voidCopyRoutine()
Flash_ptr=(char*)FlashWrite;
//SetpointertoFlashWriteroutine
RAM_ptr=(char*)0x0300;
//SetpointertoRAM
END_ptr=(char*)End_of_FlashWrite;
//SetpointertoEnd_of_FlashWrite
while(END_ptr!
=Flash_ptr)//CheckforendofFlashWrite
*RAM_ptr=*Flash_ptr;
//CopywordtoRAM
Flash_ptr++;
//IncrementFlashpointer
RAM_ptr++;
//IncrementRAMpointer
voidFlashWrite()
volatileinti;
//Useaswritecounter
Flash_ptr=(char*)0x1000;
while(FCTL3&
BUSY);
//CheckFlashBUSYbit
FCTL1=FWKEY+BLKWRT+WRT;
//Enableblock-writeoperation
i<
64;
i++)
*Flash_ptr=value;
//Writevaluetoflash
//Double-incrementFlashpointer
//ClearBLKWRT&
WRTbits
//ResetLOCKbit
return;
//Exitsroutine
voidEnd_of_FlashWrite(){}//MarksendofFlashWrite