查看: 5996|回复: 5

DZ60 Flash 擦写

[复制链接]

该用户从未签到

3

主题

27

帖子

0

新手上路

Rank: 1

积分
36
最后登录
1970-1-1
发表于 2012-3-5 14:42:39 | 显示全部楼层 |阅读模式
各位好:
        我做的一个DZ60Flash 擦写的程序,怎么就执行不了呢?能看到擦和写的子程序调到RAM里,也是在RAM里执行的,也能正常跳出;擦写子程序完全按照DZ60 手册的流程编写,不知什么问题就是擦和写的功能,附上代码,请高手赐教! 
*******************************************************************************************************************************************

byte (* pointERom_fun)(),(* pointERam_fun)(),(* pointWRom_fun)(),(* pointWRam_fun)();//定义擦除函数指针和指向擦出函数指针的指针
byte *pointERom_fun_temp, *pointERam_fun_temp, *pointWRom_fun_temp, *pointWRam_fun_temp; 
byte ProgmData[30]={0,0};
 
/**************************************************************************************
函数名: void DZ60_Init(void)
功  能: 系统时钟
说  明: 系统时钟设置请勿随意修改
输  入:无       
**************************************************************************************
void DZ60_Init(void)
{
  SOPT1=0X00;     //disable COP, enable stop mode
  MCGC2=0X36;
      //BDIV=%00   总线周期/1  
      //RANGE=1选择高频范围
      //HGO=1高增益运行配置外部振荡器
      //EREFS=1使用外部晶振
      //ERCLKEN=1外部时钟处于激活状态
  while(!MCGSC_OSCINIT);
  asm SEI   //关中断
  MCGC1=0XB0;         
  while(MCGSC_IREFST);
  while((MCGSC_CLKST)^(0X02));      //PEI--->FBE
  MCGC2_LP=1;
  asm CLI   //开中断
  MCGC1_RDIV=0X02;      //  /4
  MCGC3_VDIV=0X04;     //  *16
  MCGC3_PLLS=1;
  while(!MCGSC_PLLST);      //FBE--->BLPE
  MCGC2_LP=0;
  while(!MCGSC_LOCK)      //FBE---&gtBE
  
  MCGC1_CLKS=0X00;
  while((MCGSC_CLKST)^(0X03));  //PBE---&gtEE    
  FCDIV=39;                  //将flash时钟分频器FCDIV 0x4C  16.6
}
/******************************************************************************
函数名        :        Sector_Erase()
功  能  : 对单个区进行擦除   
******************************************************************************/
#pragma CODE_SEG EBoot_SEG
volatile void Sector_Erase(void)
    {
      byte *Addr;
      Addr=(byte *)0xDA00;
      if (FSTAT_FACCERR)        /* Check for access error */
         {                         
          FSTAT_FACCERR = 1;       /* Clear access error flag */
         }  
         while (!FSTAT_FCBEF);     /* Wait here if command buffer is full */  
         *Addr = 0x67;             /* Write to flash */  
         FCMD = 0x40;              /* Write sector erase command */  
         FSTAT_FCBEF = 1;          /* Write 1 to FCBEF to launch the command */  
         asm
           {                       /* Wait 4 cycles */
            nop
            nop
            nop
            nop
            nop
            nop
            nop
           }          
      if (FSTAT_FACCERR)        /* Check for access error访问错误标记 1错误 0 OK */
          {                         
           FSTAT_FACCERR = 1;   /* Clear access error flag */         
          }
      if (FSTAT_FPVIOL)
          {                     /* Check for protection violation 保护规则违反标记 1违反 0 OK */ 
           FSTAT_FPVIOL = 1;    /* Clear protection violation flag */          
          } 
      while (!FSTAT_FCCF);      /* Wait for command to complete  */     
     }
