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

📄 flash_40162s.c

📁 AT91fr40162的FLASH读写程序
💻 C
字号:
//*----------------------------------------------------------------------------//* File Name               : flash_4042.c//* Object                  : FLASH programmer for ://*                             AT49BV-LV4096A//* Exported Resources      : AT91FlashIdentify//*                           AT91EraseSector//*                           at91_write_flash//* Imported Resources      ://*//*//* 1.0 2004-05-31 JPP        : Creation//*----------------------------------------------------------------------------#pragma O0#include "dtm.h"#include "dtm_v3.h"#include "flash_40162s.h"#include "externdef.h"#include <stdio.h>_BYTE AT91WaitFlashReady(_WORD *wpAddress, _WORD wData );_BYTE AT91FlashCheckIdentifyID(void);_BYTE AT91EraseSector(_DWORD dwSectorAddr);_BYTE AT91WriteFlashWord (_WORD *wpFlashAddr,_WORD data);_BYTE AT91WriteBlockToFlash(_WORD *wpRamBaseAddr,_WORD * wpFlashBlockAddress,_DWORD Length);_BYTE AT91WriteImageToFlash(_WORD *wpBaseAddress,_DWORD dwLength);_BYTE AT91SetCR(_BYTE dbData);//*----------------------------------------------------------------------------//* Function Name       : AT91WaitFlashReady//* Object              : check if data is written//* Input Parameters    : Data and data address//* Output Parameters   : C_OK or C_NOTOK//* Functions called    : none//*----------------------------------------------------------------------------_BYTE AT91WaitFlashReady ( _WORD *wpAddress, _WORD wData ){    int iCount = 0 ;	_WORD wData1,wData2;    //* While two consecutive read don't give same value or timeout    wData1 = *wpAddress;    wData2 = *wpAddress;    while ((wData1 != wData2) && ( iCount++ < TIME_OUT )) {   		wData1 = *wpAddress;       	wData2 = *wpAddress;	}    //* If timeout    if ( iCount < TIME_OUT ) {        //* Return C_OK        return ( C_OK ) ;    }    //* Return C_NOTOK    return ( C_NOTOK );}//*----------------------------------------------------------------------------//* Function Name       : AT91FlashIdentify//* Object              : Read the flash manufactured code and//* Input Parameters    : flash_word *load_addr = Flash bass address//* Output Parameters   : divice code and manufactured code//* Functions called    : none//*----------------------------------------------------------------------------_BYTE AT91FlashCheckIdentifyID (){	_WORD *wpAddress;	_WORD wManufCode,wDeviceCode;    //* Enter Software Product Identification Mode	wpAddress = (_WORD*)FLASH_555;    *wpAddress = 0xAA;	wpAddress = (_WORD*)FLASH_AAA;    *wpAddress = 0x55;    	wpAddress = (_WORD*)FLASH_555;    *wpAddress = 0x90;	    //* Read Manufacturer and device code from the device	wpAddress = (_WORD*) FLASH_BASE;    wManufCode	= *wpAddress ;    wpAddress++;    wDeviceCode = *wpAddress;   	//* Exit Software Product Identification Mode	wpAddress = (_WORD*)FLASH_555;    *wpAddress = 0xAA;	wpAddress = (_WORD*)FLASH_AAA;    *wpAddress = 0x55;    	wpAddress = (_WORD*)FLASH_555;    *wpAddress = 0xF0;      	//* check ManufCode and DeviceCode   	if ((wManufCode==MANUFACTURERCODE)&&(wDeviceCode==DEVICECODE)) return(C_OK);   	return(C_NOTOK);		}//*----------------------------------------------------------------------------//* Function Name       : AT91EraseSector//* Object              : erase sector//* Input Parameters    : Data and data address//* Output Parameters   : C_OK or C_NOTOK//* Functions called    : AT91WaitFlashReady//*----------------------------------------------------------------------------_BYTE AT91EraseSector(_DWORD dwSectorAddr){	_WORD *wpAddress;	    //* Enter Sector Erase Sequence codes	wpAddress = (_WORD*)FLASH_555;    *(wpAddress) = 0xAA;	wpAddress = (_WORD*)FLASH_AAA;    *(wpAddress) = 0x55; 	wpAddress = (_WORD*)FLASH_555;    *(wpAddress) = 0x80;	wpAddress = (_WORD*)FLASH_555;    *(wpAddress) = 0xAA;	wpAddress = (_WORD*)FLASH_AAA;    *(wpAddress) = 0x55;	wpAddress = (_WORD*)FLASH_BASE;	wpAddress += dwSectorAddr;					//dwSectorAddr/2;    *(wpAddress) = 0x30 ;    if ( AT91WaitFlashReady(wpAddress, 0xFFFF) == C_NOTOK )    {        return ( C_NOTOK ) ;    }    return ( C_OK ) ;}_BYTE AT91ChipErase(){	_WORD *wpAddress;	    //* Enter Sector Erase Sequence codes	wpAddress = (_WORD*)FLASH_555;    *(wpAddress) = 0xAA;	wpAddress = (_WORD*)FLASH_AAA;    *(wpAddress) = 0x55; 	wpAddress = (_WORD*)FLASH_555;    *(wpAddress) = 0x80;	wpAddress = (_WORD*)FLASH_555;    *(wpAddress) = 0xAA;	wpAddress = (_WORD*)FLASH_AAA;    *(wpAddress) = 0x55;	wpAddress = (_WORD*)FLASH_555;    *(wpAddress) = 0x10;	wpAddress = (_WORD*)FLASH_BASE;	    if ( AT91WaitFlashReady(wpAddress, 0xFFFF) == C_NOTOK )    {        return ( C_NOTOK ) ;    }    return ( C_OK ) ;}//*----------------------------------------------------------------------------//* Function Name       : at91_write_flash//* Object              : write a word in flash//* Input Parameters    //*                       flash_word *cwLoadAddr : Flash data address//*                       flash_word wData       : Data value//* Output Parameters   : error code : FLASH_WRITE_TIME_OUT, FLASH_WRITE_ERROR//*                       or OK : FLASH_OK//* Functions called    : AT91WaitFlashReady//*----------------------------------------------------------------------------_BYTE AT91WriteFlashWord (_WORD *wpLoadAddr,_WORD wData ){    _WORD wReadData ;    _WORD *wpAddress;        //* Enter Programming code sequence	wpAddress = (_WORD *)FLASH_555;    *wpAddress = 0xAA ;    wpAddress = (_WORD *)FLASH_AAA;    *wpAddress = 0x55 ;    wpAddress = (_WORD *)FLASH_555;    *wpAddress = 0xA0 ;    *wpLoadAddr = wData ;	wpAddress = (_WORD *)wpLoadAddr;    //* Wait for Flash ready after erasing, if timeout    if ( AT91WaitFlashReady (wpAddress, wData)!= C_OK ) {        return ( FLASH_WRITE_TIME_OUT ) ;    }    if (( wReadData = *wpLoadAddr )!= wData ) {        return ( FLASH_WRITE_ERROR );    }    return ( C_OK ) ;}//*----------------------------------------------------------------------------//* 函数:AT91WriteBlockToFlash//* 功能:写一块内容到Flash中//* 参数://*       cwFlashBaseAddr : FLASH的基地址//*       cwRamBaseAddr   : Ram中数据的起始地址//*       FlashSectorAddr : Flash的起始地址//*	      Length		  : 写入数据的长度,不能跨块//*----------------------------------------------------------------------------_BYTE AT91WriteBlockToFlash(_WORD *wpRamBaseAddr,_WORD* wpFlashBlockAddress,_DWORD Length){	int i;	_WORD wData;	_WORD *wpFlashAddress,*wpRamAddress;	wpFlashAddress =  wpFlashBlockAddress;	wpRamAddress = wpRamBaseAddr;	// Write    for (i = 0; i < Length/2; i++) {      	wData = *wpRamAddress;        if (AT91WriteFlashWord(wpFlashAddress, wData) != FLASH_OK) return (FLASH_WRITE_ERROR);        wpFlashAddress++;        wpRamAddress++;    }	// Verify	wpFlashAddress = wpFlashBlockAddress;	wpRamAddress = wpRamBaseAddr;    for (i = 0; i < Length/2; i++) {      	if (*wpFlashAddress != *wpRamAddress) return (FLASH_VERIFY_ERROR);        wpFlashAddress++;        wpRamAddress++;    }	return (FLASH_OK);}//*----------------------------------------------------------------------------//* 函数:AT91WriteMemoryBlockToFlash//* 功能:写内存块的内容到AT91-Flash中//* 参数:cwBaseAddress:拟写入Image内容在RAM中的地址//* 	  dwLength:拟写入Image内容的长度_BYTE AT91WriteImageToFlash(_WORD *wpImageAddress,_DWORD dwLength){	_DWORD dwCurLength,dwCurBlockLength;	_WORD *wpCurImageAddress,*wpCurFlashAddress;	_WORD dwSectorAddress; 	_BYTE cCurBlock;	_BYTE cRetCode;	    if (dwLength>FLASH_SIZE) return(FLASH_ERROR);    dwCurLength = 0;    cCurBlock = 0;    wpCurImageAddress = wpImageAddress;    wpCurFlashAddress = (_WORD *)FLASH_BASE;	dwSectorAddress = FLASHBOOTBLOCKADDRESS;    	dwCurBlockLength = FLASH_SA0_SA7_SIZE ;    while(dwCurLength<dwLength) {     	cRetCode = AT91EraseSector(dwSectorAddress);		if (cRetCode!=FLASH_OK) return(cRetCode);		if ((dwLength-dwCurLength) < dwCurBlockLength)			dwCurBlockLength = dwLength - dwCurLength;		cRetCode = AT91WriteBlockToFlash(wpCurImageAddress,wpCurFlashAddress,dwCurBlockLength);		if (cRetCode!=C_OK) return(cRetCode);		dwCurLength += dwCurBlockLength;		wpCurFlashAddress += dwCurBlockLength;		wpCurImageAddress += dwCurBlockLength;		dwSectorAddress += dwCurBlockLength; 			}	return(C_OK);}//*----------------------------------------------------------------------------//* Function Name       : AT91SetCR//* Object              : Set Configuration Register//* Input Parameters    : Data and data address//* Output Parameters   : C_OK or C_NOTOK//* Functions called    : AT91WaitFlashReady//*----------------------------------------------------------------------------_BYTE AT91SetCR(_BYTE dbData){	 _WORD *wpAddress;	    //* Enter Sector Erase Sequence codes	wpAddress = (_WORD*)FLASH_555;    *(wpAddress) = 0xAA;	wpAddress = (_WORD*)FLASH_AAA;    *(wpAddress) = 0x55; 	wpAddress = (_WORD*)FLASH_555;    *(wpAddress) = 0xD0;	wpAddress = (_WORD*)FLASH_AAA;    *(wpAddress) = dbData;    if ( AT91WaitFlashReady(wpAddress, dbData) == C_NOTOK )    {        return ( C_NOTOK ) ;    }    return ( C_OK ) ;}//* End file

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -