微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 写MSP430片内flash

写MSP430片内flash

时间:11-25 来源:互联网 点击:

0x15000+0x200->

OSTCBCur->

从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.

流程图

图片地址:http://wenku.baidu.com/view/b82c0b1a52d380eb62946d4b.html

在擦除或者写入的过程中访问Flash,见下表

Flash的寄存器

FCTL1,选择擦除和写入模式的寄存器

FRKEY/FWKEY 高八位为密码读的密码为96h,写的密码为A5h。

BLKWRT 块写入模式选择位,可以自动被EMEX置位

WRT 字写入模式选择位,可任意自动被EMEX置位

MERASE和ERASE,擦除模式选择位

FCTL2时钟选择寄存器

FWKEYx密码位

FSSELx时钟选择位

FNx分频比 分频值等于FN+1

两个例子

#include

#include "BoardConfig.h"

void Write_A(uchar value);

void Copy_A2B(void);

void main( void )

{

// Stop watchdog timer to prevent time out reset

WDTCTL = WDTPW + WDTHOLD;

BoardConfig(0xb8);

FCTL2 = FWKEY + FSSEL0 + FN0; //Select source

uchar value = 0;

for(;;)

{

Write_A(value++); //Write data to segment A

Copy_A2B(); //Copy data from segment A to segment B

_NOP();

}

}

void Write_A(uchar value)

{

uchar i;

uchar *Flash_ptr;

Flash_ptr = (uchar *)0x1080;

FCTL1 = FWKEY + ERASE; //Set ERASE mode

FCTL3 = FWKEY; //Clear LOCK

*Flash_ptr = 0; //Dummy write

FCTL1 = FWKEY + WRT;

for(i = 0;i < 128;i++)

{

*Flash_ptr++ = value; //Write value

}

FCTL1 = FWKEY; //Clear WRT

FCTL3 = FWKEY + LOCK; //Set LOCK

}

//Copy data from B to A

void Copy_A2B(void)

{

uchar *Flash_ptrA;

uchar *Flash_ptrB;

uint i;

Flash_ptrA = (uchar *)0X1080;

Flash_ptrB = (uchar *)0x1000;

FCTL1 = FWKEY + ERASE;

FCTL3 = FWKEY;

*Flash_ptrB = 0;

FCTL1 = FWKEY + WRT;

for(i = 0;i < 128;i++)

{

*Flash_ptrB++ = *Flash_ptrA++;

}

FCTL1 = FWKEY;

FCTL3 = FWKEY + LOCK;

}

再来个块写入的(TI例程)

//****************************************************************************

// MSP430F14x Demo - Flash In-System Programming, BlockWrite

//

// Description: This program first copies the FlashWrite routine to RAM, then

// erases flash seg A, then it increments all values in seg A using the 64

// byte block write mode.

//

// Assumed default MCLK = DCO ~800 kHz.

// Minimum RAM requirement = 512 bytes

//

// ** Set Breakpoint on NOP in the Mainloop to avoid Stressing Flash **

//

// MSP430F149

// -----------------

// /|| XIN|-

// | | |

// --|RST XOUT|-

// | |

//

// H. Grewal / L. Westlund

// Texas Instruments Inc.

// Jun 2006

// Built with IAR Embedded Workbench Version: 3.30A

//******************************************************************************

#include

// Global variables

char value = 0; // 8-bit value to write to segment A

char* Flash_ptr; // Flash pointer

char* RAM_ptr; // RAM pointer

char* END_ptr; // End of FlashWrite routine

// Function prototypes

void FlashWrite();

void CopyRoutine();

void End_of_FlashWrite();

void main(void)

{

WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer

_DINT(); // Diable Interrupts

CopyRoutine(); // Copy FlashWrite routine to RAM

_EINT(); // Enable Interrupts

while(1) // Repeat forever

{

Flash_ptr = (char *) 0x1000; // Initialize Flash pointer

FCTL2 = FWKEY + FSSEL1 + FN0; // MCLK/2 for Flash Timing Generator

FCTL1 = FWKEY + ERASE; // Set Erase bit

FCTL3 = FWKEY; // Clear Lock bit

*Flash_ptr = 0; // Dummy write to erase Flash segment

while(!(FCTL3 & WAIT)); // WAIT until Flash is ready

asm("CALL #300h"); // Execute FlashWrite from RAM

// Inline Assembly

value++; // Increment value

_NOP(); // SET BREAKPOINT HERE

}

}

void CopyRoutine()

{

Flash_ptr = (char*)FlashWrite; // Set pointer to FlashWrite routine

RAM_ptr = (char*)0x0300; // Set pointer to RAM

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top