#pragma CODE_SEG ROM
 
 
 /**************************************************************************************
函数名: Page_Program(void)
功  能: 编程
**************************************************************************************/
#pragma CODE_SEG WBoot_SEG
void Page_Program(void)
     {
      byte i;
      byte *Progm_Addr,*PtrData;
      Progm_Addr=(byte *)0xDA00;
      PtrData = ProgmData; 
      if (FSTAT_FACCERR)        /* Check for access error */
         {                         
           FSTAT_FACCERR = 1;        /* Clear access error flag */
         }                         //Page_Program(FlashDataPtr; RecvDataBuf, FLASH_BURST_SIZE);
  
      for (i = 0; i < 30; i++)
         {
           while (!FSTAT_FCBEF);     /* Wait until command buffer is empty */   
           *Progm_Addr++ = *PtrData++;       
           FCMD = byte_PROGRAM;      /* Write byte program command */  
           FSTAT_FCBEF = 1;          /* Write 1 to FCBEF to launch the command */
           asm
             {                       /* Wait 4 cycles */
              nop
              nop
              nop
              nop
              nop
              nop
              nop           
             }
 
           if (FSTAT_FACCERR)        /* Check for access error */
               {                        
                 FSTAT_FACCERR = 1;      /* Clear access error flag */                
               }
           if (FSTAT_FPVIOL)
               {                       /* Check for protection violation */ 
                 FSTAT_FPVIOL = 1;       /* Clear protection violation flag */                
               }    
           while (!FSTAT_FCCF);      /* Wait for command to complete  */
         }      
       }
#pragma CODE_SEG Bootloader 
 
/*****************************************************************/
void Delay_ms(byte i) 
 {
  for(;i>0;i--) 
  {    
   ;
  }
 }  
 /**********************************************************************************
函数名: void main(void)
功  能: 主函数
************************************************************************************/
 
