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

📄 flash.c

📁 AT91fr40162的FLASH读写程序
💻 C
📖 第 1 页 / 共 3 页
字号:
#include "dtm_v3.h"
#include "dtm.h"
#include "externdef.h"
#define FLASHPAGESIZE  	64

_BYTE  FlashWriteData(_BYTE cData);
_BYTE  FlashReadData(void);
void FlashSendStartBit(void);
void FlashSendStopBit(void);
void FlashSendAckBit(void);
_BYTE FlashWritePage(_WORD wAddress,_BYTE cLength,_BYTE cChip,_BYTE *cpBuf);
_BYTE FlashReadPage(_WORD wAddress,_BYTE cLength,_BYTE cChip,_BYTE *cpBuf);
_BYTE FlashWriteBuf(_WORD wAddress,_WORD wLength,_BYTE cChip,_BYTE *cpBuf);
_BYTE FlashReadBuf(_WORD wAddress,_WORD wLength,_BYTE cChip,_BYTE *cpBuf);
_BYTE FlashInQueue(_BYTE *cpLogBuf);
_BYTE FlashOutQueue(_BYTE * cpLogBuf);
_BYTE FlashReadQueue(_WORD wIndex,_BYTE * cpLogBuf);
_BYTE FlashReadWriteQueuePoint(_BYTE *cpPointBuf,_BYTE cMode,_BYTE cType,_BYTE cChip);
_BYTE FlashRollBack(_BYTE cType,_WORD wCount);
_BYTE FlashCheckSpace(_WORD * wSpacs);
_BYTE FlashReadWriteDCB(_BYTE DCBNumber,struct StruDCB *spDCB,_BYTE cMode);
_BYTE FlashReadWriteSPB(struct StruSPB *spSPB,_BYTE cMode);
_BYTE FlashVerify(_BYTE *cpSourceBuf,_BYTE *cpTragetBuf,_WORD wLength);
_BYTE FlashReadWriteDAT(_BYTE *cpDATBuf,_BYTE cMode);
_BYTE FlashReadWriteKEYAREA(struct StruKEYAREA *spKEY,_BYTE cMode);
_BYTE FlashReadWriteCDTI(struct StruCDTI *spCDTI,_BYTE cMode);
_BYTE FlashReadWriteCSI(struct StruCSI *spCSI,_BYTE cMode);
_BYTE FlashReadWriteMB(_BYTE *cpMBBuf,_BYTE cIndex,_BYTE cMode);

/****************************************************************************
* 名称:FlashWriteData()
* 功能:向FlashC256发送一字节数据
* 参数:
*       cData		要发送的数据
* 返回:
*       成功,返回C_OK, 否则返回错误码
* 说明:发送数据时,高位先发送。
****************************************************************************/
_BYTE  FlashWriteData(_BYTE cData)
{  
    _WORD  i;
   	PIO_OER  = SDA;     				//* set SDA line as an output
  	for(i=0; i<8; i++) {				// 发送8位数据
      	/* 设置PIO的DATA输出值 */
      	if( (cData&0x80)!=0)
      		PIO_SODR = SDA;
       	else
       		PIO_CODR = SDA;
      	cData <<= 1;
      	PIO_SODR = SCL;					// SCL = 1
      	DelayMCK4(SPI_BASE_DELAY_TIME);
      	PIO_CODR = SCL;			// SCL = 0
      	DelayMCK4(SPI_BASE_DELAY_TIME-2);
   	}
    PIO_SODR = SDA;
    PIO_ODR = SDA;      //禁止SDA输出(为输入)
    PIO_SODR = SCL;			// SCL = 1
    DelayMCK4(1);
       for(i=0; i<0x900; i++){
      	if (( PIO_PDSR & SDA) == 0){
           	PIO_CODR = SCL;			// SCL = 0
          	return(C_OK);
      	}
   	}
   	return(C_FLASHERR);
}

/****************************************************************************
* 名称:FlashReadData()
* 功能:FlashC256读一字节数据
* 返回:函数返回值为读取的数据
* 说明:发送数据时,高位先发送。
****************************************************************************/
_BYTE FlashReadData(void)
{  
    _BYTE  i;
    _BYTE  cData; 
    PIO_ODR  = SDA;     					//* set SDA line as an input
   	DelayMCK4(1);
   	for(i=0; i<8; i++) {					// 发送8位数据
      	PIO_SODR = SCL;						// SCL = 1
      	cData *= 2;
      	DelayMCK4(SPI_BASE_DELAY_TIME);
      	if( ( PIO_PDSR & SDA) == SDA ) 
      		cData++;
      	PIO_CODR = SCL;						// SCL = 0
      	DelayMCK4(SPI_BASE_DELAY_TIME);
   }
   return(cData);
}

