写了一夜,把冷火的flash在线编程给开发出来了
时间:11-14 15:19 阅读:2065次
*温馨提示:点击图片可以放大观看高清大图
简介:从此,调参数再也不用一遍一遍下程序了,上代码。
#ifndef FLASH_H_
#define FLASH_H_#include "support_common.h"#define FLASH_PAGESIZE 4096#define FLASH_PROG_OFFSET 0x44000000//地址偏移量#define FLASH_PROGRAM 0x20//写入命令#define FLASH_PAGE_ERASE 0x40//页擦除命令void FlashPageErase(uint8 page);//擦除指定的页void FlashPageWrite(uint8 page,uint16 pstart,uint32 N);//写flashuint32FlashRead(uint8 page,uint16 pstart);//读flash#endif
#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.给所在页加保护 }uint32FlashRead(uint8 page,uint16 pstart)//page是页号,pstart是该页中的字号{ uint32*address; uint32 n; address=(uint32*)(page*FLASH_PAGESIZE+pstart*4);//1.算出字首地址 n=*address; return n;}