//****************************************
// Sub: fFlash_init
// Parameter:
// Return:
// Funktion: initialize flash driver
//****************************************
void fFlash_init(void){
SIM_SCGC |= SIM_SCGC_FLASH_MASK;
if(!(FTMRH->FCLKDIV & FTMRH_FCLKDIV_FDIVLD_MASK)){ // FCLKDIV register has not been written since the last reset
FTMRH->FCLKDIV |= 0x7; //divide 8MHz BUSCLK down to 1MHz
FTMRH->FCLKDIV |= FTMRH_FCLKDIV_FDIVLCK_MASK; // lock the prescaler
}
}
//****************************************
// Sub: fFlash_EraseSector
// Parameter: sector number to be erased
// Return:
// Funktion: erase flash sector, each flash sector is of 512 bytes long
//****************************************
void fFlash_EraseSector(unsigned char SectorNo){
FTMRH->FSTAT = 0x30; // Clear error flags
FTMRH->FCCOBIX = 0x0; // Write index to specify the command code to be loaded
FTMRH->FCCOBHI = _FLASH_CMD_ERASE_SECTOR; // Write command code
FTMRH->FCCOBLO = 0; // memory address bits[23:16]
FTMRH->FCCOBIX = 0x1; // Write index to specify the lower byte memory address bits[15:0] to be loaded
FTMRH->FCCOBLO = SectorNo * _FLASH_SECTOR_SIZE - 1; // Write the lower byte memory address bits[15:0]
FTMRH->FCCOBHI = (SectorNo * _FLASH_SECTOR_SIZE - 1) >> 8;
FTMRH->FSTAT = 0x80; // launch the command
while (!(FTMRH->FSTAT & FTMRH_FSTAT_CCIF_MASK)); // Wait till command is completed
}
//****************************************
// Sub: fFlash_WriteChecksum
// Parameter: word programming data
// Return:
// Funktion: program checksum to the last 2 bytes of flash
//****************************************
void fFlash_WriteChecksum(unsigned short crc){
FTMRH_FSTAT |= 0x30; //clear error flags
FTMRH->FCCOBIX = 0x0; //write index to specify the command code to be loaded
FTMRH->FCCOBHI = _FLASH_CMD_PROGRAM; //program FLASH command
FTMRH->FCCOBLO = 0x00; //target address bits[23:16]
FTMRH->FCCOBIX = 0x1; //Write index to specify the lower byte memory address bits[15:0] to be loaded
FTMRH->FCCOBLO = 0xfc;
FTMRH->FCCOBHI = 0xff;
FTMRH->FCCOBIX = 0x2; // Write index to specify the word0 (MSB word) to be programmed
FTMRH->FCCOBHI = 0xff;
FTMRH->FCCOBLO = 0xff;
FTMRH->FCCOBIX = 0x3; // Write index to specify the word1 (LSB word) to be programmed
FTMRH->FCCOBHI = crc;
FTMRH->FCCOBLO = crc >> 8;
FTMRH->FSTAT = 0x80; //launch the command
while (!(FTMRH->FSTAT & FTMRH_FSTAT_CCIF_MASK)); //Wait till command is completed
}
就是按例程写的,关键步骤都差不多。现在情况就是单步运行一切正常,连续运行的话执行到那两句就跑飞到startup_ARMLtdGCC.S里面去了,卡住位置的语句是:
#ifndef __NO_SYSTEM_INIT
// Low-level system initialise must be done here so to avoid WDOG timeout
bl SystemInitLowLevel
#endif
为啥单步好好的连续就异常了呢,我看了延时什么的都加上了的,这不科学。。。