使用两个SPI1和SPI2,都与W25Q80相接,但是SPI2一直读写不成功
/********************************* COPYRIGHT 2014 -------- ********* BEGIN OF FILE ********************************/
/**********************************************************************
* file : spi1.c
* author : --
* version : --
* date : 2014-9-2
* brief : spi1 program body
* Description : --
**********************************************************************/
/* Includes == 文件包含 --------------------------------------------*/
#include "spi1.h"
/* Private typedef == 私有类型 ----------------------------------*/
/* Private define == 私有定义 ----------------------------------*/
//-----------------------------------------//
#define sEE_SPI SPI1
#define sEE_SPI_CLK RCC_APB2Periph_SPI1
#define sEE_SPI_SCK_PIN GPIO_Pin_5 /* PA.05 */
#define sEE_SPI_SCK_GPIO_PORT GPIOA /* GPIOA */
#define sEE_SPI_SCK_GPIO_CLK RCC_AHBPeriph_GPIOA
#define sEE_SPI_SCK_SOURCE GPIO_PinSource5
#define sEE_SPI_SCK_AF GPIO_AF_SPI1
#define sEE_SPI_MISO_PIN GPIO_Pin_6 /* PA.06 */
#define sEE_SPI_MISO_GPIO_PORT GPIOA /* GPIOA */
#define sEE_SPI_MISO_GPIO_CLK RCC_AHBPeriph_GPIOA
#define sEE_SPI_MISO_SOURCE GPIO_PinSource6
#define sEE_SPI_MISO_AF GPIO_AF_SPI1
#define sEE_SPI_MOSI_PIN GPIO_Pin_7 /* PA.07 */
#define sEE_SPI_MOSI_GPIO_PORT GPIOA /* GPIOA */
#define sEE_SPI_MOSI_GPIO_CLK RCC_AHBPeriph_GPIOA
#define sEE_SPI_MOSI_SOURCE GPIO_PinSource7
#define sEE_SPI_MOSI_AF GPIO_AF_SPI1
/* Private macro == 私有宏 ------------------------------------*/
/* Private variables == 私有变量 ----------------------------------*/
/* Private functions == 私有函数定义 ------------------------------*/
/**********************************************************************
* @Fuctionname : None
* @Brief : None
* @Input : None
* @Output : None
* @Return : None
* @Remark : None
**********************************************************************/
void SPI1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
/*!< sEE_CS_GPIO, sEE_MOSI_GPIO, sEE_MISO_GPIO and sEE_SCK_GPIO
Periph clock enable */
RCC_AHBPeriphClockCmd(sEE_SPI_MOSI_GPIO_CLK | sEE_SPI_MISO_GPIO_CLK | sEE_SPI_SCK_GPIO_CLK , ENABLE);
/*!< sEE Periph clock enable */
RCC_APB2PeriphClockCmd(sEE_SPI_CLK, ENABLE);
/*!< Configure sEE pins: SCK */
GPIO_InitStructure.GPIO_Pin = sEE_SPI_SCK_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
// GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
// GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(sEE_SPI_SCK_GPIO_PORT, &GPIO_InitStructure);
/*!< Configure sEE_SPI pins: MISO */
GPIO_InitStructure.GPIO_Pin = sEE_SPI_MISO_PIN;
GPIO_Init(sEE_SPI_MISO_GPIO_PORT, &GPIO_InitStructure);
/*!< Configure sEE_SPI pins: MOSI */
GPIO_InitStructure.GPIO_Pin = sEE_SPI_MOSI_PIN;
GPIO_Init(sEE_SPI_MOSI_GPIO_PORT, &GPIO_InitStructure);
/* Connect PXx to sEE_SPI_SCK */
GPIO_PinAFConfig(sEE_SPI_SCK_GPIO_PORT, sEE_SPI_SCK_SOURCE, sEE_SPI_SCK_AF);
/* Connect PXx to sEE_SPI_MISO */
GPIO_PinAFConfig(sEE_SPI_MISO_GPIO_PORT, sEE_SPI_MISO_SOURCE, sEE_SPI_MISO_AF);
/* Connect PXx to sEE_SPI_MOSI */
GPIO_PinAFConfig(sEE_SPI_MOSI_GPIO_PORT, sEE_SPI_MOSI_SOURCE, sEE_SPI_MOSI_AF);
/*!< sEE SPI Config */
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_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;//SPI_NSS_Hard;//SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(sEE_SPI, &SPI_InitStructure);
SPI_Cmd(sEE_SPI, ENABLE); /*!< sEE_SPI enable */
}
/**********************************************************************
* @Fuctionname : None
* @Brief : None
* @Input : None
* @Output : None
* @Return : None
* @Remark : None
**********************************************************************/
u8 SPI1_ReadWriteByte(u8 dat)
{
/*!< Loop while DR register in not empty */
while (SPI_I2S_GetFlagStatus(sEE_SPI, SPI_I2S_FLAG_TXE) == RESET);
/*!< Send byte through the SPI peripheral */
SPI_SendData(sEE_SPI, dat);
/*!< Wait to receive a byte */
while (SPI_I2S_GetFlagStatus(sEE_SPI, SPI_I2S_FLAG_RXNE) == RESET);
/*!< Return the byte read from the SPI bus */
return (uint8_t)SPI_ReceiveData(sEE_SPI);
}
/********************************* COPYRIGHT 2014 -------- ********* END OF FILE **********************************/
/********************************* COPYRIGHT 2014 -------- ********* BEGIN OF FILE ********************************/
/**********************************************************************
* file : spi2.c
* author : --
* version : --
* date : 2018-12-29
* brief : spi1 program body
* Description : --
**********************************************************************/
/* Includes == 文件包含 --------------------------------------------*/
#include "spi2.h"
/* Private typedef == 私有类型 ----------------------------------*/
/* Private define == 私有定义 ----------------------------------*/
//-----------------------------------------//
#define sEE_SPI2 SPI2
#define sEE_SPI2_CLK RCC_APB1Periph_SPI2
#define sEE_SPI2_SCK_PIN GPIO_Pin_13 /* PB.13 */
#define sEE_SPI2_SCK_GPIO_PORT GPIOB /* GPIOB */
#define sEE_SPI2_SCK_GPIO_CLK RCC_AHBPeriph_GPIOB
#define sEE_SPI2_SCK_SOURCE GPIO_PinSource13
#define sEE_SPI2_SCK_AF GPIO_AF_SPI2
#define sEE_SPI2_MISO_PIN GPIO_Pin_14 /* PB.14 */
#define sEE_SPI2_MISO_GPIO_PORT GPIOB /* GPIOB */
#define sEE_SPI2_MISO_GPIO_CLK RCC_AHBPeriph_GPIOB
#define sEE_SPI2_MISO_SOURCE GPIO_PinSource14
#define sEE_SPI2_MISO_AF GPIO_AF_SPI2
#define sEE_SPI2_MOSI_PIN GPIO_Pin_15 /* PB.15 */
#define sEE_SPI2_MOSI_GPIO_PORT GPIOA /* GPIOB */
#define sEE_SPI2_MOSI_GPIO_CLK RCC_AHBPeriph_GPIOB
#define sEE_SPI2_MOSI_SOURCE GPIO_PinSource15
#define sEE_SPI2_MOSI_AF GPIO_AF_SPI2
/* Private macro == 私有宏 ------------------------------------*/
/* Private variables == 私有变量 ----------------------------------*/
/* Private functions == 私有函数定义 ------------------------------*/
/**********************************************************************
* @Fuctionname : None
* @Brief : None
* @Input : None
* @Output : None
* @Return : None
* @Remark : None
**********************************************************************/
void SPI2_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
/*!< sEE_CS_GPIO, sEE_MOSI_GPIO, sEE_MISO_GPIO and sEE_SCK_GPIO
Periph clock enable */
RCC_AHBPeriphClockCmd(sEE_SPI2_MOSI_GPIO_CLK | sEE_SPI2_MISO_GPIO_CLK | sEE_SPI2_SCK_GPIO_CLK , ENABLE);
/*!< sEE Periph clock enable */
RCC_APB1PeriphClockCmd(sEE_SPI2_CLK, ENABLE);
/*!< Configure sEE pins: SCK */
GPIO_InitStructure.GPIO_Pin = sEE_SPI2_SCK_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
// GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
// GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(sEE_SPI2_SCK_GPIO_PORT, &GPIO_InitStructure);
/*!< Configure sEE_SPI pins: MISO */
GPIO_InitStructure.GPIO_Pin = sEE_SPI2_MISO_PIN;
GPIO_Init(sEE_SPI2_MISO_GPIO_PORT, &GPIO_InitStructure);
/*!< Configure sEE_SPI pins: MOSI */
GPIO_InitStructure.GPIO_Pin = sEE_SPI2_MOSI_PIN;
GPIO_Init(sEE_SPI2_MOSI_GPIO_PORT, &GPIO_InitStructure);
/* Connect PXx to sEE_SPI_SCK */
GPIO_PinAFConfig(sEE_SPI2_SCK_GPIO_PORT, sEE_SPI2_SCK_SOURCE, sEE_SPI2_SCK_AF);
/* Connect PXx to sEE_SPI_MISO */
GPIO_PinAFConfig(sEE_SPI2_MISO_GPIO_PORT, sEE_SPI2_MISO_SOURCE, sEE_SPI2_MISO_AF);
/* Connect PXx to sEE_SPI_MOSI */
GPIO_PinAFConfig(sEE_SPI2_MOSI_GPIO_PORT, sEE_SPI2_MOSI_SOURCE, sEE_SPI2_MOSI_AF);
/*!< sEE SPI Config */
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_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;//SPI_NSS_Hard;//SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(sEE_SPI2, &SPI_InitStructure);
SPI_Cmd(sEE_SPI2, ENABLE); /*!< sEE_SPI enable */
}
/**********************************************************************
* @Fuctionname : None
* @Brief : None
* @Input : None
* @Output : None
* @Return : None
* @Remark : None
**********************************************************************/
u8 SPI2_ReadWriteByte(u8 dat)
{
/*!< Loop while DR register in not empty */
while (SPI_I2S_GetFlagStatus(sEE_SPI2, SPI_I2S_FLAG_TXE) == RESET);
/*!< Send byte through the SPI peripheral */
SPI_SendData(sEE_SPI2, dat);
/*!< Wait to receive a byte */
while (SPI_I2S_GetFlagStatus(sEE_SPI2, SPI_I2S_FLAG_RXNE) == RESET);
/*!< Return the byte read from the SPI bus */
return (uint8_t)SPI_ReceiveData(sEE_SPI2);
}
/********************************* COPYRIGHT 2014 -------- ********* END OF FILE **********************************/
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
#include "spi1.h"
#include "systick.h"
#include "usart3.h"
u16 W25QXX_TYPE=W25Q80; //默认是W25Q80
void W25QXX_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA,GPIO_Pin_4);
SPI1_Init();
// W25QXX_TYPE=W25QXX_ReadID();
}
u8 W25QXX_ReadSR(void)
{
u8 byte=0;
GPIO_ResetBits(GPIOA,GPIO_Pin_4);
SPI1_ReadWriteByte(W25X_ReadStatusReg); //发送读取状态寄存器命令
byte=SPI1_ReadWriteByte(0Xff); //读取一个字节
GPIO_SetBits(GPIOA,GPIO_Pin_4);
return byte;
}
void W25QXX_Write_SR(u8 sr)
{
GPIO_ResetBits(GPIOA,GPIO_Pin_4); //使能器件
SPI1_ReadWriteByte(W25X_WriteStatusReg);//发送写取状态寄存器命令
SPI1_ReadWriteByte(sr); //写入一个字节
GPIO_SetBits(GPIOA,GPIO_Pin_4);
}
u16 W25QXX_ReadID(void)
{
u16 Temp = 0;
GPIO_ResetBits(GPIOA,GPIO_Pin_4);
SPI1_ReadWriteByte(0x90);//发送读取ID命令
SPI1_ReadWriteByte(0x00);
SPI1_ReadWriteByte(0x00);
SPI1_ReadWriteByte(0x00);
Temp|=SPI1_ReadWriteByte(0xFF)<<8;
Temp|=SPI1_ReadWriteByte(0xFF);
GPIO_SetBits(GPIOA,GPIO_Pin_4);
return Temp;
}
void W25QXX_Write_Enable(void)
{
GPIO_ResetBits(GPIOA,GPIO_Pin_4); //使能器件
SPI1_ReadWriteByte(W25X_WriteEnable); //发送写使能
GPIO_SetBits(GPIOA,GPIO_Pin_4); //取消片选
}
void W25QXX_Write_Disable(void)
{
GPIO_ResetBits(GPIOA,GPIO_Pin_4); //使能器件
SPI1_ReadWriteByte(W25X_WriteDisable); //发送写禁止指令
GPIO_SetBits(GPIOA,GPIO_Pin_4); //取消片选
}
//读取SPI FLASH
//在指定地址开始读取指定长度的数据
//pBuffer:数据存储区
//ReadAddr:开始读取的地址(24bit)
//NumByteToRead:要读取的字节数(最大65535)
void W25QXX_Read(u8* pBuffer,u32 ReadAddr,u16 NumByteToRead)
{
u16 i;
GPIO_ResetBits(GPIOA,GPIO_Pin_4); //使能器件
SPI1_ReadWriteByte(W25X_ReadData); //发送读取命令
SPI1_ReadWriteByte((u8)((ReadAddr)>>16)); //发送24bit地址
SPI1_ReadWriteByte((u8)((ReadAddr)>>8));
SPI1_ReadWriteByte((u8)ReadAddr);
for(i=0;i<NumByteToRead;i++)
{
pBuffer[i]=SPI1_ReadWriteByte(0XFF); //循环读数
}
GPIO_SetBits(GPIOA,GPIO_Pin_4);
}
//SPI在一页(0~65535)内写入少于256个字节的数据
//在指定地址开始写入最大256字节的数据
//pBuffer:数据存储区
//WriteAddr:开始写入的地址(24bit)
//NumByteToWrite:要写入的字节数(最大256),该数不应该超过该页的剩余字节数!!!
void W25QXX_Write_Page(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite)
{
u16 i;
W25QXX_Write_Enable(); //SET WEL
GPIO_ResetBits(GPIOA,GPIO_Pin_4); //使能器件
SPI1_ReadWriteByte(W25X_PageProgram); //发送写页命令
SPI1_ReadWriteByte((u8)((WriteAddr)>>16)); //发送24bit地址
SPI1_ReadWriteByte((u8)((WriteAddr)>>8));
SPI1_ReadWriteByte((u8)WriteAddr);
for(i=0;i<NumByteToWrite;i++)SPI1_ReadWriteByte(pBuffer[i]);//循环写数
GPIO_SetBits(GPIOA,GPIO_Pin_4); //取消片选
W25QXX_Wait_Busy(); //等待写入结束
}
//无检验写SPI FLASH
//必须确保所写的地址范围内的数据全部为0XFF,否则在非0XFF处写入的数据将失败!
//具有自动换页功能
//在指定地址开始写入指定长度的数据,但是要确保地址不越界!
//pBuffer:数据存储区
//WriteAddr:开始写入的地址(24bit)
//NumByteToWrite:要写入的字节数(最大65535)
//CHECK OK
void W25QXX_Write_NoCheck(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite)
{
u16 pageremain;
pageremain=256-WriteAddr%256; //单页剩余的字节数
if(NumByteToWrite<=pageremain)pageremain=NumByteToWrite;//不大于256个字节
while(1)
{
W25QXX_Write_Page(pBuffer,WriteAddr,pageremain);
if(NumByteToWrite==pageremain)break;//写入结束了
else //NumByteToWrite>pageremain
{
pBuffer+=pageremain;
WriteAddr+=pageremain;
NumByteToWrite-=pageremain; //减去已经写入了的字节数
if(NumByteToWrite>256)pageremain=256; //一次可以写入256个字节
else pageremain=NumByteToWrite; //不够256个字节了
}
};
}
//写SPI FLASH
//在指定地址开始写入指定长度的数据
//该函数带擦除操作!
//pBuffer:数据存储区
//WriteAddr:开始写入的地址(24bit)
//NumByteToWrite:要写入的字节数(最大65535)
u8 W25QXX_BUFFER[4096];
void W25QXX_Write(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite)
{
u32 secpos;
u16 secoff;
u16 secremain;
u16 i;
u8 * W25QXX_BUF;
W25QXX_BUF=W25QXX_BUFFER;
secpos=WriteAddr/4096;//扇区地址
secoff=WriteAddr%4096;//在扇区内的偏移
secremain=4096-secoff;//扇区剩余空间大小
//printf("ad:%X,nb:%X ",WriteAddr,NumByteToWrite);//测试用
if(NumByteToWrite<=secremain)secremain=NumByteToWrite;//不大于4096个字节
while(1)
{
W25QXX_Read(W25QXX_BUF,secpos*4096,4096);//读出整个扇区的内容
for(i=0;i<secremain;i++)//校验数据
{
if(W25QXX_BUF[secoff+i]!=0XFF)break;//需要擦除
}
if(i<secremain)//需要擦除
{
W25QXX_Erase_Sector(secpos); //擦除这个扇区
for(i=0;i<secremain;i++) //复制
{
W25QXX_BUF[i+secoff]=pBuffer[i];
}
W25QXX_Write_NoCheck(W25QXX_BUF,secpos*4096,4096);//写入整个扇区
}else W25QXX_Write_NoCheck(pBuffer,WriteAddr,secremain);//写已经擦除了的,直接写入扇区剩余区间.
if(NumByteToWrite==secremain)break;//写入结束了
else//写入未结束
{
secpos++;//扇区地址增1
secoff=0;//偏移位置为0
pBuffer+=secremain; //指针偏移
WriteAddr+=secremain; //写地址偏移
NumByteToWrite-=secremain; //字节数递减
if(NumByteToWrite>4096)secremain=4096;//下一个扇区还是写不完
else secremain=NumByteToWrite; //下一个扇区可以写完了
}
};
}
//擦除整个芯片
//等待时间超长...
void W25QXX_Erase_Chip(void)
{
W25QXX_Write_Enable(); //SET WEL
W25QXX_Wait_Busy();
GPIO_ResetBits(GPIOA,GPIO_Pin_4); //使能器件
SPI1_ReadWriteByte(W25X_ChipErase); //发送片擦除命令
GPIO_SetBits(GPIOA,GPIO_Pin_4); //取消片选
W25QXX_Wait_Busy(); //等待芯片擦除结束
}
//擦除一个扇区
//Dst_Addr:扇区地址 根据实际容量设置
//擦除一个山区的最少时间:150ms
void W25QXX_Erase_Sector(u32 Dst_Addr)
{
//监视falsh擦除情况,测试用
// printf("fe:%x ",Dst_Addr);
Dst_Addr*=4096;
W25QXX_Write_Enable(); //SET WEL
W25QXX_Wait_Busy();
GPIO_ResetBits(GPIOA,GPIO_Pin_4); //使能器件
SPI1_ReadWriteByte(W25X_SectorErase); //发送扇区擦除指令
SPI1_ReadWriteByte((u8)((Dst_Addr)>>16)); //发送24bit地址
SPI1_ReadWriteByte((u8)((Dst_Addr)>>8));
SPI1_ReadWriteByte((u8)Dst_Addr);
GPIO_SetBits(GPIOA,GPIO_Pin_4); //取消片选
W25QXX_Wait_Busy(); //等待擦除完成
}
//等待空闲
void W25QXX_Wait_Busy(void)
{
while((W25QXX_ReadSR()&0x01)==0x01); // 等待BUSY位清空
}
void W25QXX_PowerDown(void)
{
GPIO_ResetBits(GPIOA,GPIO_Pin_4); //使能器件
SPI1_ReadWriteByte(W25X_PowerDown); //发送掉电命令
GPIO_SetBits(GPIOA,GPIO_Pin_4); //取消片选
delay_us(3); //等待TPD
}
//唤醒
void W25QXX_WAKEUP(void)
{
GPIO_ResetBits(GPIOA,GPIO_Pin_4); //使能器件
SPI1_ReadWriteByte(W25X_ReleasePowerDown); // send W25X_PowerDown command 0xAB
GPIO_SetBits(GPIOA,GPIO_Pin_4); //取消片选
delay_us(3); //等待TRES1
}
/********************************* COPYRIGHT 2018 -------- ********* BEGIN OF FILE ********************************/
/**********************************************************************
* @file : w25q80.c
* @Author : --
* @version : --
* @date : 2015-05-18
* @brief : w25q80 program body
* @Description : --
**********************************************************************/
/* Includes == 文件包含 --------------------------------------------*/
#include "w25q80.h"
#include "spi2.h"
#include "systick.h"
u16 W25Q80_TYPE=W25Q80;
/* Private typedef == 私有类型 ----------------------------------*/
/* Private define == 私有定义 ----------------------------------*/
#define W25Q80_CS_GPIO GPIOB
#define W25Q80_CS_PIN GPIO_Pin_12
#define W25Q80_CS_CLK RCC_AHBPeriph_GPIOB
/* Private macro == 私有宏 ------------------------------------*/
/* Private variables == 私有变量 ----------------------------------*/
/* Private functions == 私有函数定义 ------------------------------*/
/**********************************************************************
* @Brief : None
* @Input : None
* @Output : None
* @Return : None
* @Remark : None
**********************************************************************/
void W25Q80_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* GPIOD Periph clock enable */
RCC_AHBPeriphClockCmd(W25Q80_CS_CLK, ENABLE);
/* Configure PD0 and PD1 or PD3 and PD7 in output pushpull mode */
GPIO_InitStructure.GPIO_Pin = W25Q80_CS_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(W25Q80_CS_GPIO, &GPIO_InitStructure);
GPIO_SetBits(W25Q80_CS_GPIO, W25Q80_CS_PIN);
SPI2_Init();
}
/**********************************************************************
* @Brief : None
* @Input : None
* @Output : None
* @Return : None
* @Remark : None
**********************************************************************/
void W25Q80_CS_Low(void)
{
GPIO_ResetBits(W25Q80_CS_GPIO, W25Q80_CS_PIN);
}
/**********************************************************************
* @Brief : None
* @Input : None
* @Output : None
* @Return : None
* @Remark : None
**********************************************************************/
void W25Q80_CS_High(void)
{
GPIO_SetBits(W25Q80_CS_GPIO, W25Q80_CS_PIN);
}
/**********************************************************************
* @Brief : None
* @Input : None
* @Output : None
* @Return : None
* @Remark : None
**********************************************************************/
u8 W25Q80_ReadWriteByte(u8 dat)
{
return SPI2_ReadWriteByte(dat);
}
/**********************************************************************
* @Brief : None
* @Input : None
* @Output : None
* @Return : None
* @Remark : None
**********************************************************************/
u8 W25Q80_ReadSR(void)
{
u8 byte=0;
W25Q80_CS_Low();
W25Q80_ReadWriteByte(W25Q80_WriteStatusReg);
byte=W25Q80_ReadWriteByte(0Xff);
W25Q80_CS_High();
return byte;
}
/**********************************************************************
* @Brief : None
* @Input : None
* @Output : None
* @Return : None
* @Remark : None
**********************************************************************/
void W25Q80_Write_SR(u8 sr)
{
W25Q80_CS_Low();
W25Q80_ReadWriteByte(W25Q80_WriteStatusReg);
W25Q80_ReadWriteByte(sr);
W25Q80_CS_High();
}
/**********************************************************************
* @Brief : None
* @Input : None
* @Output : None
* @Return : None
* @Remark : None
**********************************************************************/
void W25Q80_Write_Enable(void)
{
W25Q80_CS_Low();
W25Q80_ReadWriteByte(W25Q80_WriteEnable);
W25Q80_CS_High();
}
/**********************************************************************
* @Brief : None
* @Input : None
* @Output : None
* @Return : None
* @Remark : None
**********************************************************************/
void W25Q80_Write_Disable(void)
{
W25Q80_CS_Low();
W25Q80_ReadWriteByte(W25Q80_WriteDisable);
W25Q80_CS_High();
}
/**********************************************************************
* @Brief : None
* @Input : None
* @Output : None
* @Return : None
* @Remark : None
**********************************************************************/
u16 W25Q80_ReadID(void)
{
u16 Temp = 0;
W25Q80_CS_Low();
W25Q80_ReadWriteByte(0x90);
W25Q80_ReadWriteByte(0x00);
W25Q80_ReadWriteByte(0x00);
W25Q80_ReadWriteByte(0x00);
Temp|=W25Q80_ReadWriteByte(0xFF)<<8;
Temp|=W25Q80_ReadWriteByte(0xFF);
W25Q80_CS_High();
return Temp;
}
/**********************************************************************
* @Brief : None
* @Input : None
* @Output : None
* @Return : None
* @Remark : None
**********************************************************************/
void W25Q80_Read(u8* pBuffer, u32 ReadAddr, u16 NumByteToRead)
{
u16 i;
W25Q80_CS_Low();
W25Q80_ReadWriteByte(W25Q80_ReadData);
W25Q80_ReadWriteByte((u8)((ReadAddr)>>16));
W25Q80_ReadWriteByte((u8)((ReadAddr)>>8));
W25Q80_ReadWriteByte((u8)ReadAddr);
for(i=0;i<NumByteToRead;i++)
{
pBuffer=W25Q80_ReadWriteByte(0XFF);
}
W25Q80_CS_High();
}
/**********************************************************************
* @Brief : None
* @Input : None
* @Output : None
* @Return : None
* @Remark : None
**********************************************************************/
void W25Q80_Write_Page(u8* pBuffer, u32 WriteAddr, u16 NumByteToWrite)
{
u16 i;
W25Q80_Write_Enable();
W25Q80_CS_Low();
W25Q80_ReadWriteByte(W25Q80_PageProgram);
W25Q80_ReadWriteByte((u8)((WriteAddr)>>16));
W25Q80_ReadWriteByte((u8)((WriteAddr)>>8));
W25Q80_ReadWriteByte((u8)WriteAddr);
for(i=0;i<NumByteToWrite;i++)
{
W25Q80_ReadWriteByte(pBuffer);
}
W25Q80_CS_High();
W25Q80_Wait_Busy();
}
/**********************************************************************
* @Brief : None
* @Input : None
* @Output : None
* @Return : None
* @Remark : None
**********************************************************************/
void W25Q80_Write_NoCheck(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite)
{
u16 pageremain;
pageremain=256-WriteAddr % 256;
if(NumByteToWrite<=pageremain)
pageremain=NumByteToWrite;
while(1)
{
W25Q80_Write_Page(pBuffer,WriteAddr,pageremain);
if(NumByteToWrite==pageremain)
break;
else //NumByteToWrite>pageremain
{
pBuffer+=pageremain;
WriteAddr+=pageremain;
NumByteToWrite-=pageremain;
if(NumByteToWrite>256)
pageremain=256;
else pageremain=NumByteToWrite;
}
};
}
/**********************************************************************
* @Brief : None
* @Input : None
* @Output : None
* @Return : None
* @Remark : None
**********************************************************************/
u8 W25Q80_BUFFER[4096];
void W25Q80_Write(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite)
{
u32 secpos;
u16 secoff;
u16 secremain;
u16 i;
u8 * W25Q80_BUF;
W25Q80_BUF=W25Q80_BUFFER;
secpos=WriteAddr/4096; // 扇区地址 0--255 for w25q80
secoff=WriteAddr%4096; // 在扇区内的偏移
secremain=4096-secoff; //
if(NumByteToWrite<=secremain)
secremain=NumByteToWrite; // 不大于4096字节
while(1)
{
W25Q80_Read(W25Q80_BUF,secpos*4096,4096); // 读出整个扇区内容
for(i=0;i<secremain;i++) // 校验数据
{
if(W25Q80_BUF[secoff+i]!=0XFF)break; // 需要擦除
}
if(i<secremain) // 需要擦除
{
W25Q80_Erase_Sector(secpos); // 擦除整个扇区
for(i=0;i<secremain;i++) // 复制数据
{
W25Q80_BUF[i+secoff]=pBuffer;
}
W25Q80_Write_NoCheck(W25Q80_BUF,secpos*4096,4096); // 写入整个扇区
}
else
W25Q80_Write_NoCheck(pBuffer,WriteAddr,secremain); // 写已经擦除了的,直接写入扇区剩余空间
if(NumByteToWrite==secremain)
break; // 写入结束
else
{
secpos++;
secoff=0;
pBuffer+=secremain;
WriteAddr+=secremain;
NumByteToWrite-=secremain;
if(NumByteToWrite>4096)secremain=4096;
else secremain=NumByteToWrite;
}
};
}
/**********************************************************************
* @Brief : None
* @Input : None
* @Output : None
* @Return : None
* @Remark : None
**********************************************************************/
void W25Q80_Erase_Chip(void)
{
W25Q80_Write_Enable();
W25Q80_Wait_Busy();
W25Q80_CS_Low();
W25Q80_ReadWriteByte(W25Q80_ChipErase);
W25Q80_CS_High();
W25Q80_Wait_Busy();
}
/**********************************************************************
* @Brief : None
* @Input : None
* @Output : None
* @Return : None
* @Remark : None
**********************************************************************/
void W25Q80_Erase_Sector(u32 Dst_Addr)
{
Dst_Addr*=4096;
W25Q80_Write_Enable();
W25Q80_Wait_Busy();
W25Q80_CS_Low();
W25Q80_ReadWriteByte(W25Q80_SectorErase);
W25Q80_ReadWriteByte((u8)((Dst_Addr)>>16));
W25Q80_ReadWriteByte((u8)((Dst_Addr)>>8));
W25Q80_ReadWriteByte((u8)Dst_Addr);
W25Q80_CS_High();
W25Q80_Wait_Busy();
}
/**********************************************************************
* @Brief : None
* @Input : None
* @Output : None
* @Return : None
* @Remark : None
**********************************************************************/
void W25Q80_Wait_Busy(void)
{
while ((W25Q80_ReadSR()&0x01)==0x01);
}
/**********************************************************************
* @Brief : None
* @Input : None
* @Output : None
* @Return : None
* @Remark : None
**********************************************************************/
void W25Q80_PowerDown(void)
{
W25Q80_CS_Low();
W25Q80_ReadWriteByte(W2580_PowerDown);
W25Q80_CS_High();
delay_us(3);
}
/**********************************************************************
* @Brief : None
* @Input : None
* @Output : None
* @Return : None
* @Remark : None
**********************************************************************/
void W25Q80_Wakeup(void)
{
W25Q80_CS_Low();
W25Q80_ReadWriteByte(W25Q80_ReleasePowerDown);
W25Q80_CS_High();
delay_us(3);
}
/********************************* COPYRIGHT 2018 -------- ********* END OF FILE **********************************/
一周热门 更多>