garfield

MCF52233_CFM

0
阅读(21887)

MCF52233_CFM——Coldfire Flash Module

  Coldfire的CFM功能是把它的flash当作EEPROM来用。使用起来还是很方便的,只要完成几个小的功能函数就行了:

  第一个是CFM的初始化CFM_init(),完成时钟的设置;

  第二个是页擦除CFM_Page_eraser(),完成一页的擦除工作,Coldfire的256K flash被分成128页,每2K为1页,只有在擦除后才能正确地向一个地址中写入数据;

  第三个是CFM_Write(),完成向某地址写入数据的功能,Coldfire的CFM是按字操作的,以4B为一个最小单位。

  CFM有5个CMD:Blank Check 、Pageerase verify、 Word program 、Page erase 、Mass erase但只要其中Word program 、Page erase 两个就能满足一般的使用了。

  在使用CFM功能时对它的地址这块可能会有些迷惑,这里简单介绍下:MCF52233有256K的flash,默认情况下起始地址是0x44000000(#define CFM_IPS_FLASH_ADDR      0x44000000),我们不能在存有程序的地方擦、写数据,最简单的办法就是在flash的最后面操作,这样就不要考虑烧进去的程序结束的位置了。起始地址加上256K得到最后一个存储单元的地址即#define FLASH_END_ADDRESS CFM_IPS_FLASH_ADDR+0x40000),我们就从FLASH_END_ADDRESS 开始往前写我们的数据。

  最后一点就是怎样读我们写的数据,其实非常简单,用read_data=(*(uint32 *)(address))就将address处的数据赋给了read_data;同样,在写数据的时候会用到(*(uint32*)(address))=write_data来将数据写到address地址处。

程序如下:

/*CFM初始化*/

uint8 CFM_init(unsigned long FlashClk)
{
 unsigned char fclk_val;
 
 if ((MCF_CFM_CFMCLKD & MCF_CFM_CFMCLKD_DIVLD)==0)
 {
     //Initialize FCLKDIV register to ensure we can program/erase */
   fclk_val = (unsigned char)(FlashClk/8/200-1);
   MCF_CFM_CFMCLKD |= MCF_CFM_CFMCLKD_DIV(fclk_val)

              | MCF_CFM_CFMCLKD_PRDIV8;
 }

 //Disable all protection (if LOCK not set)*/
 MCF_CFM_CFMPROT=0;
 
 //Clear any errors 
 MCF_CFM_CFMUSTAT |=MCF_CFM_CFMUSTAT_PVIOL

            |MCF_CFM_CFMUSTAT_ACCERR;
 return 0;
}
 

 

/*页擦除*/ 

uint8 CFM_page_erase(unsigned char page)
{
 while (!(MCF_CFM_CFMUSTAT&MCF_CFM_CFMUSTAT_CBEIF));

 //Clear any errors 
 MCF_CFM_CFMUSTAT =MCF_CFM_CFMUSTAT_PVIOL

            |MCF_CFM_CFMUSTAT_ACCERR;
 
 (*(vuint32*)(FLASH_START_ADDRESS+page*0x800))=1;
 
 MCF_CFM_CFMCMD=MCF_CFM_CFMCMD_PAGE_ERASE;
 //Launch CMD
 MCF_CFM_CFMUSTAT=MCF_CFM_CFMUSTAT_CBEIF;
 //wait for complete and check result
 while(!(MCF_CFM_CFMUSTAT&MCF_CFM_CFMUSTAT_CCIF))
 {
  if(MCF_CFM_CFMUSTAT&MCF_CFM_CFMUSTAT_PVIOL) return 0; 
 }
 if(MCF_CFM_CFMUSTAT&MCF_CFM_CFMUSTAT_ACCERR) return 0;
 return 1;
}

 

/*flash写操作*/

uint8 CFM_write(unsigned int address ,unsigned int data)
{
 if((uint32)address&0x00000001) return 0;
 //Clear any errors
 MCF_CFM_CFMUSTAT=MCF_CFM_CFMUSTAT_PVIOL

           |MCF_CFM_CFMUSTAT_ACCERR;
 
 (*(vuint32*)(FLASH_END_ADDRESS-4-address))=data;
 //launch command
 MCF_CFM_CFMCMD=MCF_CFM_CFMCMD_WORD_PROGRAM;
 MCF_CFM_CFMUSTAT=MCF_CFM_CFMUSTAT_CBEIF;
 //wait for complete and check result
 while(!(MCF_CFM_CFMUSTAT&MCF_CFM_CFMUSTAT_CCIF))
 {
  if(MCF_CFM_CFMUSTAT&MCF_CFM_CFMUSTAT_PVIOL) return 0;
 }
 if(MCF_CFM_CFMUSTAT&MCF_CFM_CFMUSTAT_ACCERR) return 0;
 return 1;
}