c - 删除外部闪存(MCF51EM256 微 Controller )

标签 c microcontroller erase flash-memory

我正在使用 MCF51EM256 Freescale 微 Controller ,但在删除外部闪存 (0x20000 - 0x2FFFF) 时遇到一些问题。

这是我的主程序:

void main(void) {
  EnableInterrupts;

  FlashInit();

  DisableInterrupts;
  EraseFlash(0x020000);
  EraseFlash(0x020800);
  EraseFlash(0x020C00);
  EnableInterrupts;

  for(;;) {
    __RESET_WATCHDOG(); /* feeds the dog */
    SetLED(2, 2);       // Toggle led if the program is running
  } /* loop forever */
  /* please make sure that you never leave main */
}

其中“EraseFlash()”:

void EraseFlash(long addr){
    // The flash sector to erase is always 1 kB

    long eraseValue = 0xFFFFFFFF;
    Flash_Cmd((unsigned long)addr, 1, &eraseValue, 0x40);
}

和“Flash_Cmd()”:

#define FLASH_MASS_ERASE_CMD  0x41
#define FLASH_ERASE_CMD       0x40
#define FLASH_PROGRAM_CMD     0x20
#define FLASH_BURST_CMD       0x25

UINT8 /*far*/ 
Flash_Cmd(UINT32 FlashAddress, 
      UINT16 FlashDataCounter, 
      UINT32 *pFlashDataPtr, 
      UINT8 FlashCommand)
{
  /* Check to see if FACCERR or PVIOL is set */
  if (FSTAT &0x30)  
  {         
      /* Clear Flags if set*/
      FSTAT = 0x30;  
  }

  if (FlashDataCounter)
  {
    do
    {
        /* Wait for the Last Busrt Command to complete */
        while(!(FSTAT&FSTAT_FCBEF_MASK)){};/*wait until termination*/

        /* Write Data into Flash*/
        (*((volatile unsigned long *)(FlashAddress))) = *pFlashDataPtr;
        FlashAddress += 4;
        pFlashDataPtr++;

        /* Write Command */
        FCMD = FlashCommand;

        /* Put FCBEF at 1 */
        FSTAT = FSTAT_FCBEF_MASK;

        asm (NOP);
        asm (NOP);
        asm (NOP);

         /* Check if Flash Access Error or Protection Violation Error are Set */
        if (FSTAT&0x30)
        {     
          /* If so, finish the function returning 1 to indicate error */
          return (1);
        }

    }while (--FlashDataCounter);
  }
  /* wait for the last command to complete */
  while ((FSTAT&FSTAT_FCCF_MASK)==0){};/*wait until termination*/

  /* Return zero to indicate that the function executed OK */
  return (0);
}

我的 main 从未到达 for(;;) 循环,因为被第三次调用 EraseFlash() 阻塞。

如果我只调用两次或更少的电话,它就会起作用。 示例:

void main(void) {
  EnableInterrupts;

  Platform_GPIO_Init();
  FlashInit();

  DisableInterrupts;
  EraseFlash(0x020000);
  EraseFlash(0x020800);
  EnableInterrupts;

  for(;;) {
    __RESET_WATCHDOG(); /* feeds the dog */
    SetLED(2, 2);       // Toggle led if the program is running
  } /* loop forever */
  /* please make sure that you never leave main */
}

有人可以告诉我我做错了什么吗?我还尝试了其他闪存地址并且没有禁用中断,结果是一样的。

编辑:两次调用都返回0,这意味着函数执行正常

谢谢大家!

最佳答案

我检查了数据表并更改了闪存驱动程序 header 中的 FLASH_CLOCK:

(SYSTEM_CLOCK/200000)(SYSTEM_CLOCK/400000)

之前:

#if (SYSTEM_CLOCK/2) > 12800000 /* 12.8 MHz */
    #define FLASH_CLOCK (UINT8)(( (SYSTEM_CLOCK/3200000) -1) | 0x40)
#else
    #define FLASH_CLOCK (unsigned char)( (SYSTEM_CLOCK/200000) -1)//<200KHz
#endif

现在:

#if (SYSTEM_CLOCK/2) > 12800000 /* 12.8 MHz */
    #define FLASH_CLOCK (UINT8)(( (SYSTEM_CLOCK/3200000) -1) | 0x40)
#else
    #define FLASH_CLOCK (unsigned char)( (SYSTEM_CLOCK/400000) -1)//<200KHz
#endif

我的程序终于可以运行了。

关于c - 删除外部闪存(MCF51EM256 微 Controller ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36283203/

相关文章:

GCC:如何在 MCU 上完全禁用堆使用?

c - 如何使用 fork() 调用创建 k 个进程,其中 k 不是 2 的幂

c - 为什么预处理器的这个输出看起来没有意义?

c - 使用 native 单元测试项目在 Visual Studio 2017 中对 C 代码进行单元测试

c - 基于整数的传感器融合/卡尔曼滤波器

c - 8位单片机生成伪随机数最快的方法是什么?

c++ - for 语句超出范围错误

iphone - 如何清除并重新绘制 quartz 图

c++ - STL 迭代器 : Assertion Error

c - 在我的 TCP 连接中,客户端发送 Hello,但服务器收到 Hellob