/****************************************************************************
* 名称:FlashSendStartBit()
* 功能:读写FLASH时发送起始位
****************************************************************************/
void FlashSendStartBit(void)  /*起始*/
{
   	PIO_PER  = (SDA | SCL);
   	PIO_OER  = SDA;    
   	PIO_OER  = SCL;
         
   	PIO_SODR = SDA;
   	PIO_SODR = SCL;
   	DelayMCK4(SPI_BASE_DELAY_TIME);
   	PIO_CODR = SDA;
   	DelayMCK4(SPI_BASE_DELAY_TIME);
   	PIO_CODR = SCL;
}

/****************************************************************************
* 名称:FlashSendStartBit()
* 功能:读写FLASH时发送终止位
****************************************************************************/
void FlashSendStopBit(void)
{
   	PIO_OER  = SDA;  
   	 
   	PIO_CODR = SDA;
   	PIO_SODR = SCL;
   	DelayMCK4(SPI_BASE_DELAY_TIME);
   	PIO_SODR = SDA;
 }

/****************************************************************************
* 名称:FlashSendAckBit()
* 功能:读写FLASH时发送应答位
****************************************************************************/
void FlashSendAckBit(void)
{
   	PIO_OER  = SDA; 
   	  
   	PIO_CODR = SDA;
   	PIO_SODR = SCL;
   	DelayMCK4(SPI_BASE_DELAY_TIME);
   	PIO_CODR = SCL;
}

/***************************************************************
* 函数: FlashWritePage
* 功能: 把BUF中的数据写入EEPROM
* 参数: wAddress: 拟写入数据在Flash的起始地址
*       cLength:  拟写入数据的长度
*       cpBuf:    拟写入FLASh中的数据,不超过64个字节
*	    cChip:    选择片0还是片1
***************************************************************/
_BYTE FlashWritePage (_WORD wAddress,_BYTE cLength,_BYTE cChip,_BYTE *cpBuf)
{
  	_BYTE j,cRetCode;
  	FlashSendStartBit();
  	DelayMCK4(SPI_BASE_DELAY_TIME);
  	if(cChip==0)
  		cRetCode=FlashWriteData(0xa0);
 	else
  		cRetCode=FlashWriteData(0xa6);
  	if(cRetCode!=C_OK) {
      	FlashSendStopBit();
      	return(cRetCode);
   	}
   	j = (_BYTE)(wAddress>>8);
   	FlashWriteData(j);
   	if(cRetCode!=C_OK) {
      	FlashSendStopBit ();
      	return(cRetCode);
   	}
   	j = (_BYTE) (wAddress);
   	FlashWriteData(j);
   	if(cRetCode!=C_OK) {
      	FlashSendStopBit ();
      	return(cRetCode);
   	}
   	for(j=0;j<cLength;j++) {
     	cRetCode= FlashWriteData(*(cpBuf+j));
     	if(cRetCode!=C_OK) {
        	FlashSendStopBit();
        	return(cRetCode);
     	}
   	}
   	FlashSendStopBit();
   	return(C_OK);
}