void main(void) 
 { 
   DZ60_Init();                                
   for(;;) 
      { 
       byte i,j;     
       pointERom_fun = Sector_Erase;
       pointERom_fun_temp = pointERom_fun;
       pointERam_fun_temp = (byte *)0x0F90;
       pointERam_fun = pointERam_fun_temp;
  

       for (i=0;i
回复

使用道具 举报

该用户从未签到

0

主题

4

帖子

0

新手上路

Rank: 1

积分
4
最后登录
1970-1-1
发表于 2012-9-15 09:30:31 | 显示全部楼层

回复:DZ60 Flash 擦写

您好。我读了一下您写的代码,写得不错。我现在正在弄BootLoader,不知你现在弄得怎么样了,大家可以交流一下。
回复 支持 反对

使用道具 举报

该用户从未签到

0

主题

2

帖子

0

新手上路

Rank: 1

积分
2
最后登录
1970-1-1
发表于 2012-10-7 13:04:24 | 显示全部楼层

回复:DZ60 Flash 擦写

回复第 2 楼 于2012-09-15 09:30:31发表:
您好。我读了一下您写的代码,写得不错。我现在正在弄BootLoader,不知你现在弄得怎么样了,大家可以交流一下。
 
回复 支持 反对

使用道具 举报

该用户从未签到

0

主题

2

帖子

0

新手上路

Rank: 1

积分
2
最后登录
1970-1-1
发表于 2012-10-7 13:04:45 | 显示全部楼层

RE:DZ60 Flash 擦写

同求交流~qq:1803741446
回复 支持 反对

使用道具 举报

该用户从未签到

0

主题

18

帖子

0

新手上路

Rank: 1

积分
13
最后登录
1970-1-1
发表于 2013-4-15 17:10:37 | 显示全部楼层

RE:DZ60 Flash 擦写

楼主是否可以将自己的代码和经验分享一下?
回复 支持 反对

使用道具 举报

该用户从未签到

2

主题

54

帖子

0

注册会员

Rank: 2

积分
100
最后登录
2020-6-21
发表于 2013-5-7 19:29:40 | 显示全部楼层

回复:DZ60 Flash 擦写

 亲,我正在写HY64 Flash的擦写程序。程序一执行就复位。不知道您是否有时间帮我指点一下。
 
#ifndef FLASH_CFG_H        /* Prevent duplicated includes */
#define FLASH_CFG_H
#define PAGE 0x3E0000
#define FLASFPRESCALER   0x1F  // The bus clock is 32MHz
#define FLAGClEAR         1    // Writing a 1 to clear the flag
// Flash Commands by Mode:FCMOD
#define PROGRAM_PFLASH      0x06 
#define ERASE_FLASH_BLOCK   0x09
#define ERASE_P_SECTOR      0x0A
#define PROGRAM_DFLASH      0x11 
#define ERASE_D_SECTOR      0x12
// Flash CCOB Index Register (FCCOBIX) 
#define FCMOD_ADD           0
#define GLOBAL_ADD          1
#define FLASH_DATA0         2
#define FLASH_DATA1         3
#define FLASH_DATA2         4
#define FLASH_DATA3         5
#endif


#ifndef FLASH_H        /* Prevent duplicated includes */
#define FLASH_H
#include "Flash_Cfg.h"
#include "Flash_Types.h"
#include "Data_Types.h"
/* Functions from flash.c */
extern void  Flash_Init(void);
extern uint8 Flash_Erase_P_Sector(uint32 global_address);
extern uint8 Flash_Erase_D_Sector(uint32 global_address);
extern uint8 Flash_Write_P_Word(uint32 global_address, uint16 *data);
extern uint8 Flash_Write_D_Word(uint32 global_address,uint16 *data);
/* Error codes */
#define Flash_Clock_NotChanged       1
#define Flash_Odd_Access             2
#define Misaligned_Phrase_Address    3
#define Not_StartofSector_Error      4
#define Flash_Not_Erased             5
#define Access_Error                 6
#define Protection_Error             7
#endif   /*FLASH_H*/




#include  
#include "Flash.h"                                         /* include flash error information */
#define Flash_Sector_Size 0x200         /* must be modified for particual device */
                                 // The P-Flash sectors size is 512 bytes
//extern DoOnStack(unsigned int *far address);
void Flash_Init()
{
  FCLKDIV_FDIV =FLASFPRESCALER; // FDIV8 set to effectively divide BUSCLK down to 1 MHz
        //FPROT = 0xFF; 
        FPROT_FPOPEN =1;  //FPORT-Flash Protection Register
        FPROT_FPHDIS =1;
        FPROT_FPLDIS =1; // Disable all protection (only in special modes)
        DFPROT=0x80;    // D-Flash No  protection
        //FSTAT |= (FSTAT_PVIOL|FSTAT_ACCERR);/* Clear any errors  */
        FSTAT_CCIF   =FLAGClEAR;  // Clear Command Complete Interrupt Flag
        FSTAT_ACCERR =FLAGClEAR;  // Clear Flash Access Error Flag
        FSTAT_FPVIOL =FLAGClEAR;  // Clear Flash Protection Violation Flag
}
#pragma CODE_SEG NON_BANKED
uint8 Flash_Erase_P_Sector(uint32 global_address)
{
  unsigned int address;
  unsigned char i;
        address = (unsigned int)global_address;                        // strip page off
        if(FCLKDIV_FDIVLD==0) {return Flash_Clock_NotChanged;}
        if((unsigned int)address & 0x0001) {return Flash_Odd_Access;}   // Aligned word?
        if((unsigned int)address & 0x0007) {return Misaligned_Phrase_Address;} // global address [2:0]= 000)                                                                      // global address [2:0] != 000)
        if((unsigned int)address % Flash_Sector_Size !=0) {return Not_StartofSector_Error;}
 FSTAT_ACCERR =FLAGClEAR;  // Clear Flash Access Error Flag
        FSTAT_FPVIOL =FLAGClEAR;  // Clear Flash Protection Violation Flag
  while(!FSTAT_CCIF);       // The MMC must complete the execution of a command before 
                                  // the FCCOB register can be written to with a new command.
        //  *(uint32*)global_address = 0xFD0F;        /* Dummy store to page to be erased */
        FCCOBIX = FCMOD_ADD ;                     //Flash Command mode
        FCCOBHI = ERASE_P_SECTOR;                        //Erase the P-Flash Sector command
        FCCOBLO =((uint32)global_address >>16);   //Global address [17:16] to identify P-Flash block to be erased
//  FCCOBLO = 0x00;
        FCCOBIX = GLOBAL_ADD ;                    //Global address mode       
        FCCOB   = (uint16)global_address ;        //Global address [15:0] anywhere within the sector to be erased
       FSTAT_CCIF =FLAGClEAR;  // clearing CCIF to launch the Erase P-Flash Sector command
while(!FSTAT_CCIF);     // wait for Flash command completed 
        if (FSTAT_ACCERR) {return Access_Error;}
        if (FSTAT_FPVIOL) {return Protection_Error;}
        return 0x0F;
}


uint8 Flash_Erase_D_Sector(uint32 global_address)
{
  unsigned int address;
  unsigned char i;
        address = (unsigned int)global_address;                        // strip page off
        if(FCLKDIV_FDIVLD==0) {return Flash_Clock_NotChanged;}
        if((unsigned int)address & 0x0001) {return Flash_Odd_Access;}   // Aligned word?
        if((unsigned int)address & 0x0007) {return Misaligned_Phrase_Address;} // global address [2:0]= 000)                                                                      // global address [2:0] != 000)
        if((unsigned int)address % Flash_Sector_Size !=0) {return Not_StartofSector_Error;}
 FSTAT_ACCERR =FLAGClEAR;  // Clear Flash Access Error Flag
        FSTAT_FPVIOL =FLAGClEAR;  // Clear Flash Protection Violation Flag
  while(!FSTAT_CCIF);       // The MMC must complete the execution of a command before 
                                  // the FCCOB register can be written to with a new command.
//  *(uint32*)global_address = 0xFD0F;        /* Dummy store to page to be erased */
FCCOBIX = FCMOD_ADD ;                     //Flash Command mode
        FCCOBHI = ERASE_D_SECTOR;                        //Erase the P-Flash Sector command
        FCCOBLO =((uint32)global_address >>16);   //Global address [17:16] to identify P-Flash block to be erased
       FCCOBIX = GLOBAL_ADD ;                    //Global address mode       
        FCCOB   = (uint16)global_address ;        //Global address [15:0] anywhere within the sector to be erased
FSTAT_CCIF =FLAGClEAR;  // clearing CCIF to launch the Erase P-Flash Sector command
while(!FSTAT_CCIF);     // wait for Flash command completed 
        if (FSTAT_ACCERR) {return Access_Error;}
        if (FSTAT_FPVIOL) {return Protection_Error;}
        return 0x0F;
}
uint8 Flash_Write_P_Word(uint32 global_address,uint16 *data)
{
uint16 address;
uint8 i;
        address = (uint16)global_address;                        // strip page off
        while(!FSTAT_CCIF);      // The MMC must complete the execution of a command before 
                                 // the FCCOB register can be written to with a new command.
      //        FSTAT_CCIF   =FLAGClEAR;  // Clear Command Complete Interrupt Flag
        FSTAT_ACCERR =FLAGClEAR;  // Clear Flash Access Error Flag
        FSTAT_FPVIOL =FLAGClEAR;  // Clear Flash Protection Violation Flag
         if((uint16)address & 0x0001) {return Flash_Odd_Access;} // Aligned word?
        if((unsigned int)address & 0x0007) {return Misaligned_Phrase_Address;} // global address [2:0]= 000)
        if(*(uint32*)global_address != 0xFFFF) {return Flash_Not_Erased;}         // Is it erased?
       FCCOBIX = FCMOD_ADD ;                     //Erase P-Flash Sector Command
        FCCOBHI = PROGRAM_PFLASH;
        FCCOBLO =((uint32)global_address>>16);   //Global address [17:16] to identify P-Flash block to be erased
//        FCCOBLO = 0x00;
        FCCOBIX = GLOBAL_ADD;                     //Global address mode       
        FCCOB   = (uint16)global_address ;        //Global address [15:0] anywhere within the sector to be erased        (*global_address) = data;           
        FCCOBIX = FLASH_DATA0 ;                   //Program data value0 mode       
        FCCOB   = *data ;                         //data0 value
        FCCOBIX = FLASH_DATA1 ;                   //Program data value0 mode       
        FCCOB   = *(data+1) ;                     //data1 value
       FCCOBIX = FLASH_DATA2 ;                   //Program data value0 mode       
        FCCOB   = *(data+2) ;                     //data2 value
       FCCOBIX = FLASH_DATA3 ;                   //Program data value0 mode       
        FCCOB   = *(data+3) ;                     //data3 value
       FSTAT_CCIF =FLAGClEAR;  // clearing CCIF to launch the Erase P-Flash Sector command
        while(!FSTAT_CCIF);     // wait for Flash command completed 
        if (FSTAT_ACCERR) {return Access_Error;}
        if (FSTAT_FPVIOL) {return Protection_Error;}
        return 0x0F;
}
uint8 Flash_Write_D_Word(uint32 global_address,uint16 *data)
{
uint16 address;
uint8 i;
        address = (uint16)global_address;                        // strip page off
        while(!FSTAT_CCIF);      // The MMC must complete the execution of a command before 
                                 // the FCCOB register can be written to with a new command.
 //        FSTAT_CCIF   =FLAGClEAR;  // Clear Command Complete Interrupt Flag
        FSTAT_ACCERR =FLAGClEAR;  // Clear Flash Access Error Flag
        FSTAT_FPVIOL =FLAGClEAR;  // Clear Flash Protection Violation Flag
        if((uint16)address & 0x0001) {return Flash_Odd_Access;} // Aligned word?
        if((unsigned int)address & 0x0007) {return Misaligned_Phrase_Address;} // global address [2:0]= 000)
        if(*(uint32*)global_address != 0xFFFF) {return Flash_Not_Erased;}         // Is it erased?
         FCCOBIX = FCMOD_ADD ;                     //Erase P-Flash Sector Command
        FCCOBHI = PROGRAM_DFLASH;
        FCCOBLO =((uint32)global_address>>16);   //Global address [17:16] to identify P-Flash block to be erased
        FCCOBIX = GLOBAL_ADD;                     //Global address mode       
        FCCOB   = (uint16)global_address ;        //Global address [15:0] anywhere within the sector to be erased        (*global_address) = data;                  
        FCCOBIX = FLASH_DATA0 ;                   //Program data value0 mode       
        FCCOB   = *data ;                         //data0 value
        FCCOBIX = FLASH_DATA1 ;                   //Program data value0 mode       
        FCCOB   = *(data+1) ;                     //data1 value
       FCCOBIX = FLASH_DATA2 ;                   //Program data value0 mode       
        FCCOB   = *(data+2) ;                     //data2 value
       FCCOBIX = FLASH_DATA3 ;                   //Program data value0 mode       
        FCCOB   = *(data+3) ;                     //data3 value
       FSTAT_CCIF =FLAGClEAR;  // clearing CCIF to launch the Erase P-Flash Sector command
        while(!FSTAT_CCIF);     // wait for Flash command completed 
        if (FSTAT_ACCERR) {return Access_Error;}
        if (FSTAT_FPVIOL) {return Protection_Error;}
        return 0x0F;
}
#pragma CODE_SEG DEFAULT
 
#include           /* common defines and macros */
#include      /* derivative information */
#include "Data_Types.h" 
#include "Mcu.h"
 
#include "Flash.h"
 
 uint8 aa; 
 uint8 bb; 
 uint8 cc; 
 uint8 dd; 
 
 uint32 p_global_address=0x34000;
 uint32 d_global_address=0x04800;
 
 
 uint16 w_data[4]=
  {
    0x1122,0x2233,0x3344,0x4455
  } ;
 
 void main(void) {
  /* put your own code here */
    EnableInterrupts;
    Mcu_Init();
    Flash_Init();
 
    aa=0xFF;
    bb=0xFF;
    cc=0xFF;
    dd=0xFF;
    
    aa=Flash_Erase_P_Sector(p_global_address);
    bb=Flash_Write_P_Word(p_global_address,w_data);
    
    cc=Flash_Erase_D_Sector(p_global_address);
    dd=Flash_Write_D_Word(p_global_address,w_data);
  
  for(;;) 
    {
  
    }/* wait forever */
  /* please make sure that you never leave this function */
}
 

Flash_ask.rar

254.48 KB, 下载次数: 15

回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /3 下一条

Archiver|手机版|小黑屋|恩智浦技术社区

GMT+8, 2025-9-19 06:18 , Processed in 0.100553 second(s), 24 queries , MemCache On.

Powered by Discuz! X3.4

Copyright © 2001-2024, Tencent Cloud.

快速回复 返回顶部 返回列表