【发布时间】:2016-03-29 11:37:50
【问题描述】:
我正在使用 MCF51EM256 Freescale 微控制器,但在擦除外部闪存 (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,表示函数执行正常
谢谢大家!
【问题讨论】:
-
与this相比,问题略有不同,但我很确定两者的答案都是一样的。
-
外部闪存是什么意思? AFAIK 这些例程适用于 onChip 存储闪存。
标签: c microcontroller erase flash-memory