共2条
1/1 1 跳转至页
FLASH 【原创】FLASH操作函数
问
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(); //打开中断
}
{
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 跳转至页
回复
有奖活动 | |
---|---|
【有奖活动】分享技术经验,兑换京东卡 | |
话不多说,快进群! | |
请大声喊出:我要开发板! | |
【有奖活动】EEPW网站征稿正在进行时,欢迎踊跃投稿啦 | |
奖!发布技术笔记,技术评测贴换取您心仪的礼品 | |
打赏了!打赏了!打赏了! |
打赏帖 | |
---|---|
vscode+cmake搭建雅特力AT32L021开发环境被打赏30分 | |
【换取逻辑分析仪】自制底板并驱动ArduinoNanoRP2040ConnectLCD扩展板被打赏47分 | |
【分享评测,赢取加热台】RISC-V GCC 内嵌汇编使用被打赏38分 | |
【换取逻辑分析仪】-基于ADI单片机MAX78000的简易MP3音乐播放器被打赏48分 | |
我想要一部加热台+树莓派PICO驱动AHT10被打赏38分 | |
【换取逻辑分析仪】-硬件SPI驱动OLED屏幕被打赏36分 | |
换逻辑分析仪+上下拉与多路选择器被打赏29分 | |
Let'sdo第3期任务合集被打赏50分 | |
换逻辑分析仪+Verilog三态门被打赏27分 | |
换逻辑分析仪+Verilog多输出门被打赏24分 |