📄 flashdrv.c
字号:
/****************************************************************************
************************ Driver for urat of lpc2210 **********************
* File Name: SPIDrv.c
* Function:
* Author: Strive_J
* Date: 2004.12.15
* Version: 0.1
* Descripion:
* spi driver for lpc2210
******************************************************************************/
#include "spiDrv.h"
#include "LPC2294.h"
/***************************
The program is to program the AT45DB041 FLASH chip,
the basic way is to write/read based on the 256bytes/page,
and write and read 1page one time
******************************/
extern uint8 *SpiBufPtr;
extern uint8 SpiBuf[300];
extern uint32 SpiLength;
extern uint8 SpiState;
//uint8 FlashBufOut[20];
#define SSEL (IO0SET |= (1 << 7))
#define nSSEL (IO0CLR |= (1 << 7))
uint8 ReadStatusReg(void)
{
SpiBuf[0] = 0xD7; //read ID command
SpiBuf[1] = 0x00; //dummy byte
SpiBufPtr = SpiBuf;
SpiLength = 2;
SpiState = SPI_BUSY;
nSSEL;
SPDR = *SpiBufPtr;
while(SpiState == SPI_BUSY);
SSEL;
//return buf[0];
return SpiBuf[1];
}
void ConfigPageSize(void)
{
SpiBuf[0] = 0x3d; //configration page size command
SpiBuf[1] = 0x2a;
SpiBuf[2] = 0x80;
SpiBuf[3] = 0xa6;
SpiBufPtr = SpiBuf;
SpiLength = 4;
SpiState = SPI_BUSY;
nSSEL;
SPDR = *SpiBufPtr;
while(SpiState == SPI_BUSY);
SSEL;
}
void ReadFlashId(void)
{
SpiBuf[0] = 0x9f; //read ID command
SpiBufPtr = SpiBuf;
SpiLength = 5;
SpiState = SPI_BUSY;
nSSEL;
SPDR = *SpiBufPtr;
while(SpiState == SPI_BUSY);
SSEL;
}
void ChipErase(void)
{
uint8 tmp;
SpiBuf[0] = 0xc7; //configration page size command
SpiBuf[1] = 0x94;
SpiBuf[2] = 0x80;
SpiBuf[3] = 0x9a;
SpiBufPtr = SpiBuf;
SpiLength = 4;
SpiState = SPI_BUSY;
nSSEL;
SPDR = *SpiBufPtr;
while(SpiState == SPI_BUSY);
SSEL;
tmp = ReadStatusReg();
while((tmp&0x80) == 0x00)
{
tmp = ReadStatusReg();
}
}
void SectorErase(uint8 SectorNumber)
{
uint8 tmp;
if(SectorNumber == 0x00)
{
SpiBuf[0] = 0X7C; //configration page size command
SpiBuf[1] = 0x00;
SpiBuf[2] = 0x00; //seelct 0a sector
SpiBuf[3] = 0x00;
SpiBufPtr = SpiBuf;
SpiLength = 4;
SpiState = SPI_BUSY;
nSSEL;
SPDR = *SpiBufPtr;
while(SpiState == SPI_BUSY);
SSEL;
tmp = ReadStatusReg();
while((tmp&0x80) == 0x00)
{
tmp = ReadStatusReg();
}
SpiBuf[0] = 0x7C; //configration page size command
SpiBuf[1] = 0x00;
SpiBuf[2] = 0x08; //select the 0b sector
SpiBuf[3] = 0x00;
SpiBufPtr = SpiBuf;
SpiLength = 4;
SpiState = SPI_BUSY;
nSSEL;
SPDR = *SpiBufPtr;
while(SpiState == SPI_BUSY);
SSEL;
tmp = ReadStatusReg();
while((tmp&0x80) == 0x00)
{
tmp = ReadStatusReg();
}
}
else if(SectorNumber<8)
{
SpiBuf[0] = 0x7C; //configration page size command
SpiBuf[1] = SectorNumber;
SpiBuf[2] = 0x00; //select the 0b sector
SpiBuf[3] = 0x00;
SpiBufPtr = SpiBuf;
SpiLength = 4;
SpiState = SPI_BUSY;
nSSEL;
SPDR = *SpiBufPtr;
while(SpiState == SPI_BUSY);
SSEL;
tmp = ReadStatusReg();
while((tmp&0x80) == 0x00)
{
tmp = ReadStatusReg();
}
}
}
void BlockErase(uint8 BlockNumber)
{
uint8 tmp;
SpiBuf[0] = 0x50; //configration page size command
tmp = ((BlockNumber&0xE0)>>5);
//tmp = ((PageNumber&0x700)>>8);
SpiBuf[1] = tmp; //page address2
tmp = ((BlockNumber&0x1F)<<3);
//tmp = (PageNumber&0xFF);
SpiBuf[2] = tmp; //page address1
SpiBuf[3] = 0x00; //don't care bits
SpiBufPtr = SpiBuf;
SpiLength = 4;
SpiState = SPI_BUSY;
nSSEL;
SPDR = *SpiBufPtr;
while(SpiState == SPI_BUSY);
SSEL;
tmp = ReadStatusReg();
while((tmp&0x80) == 0x00)
{
tmp = ReadStatusReg();
}
}
void BufferWrite(uint8 *ptr)
{
uint8 buf[4];
buf[0] = 0x84; //buffer write command
buf[3] = 0x00; //buffer start address: 0x00
SpiBufPtr = buf;
SpiLength = 4; //2bytes buffer write command
SpiState = SPI_BUSY;
nSSEL;
SPDR = *SpiBufPtr;
while(SpiState == SPI_BUSY);
SpiBufPtr = ptr;
SpiLength = 256; //256 bytes data
SpiState = SPI_BUSY;
SPDR = *SpiBufPtr;
while(SpiState == SPI_BUSY);
SSEL;
}
void BufMemoryWrite(uint16 PageNumber)
{
uint8 tmp, buf[4];
buf[0] = 0x88; //buffer to memory write command
tmp = 0;
tmp = ((PageNumber&0x700)>>8);
buf[1] = tmp; //page address2
tmp = (PageNumber&0xFF);
buf[2] = tmp; //page address1
buf[3] = 0x00; //don't care bits
SpiBufPtr = buf;
SpiLength = 4;
SpiState = SPI_BUSY;
nSSEL;
SPDR = *SpiBufPtr;
while(SpiState == SPI_BUSY);
SSEL;
tmp = ReadStatusReg();
while((tmp&0x80) == 0x00)
{
tmp = ReadStatusReg();
}
}
void ArrayRead(uint16 PageNumber, uint8 *ptr)
{
uint8 tmp;
SpiBufPtr = ptr;
*SpiBufPtr++ = 0xE8; //buffer to memory write command
tmp = 0;
tmp = ((PageNumber&0x700)>>8);
*SpiBufPtr++ = tmp; //page address2
tmp = (PageNumber&0xFF);
*SpiBufPtr++ = tmp; //page address1
*SpiBufPtr++ = 0x00; //read from address0 in a page
SpiBufPtr = ptr;
SpiLength = 264; //send 4 bytes array read command+ 4bytes don't care bytes, read 256bytes data
SpiState = SPI_BUSY;
nSSEL;
SPDR = *SpiBufPtr;
while(SpiState == SPI_BUSY);
SSEL;
}
uint8 BufMemoryCompare(uint16 PageNumber)
{
uint8 tmp, buf[4];
buf[0] = 0x60; //buffer1 to memory compare command
tmp = 0;
tmp = ((PageNumber&0x700)>>8);
buf[1] = tmp; //page address2
tmp = (PageNumber&0xFF);
buf[2] = tmp; //page address1
buf[3] = 0x00; //don't care bits
SpiBufPtr = buf;
SpiLength = 4;
SpiState = SPI_BUSY;
nSSEL;
SPDR = *SpiBufPtr;
while(SpiState == SPI_BUSY);
SSEL;
tmp = ReadStatusReg();
while((tmp&0x80) == 0x00)
{
tmp = ReadStatusReg();
}
if(tmp&0x40) return 1;
else return 0;
}
/*********************************************************************************************************
** End Of File
********************************************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -