⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 《ARM与嵌入式系统基础教程》
💻 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	NEED_SELECT_BOOTMEM			1
int main(void)
{	
	AT91F_LowLevelInit();
	
	printf("Downloading file, please wait......\n\r");	
	
	__DownloadFile(0x00100000, "D:\\work\\Led_PIT_IntNesting\\Led_PIT_IntNesting_Data\\Release\\Led_PIT_IntNesting.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 + -