新闻中心

EEPW首页 > 嵌入式系统 > 设计应用 > SST25VF016B串行flash驱动程序基于STM32F10x

SST25VF016B串行flash驱动程序基于STM32F10x

作者:时间:2016-11-26来源:网络收藏
#define Dummy_Byte0xff

#define Read_Data0x03//读取存储器数据
#define HighSpeedReadData0x0B//快速读取存储器数据
#define SectorErace_4KB0x20//扇区擦除
#define BlockErace_32KB0x52//32KB块擦除
#define BlockErace_64KB0xD8//64KB块擦除
#define ChipErace0x60//片擦除

本文引用地址:http://www.eepw.com.cn/article/201611/321914.htm

#define Byte_Program0x02//页面编程--写数据
#define AAI_WordProgram0xAD
#define ReadStatusRegister0x05//读状态寄存器
#define EnableWriteStatusRegister0x50
#define WriteStatusRegister0x01//写状态寄存器

#define WriteEnable0x06//写使能,设置状态寄存器
#define WriteDisable0x04//写禁止
#define ReadDeviceID0xAB//获取设备ID信息

#define ReadJedec_ID0x9F//JEDEC的ID信息

#define EBSY0X70
#define DBSY0X80

//初始化SPI口
void SPI2init(void)
{
SPI_InitTypeDefSPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;


GPIO_InitStructure.GPIO_Pin = GPIO_SCK | GPIO_MISO | GPIO_MOSI;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);


GPIO_InitStructure.GPIO_Pin = GPIO_CS;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIO_PORT_CS, &GPIO_InitStructure);


SST25V_CS_HIGH();


SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI2, &SPI_InitStructure);


SPI_Cmd(SPI2, ENABLE);
}

//flash初始化
void SST25V_Init(void)
{
{
GPIO_InitTypeDef GPIO_InitStructure;


GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
// GPIO_ResetBits(GPIOD, GPIO_Pin_15);//关闭Flash电源
// Delay(10);//延时10mS
GPIO_SetBits(GPIOD, GPIO_Pin_15);//打开Flash电源
//GPIO_ResetBits(GPIOC, GPIO_Pin_8);
}
SPI2init();
SST25V_CS_HIGH();
SST25V_EnableWriteStatusRegister();//使能写状态寄存器
SST25V_WriteStatusRegister(0);//写状态寄存器设置BPL BP3 BP2 BP1 BP00 0 0 0 0
SST25V_DBSY();
}

//读字节,参数:地址
u8 SST25V_ByteRead(u32 ReadAddr)
{
u8 Temp = 0;
SST25V_CS_LOW();
SPI_Flash_SendByte(Read_Data);
SPI_Flash_SendByte((ReadAddr & 0xFF0000) >> 16);
SPI_Flash_SendByte((ReadAddr& 0xFF00) >> 8);
SPI_Flash_SendByte(ReadAddr & 0xFF);

Temp = SPI_Flash_ReceiveByte();
SST25V_CS_HIGH();
return Temp;
}

//读字节串,参数:起始地址、读入缓冲、读取长度
void SST25V_BufferRead(u32 ReadAddr, u8* pBuffer, u16 NumByteToRead)
{
SST25V_CS_LOW();
SPI_Flash_SendByte(Read_Data);
SPI_Flash_SendByte((ReadAddr & 0xFF0000) >> 16);
SPI_Flash_SendByte((ReadAddr& 0xFF00) >> 8);
SPI_Flash_SendByte(ReadAddr & 0xFF);

while(NumByteToRead--)
{
*pBuffer = SPI_Flash_ReceiveByte();
pBuffer++;
}
SST25V_CS_HIGH();
}

//高速读字节串,参数:起始地址、读入缓冲、读取长度
void SST25V_HighSpeedBufferRead( u32 ReadAddr,u8* pBuffer, u16 NumByteToRead)
{
SST25V_CS_LOW();
SPI_Flash_SendByte(HighSpeedReadData);
SPI_Flash_SendByte((ReadAddr & 0xFF0000) >> 16);
SPI_Flash_SendByte((ReadAddr& 0xFF00) >> 8);
SPI_Flash_SendByte(ReadAddr & 0xFF);
SPI_Flash_SendByte(Dummy_Byte);

while(NumByteToRead--)
{
*pBuffer = SPI_Flash_ReceiveByte();
pBuffer++;
}
SST25V_CS_HIGH();
}

//读字节,参数:地址
u8 SST25V_HighSpeedRead(u32 ReadAddr)
{
u32 Temp = 0;
SST25V_CS_LOW();
SPI_Flash_SendByte(HighSpeedReadData);
SPI_Flash_SendByte((ReadAddr & 0xFF0000) >> 16);
SPI_Flash_SendByte((ReadAddr& 0xFF00) >> 8);
SPI_Flash_SendByte(ReadAddr & 0xFF);
SPI_Flash_SendByte(Dummy_Byte);
Temp = SPI_Flash_ReceiveByte();
SST25V_CS_HIGH();
return Temp;
}


u8 SPI_Flash_SendByte(u8 byte)
{
while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI2, byte);

while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);
return SPI_I2S_ReceiveData(SPI2);
}

u8 SPI_Flash_ReceiveByte(void)
{
return (SPI_Flash_SendByte(Dummy_Byte));
}

//写字节,参数:待写字节、写入地址
void SST25V_ByteWrite(u8 Byte, u32 WriteAddr)
{
SST25V_WriteEnable();
SST25V_CS_LOW();
SPI_Flash_SendByte(Byte_Program);
SPI_Flash_SendByte((WriteAddr & 0xFF0000) >> 16);
SPI_Flash_SendByte((WriteAddr & 0xFF00) >> 8);
SPI_Flash_SendByte(WriteAddr & 0xFF);
SPI_Flash_SendByte(Byte);
SST25V_CS_HIGH();
SST25V_WaitForWriteEnd();
SST25V_WriteDisable();
SST25V_WaitForWriteEnd();
}

