新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > SST25VF016B串行flash驅(qū)動程序基于STM32F10x

SST25VF016B串行flash驅(qū)動程序基于STM32F10x

作者: 時間:2016-11-26 來源:網(wǎng)絡(luò) 收藏
#define Dummy_Byte0xff

#define Read_Data0x03//讀取存儲器數(shù)據(jù)
#define HighSpeedReadData0x0B//快速讀取存儲器數(shù)據(jù)
#define SectorErace_4KB0x20//扇區(qū)擦除
#define BlockErace_32KB0x52//32KB塊擦除
#define BlockErace_64KB0xD8//64KB塊擦除
#define ChipErace0x60//片擦除

本文引用地址:http://m.butianyuan.cn/article/201611/321914.htm

#define Byte_Program0x02//頁面編程--寫數(shù)據(jù)
#define AAI_WordProgram0xAD
#define ReadStatusRegister0x05//讀狀態(tài)寄存器
#define EnableWriteStatusRegister0x50
#define WriteStatusRegister0x01//寫狀態(tài)寄存器

#define WriteEnable0x06//寫使能,設(shè)置狀態(tài)寄存器
#define WriteDisable0x04//寫禁止
#define ReadDeviceID0xAB//獲取設(shè)備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);//關(guān)閉Flash電源
// Delay(10);//延時10mS
GPIO_SetBits(GPIOD, GPIO_Pin_15);//打開Flash電源
//GPIO_ResetBits(GPIOC, GPIO_Pin_8);
}
SPI2init();
SST25V_CS_HIGH();
SST25V_EnableWriteStatusRegister();//使能寫狀態(tài)寄存器
SST25V_WriteStatusRegister(0);//寫狀態(tài)寄存器設(shè)置BPL BP3 BP2 BP1 BP00 0 0 0 0
SST25V_DBSY();
}

//讀字節(jié),參數(shù):地址
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;
}

//讀字節(jié)串,參數(shù):起始地址、讀入緩沖、讀取長度
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();
}

//高速讀字節(jié)串,參數(shù):起始地址、讀入緩沖、讀取長度
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();
}

//讀字節(jié),參數(shù):地址
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));
}

//寫字節(jié),參數(shù):待寫字節(jié)、寫入地址
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();
}

////寫字節(jié)串,參數(shù):起始地址、待寫數(shù)據(jù)、寫入長度
void SST25V_BufferWrite(u32 WriteAddr,u8* pBuffer, u16 NumByteToWrite)
{
u16 i,length;
length=NumByteToWrite;
if(length%2!=0)length++;//不足雙數(shù)補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();
}

// 扇區(qū)擦除4Kbit,參數(shù):地址
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();
}



評論


技術(shù)專區(qū)

關(guān)閉