標(biāo)題:
16MB的FLASHW25Q128驅(qū)動
[打印本頁]
作者:
xiaos
時間:
2015-4-10 17:00
標(biāo)題:
16MB的FLASHW25Q128驅(qū)動
#define SPI_SCK_LOW() GPIOB->ODR&=~(1<<3)
#define SPI_SCK_HIGH() GPIOB->ODR|=1<<3
#define SPI_MOSI_LOW()GPIOB->ODR&=~(1<<5)
#define SPI_MOSI_HIGH()GPIOB->ODR|=1<<5
#define SPI_CS_LOW()GPIOB->ODR&=~(1<<14)
#define SPI_CS_HIGH() GPIOB->ODR|=1<<14
#define SPI_MISO_Read()(GPIOB->IDR&(1<<4))
#define W25X_WriteEnable 0x06 //寫允許命令
#define W25X_WriteDisable 0x04 //禁止命令
#define W25X_ReadStatusReg 0x05 //讀狀態(tài)寄存器
#define W25X_WriteStatusReg 0x01 //寫狀態(tài)寄存器
#define W25X_ReadData 0x03 //讀數(shù)據(jù)
#define W25X_FastReadData 0x0B //快讀
#define W25X_FastReadDual 0x3B
#define W25X_PageProgram 0x02 //頁寫
#define W25X_BlockErase 0xD8 //快擦除
#define W25X_SectorErase 0x20 //扇區(qū)擦除
#define W25X_ChipErase 0xC7 //整盤擦除
#define W25X_PowerDown 0xB9 //低功耗
#define W25X_ReleasePowerDown 0xAB
#define W25X_DeviceID 0xAB
#define W25X_ManufactDeviceID 0x90
#define W25X_JedecDeviceID 0x9F
static void SPI_SendByte(uint8_t Byte) //使用SPI總線發(fā)送1個字節(jié)的數(shù)據(jù)
{
//uint8_t Cnt;
//SPI_SCK_LOW();
//for(Cnt=0;Cnt<8;Cnt++)
//{
//if(Byte&0x80)
//SPI_MOSI_HIGH();
//else
//SPI_MOSI_LOW();
//SPI_SCK_HIGH();
//Byte<<=1;
//SPI_SCK_LOW();
//}
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE)==RESET);
SPI_I2S_SendData(SPI1,Byte);
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE)==RESET);
SPI_I2S_ReceiveData(SPI1);
}
static uint8_t SPI_ReceiveByte(void) //使用SPI總線接收1個字節(jié)的數(shù)據(jù)
{
//uint8_t Byte=0,Cnt;
////GPIOB->ODR|=1<<4;
//for(Cnt=0;Cnt<8;Cnt++)
//{
//SPI_SCK_HIGH();
//Byte<<=1;
//if(SPI_MISO_Read())
//Byte++;
//SPI_SCK_LOW();
//}
//return Byte;
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE)==RESET);
SPI_I2S_SendData(SPI1,0xff);
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE)==RESET);
return SPI_I2S_ReceiveData(SPI1);
}
/************************FLASH****************************/
uint8_t FLASH_ReadStatusReg(void) //讀狀態(tài)寄存器
{
uint8_t Status;
SPI_CS_LOW();
SPI_SendByte(W25X_ReadStatusReg);
Status=SPI_ReceiveByte();
SPI_CS_HIGH();
return Status;
}
void FLASH_WriteEnable(void)
{
SPI_CS_LOW();
SPI_SendByte(W25X_WriteEnable);
SPI_CS_HIGH();
}
void FLASH_WriteByte(uint32_t Address,uint8_t Byte)
{
FLASH_WriteEnable();//改變磁盤的操作都需要寫允許命令
SPI_CS_LOW();
SPI_SendByte(W25X_PageProgram);
SPI_SendByte(Address>>16);
SPI_SendByte(Address>>8);
SPI_SendByte(Address);
SPI_SendByte(Byte);
SPI_CS_HIGH();
while(FLASH_ReadStatusReg()&0x01);
}
void FLASH_ReadByte(uint32_t Address ,uint8_t *pByte)
{
SPI_CS_LOW();
SPI_SendByte(W25X_ReadData);
SPI_SendByte(Address>>16);
SPI_SendByte(Address>>8);
SPI_SendByte(Address);
*pByte=SPI_ReceiveByte();
SPI_CS_HIGH();
}
uint16_t FLASH_ReadID(void)
{
uint16_t Temp=0;
SPI_CS_LOW();
SPI_SendByte(W25X_ManufactDeviceID);
SPI_SendByte(0x00);
SPI_SendByte(0x00);
SPI_SendByte(0x00);
Temp|=SPI_ReceiveByte()<<8;
Temp|=SPI_ReceiveByte();
SPI_CS_HIGH();
return Temp;
}
void FLASH_Erase_Sector(uint32_t Address)
{
FLASH_WriteEnable();
while(FLASH_ReadStatusReg()&0x01);
SPI_CS_LOW();
SPI_SendByte(W25X_SectorErase);
SPI_SendByte(Address>>16);
SPI_SendByte(Address>>8);
SPI_SendByte(Address);
SPI_CS_HIGH();
while(FLASH_ReadStatusReg()&0x01);
}
void FLASH_Wrase_Chip(void)
{
FLASH_WriteEnable();
SPI_SendByte(0x00);
while(FLASH_ReadStatusReg()&0x01);
SPI_CS_LOW();
SPI_SendByte(W25X_ChipErase);
SPI_CS_HIGH();
while(FLASH_ReadStatusReg()&0x01);
}
復(fù)制代碼
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1