这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 综合技术 » 基础知识 » FLASH 【原创】FLASH操作函数

共2条 1/1 1 跳转至

FLASH 【原创】FLASH操作函数

院士
2006-09-17 18:14:16     打赏
FLASH 【原创】FLASH操作函数



关键词: FLASH     原创     操作     函数    

院士
2006-12-22 22:43:00     打赏
2楼
问 int flash_block_erase( int block_num)
{
int i;
unsigned char *p;

if( block_num==0) return FLASH_BLOCKNUM_ERR;
if( block_num<MAX_BLOCK)
{
_DINT();
WDTCTL = WDTPW + WDTHOLD;

FCTL3=FWKEY;
while( FCTL3 & BUSY)
{
i++;
if (i>30000)
{
FCTL3=FWKEY+LOCK;
return FLASH_WRITE_ERR;
}
}

// FCTL2=FWKEY+SSEL1+FN2;
FCTL1=FWKEY+ERASE;

while ( FCTL3 & WAIT)
{
i++;
if (i>30000)
{
FCTL3=FWKEY+LOCK;
return FLASH_WRITE_ERR;
}
}
p=(unsigned char * ) (0xff00-block_num*512);

*p=0x0ff;

while ( FCTL3 & WAIT)
{
i++;
if (i>30000)
{
FCTL3=FWKEY+LOCK;
return FLASH_WRITE_ERR;
}

}


_EINT();

return 0;
}

if (block_num==120)
{
_DINT();
WDTCTL = WDTPW + WDTHOLD;
FCTL3=FWKEY;
while ( FCTL3 & BUSY)
{
i++;
if (i>30000)
{
FCTL3=FWKEY+LOCK;
return FLASH_WRITE_ERR;
}
}

FCTL1=FWKEY+ERASE;

while ( FCTL3 & WAIT)
{
i++;
if (i>30000)
{
FCTL3=FWKEY+LOCK;
return FLASH_WRITE_ERR;
}
}
p=(unsigned char * ) 0x1080;

*p=0x0ff;

while ( FCTL3 & WAIT)
{
i++;
if (i>30000)
{
FCTL3=FWKEY+LOCK;
return FLASH_WRITE_ERR;
}
}


_EINT();

return 0;
}
if (block_num==121)
{
_DINT();
WDTCTL = WDTPW + WDTHOLD;
FCTL3=FWKEY;
while ( FCTL3 & BUSY)
{
i++;
if (i>30000)
{
FCTL3=FWKEY+LOCK;
return FLASH_WRITE_ERR;
}
}
FCTL1=FWKEY+ERASE;

while ( FCTL3 & WAIT)
{
i++;
if (i>30000)
{
FCTL3=FWKEY+LOCK;
return FLASH_WRITE_ERR;
}
}


p=(unsigned char * ) 0x1000;

*p=0x0ff;

while ( FCTL3 & WAIT)
{
i++;
if (i>30000)
{
FCTL3=FWKEY+LOCK;
return FLASH_WRITE_ERR;
}

}


_EINT();

return 0;

}



return FLASH_BLOCKNUM_ERR;

}



int flash_char_write (char *buf ,char content)
{
int i;

WDTCTL = WDTPW + WDTHOLD;
_DINT();
FCTL3=FWKEY;
while( FCTL3 & BUSY)
{
i++;
if (i>30000)
{ FCTL3=FWKEY+LOCK;
return FLASH_WRITE_ERR;
}
}

// FCTL2=FWKEY+SSEL1+FN2;

FCTL1=FWKEY+WRT;

while( FCTL3 & BUSY)
{
i++;
if (i>30000)
{ FCTL3=FWKEY+LOCK;
return FLASH_WRITE_ERR;
}
}
*buf =content;

while( FCTL3 & BUSY)
{
i++;
if (i>30000)
{ FCTL3=FWKEY+LOCK;
return FLASH_WRITE_ERR;
}

}
FCTL1=FWKEY;
FCTL3=FWKEY+LOCK;
_EINT();
return 0;

}

int flash_block_write( unsigned char *dest ,unsigned char *source ,int buf_size)

{
unsigned char content;
int err_no;

while ( buf_size--)
{
content= *source;
err_no=flash_char_write( dest,content);
if (err_no!=0) return err_no;
source++;
dest++;
}

return 0;



} 1: 最好能加上注释,要不然看得头大。 2: 是啊,同意楼上的见解,给我们加点关键注释吧 3: 函数名字那么明白,就不要注释了吧 4: 好像缺了头文件 5: 加点注释!锻炼一下文采嘛! 6: void erase_seg(unsigned char segment) //擦除某一段的子程序,可以单独使用
{
unsigned int *flash_ptr;
_DINT(); //关闭中断
FCTL2 = FWKEY+FSSEL0+FN0; //选择FLASH的擦写时钟
switch(segment)
{
case 'A':flash_ptr=(unsigned int *)0x1000;break;//information segment A
case 'B':flash_ptr=(unsigned int *)0x1080;break;//information segment B
case 1:flash_ptr=(unsigned int *)0xFC00;break;//main segment 1
case 2:flash_ptr=(unsigned int *)0xFA00;break;//main segment 2
case 3:flash_ptr=(unsigned int *)0xF800; //main segment 3

default:break;
}
FCTL1=FWKEY+ERASE; //使能擦除操作
FCTL3=FWKEY; //FLASH解锁
*flash_ptr=0; //空写,启动擦除
FCTL1=FWKEY; //关闭擦除操作
FCTL3=FWKEY+LOCK; //FLASH锁定
_EINT(); //打开中断
}

共2条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]