查看: 2289|回复: 0

[S12] MC9S12HY64 Flash 擦写 总是不成功

[复制链接]

该用户从未签到

2

主题

54

帖子

0

注册会员

Rank: 2

积分
100
最后登录
2020-6-21
发表于 2013-4-24 22:36:28 | 显示全部楼层 |阅读模式
 调试了很久的HY64的Flash程序,总是不成功。程序一执行便复位。
请大家帮我分析分析原因。不吝赐教。
源程序如下:
#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_Write_Word(uint32 global_address, uint16 *data);
extern uint8 Flash_Erase_Sector(uint32 global_address);
extern uint8 Flash_Write_Block(uint16 *far address_source,uint16 *far far_address_destination,uint16 count);
extern uint8 Flash_Erase_Block(uint16 *far start_address,uint16 *far end_address);
 
/* 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
#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
 
// 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   /*FLASH_H*/
 
以下是Flash.c文件:
 
#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
}
 
 
 
uint8 Flash_Erase_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 = 0x03;
       
        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 
       
//        (void)DoOnStack(far_address);
        if (FSTAT_ACCERR) {return Access_Error;}
        if (FSTAT_FPVIOL) {return Protection_Error;}
        return 0x0F;
}
 
 
uint8 Flash_Write_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 = 0x03;
       
  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 
 
//        (void)DoOnStack(far_address);            // just passed for PPAGE
        if (FSTAT_ACCERR) {return Access_Error;}
        if (FSTAT_FPVIOL) {return Protection_Error;}
        return 0x0F;
}
 
主调函数如下:
#include           /* common defines and macros */
#include      /* derivative information */
#include "Data_Types.h" 
#include "Mcu.h"
#include "Ioa.h"
#include "Adc.h"
#include "MSCAN.h"
#include "Timer.h" 
#include "Flash.h"
 
 
 struct msg lfiu_sig_status;
 extern void Ioa_ReadAdValue(void);
 extern void Ioa_ReadDigInput(void);
// uint8 MSCAN_SendFrame(uint32 CAN_ID, uint8 Msg_Prio, uint8 Msg_Length, uint8 *CANTxData);
 uint8 aa; 
 uint8 bb; 
// uint32 p_global_address=0xE000;
// uint32 p_global_address1=0xC000;
// uint32 *p_global_address;
// uint16 w_data[4]; 
 
 uint32 p_global_address=0x34400;
// uint32 p_global_address1=0x3D008; 
// p_global_address=0x38000;
 
 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;
   
    aa=Flash_Erase_Sector(p_global_address);
    Delay(2000);
    bb=Flash_Write_Word(p_global_address+16,w_data);
  
  
  for(;;) 
    {
   }/* wait forever */
  /* please make sure that you never leave this function */
}
 
谢谢大家指教!

 
 
我知道答案 目前已有0人回答

Flash.rar

2.56 KB, 下载次数: 11, 下载积分: 威望 1

回复

使用道具 举报

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

本版积分规则

关闭

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

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

GMT+8, 2025-7-21 11:54 , Processed in 0.085043 second(s), 22 queries , MemCache On.

Powered by Discuz! X3.4

Copyright © 2001-2024, Tencent Cloud.

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