📄 at45spiflash.c
字号:
* Description : Erases the specified FLASH sector.
* Input : SectorAddr: address of the sector to erase.
* Output : None
* Return : None
*******************************************************************************/
void SPI_FLASH_SectorErase(u32 SectorAddr)
{
/* Send write enable instruction */
// SPI_FLASH_WriteEnable();
/* Sector Erase */
/* Select the FLASH: Chip Select low */
SPI_FLASH_CS_LOW();
/* Send Sector Erase instruction */
// SPI_FLASH_SendByte(SE);
/* Send SectorAddr high nibble address byte */
SPI_FLASH_SendByte((SectorAddr & 0xFF0000) >> 16);
/* Send SectorAddr medium nibble address byte */
SPI_FLASH_SendByte((SectorAddr & 0xFF00) >> 8);
/* Send SectorAddr low nibble address byte */
SPI_FLASH_SendByte(SectorAddr & 0xFF);
/* Deselect the FLASH: Chip Select high */
SPI_FLASH_CS_HIGH();
/* Wait the end of Flash writing */
//SPI_FLASH_WaitForWriteEnd();
}
//擦除指定的主存储器页(地址范围0-4095)
void DF_page_earse(unsigned int page)
{
DF_wait_busy();
SPI_FLASH_CS_LOW();
SPI_FLASH_SendByte(PAGE_ERASE);
SPI_FLASH_SendByte((unsigned char)(page >> 6));
SPI_FLASH_SendByte((unsigned char)(page << 2));
SPI_FLASH_SendByte(0x00);
SPI_FLASH_CS_HIGH();
}
//将保存在数组DF_buffer[]中的一页数据写入第二缓冲区后送入主存储区
//(先擦除后写入模式,页地址范围0-4095)
void DF_write_page(u8* pBuffer,u16 page)
{
unsigned int i;
DF_wait_busy();
SPI_FLASH_CS_LOW();
SPI_FLASH_SendByte(BUFFER_2_WRITE);
SPI_FLASH_SendByte(0x00);
SPI_FLASH_SendByte(0x00);
SPI_FLASH_SendByte(0x00);
for (i=0;i<SPI_FLASH_PageSize;i++)
{
SPI_FLASH_SendByte(*pBuffer);
pBuffer++;
}
SPI_FLASH_CS_HIGH();
if (page<4096)
{
SPI_FLASH_CS_LOW();
SPI_FLASH_SendByte(B2_TO_MM_PAGE_PROG_WITH_ERASE);
SPI_FLASH_SendByte((unsigned char)(page>>6));
SPI_FLASH_SendByte((unsigned char)(page<<2));
SPI_FLASH_SendByte(0x00);
SPI_FLASH_CS_HIGH();
DF_wait_busy();
}
}
//将指定主存储器页的数据转入第一缓冲区后读出,保存在DF_buffer[]数组中
//(页地址范围0-4095)
void DF_read_page (u8* pBuffer,u16 page)
{
unsigned int i;
// while(!(DF_STA_PORT & (1<< DF_STATE)));
DF_wait_busy();
SPI_FLASH_CS_LOW();
SPI_FLASH_SendByte(MM_PAGE_TO_B1_XFER);
SPI_FLASH_SendByte((unsigned char)(page >> 6));
SPI_FLASH_SendByte((unsigned char)(page << 2));
SPI_FLASH_SendByte(0x00);
SPI_FLASH_CS_HIGH();
DF_wait_busy();
SPI_FLASH_CS_LOW();
SPI_FLASH_SendByte(BUFFER_1_READ);
SPI_FLASH_SendByte(0x00);
SPI_FLASH_SendByte(0x00);
SPI_FLASH_SendByte(0x00);
SPI_FLASH_SendByte(0x00);
for (i=0;i<SPI_FLASH_PageSize;i++)
{
*pBuffer = SPI_FLASH_ReadByte(0xFF);
pBuffer++;
}
SPI_FLASH_CS_HIGH();
}
//以直接读取方式读取指定的主存储器页数据(页地址范围0-4095)
//读取状态寄存器
// bit7 bit6 bit6 bit6 bit6 bit6 bit6 bit6
//RDY/BUSY COMP 1 0 1 1 PROTECT PAGE SIZE
unsigned char DF_read_reg(void)
{
unsigned char temp;
SPI_FLASH_CS_LOW();
SPI_FLASH_SendByte(READ_STATE_REGISTER);
SPI_FLASH_SendByte(0x00);
SPI_FLASH_SendByte(0x00);
SPI_FLASH_SendByte(0x00);
temp= SPI_FLASH_ReadByte(0x00);
SPI_FLASH_CS_HIGH();
return temp;
}
//检查状态寄存器最高位是否为忙,并等待空闲
void DF_wait_busy(void)
{
unsigned char state_reg=0x00;
SPI_FLASH_CS_LOW();
SPI_FLASH_SendByte(READ_STATE_REGISTER);
SPI_FLASH_SendByte(0x00);
SPI_FLASH_SendByte(0x00);
SPI_FLASH_SendByte(0x00);
while((state_reg&0x80) == 0)
{
state_reg = SPI_FLASH_ReadByte(0x00);
}
SPI_FLASH_CS_HIGH();
}
//将指定主存储器页的数据转入指定缓冲区
void DF_mm_to_buf(unsigned char buffer,unsigned int page)
{
DF_wait_busy();
SPI_FLASH_CS_LOW();
if (buffer==1)
SPI_FLASH_SendByte(MM_PAGE_TO_B1_XFER);
else
SPI_FLASH_SendByte(MM_PAGE_TO_B2_XFER);
SPI_FLASH_SendByte((unsigned char)(page >> 6));
SPI_FLASH_SendByte((unsigned char)(page << 2));
SPI_FLASH_SendByte(0x00);
SPI_FLASH_CS_HIGH();
}
//读取指定缓冲区指定单元的数据,保存在DF_buffer[]数组中
u8 DF_read_buf(u8 buffer,u16 start_address,u16 length,u8* pBuffer)
{
unsigned int i;
if ((SPI_FLASH_PageSize-start_address)>=length)
{
DF_wait_busy();
SPI_FLASH_CS_LOW();
if (buffer==1)
SPI_FLASH_SendByte(BUFFER_1_READ);
else
SPI_FLASH_SendByte(BUFFER_2_READ);
SPI_FLASH_SendByte(0x00);
SPI_FLASH_SendByte((unsigned char)(start_address >> 8));
SPI_FLASH_SendByte((unsigned char)start_address);
SPI_FLASH_SendByte(0x00);
for (i=0;i<length;i++)
{
*pBuffer = SPI_FLASH_ReadByte(0xFF);
pBuffer++;
}
SPI_FLASH_CS_HIGH();
return 1;
}
else
return 0;
}
//将DF_buffer[]数组中指定长度的数据写入指定缓冲区
u8 DF_write_buf(u8 buffer,u16 start_address,u16 length,u8* pBuffer)
{
unsigned int i;
if ((SPI_FLASH_PageSize-start_address)>=length)
{
DF_wait_busy();
SPI_FLASH_CS_LOW();
if (buffer==1)
SPI_FLASH_SendByte(BUFFER_1_WRITE);
else
SPI_FLASH_SendByte(BUFFER_2_WRITE);
SPI_FLASH_SendByte(0x00);
SPI_FLASH_SendByte((unsigned char)(start_address >> 8));
SPI_FLASH_SendByte((unsigned char)start_address);
for (i=0;i<length;i++)
{SPI_FLASH_SendByte(*pBuffer);pBuffer++;}
SPI_FLASH_CS_HIGH();
return 1;
}
else
return 0;
}
//将指定缓冲区中的数据写入主存储区的指定页
void DF_buf_to_mm(unsigned char buffer,unsigned int page)
{
DF_wait_busy();
if (page<4096)
{
SPI_FLASH_CS_LOW();
if (buffer==1)
SPI_FLASH_SendByte(B1_TO_MM_PAGE_PROG_WITH_ERASE);
else
SPI_FLASH_SendByte(B2_TO_MM_PAGE_PROG_WITH_ERASE);
SPI_FLASH_SendByte((unsigned char)(page>>6));
SPI_FLASH_SendByte((unsigned char)(page<<2));
SPI_FLASH_SendByte(0x00);
SPI_FLASH_CS_HIGH();
}
}
/*******************************************************************************
* Function Name : SPI_FLASH_ReadID
* Description : Reads FLASH identification.
* Input : None
* Output : None
* Return : FLASH identification
*******************************************************************************/
u32 SPI_FLASH_ReadID(void)
{
u32 Temp = 0, Temp0 = 0, Temp1 = 0, Temp2 = 0;
/* Select the FLASH: Chip Select low */
SPI_FLASH_CS_LOW();
/* Send "RDID " instruction */
SPI_FLASH_SendByte(0x9F);
/* Read a byte from the FLASH */
Temp0 = SPI_FLASH_SendByte(Dummy_Byte1);
// printf("\nTemp0 %x",Temp0);
/* Read a byte from the FLASH */
Temp1 = SPI_FLASH_SendByte(Dummy_Byte1);
// printf("\nTemp1 %x",Temp1);
/* Read a byte from the FLASH */
Temp2 = SPI_FLASH_SendByte(Dummy_Byte1);
// printf("\nTemp2 %x",Temp2);
/* Deselect the FLASH: Chip Select high */
SPI_FLASH_CS_HIGH();
Temp = (Temp0 << 16) | (Temp1 << 8) | Temp2;
return Temp;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -