/*********************************************************************************
*函数原型:void Earse_Flash(void)
*函数功能:整片擦除
*函数参数:
*返回值:
*说明:整片擦除时间为5秒左右
*********************************************************************************/
void Earse_Flash(void)
{
U8 state = 0;
CLS_CS;
SPI_WriteByte(WREN);
SET_CS;
CLS_CS;
SPI_WriteByte(CE); //60 or C7 to erase whole chip
SET_CS;
CLS_CS;
SPI_WriteByte(RDSR); //to read out the values of the status register
do
{
state = SPI_ReadByte();
}
while(state&0x01);
SET_CS;
}
#include "MX25L1605D.h"
#include "config.h"
#include <intrins.H>
/*********************************************************************************
*函数原型:SPI_WriteByte(U8 byte)
*函数功能: SCK上升沿向芯片输入数据有效
*函数参数:
*返回值:
*说明:入口是,sck为高,出口是sck仍为高
*********************************************************************************/
void SPI_WriteByte(U8 byte)
{
U8 data count;
for(count=0;count<8;count++)
{
SCK=0;
_nop_();
if (byte&0x80)
{
SI=1;
}
else
{
SI=0;
}
byte=byte<<1;
SCK=1;
_nop_();
}
}
/*********************************************************************************
*函数原型:U8 SPI_ReadByte()
*函数功能:SCK下降沿从芯片读出数据有效
*函数参数:
*返回值:
*说明:入口是,sck为高,出口是sck仍为高
*********************************************************************************/
U8 SPI_ReadByte()
{
U8 data count,byte;
byte=0;
for(count=0;count<8;count++)
{
SCK=0;
byte<<=1;
if(SO)
{
byte++;
}
_nop_();
SCK=1;
_nop_();
}
return(byte);
}
/*********************************************************************************
*函数原型:void Flash_WaitBusy(void)
*函数功能:
*函数参数:
*返回值:
*说明:
*********************************************************************************/
void Flash_WaitBusy(void)
{
U8 state_reg = 0x00;
CLS_CS;
SPI_WriteByte(WREN);
SET_CS;
CLS_CS;
SPI_WriteByte(RDSR);
do
{
state_reg = SPI_ReadByte();
}
while(state_reg&0x01);
SET_CS;
}
/*********************************************************************************
*函数原型:U8 Flash_Read_ID(void)
*函数功能:
*函数参数:
*返回值:
*说明:
*********************************************************************************/
U8 Flash_Read_ID(void) //ID读取
{
U8 Manu_ID;
CLS_CS;
SPI_WriteByte(RDID); //ID = C2
Manu_ID = SPI_ReadByte();
SET_CS;
Flash_WaitBusy();
return Manu_ID;
}
/*********************************************************************************
*函数原型:void Earse_Flash(void)
*函数功能:整片擦除
*函数参数:
*返回值:
*说明:整片擦除时间为5秒左右
*********************************************************************************/
void Earse_Flash(void)
{
U8 state = 0;
CLS_CS;
SPI_WriteByte(WREN);
SET_CS;
CLS_CS;
SPI_WriteByte(CE); //60 or C7 to erase whole chip
SET_CS;
CLS_CS;
SPI_WriteByte(RDSR); //to read out the values of the status register
do
{
state = SPI_ReadByte();
}
while(state&0x01);
SET_CS;
}
/*********************************************************************************
*函数原型:void Earse_Sector(U16 i)
*函数功能:扇区擦除
*函数参数:块地址
*返回值:
*说明: 4K-byte
*********************************************************************************/
void Earse_Sector(U16 i)
{
U8 state = 0;
CLS_CS;
SPI_WriteByte(WREN);
SET_CS;
CLS_CS;
SPI_WriteByte(SE); //to erase the selected sector
SPI_WriteByte(i>>4);
SPI_WriteByte(i<<4);
SPI_WriteByte(0x00);
SET_CS;
CLS_CS;
SPI_WriteByte(RDSR); //to read out the values of the status register
do
{
state = SPI_ReadByte();
}
while(state&0x01);
SET_CS;
}
/*********************************************************************************
*函数原型:Flash_PageWrite(u16 page,u8 *Data_Buf)
*函数功能:
*函数参数:
*返回值:
*说明: 写一整页,页范围0-4095
*********************************************************************************/
void Flash_PageWrite(U16 page,U8 *Pdata)
{
U16 j=page;
U16 i;
CLS_CS;
SPI_WriteByte(WREN);
SET_CS;
CLS_CS;
SPI_WriteByte(PP); //to program the selected page
SPI_WriteByte((U8)j>>8);
SPI_WriteByte((U8)j);
SPI_WriteByte(0x00);
for(i = 0; i< 256; i++)
{
SPI_WriteByte(Pdata);
}
SET_CS;
Flash_WaitBusy();
}
/*********************************************************************************
*函数原型:void Flash_PageRead(U16 page,U8 *Data_Buf)
*函数功能:
*函数参数:
*返回值:
*说明:
*********************************************************************************/
void Flash_PageRead(U16 page,U8 *Pdata) //页读取
{
unsigned long adr = page;
unsigned long i = 0;
CLS_CS;
SPI_WriteByte(WREN);
SET_CS;
CLS_CS;
SPI_WriteByte(FAST_READ); //n bytes read out until CS# goes high
SPI_WriteByte((U8)adr>>16);
SPI_WriteByte((U8)adr>>8);
SPI_WriteByte((U8)adr);
SPI_WriteByte((U8)0x00);
for(i = 0 ; i< 256; i++)
{
Pdata = SPI_ReadByte();
}
SET_CS;
Flash_WaitBusy();
}
/*********************************************************************************
*函数原型:void SPI_Read(U16 page,U16 addr,U16 len,U8 *Data_Buf)
*函数功能:从指定页指定地址读出指定长度数据
*函数参数:
*返回值:
*说明:
*********************************************************************************/
void Flash_Read(U16 page,U16 addr,U16 len,U8 *Data_Buf)
{
unsigned long adr = (page<<9)+addr;
unsigned long i = 0;
CLS_CS;
SPI_WriteByte(WREN); //sets the (WEL)write enable latch bit
SET_CS;
CLS_CS;
SPI_WriteByte(FAST_READ); //n bytes read out until CS# goes high
SPI_WriteByte((U8)adr>>16); //AD1 send out the address, high byte, middle byte, then low byte
SPI_WriteByte((U8)adr>>8); //AD2
SPI_WriteByte((U8)adr); //AD3
SPI_WriteByte((U8)0x00); //Dummy
for(i = 0 ; i!= len; i++)
{
Data_Buf = SPI_ReadByte();
}
SET_CS;
Flash_WaitBusy();
}
一周热门 更多>