?? main.c
字號:
//*------------------------------------------------------------------------------------------------
//* 文件名 : main.c
//* 功能描述 : AT91SAM7X256編程器入口文件
//* 作者 : 焦海波
//* 版本 : 0.1
//* 建立日期、時間 : 2006/06/28 14:50
//* 最近修改日期、時間 :
//* 修改原因 :
//*------------------------------------------------------------------------------------------------
//*------------------------------------------ 頭文件 -----------------------------------------------
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "/at91sam7x256/include/AT91SAM7X256.h"
#include "/at91sam7x256/drivers/flash/flash.h"
//*------------------------------------- 導入函數原型聲明 -------------------------------------------
extern void AT91F_LowLevelInit(void);
extern int DFL_IsLockedPage(short sPageIdx);
extern int DFL_PageLock(short sPageIdx);
extern int DFL_PageUnlock(short sPageIdx);
extern unsigned int DFL_WriteFlash(unsigned int *punFrom, unsigned int unTo, int nSize);
extern unsigned int DFL_WriteAndLockFlash(unsigned int *punFrom, unsigned int unTo, int nSize);
extern int DFL_EraseAllFlash(void);
extern int DFL_SetNVM(char cNVMBit);
extern int DFL_ClearNVM(char cNVMBit);
//*================================================================================================
//* 函 數 區
//*================================================================================================
//*------------------------------------------------------------------------------------------------
//* 函數名稱 : __ilIsDataCorrect
//* 功能描述 : 在軟件層面對數據作再做一次判斷,看其是否正確
//* 入口參數 : <punFrom>[in] 指向原始數據的指針
//* : <punTo>[in] 指向目標數據的指針
//* : <unSize>[in] 進行比較的數據長度
//* 出口參數 : 無
//*------------------------------------------------------------------------------------------------
__inline unsigned int __ilIsDataCorrect(unsigned int *punFrom, unsigned int* punTo, unsigned int unSize)
{
while(unSize > 0)
{
if(*punFrom != *punTo)
return ((unsigned int)punTo);
unSize-=4;
punFrom++;
punTo++;
}
return 0x00000000;
}
//*------------------------------------------------------------------------------------------------
//* 函數名稱 : __DownloadFile
//* 功能描述 : 將相關文件下載到FLASH中的指定位置
//* 入口參數 : <unTo>[in] 指定FLASH中的下載位置
//* : <pcFile>[in] 指向要下載的文件名稱的指針,包括完整的文件路徑
//* : <cIsWriteFileLen>[in] 在寫入位置的開頭是否寫入文件長度,實際的數據內容向后偏移4個字節的位置
//* 出口參數 : 無
//*------------------------------------------------------------------------------------------------
static void __DownloadFile(unsigned int unTo, char *pcFile, char cIsWriteFileLen)
{
FILE *__pstFile;
long __lStart, __lEnd;
unsigned int __unFileSize, __unTo = unTo, __unReadBytes, __unResult;
__align(4) char __caBuf[4096];
char __c2aErrMsg[4][64] = {"The address %#010x is not effective FLASH address!\n\r",
"The page in programming has been locked!\n\r",
"Programming makes mistakes!\n\r",
"For page untie lock failure!\n\r"};
__pstFile = fopen(pcFile, "rb");
if(__pstFile != NULL)
{
if(cIsWriteFileLen)
{
//* 獲得文件的開始及結束位置
fseek(__pstFile, 0L, SEEK_SET);
__lStart = ftell(__pstFile);
fseek(__pstFile, 0L, SEEK_END);
__lEnd = ftell(__pstFile);
__unFileSize = (unsigned int)(__lEnd - __lStart);
fseek(__pstFile, 0L, SEEK_SET);
*((unsigned int*)__caBuf) = __unFileSize;
__unReadBytes = fread(&__caBuf[4], sizeof(char), sizeof(__caBuf)-4, __pstFile);
__unReadBytes += 4;
}
else
__unReadBytes = fread(__caBuf, sizeof(char), sizeof(__caBuf), __pstFile);
do{
__unResult = DFL_WriteAndLockFlash((unsigned int*)__caBuf, __unTo, __unReadBytes);
if(__unResult < (unsigned int)AT91C_IFLASH)
{
printf("\n\rDownload file %s to occur an error: \n\r - ", pcFile);
printf(__c2aErrMsg[__unResult-1], unTo);
fclose(__pstFile);
return;
}
if((__unTo = __ilIsDataCorrect((unsigned int*)__caBuf, (unsigned int*)__unResult, ALIGN32(__unReadBytes))) != 0x00000000)
{
printf("\n\rDownload file %s to occur an error: \n\r - ", pcFile);
printf("The data at address %#010x and original data is not equal!\n\r ", __unTo);
fclose(__pstFile);
return;
}
__unTo = __unResult + __unReadBytes;
memset(__caBuf, 0, sizeof(__caBuf));
}while((__unReadBytes = fread(__caBuf, sizeof(char), sizeof(__caBuf), __pstFile)) > 0);
fclose(__pstFile);
printf("\n\rFile %s downloads success!", pcFile);
}
else
printf("\n\rFile %s that will be downloaded can not open!", pcFile);
}
//*------------------------------------------------------------------------------------------------
//* 函數名稱 : main
//* 功能描述 : 系統入口
//* 入口參數 : 無
//* 出口參數 : 無
//*------------------------------------------------------------------------------------------------
#define BOOT_MEM_IROM 0 //* 引導存儲器為內部ROM
#define BOOT_MEM_IFLASH 1 //* 引導存儲器為內部FLASH
#define BOOT_MEM_TYPE BOOT_MEM_IFLASH
#define DFL_STORE_ADDR 0x00100200 //* 動態函數庫下載地址
#define SYS_STORE_ADDR 0x00100A00 //* 應用系統下載地址
#define NEED_SELECT_BOOTMEM 0
int main(void)
{
AT91F_LowLevelInit();
printf("Downloading file, please wait......\n\r");
__DownloadFile(0x00100000, "D:\\work\\BootLoaderFor7x256\\BootLoaderFor7x256_Data\\Release\\BootLoaderFor7x256.bin", FALSE);
__DownloadFile(DFL_STORE_ADDR, "D:\\work\\FunLibInRAM\\FunLibInRAM_Data\\Release\\FunLibInRAM.bin", TRUE);
__DownloadFile(SYS_STORE_ADDR, "D:\\work\\MainSYS\\MainSYS_Data\\Release\\MainSYS.bin", FALSE);
#if NEED_SELECT_BOOTMEM
#if BOOT_MEM_TYPE == BOOT_MEM_IFLASH
if(DFL_SetNVM(2))
printf("\n\rSelects the boot from the FLASH!");
else
printf("\n\rSelects the boot from the ROM!");
#else
if(DFL_ClearNVM(2))
printf("\n\rSelects the boot from the ROM!");
else
printf("\n\rSelects the boot from the FLASH!");
#endif
#endif
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -