freescale

来自「Freescale 系列单片机常用模块与综合系统设计」· 代码 · 共 91 行

TXT
91
字号
#include "jm60_flash.h"

extern char __SEG_START_FLASH_ROUTINES[];
extern char __SEG_SIZE_FLASH_ROUTINES[];

unsigned char FlashBuffer[55] = {"These bytes will be programed into a Flash location..."};


                              
#pragma CODE_SEG DEFAULT
void FlashInit(void)
{
  if (!(FCDIV & 0x80))
  {
    if (FSTAT&0x30)         //Check to see if FACCERR is set
    {
        FSTAT |= 0x30;  //write a 1 to FACCERR to clear
    }
    FCDIV = FLASH_CLOCK;
  }
}

#pragma CODE_SEG FLASH_ROUTINES
unsigned char Flash_Cmd(unsigned int FlashAddress, unsigned int FlashDataCounter, unsigned char* pFlashDataPtr, unsigned char FlashCommand)
{
    /* 标志位FACCERR或PVIOL是否被置1 */
    if (FSTAT&0x30)  
    {         
        /* Clear Flags if set*/
        FSTAT = FSTAT | 0x30;  
    }

    if (FlashDataCounter)
    {
      do
      {
          /* 等待最后一个突发命令完成*/
          while(!(FSTAT&0x80));
          
          /* 向Flash中写数据*/
          (*((volatile unsigned char *)(FlashAddress++))) = *pFlashDataPtr;
          pFlashDataPtr++;
          /* 写命令 */
          FCMD = FlashCommand;
          /* Put FCBEF at 1 */
          FSTAT = 0x80;
          
          /* 延时4个周期*/
          _asm NOP;
          _asm NOP;
          _asm NOP;
          _asm NOP;
          
          /* 检查Flash访问出错标志位或保护冲突标志位是否被置1 */
          if (FSTAT&0x30)
          {     
            /* 如果被置1,则函数返回1,表示错误 */
            return (1);
          }
      }while (--FlashDataCounter);
    }
    /* 等待最后一个命令完成 */
    while ((FSTAT&0x40)==0);
    /* 返回值为0,表示函数执行正确 */
    return (0);
}

#pragma CODE_SEG DEFAULT
/*
  This section of the code is the one that copies the routine into RAM. It is following the steps
  documented in Technical Note 228
*/

/*
Start_Copy_In_RAM refers to the begining of the segment ToCopyToRAM. 
This segment contains the functions after they have been copied to RAM.
*/
#define Start_Copy_In_RAM __SEG_START_FLASH_ROUTINES
#define Size_Copy_In_RAM __SEG_SIZE_FLASH_ROUTINES

void CopyInRAM(void) 
{
  char *srcPtr, *dstPtr;
  int count;
  srcPtr = (char *)Start_Copy_In_RAM;
  dstPtr = (char *)&Flash_Cmd;
  for (count = 0; count < (int) Size_Copy_In_RAM;  count++, dstPtr++, srcPtr++) 
  {
    *dstPtr = *srcPtr;
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?