又遇到新问题了。。。
我写了段程序,计算Flash CRC16校验码,存放在最后2 Byte位置,以后开机时做Flash自检用的。在向Flash写入校验码这一段,单步能运行下去,连续运行就不对了,不知是何原因?下面是程序。
//****************************************
// 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
}
我函数调用的顺序是先init再erase然后write。单步运行能够顺利写入,数据再读出来看也是对的。连续运行时程序会卡在fFlashWrite_Checksum的最后两句也就是启动写操作以及等待操作完成,而且程序是进入了一个系统文件startup_ARMLtdGCC.S,这个文件里面我看了是一些系统reset的设置。不明白怎么会运行到这边,我的设置有什么问题吗?请大家帮忙看看,谢谢了! |
|