////写字节串,参数:起始地址、待写数据、写入长度
void SST25V_BufferWrite(u32 WriteAddr,u8* pBuffer, u16 NumByteToWrite)
{
u16 i,length;
length=NumByteToWrite;
if(length%2!=0)length++;//不足双数补1

SST25V_Init();//

SST25V_WriteEnable();
SST25V_CS_LOW();
SPI_Flash_SendByte(AAI_WordProgram);
SPI_Flash_SendByte((WriteAddr & 0xFF0000) >> 16);
SPI_Flash_SendByte((WriteAddr & 0xFF00) >> 8);
SPI_Flash_SendByte(WriteAddr & 0xFF);

SPI_Flash_SendByte(pBuffer[0]);
SPI_Flash_SendByte(pBuffer[1]);

SST25V_CS_HIGH();
SST25V_Wait_Busy_AAI();

for(i=2;i
{
SST25V_CS_LOW();
SPI_Flash_SendByte(AAI_WordProgram);
SPI_Flash_SendByte(pBuffer[i]);
SPI_Flash_SendByte(pBuffer[i+1]);
SST25V_CS_HIGH();
SST25V_Wait_Busy_AAI();
}
SST25V_WriteDisable();
SST25V_Wait_Busy_AAI();
}


void SST25V_Wait_Busy_AAI(void)
{
while (SST25V_ReadStatusRegister() == 0x43)
SST25V_ReadStatusRegister();
}

// 扇区擦除4Kbit,参数:地址
void SST25V_SectorErase_4KByte(u32 Addr)
{
SST25V_Init();//
SST25V_WriteEnable();
SST25V_CS_LOW();
SPI_Flash_SendByte(SectorErace_4KB);
SPI_Flash_SendByte((Addr & 0xFF0000) >> 16);
SPI_Flash_SendByte((Addr & 0xFF00) >> 8);
SPI_Flash_SendByte(Addr & 0xFF);
SST25V_CS_HIGH();
SST25V_WaitForWriteEnd();
SST25V_WriteDisable();
SST25V_WaitForWriteEnd();
}


void SST25V_BlockErase_32KByte(u32 Addr)
{
SST25V_WriteEnable();
SST25V_CS_LOW();
SPI_Flash_SendByte(BlockErace_32KB);
SPI_Flash_SendByte((Addr & 0xFF0000) >> 16);
SPI_Flash_SendByte((Addr & 0xFF00) >> 8);
SPI_Flash_SendByte(Addr & 0xFF);
SST25V_CS_HIGH();
SST25V_WaitForWriteEnd();
SST25V_WriteDisable();
SST25V_WaitForWriteEnd();

}

void SST25V_BlockErase_64KByte(u32 Addr)
{
SST25V_WriteEnable();
SST25V_CS_LOW();
SPI_Flash_SendByte(BlockErace_64KB);
SPI_Flash_SendByte((Addr & 0xFF0000) >> 16);
SPI_Flash_SendByte((Addr & 0xFF00) >> 8);
SPI_Flash_SendByte(Addr & 0xFF);
SST25V_CS_HIGH();
SST25V_WaitForWriteEnd();
SST25V_WriteDisable();
SST25V_WaitForWriteEnd();
}

void SST25V_ChipErase(void)
{
SST25V_Init();
SST25V_WriteEnable();
SST25V_CS_LOW();
SPI_Flash_SendByte(ChipErace);
SST25V_CS_HIGH();
SST25V_WaitForWriteEnd();
SST25V_WriteDisable();
SST25V_WaitForWriteEnd();
}

u8 SST25V_ReadStatusRegister(void)
{
u8 StatusRegister = 0;
SST25V_CS_LOW();
SPI_Flash_SendByte(ReadStatusRegister);
StatusRegister = SPI_Flash_ReceiveByte();
SST25V_CS_HIGH();
return StatusRegister;
}

void SST25V_WriteEnable(void)
{
SST25V_CS_LOW();
SPI_Flash_SendByte(WriteEnable);
SST25V_CS_HIGH();
}

void SST25V_WriteDisable(void)
{
SST25V_CS_LOW();
SPI_Flash_SendByte(WriteDisable);
SST25V_CS_HIGH();
}

void SST25V_EnableWriteStatusRegister(void)
{
SST25V_CS_LOW();
SPI_Flash_SendByte(EnableWriteStatusRegister);
SST25V_CS_HIGH();
}

void SST25V_WriteStatusRegister(u8 Byte)
{
SST25V_EnableWriteStatusRegister();
SST25V_CS_LOW();
SPI_Flash_SendByte(WriteStatusRegister);
SPI_Flash_SendByte(Byte);
SST25V_CS_HIGH();
}

void SST25V_WaitForWriteEnd(void)
{
u8 FLASH_Status = 0;
SST25V_CS_LOW();
SPI_Flash_SendByte(ReadStatusRegister);
do
{
FLASH_Status = SPI_Flash_SendByte(Dummy_Byte);

} while((FLASH_Status & WriteStatusRegister)!=0);

SST25V_CS_HIGH();
}

void SST25V_EBSY(void)
{
SST25V_CS_LOW();
SPI_Flash_SendByte(EBSY);
SST25V_CS_HIGH();
}

void SST25V_DBSY(void)
{
SST25V_CS_LOW();
SPI_Flash_SendByte(DBSY);
SST25V_CS_HIGH();
}



评论


技术专区

关闭