【问题标题】:Erasing external FLASH擦除外部 FLASH
【发布时间】:2016-03-28 10:39:00
【问题描述】:

我正在使用 MCF51EM256 飞思卡尔微控制器,但在将数据存储在外部闪存中以使其持久存在时遇到了一些问题。

我需要存储这个结构:

typedef struct {
  ui64_s Ea_ps; 
  ui64_s Ea_ng;  
  ui64_s Er_q1;
  ui64_s Er_q2;
  ui64_s Er_q3;
  ui64_s Er_q4;
  uint16 F_ea;
  uint16 F_er;
}Ws_EnergyAcc64;

地点:

typedef union{    
  uint64 v;
  uint32 p[2];  
} ui64_s;

和:

typedef unsigned long long int uint64;
typedef unsigned long int uint32;
typedef unsigned short int uint16;

为了做到这一点,我实现了这个功能:

void Save_Flash_WsEnergyAcc(long addr, Ws_EnergyAcc64* Acc) {

  // WsEnergyAcc struct needs 56 bytes in Flash

  uint32 F_ea_32 = (uint32) Acc->F_ea;
  uint32 F_er_32 = (uint32) Acc->F_er;

  Flash_Erase(addr);
  Flash_Erase(addr + 4);
  Flash_Burst(addr, 2, Acc->Ea_ps.p);

  Flash_Erase(addr + 8);
  Flash_Erase(addr + 12);
  Flash_Burst(addr + 8, 2, Acc->Ea_ng.p);

  Flash_Erase(addr + 16);
  Flash_Erase(addr + 20);
  Flash_Burst(addr + 16, 2, Acc->Er_q1.p);

  Flash_Erase(addr + 24);
  Flash_Erase(addr + 28);
  Flash_Burst(addr + 24, 2, Acc->Er_q2.p);

  Flash_Erase(addr + 32);
  Flash_Erase(addr + 36);
  Flash_Burst(addr + 32, 2, Acc->Er_q3.p);

  Flash_Erase(addr + 40);
  Flash_Erase(addr + 44);
  Flash_Burst(addr + 40, 2, Acc->Er_q4.p);

  Flash_Erase(addr + 48);
  Flash_Burst(addr + 48, 2, &F_ea_32);

  Flash_Erase(addr + 52);
  Flash_Burst(addr + 52, 2, &F_er_32);

}

其中“Flash_Burst”和“Flash_Erase”:

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

/* Macros to call the function using the different features */
#define Flash_Erase(Address) \
      Flash_Cmd((UINT32)Address, (UINT16)1, (UINT32*)CUSTOM_ROM_ADDRESS, FLASH_ERASE_CMD)

#define Flash_Burst(Address, Size, DataPtr) \
      Flash_Cmd((UINT32)Address, (UINT16)Size, (UINT32*)DataPtr, FLASH_BURST_CMD)

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);
}

我也定义了:

extern unsigned char __CUSTOM_ROM[];
extern unsigned char __CUSTOM_ROM_SIZE[];

#define CUSTOM_ROM_ADDRESS      (unsigned long int)__CUSTOM_ROM
#define CUSTOM_ROM_SIZE         (unsigned long int)__CUSTOM_ROM_SIZE

我不明白什么是 CUSTOM_ROM_ADDRESS,它会导致我的项目出现链接错误:

C:/Freescale/CW MCU    v10.6.4/MCU/ColdFire_Tools/Command_Line_Tools/mwldmcf|Linker|Error
>Undefined : "__CUSTOM_ROM"

我认为它可能是存储在已擦除地址中的数据,我尝试做这样的事情(而不是 Flash_Erase(address)):

void EraseFlash(long addr) {

    uint32 eraseData = 0xFFFFFFFF;
    Flash_Cmd((uint32)addr, (uint16)1, (uint32*)&eraseData, 0x40);

}

它在“Save_Flash_WsEnergyAcc”的第一次执行中起作用,但我无法解释为什么它会在下一次阻塞 MCU。

¿有人能告诉我我做错了什么吗? 谢谢大家!

【问题讨论】:

  • CUSTOM_ROM(以及相关的#defines)看起来不像您的程序需要的任何东西 - 也许它是一些链接器魔法,应该在您的电路板文档中的某处进行描述。另外,我强烈怀疑您需要在执行 EraseFlash 命令后重置某些内容。
  • 强烈建议使用:#include <stdint.h> 来定义uint32 等。这将使代码更不容易出错,更容易维护,也更容易调试。它还将使代码更加可移植。
  • 所有涉及的指针必须声明为volatile
  • “它在“Save_Flash_WsEnergyAcc”的第一次执行中有效,但我无法解释为什么它会在下一次阻塞 MCU。”这闻起来就好像您正在编程时从同一个闪存库执行闪存编程代码。尝试单步执行程序,突然一切正常了吗?如果是这样,那么这就是问题所在。您必须确保闪存驱动程序不在正在编程的存储区中。
  • 此外,大多数飞思卡尔微控制器闪存都对正确的预分频器时钟很挑剔。我不知道这个特定的设备,但 IIRC 通常他们想要一个提供 150-200kHz 时钟的预分频器(查看数据表)。如果时钟不在允许的频率范围内,你会得到各种奇怪的行为。在编写这些闪存驱动程序时,获得正确的时钟始终是最棘手的部分。您可以检查一个标志以确保时钟正常,您必须检查该标志!

标签: c embedded microcontroller erase flash-memory


【解决方案1】:

飞思卡尔 MCF51EM256 微控制器仅允许擦除 1024 字节扇区的闪存。

如果我每次要写入闪存时都调用 Erase 函数,MCU 将被阻塞。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-11
    • 2021-10-20
    • 2021-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多