标题:
16MB的FLASHW25Q128驱动
[打印本页]
作者:
xiaos
时间:
2015-4-10 17:00
标题:
16MB的FLASHW25Q128驱动
#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 //读状态寄存器
#define W25X_WriteStatusReg 0x01 //写状态寄存器
#define W25X_ReadData 0x03 //读数据
#define W25X_FastReadData 0x0B //快读
#define W25X_FastReadDual 0x3B
#define W25X_PageProgram 0x02 //页写
#define W25X_BlockErase 0xD8 //快擦除
#define W25X_SectorErase 0x20 //扇区擦除
#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总线发送1个字节的数据
{
//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个字节的数据
{
//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) //读状态寄存器
{
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);
}
复制代码
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1