会长
- 积分
- 3077
- 威望
- 1484
- 贡献
- 901
- 兑换币
- 931
- 注册时间
- 2013-6-7
- 在线时间
- 346 小时
- 毕业学校
- 燕山大学
|
3#
楼主 |
发表于 2014-6-5 02:04:06
|
只看该作者
#include"flash.h"
void FlashPageErase(uint8 page)//page为要擦除的页(0=<page<=127),选第127页
{
uint32*address;
uint8 sector;
address=(uint32*)(page*FLASH_PAGESIZE+FLASH_PROG_OFFSET);//1.算出页首地址
sector=page/4;//对应的扇区
MCF_CFM_CFMPROT&=~MCF_CFM_CFMPROT_PROTECT(sector);//2.解除对页所在扇区的保护
MCF_CFM_CFMCLKD=MCF_CFM_CFMCLKD_DIV(30)
|MCF_CFM_CFMCLKD_PRDIV8;//3,设置flash的工作频率为161.3khz
while(!(MCF_CFM_CFMUSTAT&MCF_CFM_CFMUSTAT_CBEIF));//4等待命令缓冲区空
MCF_CFM_CFMUSTAT|=MCF_CFM_CFMUSTAT_PVIOL;//清侵害保护标志位
MCF_CFM_CFMUSTAT|=MCF_CFM_CFMUSTAT_ACCERR;//清访问出错标志位
*address=0x0;//5向擦除页内任意地址写任意值,以定位要擦除的的页
MCF_CFM_CFMCMD=MCF_CFM_CFMCMD_CMD(FLASH_PAGE_ERASE);//6页擦除命令
MCF_CFM_CFMUSTAT|=MCF_CFM_CFMUSTAT_CBEIF;//7,启动命令执行
while(!(MCF_CFM_CFMUSTAT&MCF_CFM_CFMUSTAT_CCIF));//8等待命令执行完成
}
void FlashPageWrite(uint8 page,uint16 pstart,uint32 N)//page是页号,pstart是该页中的字号(0=<page<=127),(0=<pstart<=1023)
{
uint32*address;
uint8 sector;
address=(uint32*)(page*FLASH_PAGESIZE+pstart*4+FLASH_PROG_OFFSET);//1.算出字首地址
sector=page/4;//对应的扇区
MCF_CFM_CFMPROT&=~MCF_CFM_CFMPROT_PROTECT(sector);//2.解除对页所在扇区的保护
MCF_CFM_CFMCLKD=MCF_CFM_CFMCLKD_DIV(30)
|MCF_CFM_CFMCLKD_PRDIV8;//3,设置flash的工作频率为161.3khz
while(!(MCF_CFM_CFMUSTAT&MCF_CFM_CFMUSTAT_CBEIF));//4等待命令缓冲区空
MCF_CFM_CFMUSTAT|=MCF_CFM_CFMUSTAT_PVIOL;//清侵害保护标志位
MCF_CFM_CFMUSTAT|=MCF_CFM_CFMUSTAT_ACCERR;//清访问出错标志位
*address=N;//5写入数据
MCF_CFM_CFMCMD=MCF_CFM_CFMCMD_CMD(FLASH_PROGRAM);//6写入命令
MCF_CFM_CFMUSTAT|=MCF_CFM_CFMUSTAT_CBEIF;//7,启动命令执行
while(!(MCF_CFM_CFMUSTAT&MCF_CFM_CFMUSTAT_CCIF));//8等待命令执行完成
MCF_CFM_CFMPROT|=MCF_CFM_CFMPROT_PROTECT(sector);//9.给所在页加保护
}
uint32 FlashRead(uint8 page,uint16 pstart)//page是页号,pstart是该页中的字号
{
uint32*address;
uint32 n;
address=(uint32*)(page*FLASH_PAGESIZE+pstart*4);//1.算出字首地址
n=*address;
return n;
} |
|