智能车制作

标题: XS128写p_flash的问题 [打印本页]

作者: midanfushi    时间: 2010-5-18 22:09
标题: XS128写p_flash的问题
我想在XS128的P_flash上写点数据。。
在认真看了技术文档之后,我写了如下代码。。

void delayflash(uint j)
{

  uchar i;
  for(;j>0;j--)
  for(i=20;i>0;i--);
}

void Start_Command()
{
  FSTAT_CCIF = 1;//To start the command.
}

void Wait_Complete_Command()
{
  while(FSTAT_CCIF == 0);
}


void Pre_Start()
{

if(FCLKDIV_FDIVLD ==0)
FCLKDIV |= 0x0f;  //设置时钟分频,外部晶振为16Mhz

}


void Write_P_Flash(uint value)
{
  uchar number;
  uchar count=0;
  
  for(number=1;number>0;number--)
  {

     Pre_Start();
     
     if(FSTAT_CCIF)
     {
        if(FSTAT_ACCERR)
        {
         
          FSTAT_ACCERR = 1;
          delayflash(100);   
        }
        if(FSTAT_FPVIOL)
        {
         
          FSTAT_FPVIOL = 1;
          delayflash(100);
        }
        FCCOBIX=0;
        FCCOB=0X067F;
        
        FCCOBIX =1;
        FCCOB = des_addr;
   
        FCCOBIX=2;
        FCCOB=0xee;
  
        FCCOBIX=3;
        FCCOB=0xcc;

        FCCOBIX=4;
        FCCOB=0xdd;

        FCCOBIX=5;
        FCCOB=0xaa;

        des_addr+=8;     
      Start_Command();
      
      Wait_Complete_Command();
     }
   
  }

void main()
{
    ..........
    des_addr=0x4000;
    Write_P_Flash(0xee);
    Write_P_Flash(0xdd);
    ..........
}

参考了pdf文档中的如下描述
关于选频。。
When the FCLKDIV register is written, the FDIVLD bit is set automatically. If the FDIVLD bit is 0, the
FCLKDIV register has not been written since the last reset. If the FCLKDIV register has not been written,
any Flash program or erase command loaded during a command write sequence will not execute and the
ACCERR bit in the FSTAT register will set.
关于写命令。。
NVM command mode uses the indexed FCCOB register to provide a command code and its relevant
parameters to the Memory Controller. The user first sets up all required FCCOB fields and then initiates
the command’s execution by writing a 1 to the CCIF bit in the FSTAT register (a 1 written by the user
clears the CCIF command completion flag to 0). When the user clears the CCIF bit in the FSTAT register
all FCCOB parameter fields are locked and cannot be changed by the user until the command completes
(as evidenced by the Memory Controller returning CCIF to 1). Some commands return information to the
FCCOB register array.
关于写命令前的注意事项。。
Before launching a command, the ACCERR and FPVIOL bits in the FSTAT register must be clear (see
Section 19.3.2.7) and the CCIF flag should be tested to determine the status of the current command write
sequence. If CCIF is 0, the previous command write sequence is still active, a new command write
sequence cannot be started, and all writes to the FCCOB register are ignored.


现在的问题是。。执行一次写操作没问题,(无论是单步调试还是让程序自由运行)

可是如果让他执行两次写操作。。那么。。
再第二次执行 Start_Command()后FSTAT寄存器的MGSTAT[1:0]两位全部至1,并导致写失败。

一度以为是flash地址保护的原因,选了不同的地址段,并查看了FPROT寄存器的值,发现不是。。
另外,在写操作前我也尝试加了不少延时,不过,没什么效果。比较郁闷了。


。。。。
作者: zyhqq    时间: 2010-6-5 15:40
你好,我刚刚接触flash,用你的这个程序调试时,在memory的哪个区域寻找写入的数据啊??
作者: zyhqq    时间: 2010-6-5 21:09
你好,今天我发现了你的问题所在了,擦出flash的前提是CCIF位为0,希望对你有帮助……
作者: bingdianzxl    时间: 2011-4-17 15:06
hao




欢迎光临 智能车制作 (http://111.231.132.190/) Powered by Discuz! X3.2