在线时间2 小时
UID388884
注册时间2012-9-13
NXP金币0
该用户从未签到
注册会员

- 积分
- 100
- 最后登录
- 2020-6-21
|
调试了很久的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 */
}
谢谢大家指教!
|
|