/*********************************************************
* 函数: FlashReadPage
* 功能: 从EEPROM中读取数据到BUF中
* 参数: wAddress: 拟读取数据在Flash的起始地址
*       cLength:  拟读取数据的长度
*       cpBuf:    存放读取数据的缓冲区,不超过64个字节
*	    cChip:    选择片0还是片1
**********************************************************/
_BYTE FlashReadPage(_WORD wAddress,_BYTE cLength,_BYTE cChip,_BYTE *cpBuf)
{
   	_BYTE j,cRetCode;
    FlashSendStartBit();
    DelayMCK4(SPI_BASE_DELAY_TIME);
   	if(cChip==0)
  		cRetCode=FlashWriteData(0xa0);				// 写芯片地址
  	else
  		cRetCode=FlashWriteData(0xa6);
   	if(cRetCode!=C_OK) {
      	FlashSendStopBit();
      	return(cRetCode);                           
   	}
   	j=(_BYTE) (wAddress>>8);						// 写Flash地址,高位
   	cRetCode = FlashWriteData(j);
   	if(cRetCode!=C_OK) {
      	FlashSendStopBit();
      	return(cRetCode);
   	}
   	j=(_BYTE) (wAddress);							// 写Flash地址,低位
   	cRetCode = FlashWriteData(j);
   	if(cRetCode!=C_OK) {
      	FlashSendStopBit();
      	return(cRetCode);
   	}
   	FlashSendStartBit();							// 发送起始位
   	if(cChip==0)
  		cRetCode=FlashWriteData(0xa1);
  	else
  		cRetCode=FlashWriteData(0xa7);
   	if(cRetCode!=C_OK) {
      	FlashSendStopBit ();
      	return(cRetCode);
   	}
   	*(cpBuf)=(FlashReadData());
   	if(cLength>1){
     	FlashSendAckBit ();
     	for(j=1;j<cLength-1;j++){
        	*(cpBuf+j)=(FlashReadData());
        	FlashSendAckBit();
      	}
     	*(cpBuf+cLength-1)=(FlashReadData ());
    }
   	FlashSendStopBit();
   	return(C_OK);
}


/***************************************************************
* 函数: FlashWriteBuf
* 功能: 把BUF中的数据写入EEPROM
* 参数: wAddress: 拟写入数据在Flash的起始地址
*       cLength:  拟写入数据的长度
*       cpBuf:    拟写入FLASh中的数据
*	    cChip:    选择片0还是片1
***************************************************************/
_BYTE FlashWriteBuf(_WORD wAddress,_WORD wLength,_BYTE cChip,_BYTE *cpBuf)
{
   	_BYTE cLength,cAddL,cRetCode;
   	_WORD wAdd,wCurLen;
   	wAdd=wAddress;
   	wCurLen = 0;
   	while (wCurLen<wLength) {
   		if ((wAdd&0x003f)==0) {				// 一页的起始
   			cLength = (_BYTE)((wCurLen+FLASHPAGESIZE>=wLength)?wLength-wCurLen:FLASHPAGESIZE);
   		}
   		else {								// 一页的中间
   			cAddL = (_BYTE)(wAdd&0x3f);
   			cLength = FLASHPAGESIZE - cAddL;				// 本页剩余字节的长度
   			
   		    if (wCurLen + cLength >wLength) {	// 剩余字节长度不够
   		    	cLength = (_BYTE)(wLength - wCurLen);
   		    }
   		}
     	cRetCode = FlashWritePage(wAdd,cLength,cChip,cpBuf);
       	DelayMCK4(5000*SPI_BASE_DELAY_TIME);
    	if (cRetCode!=C_OK) return(cRetCode);
      	wAdd += cLength;
 		wCurLen += cLength;     	
      	cpBuf += cLength;
		   		
   	}
   	return(C_OK);
}


/***************************************************************
* 函数: FlashReadBuf
* 功能: 把EEPROM的数据写到BUF中
* 参数: wAddress: 拟写入数据在Flash的起始地址
*       cLength:  拟写入数据的长度
*       cpBuf:    拟写入BUF中的数据
*	    cChip:    选择片0还是片1
***************************************************************/
_BYTE FlashReadBuf(_WORD wAddress,_WORD wLength,_BYTE cChip,_BYTE *cpBuf)
{
   	_BYTE cLength,cAddL,cRetCode;
   	_WORD wAdd,wCurLen;
   	wAdd=wAddress;
   	wCurLen = 0;
   	while (wCurLen<wLength) {
   		if ((wAdd&0x003f)==0) {				// 一页的起始
   			cLength = (_BYTE)((wCurLen+FLASHPAGESIZE>=wLength)?wLength-wCurLen:FLASHPAGESIZE);
   		}
   		else {								// 一页的中间
   			cAddL = (_BYTE)(wAdd&0x3f);
   			cLength = FLASHPAGESIZE - cAddL;				// 本页剩余字节的长度
   			
   		    if (wCurLen + cLength >wLength) {	// 剩余字节长度不够
   		    	cLength = (_BYTE)(wLength - wCurLen);
   		    }
   		}
     	cRetCode = FlashReadPage(wAdd,cLength,cChip,cpBuf);

⌨️ 快捷键说明

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